[Tutor] New Python testing book
Amazon have just brought to my attention the following title: Python Testing: Beginner's Guide Has anyone seen this? Is it any good? What test frameworks does it cover? Amazon are remarkably reticent about the actual contents. Its very new, only published on 22nd January. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Python testing book
Hi Alan, On 12/02/10 17:34, Alan Gauld wrote: Amazon are remarkably reticent about the actual contents. See here: http://tinyurl.com/y9dy62p I am, btw, always happy to see 'book announcements' on this list -- keep them coming! David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Python testing book
On Fri, Feb 12, 2010 at 10:26 AM, David wrote: > Hi Alan, > > > On 12/02/10 17:34, Alan Gauld wrote: > > Amazon are remarkably reticent about the actual contents. >> > See here: http://tinyurl.com/y9dy62p > > I am, btw, always happy to see 'book announcements' on this list -- keep > them coming! > > David For those who don't like shortened URL, here's the actual link http://www.packtpub.com/view_popup/page/python-testing-beginners-guide-table-of-contents It's just the Table of Content and nothing else. -- Evans > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Coin flip game
Hi, random.choice offers an intuitive way to write the code: import random for i in range(10): print random.choice(["head", "tail"]) Cheers!! Albert-Jan ~~ In the face of ambiguity, refuse the temptation to guess. ~~ --- On Fri, 2/12/10, David wrote: From: David Subject: Re: [Tutor] Coin flip game To: tutor@python.org Date: Friday, February 12, 2010, 1:49 AM Hello Lawrence, let me try to clarify this (warning: am a beginner myself). On 12/02/10 06:15, Jones, Lawrence D wrote: > Hi, > > I'm new to the list and to Python. I'm reading through Michael Dawson's > 'Python programming: for absolute beginners' and at the end of chapter 3 he's > set a challenge where the reader has to create a coin flip game. My code now > works, but only after I randomly switched pieces of the code around and, > basically, pulled my hair out because it wouldn't work. > > My code is below. But can someone please explain to me why the following > variable has to be placed where it is for the code to work? I thought it > would need to go nearer the start of the code i.e. just before heads = 0, > tails = 0 etc: > > coin = random.randrange(2) Python runs through your code, step by step. I believe it starts at the top and goes down, following the logic of your code. When you make Python refer to a variable in your while loop that Python has not encountered yet, then it will not know what to do -- and complain about it. Solution: let Python know of the variable _before_ you then start to work with it. > > Also, why does the randrange integer have to be '2'? I only discovered this > worked by complete accident. I tried '1' and '0,1' as my integers but they > just didn't work. That is because your coin has _two_ sides, and you therefore want a random choice out of _two_ possibilities. With the random.randrange(2) function the choices will be 0 and 1, satisfying your demands. This means that the randrange() function goes up to, but not including, the integer you supply. It amounts to two choices in the end all the same because the counting starts with 0 instead of 1. That is, if you chose randrange(1) you will get only one answer, namely 0. If you type randrange(0) then you will get an error message (ValueError: empty range for randrange). Which makes sense. Remember, randrange() goes up to, but not including the integer supplied. HTH, David > > Thanks, > > Lawrence > > > import random > print "The Coin Flip Game\n" > > heads = 0 > tails = 0 > count = 0 > > while count< 100: > coin = random.randrange(2) > if coin == 0: > heads = heads + 1 > else: > tails = tails + 1 > count += 1 > > print "Heads: ", heads > print "Tails: ", tails > > raw_input("\nPress enter to exit.") > > > > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tutor list as pair progamming plush toy
Have you ever got that piece of advice about - when you have stuck on a bug you seem unable to track - getting a plush toy to whom you explain your code? (This is of course a workaround if you do not have a fellow developer to help you out). Well... I found out this advice kind of works for me, with the notable difference that my plush toy is this mailing list. It works so wonderfully that indeed is several months I do not post any message: whenever I get stuck, I begin to write a message to the list, and in the process of explaining what is the intended behaviour and outcome of my code, I systematically find the bug by myself. I know - this is slightly OT for the list - but I thought to share as maybe this is a "hidden benefit" the list is bringing to a few people without the tutors even knowing it. Does anybody else experience the same? Cheers, :) Mac. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor list as pair progamming plush toy
In similar vein, I find that a concept suddenly makes more sense to me when I try to explain it to someone else (or I realize that I don't fully understand and need to do some more research). But with regard to the plush toy you mention, I just ran into that anecdote in Coders at Work. Can't recall off the top of my head which developer mentioned it, but it's an interesting concept. Btw, I'd recommend that book to anyone interested in expanding their horizons on coding philosophies and methodologies. Some of it's pretty high-brow comp sci discussion, but there's a lot about basic testing and debugging approaches, and how one should go about reading someone else's code. I'm only half-way through the book, but the variety of responses and approaches is fascinating. Serdar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor list as pair progamming plush toy
> -Original Message- > From: tutor-bounces+mike.hansen=atmel@python.org > [mailto:tutor-bounces+mike.hansen=atmel@python.org] On > Behalf Of Mac Ryan > Sent: Friday, February 12, 2010 8:33 AM > To: tutor@python.org > Subject: [Tutor] Tutor list as pair progamming plush toy > > Have you ever got that piece of advice about - when you have > stuck on a > bug you seem unable to track - getting a plush toy to whom you explain > your code? (This is of course a workaround if you do not have a fellow > developer to help you out). > > Well... I found out this advice kind of works for me, with the notable > difference that my plush toy is this mailing list. It works so > wonderfully that indeed is several months I do not post any message: > whenever I get stuck, I begin to write a message to the list, > and in the > process of explaining what is the intended behaviour and outcome of my > code, I systematically find the bug by myself. > > I know - this is slightly OT for the list - but I thought to share as > maybe this is a "hidden benefit" the list is bringing to a few people > without the tutors even knowing it. > > Does anybody else experience the same? > > Cheers, :) > Mac. > This kind of sounds like the rubber duck method of debugging. http://lists.ethernal.org/oldarchives/cantlug-0211/msg00174.html Mike ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor list as pair progamming plush toy
I've discovered that same thing. Usually I end out not sending my message because in the process of composing my email I end out I, too, find the errors. -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] batch file processing w/ python using cmd line executable?
hi I am new territory here and not even sure where to start poking around other than the os module some. Essentially i need to do something like a shell script for batch processing gobs of files. I am trying to use a command line tool (sox, an open source sound file converter that runs from the unix command line) and I don't want to edit the command line, run the job, edit the command line, etc over and over again for hundreds of small files. I wonder if it is possible to use python to call sox and have it do os.mkdir, process all the input files in a particular directory and put the converted files into the directory it made with mkdir... so if i had kp/flute/ST kp8/flute/ST/foo01.aif kp8/flute/ST/foo02.aif kp8/flute/ST/foo03.aif kp8/flute/ST/foo04.aif kp8/flute/ST/foo05.aif The script would call sox repeatedly and create a new dir with a converted file for each found in the original folder. like so: kp/flute/STout/ kp/flute/STout/foo01.wav kp/flute/STout/foo02.wav kp/flute/STout/foo03.wav kp/flute/STout/foo04.wav kp/flute/STout/foo05.wav what makes this especially hairy is that sox is a monster an typically needs a crazy number of arguments, though these would be the same for the whole batch. A typical command line call i need to make to, say, batch convert files from one sample rate and bit depth to another would look like so: % sox -V3 -D -S St.01.aif -b16 kp/flute/STout/St.01.wav rate -s -v 44100 Is there away to do this in python, by just pointing it to a whole dir of files and say "do it" to all of these? cheers, kevin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm for combination analysis suggestion.
Matthew Matson wrote: Hi Tutors, I am looking for the proper approach regarding the analysis of a dictionary of combinations I have. What I need to do is read from a supplied text file that has a unique ID and that unique ID's associated combination of elements. So let's say I have the following lines in a text file (real file could be millions of lines): "ID""Elements" 1'A, B, C, D' 2'A, D' 3'D, E' 4'A, D' 5'A, B' 6'A, C, D' and I do something like... combinationDict = {} for line in file: data = line.split('\t') comb = tuple(data[1].split(',')) if comb not in combinationDict: combinationDict[comb] = 1 else: combination[comb] +=1 Now after I read all of the data I end up with a dictionary with the combination as the key and the associated total qty as its value. print combinationDict {('A','B','C','D'):1, ('A','D'):2, ('D','E'):1, ('A','B'):1, ('A', 'C', 'D'):1} What I am looking for is a starting point for a solution in python to analyze the combination list so that I can determine for example that ('A', 'D') is the most popular combination and then determining how many other combinations in the dictionary contain this combination. I would like to incorporate some parameters so for example the combination ('A','B','C','D') and ('A', 'C', 'D') contain ('A','D') so they are valid but I could also say that as long as one element is contained in a combination it is valid as well provided I add no more than one additional item to the combination. If I apply this logic then ('D','E') can ('A','B') can contain ('A', 'D') and if I apply this to the combination dictionary I have: {('B','C', ('A', 'D')):1, ('A','D'):2, ('E', ('A', 'D')):1, ('B', ('A', 'D')):1, ('C', ('A', 'D')):1} which I could then query the keys for ('A', 'D') inclusion to get a total of 4 for ('A', 'D'). I hope this isn't too long and confusing but I am looking for an approach where I can analyze for the highest quantity of combinations and then iterate through the dictionary substituting those combinations that were determined a "highest qty" combination into other low qty combinations when valid. I was hoping to have parameters to qualify a high qty combination (e.g. every combination with qty above 10,000) with the highest quantity of that determined set taking precedence for substitution for the first pass then moving on to the next highest combination for the second pass of substitution etc.. The other parameter would be for the combination that would receive a substitution whereby I might say that I can only substitute if a substitution results in only one additional (superfluous) value being added to the combination existing low qty combination. I have looked around and this sounds like it might be similar to a packing problem and in particular the knapsack problem but I can't seem to wrap my head around an approach for this in python. I am not looking for a solution just some guidance on a starting point or perhaps libraries that may be helpful. Thank you. Hey, never heard of commas! I'm out of breath after that ;-) It sounds something like Linear Programming - not that I know much about it, but maybe asking on other lists, eg. scipy, might turn up some kind of standard approach to this kind of problem. One idea for the 'is a combination contained in another combination' issue is using bitmasks. Create a bitmask for each combination, then C1 is a component of C2 if C1 & C2 == C1. (Hope i've got that right!) Some code doodling below shows the idea. Regards G.F. - from collections import defaultdict data = """ 1A, B, C, D 2A, D 3D, E 4A, D 5A, B 6A, C, D 7E """.splitlines() atoms = {} key2mask = {} mask2key = {} combos = defaultdict(int) for line in data: if line: key = '' mask = 0 for atom in line[5:].split(','): atom = atom.strip() key += atom mask |= atoms.setdefault(atom, 1 << len(atoms)) key2mask[key] = mask mask2key[mask] = key combos[mask] += 1 for combo in ['AB', 'AD', 'DE', 'E']: mask = key2mask[combo] print for c in combos.iterkeys(): if mask & c == mask: print '%s is a component of %s' % (combo, mask2key[c]) print 'there are %s %ss' % (combos[mask], combo) -- def xuniqueCombinations(items, n): if n==0: yield [] else: for i in xrange(len(items)): for cc in xuniqueCombinations(items[i+1:],n-1): yield [items[i]]+cc def allcombos(): for k in range(1,6): for c in xuniqueCombinations('ABCDE', k): yield c --- ___ Tutor maillist - Tutor@python.org To unsubscribe or c
Re: [Tutor] Tutor list as pair progamming plush toy
--- On Fri, 2/12/10, Hansen, Mike wrote: From: Hansen, Mike Subject: Re: [Tutor] Tutor list as pair progamming plush toy To: tutor@python.org Date: Friday, February 12, 2010, 11:55 AM > -Original Message- > From: tutor-bounces+mike.hansen=atmel@python.org > [mailto:tutor-bounces+mike.hansen=atmel@python.org] On > Behalf Of Mac Ryan > Sent: Friday, February 12, 2010 8:33 AM > To: tutor@python.org > Subject: [Tutor] Tutor list as pair progamming plush toy > > Have you ever got that piece of advice about - when you have > stuck on a > bug you seem unable to track - getting a plush toy to whom you explain > your code? (This is of course a workaround if you do not have a fellow > developer to help you out). > > Well... I found out this advice kind of works for me, with the notable > difference that my plush toy is this mailing list. It works so > wonderfully that indeed is several months I do not post any message: > whenever I get stuck, I begin to write a message to the list, > and in the > process of explaining what is the intended behaviour and outcome of my > code, I systematically find the bug by myself. > > I know - this is slightly OT for the list - but I thought to share as > maybe this is a "hidden benefit" the list is bringing to a few people > without the tutors even knowing it. > > Does anybody else experience the same? > > Cheers, :) > Mac. > +1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor list as pair progamming plush toy
"Mac Ryan" wrote I know - this is slightly OT for the list - but I thought to share as maybe this is a "hidden benefit" the list is bringing to a few people without the tutors even knowing it. Actually I think it is bang on topic. One of the most common benefits of any online community is the way we are foced to think about propblems to write them down. Doing so ioften brings new solutions to mind. In fact I often find solutioons to my own problems while replying to others! - even on seemingly unrelated issues :-) And of course if the solution doesn't come you have the 2nd level support option of actually posting and getting replies! :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] batch file processing w/ python using cmd line executable?
"kevin parks" wrote I wonder if it is possible to use python to call sox and have it do os.mkdir, process all the input files in a particular directory and put the converted files into the directory it made with mkdir... Of course. What do you perceive to be the problem? This would just be a standard loop or os.walk call. The script would call sox repeatedly and create a new dir with a converted file for each found in the original folder. like so: Presumably a new dir for each dir found? Thus mirroring the source structure? Thats easily done with os.walk and would be a good place to start writing the script. Calling sox can be done using the subprocess module. what makes this especially hairy is that sox is a monster an typically needs a crazy number of arguments, though these would be the same for the whole batch. So store them in a list and apply them to each call. subprocess makes this easy. % sox -V3 -D -S St.01.aif -b16 kp/flute/STout/St.01.wav rate -s -v 44100 Is there away to do this in python, by just pointing it to a whole dir of files and say "do it" to all of these? I'd check out os.walk and subprocess. Both are discussed in the Using the OS topic in my tutorial. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Defining operators for custom types
I created a custom vector type, inheriting from object, and defined __mul__, __add__, etc. Unfortunately, the operators only work if I type "vector * (int/float/number)", in that exact order. My program produces an error if I instead type "number * vector". This makes sense to me, because I haven't told the number (int, float, whatever) how to deal with an object of type vector, (At least, I think that's why it doesn't work.). Is there any way to allow "number (operator) vector", short of modifying the standard types' behavior? Here's an example of the error. vec1 = vector(5,6,7) >>> vec1 * 2 (10, 12, 14) >>> 2 * vec1 Traceback (most recent call last): File "", line 1, in 2 * vec1 TypeError: unsupported operand type(s) for *: 'int' and 'vector' I'm using python 3.1. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Defining operators for custom types
Mark Young wrote: I created a custom vector type, inheriting from object, and defined __mul__, __add__, etc. Unfortunately, the operators only work if I type "vector * (int/float/number)", in that exact order. My program produces an error if I instead type "number * vector". Use __rmul__, __radd__, etc. This makes sense to me, because I haven't told the number (int, float, whatever) how to deal with an object of type vector, (At least, I think that's why it doesn't work.). Is there any way to allow "number (operator) vector", short of modifying the standard types' behavior? Here's an example of the error. vec1 = vector(5,6,7) >>> vec1 * 2 (10, 12, 14) >>> 2 * vec1 Traceback (most recent call last): File "", line 1, in 2 * vec1 TypeError: unsupported operand type(s) for *: 'int' and 'vector' I'm using python 3.1. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Defining operators for custom types
Thanks for the help. That's exactly the kind of solution I wanted. I've been to that page several times already, but just ignored that section apparently. Thanks alot. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] aliasing an imported module
I want to be able to import multiple instances of a module and call each by a unique name and it doesn't appear at first glance that either import or __import__ have what I need. I need this because of computing platform I have developed where users write python scripts to do some analysis of science problems where they call modules that have code for monipulating data and where the imported module does some specific scientfic manipulation of data. The key problem is that the module might locally store some partial results ready for the next time its called to save CPU time (typically the results for one timestep ready for the next timestep). But if the module is called for two different purposes in two different parts of the script then the second call will actually see the partial results from the 1st call and vice versa. The simple solution is if it were possible to import the same module file twice but for them to act like they were different modules. It was also make it easy to parallelise code without the called module needing to be thread safe (each instance would be one thread) but that is a lesser priority for the moment. If I have module in a file called stuff.py has in it a global variable somevariable I want to be able to import multiple instance (with seperate name spaces etc) of stuff so that I could have for example a=instance1ofstuff.somevariable b=instance2ofstuff.somevariable and a and b are referring to different variables in entirely different modules. IS there any way to do this? Prof Garry Willgoose, Australian Professorial Fellow in Environmental Engineering, Director, Centre for Climate Impact Management (C2IM), School of Engineering, The University of Newcastle, Callaghan, 2308 Australia. Centre webpage: www.c2im.org.au Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574 (Fri PM-Mon) FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal and Telluric) Env. Engg. Secretary: (International) +61 2 4921 6042 email: garry.willgo...@newcastle.edu.au; g.willgo...@telluricresearch.com email-for-life: garry.willgo...@alum.mit.edu personal webpage: www.telluricresearch.com/garry "Do not go where the path may lead, go instead where there is no path and leave a trail" Ralph Waldo Emerson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] aliasing an imported module
"Garry Willgoose" wrote I want to be able to import multiple instances of a module and call each by a unique name and it doesn't appear at first glance that either import or __import__ have what I need. No because it would be bad practice. Stateful modules lead to hard to debug prolems. computing platform I have developed where users write python scripts to do some analysis of science problems where they call modules that have code for monipulating data and where the imported module does some specific scientfic manipulation of data. The key problem is that the module might locally store some partial results ready for the next time its called to save CPU time This is where classes come in. You can define a class in your module with the analysis done in a method and have it store the results in instance variables. Then you can either call another method which can access those variables or you can create another instance of the class with its own independant copies of its values. But if the module is called for two different purposes in two different parts of the script then the second call will actually see the partial results from the 1st call and vice versa. Technically you don't call a module you import it which has the side-effect of executing the code in the module. In a well structured module the code should really be inside function definitions so that nothing haoppens until you call the modules functions. importing modules as a way of executing the code within them is not good practice. The simple solution is if it were possible to import the same module file twice but for them to act like they were different modules. It was also make it easy to parallelise code without the called module needing to be thread safe (each instance would be one thread) but that is a lesser priority for the moment. Again classes can do that for you. Either by having two instances of the class in a single thread and multiplexing the calls to their methods (round-robin style) or, if necessary, by having two instances each in their own thread executing independantly. If I have module in a file called stuff.py has in it a global variable somevariable I want to be able to import multiple instance (with seperate name spaces etc) of stuff so that I could have for example a=instance1ofstuff.somevariable b=instance2ofstuff.somevariable With a class that becomes: import stuff a = stuff.myClass() b = stuff.myClass() a.myFunction(x,y,z) b.myFunction(a,b,c) print a.myvar, b.myvar etc. and a and b are referring to different variables in entirely different modules. IS there any way to do this? a and b refer to independant instances of the same class each with its own myvar variable inside. See the OOP topic of my tutorial for more on Object Oriented Programming HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Just a Note
By the way, Alan Gauld's emails generate an error in my system. His is the only emails I have a problem with so far. At first it tried to open a News service. Randy Raymond ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just a Note
This doesn't make any sense. What do you mean "tries to open up a news service"? When you read the e-mail? What system are you using? On Fri, Feb 12, 2010 at 8:30 PM, Randy Raymond wrote: > By the way, Alan Gauld's emails generate an error in my system. His is > the only emails I have a problem with so far. At first it tried to open a > News service. > > Randy Raymond > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] The Order of Imports and install order of modules and other matters (XP vs W7, ...)
There seems to be something of a general consensus in ordering import statements. Something like standard library imports first. When using tools like matlablib or tkinter (maybe), must one keep an order among the relevant imports? Related to this is the order in which modules are installed. Does it make a difference? Finally, I'm in the process of moving Python code from XP to Win7. I just grabbed all the install files I have from XP, and executed them on W7. Everything seems to be working as expected, but one strange thing happened with scipy. It produced a warning about something like "unable to provide key". I continued anyway. All seems well. Was I supposed to use some W7 version of the "XP" files? This is anomaly 1. OK, this the last one. In both XP and W7, I've found executing a program by use of the py file (not IDLE. Is there a name for this method?) using numpy that see early on in the DOS-like window (is there name for it too?) it's complaining (alerting me) about deprecations and some use of a numpy test. What's that about? This is anomaly 2. -- "Crime is way down. War is declining. And that's far from the good news." -- Steven Pinker (and other sources) Why is this true, but yet the media says otherwise? The media knows very well how to manipulate us (see limbic, emotion, $$). -- WTW ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Not understanding this code example. Help, please.
Hello! I was reading the latest version of Mark Pilgrim's "Dive into Python" and am confused with these example about the pluralization rules. See http://diveintopython3.org/examples/plural3.py and http://diveintopython3.org/generators.html#a-list-of-patterns Here is part of the code: import re def build_match_and_apply_functions(pattern, search, replace): def matches_rule(word): return re.search(pattern, word) def apply_rule(word): return re.sub(search, replace, word) return (matches_rule, apply_rule) patterns = \ ( ('[sxz]$', '$', 'es'), ('[^aeioudgkprt]h$', '$', 'es'), ('(qu|[^aeiou])y$', 'y$', 'ies'), ('$','$', 's') ) rules = [build_match_and_apply_functions(pattern, search, replace) for (pattern, search, replace) in patterns] def plural(noun): for matches_rule, apply_rule in rules: if matches_rule(noun): return apply_rule(noun) this example works on IDLE: print plural("baby") My question is "baby" assigned to "word" in the inner function? It's a little mind bending for me... Thanks, Eduardo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor