Re: [Tutor] thesaurus
"Pete Froslie" wrote This seems the basic form for a function: *def** hello*(): *print* "Hello World!" *return* That is a very limited form that encourages bad practice. The more general form is: def aFunction(inputValue1, inputValue2,...): # do some processing here return output_value1, outputValue2 By adding the input and output values we make the function independant of its immediate surroundings and therefore more easily reusable across different programs. We can then call it like: x,y = aFunction(a,b) I assume each part that can be separated will be done so in this format at the start of the code; consequently, allowing me to call 'hello' later when I need it. Does this also mean that I will be able to call those functions separately later when I import 'thesaurus.py' into a new code also? Exactly. The first benefit of functions is in encapsulating parts of your program and providing structure which makles it easier to read but the real power of functions is in making those blocks of code available to other programs too. Read my tutorial topic on Moduules and Functions more information. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] String manipulation: Pig latin translator
Hi guys, What I have here is more of a logic problem that I can't get my head around than a programming problem. I am trying to program an English to Pig Latin translator. I've got the English to Pig Latin part down and its working great. The part I am having difficulty with is the Pig Latin to English part. Say I have the word ellohay (hello) which I wish to convert into English. The first thing I am doing is removing the added ay at the end and the new word becomes elloh. This is where I have the problem. I have to somehow identify which letters to remove from the end and add back to the front of the word (i.e. h needs to be removed and added back to the front). However with a word such as string which translates as ingstray, when we remove the ay we are left with ingstr, and need to identify that str needs to be removed and added back on the front. I am lost as to how to identify which characters need to be removed to be re-added to the front and can't find a common rule to program by. There are translators out there already like this, so it works somehow, I just need a push in the right direction i think [image: :)] Thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String manipulation: Pig latin translator
Eddie wrote: Hi guys, What I have here is more of a logic problem that I can't get my head around than a programming problem. I am trying to program an English to Pig Latin translator. I've got the English to Pig Latin part down and its working great. The part I am having difficulty with is the Pig Latin to English part. Say I have the word ellohay (hello) which I wish to convert into English. The first thing I am doing is removing the added ay at the end and the new word becomes elloh. This is where I have the problem. I have to somehow identify which letters to remove from the end and add back to the front of the word (i.e. h needs to be removed and added back to the front). However with a word such as string which translates as ingstray, when we remove the ay we are left with ingstr, and need to identify that str needs to be removed and added back on the front. I am lost as to how to identify which characters need to be removed to be re-added to the front and can't find a common rule to program by. There are translators out there already like this, so it works somehow, I just need a push in the right direction i think [image: :)] Thanks You can't find an algorithm, because there isn't one. Pig-latin conversion is a non-reversible transformation. I suspect what existing implementations do is to have a dictionary of legal words. Then the algorithm might become: while word ends with consonant, move consonant to beginning if word is in dictionary, break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String manipulation: Pig latin translator
Hello Eddie! On Sunday 12 July 2009, Eddie wrote: > Hi guys, > > What I have here is more of a logic problem that I can't get my > head around than a programming problem. I am trying to program an > English to Pig Latin translator. I've got the English to Pig Latin > part down and its working great. > > The part I am having difficulty with is the Pig Latin to English > part. Say I have the word ellohay (hello) which I wish to convert > into English. The first thing I am doing is removing the added ay > at the end and the new word becomes elloh. > > This is where I have the problem. I have to somehow identify which > letters to remove from the end and add back to the front of the > word (i.e. h needs to be removed and added back to the front). > However with a word such as string which translates as ingstray, > when we remove the ay we are left with ingstr, and need to identify > that str needs to be removed and added back on the front. > > I am lost as to how to identify which characters need to be removed > to be re-added to the front and can't find a common rule to program > by. There are translators out there already like this, so it works > somehow, I just need a push in the right direction i think [image: > :)] > > Thanks I didn't look up the rules for creating Pig Latin. But from looking at your examples: hello --> ellohay, string --> ingstray, I think that information loss occurs in the conversion English --> Pig Latin. This means that the operation can not be reversed with an algorithm that only sees the word in question. The most easy solution IMHO is to download a big English text from the Internet, and translate it to Pig Latin. During the translation you create a big reverse mapping: Pig Latin --> English, which for use in the reverse translator. This method exploits the fact that not each character combination is a valid English word. But there may be collisions: two different English words that have the same Pig Latin translation. It would be interesting to see if such collisions do really occur with English source text. Kind regards, Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String manipulation: Pig latin translator
On 7/12/2009 5:52 AM Eike Welk said... This method exploits the fact that not each character combination is a valid English word. But there may be collisions: two different English words that have the same Pig Latin translation. stop -- opstay tops -- opstay Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] segmentation fault
I've got a script that I wrote several years ago and have been happily using daily. Suddenly when I try to run it I get a segmentation fault. I'm running debian testing so probably some recent update caused the breakage. How can I find out what's gone wrong? -- "Happiness is having a large, loving, caring, close-knit family in another city." -- George Burns Rick Pasottor...@niof.nethttp://www.niof.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] segmentation fault
On 7/12/2009 9:09 AM Rick Pasotto said... I've got a script that I wrote several years ago and have been happily using daily. Suddenly when I try to run it I get a segmentation fault. I'm running debian testing so probably some recent update caused the breakage. How can I find out what's gone wrong? If you're certain it's not otherwise environmental (out of space, bad memory or disk, packet flooding on network, etc), I'd try rolling back recent changes if that's convenient. Sometimes an strace on the process leads me in the right direction. If neither get you started, I'd try littering print statements through the code the track progress and try to narrow down where in the code it's happening. I've sometimes stumbled over a specific line causing the problem, and can eliminate the problem by refactoring. Be sure to report what you find upstream. There are probably other ways to isolate the problem depending on your focus. Maybe someone will point you in those directions. Of course, this is not entirely unexpected when you're running testing. It could resolve itself on its own. Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] segmentation fault
On Sun, Jul 12, 2009 at 09:54:26AM -0700, Emile van Sebille wrote: > On 7/12/2009 9:09 AM Rick Pasotto said... >> I've got a script that I wrote several years ago and have been happily >> using daily. Suddenly when I try to run it I get a segmentation fault. >> >> I'm running debian testing so probably some recent update caused the >> breakage. How can I find out what's gone wrong? >> > > If you're certain it's not otherwise environmental (out of space, bad > memory or disk, packet flooding on network, etc), I'd try rolling back > recent changes if that's convenient. Sometimes an strace on the process > leads me in the right direction. If neither get you started, I'd try > littering print statements through the code the track progress and try > to narrow down where in the code it's happening. I've sometimes > stumbled over a specific line causing the problem, and can eliminate the > problem by refactoring. Be sure to report what you find upstream. > > There are probably other ways to isolate the problem depending on your > focus. Maybe someone will point you in those directions. > > Of course, this is not entirely unexpected when you're running testing. > It could resolve itself on its own. I downgraded 2.5.2-1.1 to 2.5.2-1 and all is well. I've sent a reportbug that includes the last several lines of an strace. Thanks for pointing me in the right direction. -- "Upon common theatres, indeed, the applause of the audience is of more importance to the actors than their own approbation. But upon the stage of life, while conscience claps, let the world hiss! On the contrary if conscience disapproves, the loudest applauses of the world are of little value." -- John Adams Rick Pasottor...@niof.nethttp://www.niof.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] segmentation fault
On Sunday 12 July 2009 11:09, Rick Pasotto wrote: > I've got a script that I wrote several years ago and have been happily > using daily. Suddenly when I try to run it I get a segmentation fault. > > I'm running debian testing so probably some recent update caused the > breakage. How can I find out what's gone wrong? Crashing the Python interpreter isn't easy. The most common ways I've seen are errors in C extensions, unpickling, and the ctypes module. You might have some C extensions that need to be recompiled for the new version of Python, or an update to your C extensions that requires a newer version of Python. The usual troubleshooting advice applies. Strategically placed print statements (I actually prefer using UDP sockets for logging), separate your code into blocks and see if you can narrow down where the error is occurring, use a debugger, etc. Depending on the complexity of your code, you might even be able to find out if there's some input somewhere that isn't validated, although this probably isn't the problem in your case. At each step, verify what you think you've learned. If it isn't repeatable, you can't test the fix. But yeah, check for resource exhaustion too. That one can bite you unexpectedly. Cheers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] thesaurus
so, I've been playing with the functions a bit-- took a little time to get the hang of them after I started that other mess. Initially, I've done the opposite suggested here by creating functions without the input values. I do see the where it will pay off in re-usability that route.. Alan, I'll read your tutorial on modules and functions. Things that are arising for me now: (1)the use of global variables- seems it would be easier to communicate between functions with 'I/O' values if I understood where the best location for declaration is.. (2)not sure why this function doesn't work: word_count = 0 #set up variable to increment through text def increment(x): > return x+1 > > increment(word_count) > (3) related to strings: Trying a function that will strip unwanted words off of the url before I bounce it off the website. For instance, I don't need to search for a, was, is... This is working, but with strange results-- often it will find an 'a' in the middle of a word and replace it with text and such. using rstrip, but with varying results: #this is the url as a string > http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/ > > thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and > re-adding the '/' > The url function btw: def url(): > fin = open("journey_test.txt", "r") > response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']", > fin.read()) > thesaurus = API_URL + response[word_number] + '/' #API_URL is > established at the start of the code > return thesaurus > Pete F On Sun, Jul 12, 2009 at 4:00 AM, Alan Gauld wrote: > > "Pete Froslie" wrote > > This seems the basic form for a function: >> >> *def** hello*(): >> >>>*print* "Hello World!" >>>*return* >>> >> > That is a very limited form that encourages bad practice. The more > general form is: > > def aFunction(inputValue1, inputValue2,...): > # do some processing here > return output_value1, outputValue2 > > By adding the input and output values we make the function > independant of its immediate surroundings and therefore > more easily reusable across different programs. > > We can then call it like: > > x,y = aFunction(a,b) > > I assume each part that can be separated will be done so in this format at >>> >> the start of the code; consequently, allowing me to call 'hello' later >> when >> I need it. Does this also mean that I will be able to call those functions >> separately later when I import 'thesaurus.py' into a new code also? >> > > Exactly. The first benefit of functions is in encapsulating parts of your > program and providing structure which makles it easier to read but > the real power of functions is in making those blocks of code available > to other programs too. > > Read my tutorial topic on Moduules and Functions more information. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Pete Froslie 617.314.0957 http://www.froslie.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] thesaurus
On 7/12/2009 12:01 PM Pete Froslie said... (2)not sure why this function doesn't work: word_count = 0 #set up variable to increment through text def increment(x): return x+1 increment(word_count) You need to capture and assign the result (or possibly restructure to work with a mutable or simply use word_count+=1 ): word_count = increment(word_count) Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] segmentation fault
"Rick Pasotto" wrote using daily. Suddenly when I try to run it I get a segmentation fault. I'm running debian testing so probably some recent update caused the breakage. How can I find out what's gone wrong? Have you got the core file? If so you should be able to use gdb to examine the stack trace or at least the register dump. It should provide a clue as to which function of which process was at fault, and tell you the input parameters at the time of crash. If you have it, you might find adb useful too. But I don't know if Debian Linux has an adb... Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] thesaurus
Pete Froslie wrote: so, I've been playing with the functions a bit-- took a little time to get the hang of them after I started that other mess. Initially, I've done the opposite suggested here by creating functions without the input values. I do see the where it will pay off in re-usability that route.. Alan, I'll read your tutorial on modules and functions. Things that are arising for me now: (1)the use of global variables- seems it would be easier to communicate between functions with 'I/O' values if I understood where the best location for declaration is.. You don't seem to understand functions. Part of the point is to *avoid* global variables. If everything a function uses is passed in as arguments, and if everything it produces is returned as return value(s), it's much easier to see its effect. And to make sure it doesn't have bugs. (2)not sure why this function doesn't work: word_count = 0 #set up variable to increment through text def increment(x): return x+1 increment(word_count) This function doesn't have any effect on global variables. It returns a value that's one more than its argument. Period. If you wanted to increment word_count using that function, you'd do something like: word_count = increment(word_count) (3) related to strings: Trying a function that will strip unwanted words off of the url before I bounce it off the website. For instance, I don't need to search for a, was, is... This is working, but with strange results-- often it will find an 'a' in the middle of a word and replace it with text and such. using rstrip, but with varying results: #this is the url as a string http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/ thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and re-adding the '/' The url function btw: def url(): fin = open("journey_test.txt", "r") response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']", fin.read()) thesaurus = API_URL + response[word_number] + '/' #API_URL is established at the start of the code return thesaurus Pete F I have no clue what this url() function is trying to do. Nor how you expect to use it. Are you planning to open this file for each word contained in it? DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] thesaurus
> > You don't seem to understand functions. Part of the point is to *avoid* > global variables. If everything a function uses is passed in as arguments, > and if everything it produces is returned as return value(s), it's much > easier to see its effect. And to make sure it doesn't have bugs. True enough- that's why I seek advice. My trouble was with the bit of code I was writing inline; while adapting to the functions I misunderstood their value regarding global variables. I'm happy to say the code is using functions more appropriately now and that, as I learn, it will get closer to something more respectable. I see the advantages for debugging word_count = 0 #set up variable to increment through text > > def increment(x): > >>return x+1 >> increment(word_count) >> >> >> > This function doesn't have any effect on global variables. It returns a > value that's one more than its argument. Period. If you wanted to > increment word_count using that function, you'd do something like: > Thanks-- that makes complete sense; it appears that I just was wrapping my head around functions right away. Initially, I assumed this function could be something quick for any increments. Rather than writing out: 'word_count = word_count + 1' when needed.. I have no clue what this url() function is trying to do. Nor how you expect > to use it. Are you planning to open this file for each word contained in > it? yes. Essentially, it grabs each word from a text file and combines it with the other stuff to create a url string that I send through an API to an online thesaurus. I was attempting to strip it this way as a weak method for cleaning out the words I don't want searched from the text file. Along with the the other functions the code currently scans a text file and replaces each of its words with one of their synonyms.. slightly legible gibberish, but that's what I'm interested in for now. It is a project to help me start learning pyhton as well being intended to take a different form in an artwork I'm working on. thanks. petef On Sun, Jul 12, 2009 at 7:40 PM, Dave Angel wrote: > Pete Froslie wrote: > >> so, I've been playing with the functions a bit-- took a little time to get >> the hang of them after I started that other mess. Initially, I've done the >> opposite suggested here by creating functions without the input values. I >> do >> see the where it will pay off in re-usability that route.. Alan, I'll read >> your tutorial on modules and functions. >> >> Things that are arising for me now: >> >> (1)the use of global variables- seems it would be easier to communicate >> between functions with 'I/O' values if I understood where the best >> location >> for declaration is.. >> >> >> > You don't seem to understand functions. Part of the point is to *avoid* > global variables. If everything a function uses is passed in as arguments, > and if everything it produces is returned as return value(s), it's much > easier to see its effect. And to make sure it doesn't have bugs. > >> (2)not sure why this function doesn't work: >> >> word_count = 0 #set up variable to increment through text >> >> def increment(x): >> >> >>>return x+1 >>> >>> increment(word_count) >>> >>> >>> >> This function doesn't have any effect on global variables. It returns a > value that's one more than its argument. Period. If you wanted to > increment word_count using that function, you'd do something like: > > word_count = increment(word_count) > > (3) related to strings: Trying a function that will strip unwanted words >> off >> of the url before I bounce it off the website. For instance, I don't need >> to >> search for a, was, is... This is working, but with strange results-- often >> it will find an 'a' in the middle of a word and replace it with text and >> such. >> >> using rstrip, but with varying results: >> >> #this is the url as a string >> >> >>> http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/ >>> >>> thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and >>> re-adding the '/' >>> >>> >>> >> >> The url function btw: >> >> def url(): >> >> >>>fin = open("journey_test.txt", "r") >>>response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']", >>> fin.read()) >>>thesaurus = API_URL + response[word_number] + '/' #API_URL is >>> established at the start of the code >>>return thesaurus >>> >>> >>> >> >> Pete F >> >> >> > > I have no clue what this url() function is trying to do. Nor how you > expect to use it. Are you planning to open this file for each word > contained in it? > > DaveA > -- Pete Froslie 617.314.0957 http://www.froslie.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mac os x executable
Hi, the way I do this on mac os x is to use applescript like so. --start of applescript tell application "Terminal" do script "python /Volumes/Seagate750/drive_01/myPythonScript.py" end tell --end of applescript Where you simply point the terminal to the python script using applescript. If you save the applescript as an app, you double click it and it runs the python script. Pete PS this is my first post on this board by the way, so hi everyone. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] find and replace relative to an nearby search string in a file
Hi, I am trying to do a find and replace in a text file (a nuke script). Here are a couple excerpts from the file that demonstrate what I want to do. I am always looking for the line " name Write1" as my starting point. In the first example below, I want to replace the path, which is 2 lines above it. I have made a basic script to do that and it works fine. The problem I am having is when the number of lines between " name Write1" and the path above it is not 1, my script breaks. I'm not sure how to say in python "when you find the line " name Write1", go back line by line until you find a line that begins with " file /Volumes/" and then assign a new variable to the path on that line". At the very bottom of this post I have included what I have so far (The script which works but breaks). Any help would be greatly appreciated. Pete Write { file /Volumes/raid0/Z353_002_comp_v27.%04d.cin file_type cin name Write1 xpos 13762 ypos -364 } Write { file /Volumes/raid0/Z353_002_comp_v04.%04d.exr colorspace linear raw true file_type exr name Write1 selected true xpos -487 ypos -155 } # This is the basic script import re theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk',"r") theNukeScriptText = theFile.read() theFile.close() #p = re.compile('.+_comp_v\d\d.%04d.cin\n file_type cin\n name Write1') p = re.compile(r'.+_comp_v\d\d.%04d.cin\n.+\n name Write1') m = p.finditer(theNukeScriptText) the3Lines = p.findall(theNukeScriptText) the3LinesString = ''.join(the3Lines) theOldSeq = the3LinesString.split('\n')[0] print str(the3LinesString.split('\n')[0]) theNewSeq = 'file /Volumes/raid0/Z353_002_comp_v27.%04d.cin' theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk', "w") theFile.write(theNukeScriptText.replace(theOldSeq,theNewSeq)) theFile.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor