Re: Some more odd behaviour from the Regexp library
In article <[EMAIL PROTECTED]>, "David Veerasingam" <[EMAIL PROTECTED]> wrote: > Can anyone explain why it won't give me my captured group? > > In [1]: a = 'exit: gkdfjgfjdfsgdjglkghdfgkd' > In [2]: import re > In [3]: b = re.search(r'exit: (.*?)', a) > In [4]: b.group(0) > Out[4]: 'exit: ' > > In [5]: b.group(1) > Out[5]: '' > > In [6]: b.group(2) > IndexError: no such group The ? tells (.*?) to match as little as possible and that is nothing. If you change it to (.*) it should do what you want. -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: Favorite non-python language trick?
In article <[EMAIL PROTECTED]>, "Enrico" <[EMAIL PROTECTED]> wrote: > "Joseph Garvin" <[EMAIL PROTECTED]> ha scritto nel messaggio > news:[EMAIL PROTECTED] > > --This code won't run because it's in a comment block > > --[[ > > print(10) > > --]] > > > > --This code will, because the first two dashes make the rest a comment, > > breaking the block > > ---[[ > > print(10) > > --]] > > > > So you can change whether or not code is commented out just by adding a > > dash. This is much nicer than in C or Python having to get rid of """ or > > /* and */. Of course, the IDE can compensate. But it's still neat :) > > python: > > """ > print 10 > """ > > and > > #""" > print 10 > #""" It seems to me that this trick works in Python,too. """ print 10 #""" and #""" print 10 #""" You only have to change the first triple quote. -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: programmatically calling a function
In article <[EMAIL PROTECTED]>, Dave Ekhaus <[EMAIL PROTECTED]> wrote: > hi > > i'd like to call a python function programmatically - when all i have > is the functions name as a string. i.e. > > > fnames = ['foo', 'bar'] > > for func in fnames: > > # > # how do i call function 'func' when all i have is the name of the > function ??? > # > > > > def foo(): > > print 'foo' > > def bar(): > > print 'bar' > > > i'd really appreciate any help the 'group' has to offer. > > > thanks > dave Dave, I think eval might be what you're looking for: f = eval('len') length = f([1,2,3]) By the way, are you the Dave Ekhaus I used to work with at Kodak? -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: programmatically calling a function
In article <[EMAIL PROTECTED]>, Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: > Doug Schwarz wrote: > > > Dave, > > > > I think eval might be what you're looking for: > > > > f = eval('len') > > length = f([1,2,3]) > > But only if the string given to eval is checked thorougly for allowed > contents. Better use getattr. > > Reinhold Actually, upon reading Peter Hansen's reply more carefully, I wil defer to his approach, namely, def foo(): print "foo" f = globals()["foo"] as I suspect that it will be more efficient. You still need to make sure that the string in question is one of the keys in the globals() dictionary or else handle the error -- just as with eval. I don't see how getattr solves the original problem. What, exactly, is the first argument to getattr? -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> wrote: > Hello, > > I have strings represented as a combination of an alphabet (AGCT) and a an > operator "/", that signifies degeneracy. I want to split these strings into > lists of lists, where the degeneracies are members of the same list and > non-degenerates are members of single item lists. An example will clarify > this: > > "ATT/GATA/G" > > gets split to > > [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']] How about this? import re s = "ATT/GATA/G" result1 = re.findall(r"./.|.", s) consensus = [c.split("/") for c in result1] -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> wrote: > Hello, > > I have strings represented as a combination of an alphabet (AGCT) and a an > operator "/", that signifies degeneracy. I want to split these strings into > lists of lists, where the degeneracies are members of the same list and > non-degenerates are members of single item lists. An example will clarify > this: > > "ATT/GATA/G" > > gets split to > > [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']] How about this? import re s = "ATT/GATA/G" result1 = re.findall(r"./.|.", s) consensus = [c.split("/") for c in result1] -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> wrote: > Hello, > > I have strings represented as a combination of an alphabet (AGCT) and a an > operator "/", that signifies degeneracy. I want to split these strings into > lists of lists, where the degeneracies are members of the same list and > non-degenerates are members of single item lists. An example will clarify > this: > > "ATT/GATA/G" > > gets split to > > [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']] How about this? import re s = "ATT/GATA/G" result1 = re.findall(r"./.|.", s) consensus = [c.split("/") for c in result1] -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compute pi to base 12 using Python?
In article <[EMAIL PROTECTED]>, Dick Moores <[EMAIL PROTECTED]> wrote: > Dan wrote at 18:02 4/13/2005: > >On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <[EMAIL PROTECTED]> > >wrote: > > > > > I'm just trying to help an artist acquaintance who needs (I just > > >learned) the first 3003 digits of pi to the base 12. > > > >Now you've got me curious. Why would an artist want the first 3003 > >digits of pi to the base 12? > > He says, > Do you know how I can get "base12 pi"? > Because the chromatic scale is base12. > c c# d d# e f f# g g# a a# b > > Dick Does your artist friend have any idea what base 12 means? The chromatic scale is based on one twelfth powers of two, i.e., if the frequency of a note in the scale is f(n), then the frequency of the next note is given by f(n+1) = f(n) * 2^(1/12) so by the time you go all 12 notes in an octave you have doubled the frequency. There is nothing here involving base 12 or pi. -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: Filename case-insensitivity on OS X
In article <[EMAIL PROTECTED]>, Tom Anderson <[EMAIL PROTECTED]> wrote: > Afternoon all, > > MacOS X seems to have some heretical ideas about the value of case in > paths - it seems to believe that it doesn't exist, more or less, so "touch > foo FOO" touches just one file, you can't have both 'makefile' and > 'Makefile' in the same directory, > "os.path.exists(some_valid_path.upper())" returns True even when > "os.path.split(some_valid_path.upper())[1] in > os.listdir(os.path.split(some_valid_path)[0])" returns False, etc > (although, of course, "ls *.txt" doesn't mention any of those .TXT files > lying around). Strictly speaking, it's not OS X, but the HFS file system that is case insensitive. You can use other file systems, such as "UNIX File System". Use Disk Utility to create a disk image and then erase it (again, using Disk Utility) and put UFS on it. You'll find that "touch foo FOO" will create two files. -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex for repeated character?
In article <[EMAIL PROTECTED]>, Leif K-Brooks <[EMAIL PROTECTED]> wrote: > How do I make a regular expression which will match the same character > repeated one or more times, instead of matching repetitions of any > (possibly non-same) characters like ".+" does? In other words, I want a > pattern like this: > > >>> re.findall(".+", "foo") # not what I want > ['foo'] > >>> re.findall("something", "foo") # what I want > ['f', 'oo'] How's this? >>> [x[0] for x in re.findall(r'((.)\2*)', 'abbccccccbba')] ['a', 'bb', 'ccc', '', 'ccc', 'bb', 'a'] -- Doug Schwarz dmschwarz&urgrad,rochester,edu Make obvious changes to get real email address. -- http://mail.python.org/mailman/listinfo/python-list