Re: Cascading ifs

2007-04-09 Thread Ernesto García García
> tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3, > doSomething3)] > for regex, fun in tbl: > match = regexp.match(line) > if match: >fun(line) >break Thank you for the idea. This is a bit more difficult when functions need to work with a common con

Cascading ifs

2007-04-02 Thread Ernesto García García
Hi experts, How would you do this without the more and more indenting cascade of ifs?: match = my_regex.search(line) if match: doSomething(line) else: match = my_regex2.search(line) if match: doSomething2(line) else: match = my_regex3.search(line) if match: doSom

Re: Is there a commas-in-between idiom?

2006-11-06 Thread Ernesto García García
> I've collected a bunch of list pydioms and other notes here: > >http://effbot.org/zone/python-list.htm Thank you for the suggestion. Ernesto -- http://mail.python.org/mailman/listinfo/python-list

Re: Is there a commas-in-between idiom?

2006-11-05 Thread Ernesto García García
Tim Peters wrote: > More idiomatic as > >if len(list) > 0: > > and even more so as plain > >if list: > >>print list[0], >>for element in list[1:]: >> print ',', element, > > > Do you really want a space before and after each inter-element comma? No, but it was only an e

Re: Is there a commas-in-between idiom?

2006-11-05 Thread Ernesto García García
Ernesto García García wrote: > Hi experts, > > it's very common that I have a list and I want to print it with commas > in between. How do I do this in an easy manner, whithout having the > annoying comma in the end? > > > > list = [1,2,3,4,5,6] > >

Is there a commas-in-between idiom?

2006-11-05 Thread Ernesto García García
Hi experts, it's very common that I have a list and I want to print it with commas in between. How do I do this in an easy manner, whithout having the annoying comma in the end? list = [1,2,3,4,5,6] # the easy way for element in list: print element, ',', print # this is what I really w

Re: building lists of dictionaries

2006-07-23 Thread Ernesto García García
>>parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], >>'limits':[0.,0.]}.copy() for i in xrange(0,6)] >> >>However, this will still reference internal lists that have >>been referenced multiple times, such that >> >> >>> parinfo[5]['limited'] >>[0, 0] >> >>> parinfo[4]['limited'][0] = 2 >> >>> par

Re: [Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
Thank you guys. So the answer is to keep with the original form, perhaps with xrange. Ernesto -- http://mail.python.org/mailman/listinfo/python-list

Re: building lists of dictionaries

2006-07-23 Thread Ernesto García García
> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 With this, you are creating a list with 6 references to the same list. Note that the left operand of '*' is evaluated only once before "multiplying" it six times. Regards, Tito -- http://mail.python.org/mailman/listinfo

Re: [Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
>>I'm sure there is a better way to do this: >> >>[random.choice(possible_notes) for x in range(length)] > There is at least a better way to ask the question. The subject has > nothing to do with the body of your post. Or am I missing something? Sorry, I should have explained better. I just wan

[Newbie] List from a generator function

2006-07-23 Thread Ernesto García García
Hi all, I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] Regards, Ernesto -- http://mail.python.org/mailman/listinfo/python-list

Re: [Newbie] Referring to a global variable inside a function

2006-04-09 Thread Ernesto García García
>>How would you do this? > > def line_action(line, match_dictionary): > global count # make it a module-global variable, not a function-local > count = count + 1 > > OK, I had put it on the global block. Thanks, Ernesto -- http://mail.python.org/mailman/listinfo/python-lis

[Newbie] Referring to a global variable inside a function

2006-04-09 Thread Ernesto García García
Hi experts, I've built a class for parsing a user-defined list of files and matching lines with a user-defined list of regular expressions. It looks like this: import re import glob class LineMatcher: """ Parses a list of text files, matching their lines with the given regular expression