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 context, but in that case I could store the 
context in an object and use the object's methods.

Thanks,
Ernesto
-- 
http://mail.python.org/mailman/listinfo/python-list


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:
   doSomething3(line)

etc.

Thanks in advance and regards,
Ernesto
-- 
http://mail.python.org/mailman/listinfo/python-list


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


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?

code

list = [1,2,3,4,5,6]

# the easy way
for element in list:
   print element, ',',

print


# this is what I really want. is there some way better?
if (len(list)  0):
   print list[0],
   for element in list[1:]:
 print ',', element,

/code

Thx,
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
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?
 
 code
 
 list = [1,2,3,4,5,6]
 
 # the easy way
 for element in list:
   print element, ',',
 
 print
 
 
 # this is what I really want. is there some way better?
 if (len(list)  0):
   print list[0],
   for element in list[1:]:
 print ',', element,
 
 /code
 
 Thx,
 Ernesto

mylist = [1,2,3,4,5,6]
print ','.join(map(str, mylist))

Great solution!

Thank all of you,
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 example. I usually go for string concatenation.

 An often-overlooked alternative to playing with ,.join() is:
 
print str(list)[1:-1]

That's funny! Not that I like it more that the join solution, but funny 
nevertheless.

Thank you,
Ernesto
-- 
http://mail.python.org/mailman/listinfo/python-list


[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] 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 want to build a fix length 
list made up of elements generated by a function, in this case 
random.choice(). I don't like my list comprehension, because I'm using 
that dumb variable x and the range() list.

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/python-list


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.]}.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
  parinfo[5]['limited']
[2, 0]
 
 Interesting. Cut-and-paste to my python prompt and I get
 
parinfo[5]['limited']
 
 [0, 0]
 
 Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2

Of course. The expression within the list comprehension is evaluated for 
each iteration, so that the objects are recreated each time. The copy() 
for the dictionary is also not needed:

  parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 
'limits':[0.,0.]} for i in xrange(0,6)]
  parinfo
[{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}]
  parinfo[0]['limited']
[0, 0]
  parinfo[0]['limited'][0]=1
  parinfo
[{'limited': [1, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, 
{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}]

Ernesto
-- 
http://mail.python.org/mailman/listinfo/python-list


[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:

code
import re
import glob

class LineMatcher:
Parses a list of text files, matching their lines with the given
   regular expressions and executing associated actions.

   def __init__(self):
 # List of file names to parse for matching.
 self.file_names = []
 # Association of reg expressions to actions to execute when match.
 self.regexp_action = {}

   def go(self):
 for file_name in self.file_names:
   file = open(file_name)
   for line in file:
 for regexp, action in self.regexp_action.items():
   match = regexp.match(line)
   if match:
 action(line, match.groupdict())

   def add_files(self, file_pattern):
 self.file_names.extend(glob.glob(file_pattern))

   def add_action(self, regexp_string, action):
 self.regexp_action[re.compile(regexp_string)] = action
/code

But then, when I try to use my class using actions with memory it will 
fail:

code
import LineMatcher

global count
count = 0

def line_action(line, match_dictionary):
   count = count + 1

line_matcher = LineMatcher.LineMatcher()
line_matcher.add_files('*')
line_matcher.add_action(r'(?Pline.*)', line_action)
line_matcher.go()
/code

The error is:
console
Traceback (most recent call last):
   File Test.py, line 12, in ?
 line_matcher.go()
   File LineMatcher.py, line 21, in go
 action(line, match.groupdict())
   File Test.py, line 7, in line_action
 count = count + 1
UnboundLocalError: local variable 'count' referenced before assignment
/console

How would you do this?

Regards,
Tito
-- 
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
 
 /F

OK, I had put it on the global block.

Thanks,
Ernesto
-- 
http://mail.python.org/mailman/listinfo/python-list