Problem with subprocess module on Windows with open file in append mode

2009-10-03 Thread Andrew Savige
When I run this little test program on Linux: import subprocess subprocess.call(["python","-V"], stderr=open("log.tmp","a")) the file log.tmp is appended to each time I run it. When I run it on Windows, however, the file log.tmp gets overwritten each time I run it. Though I can make it append on

Lexical scope: converting Perl to Python

2009-06-12 Thread Andrew Savige
I'd like to convert the following Perl code to Python:  use strict;  {    my %private_hash = ( A=>42, B=>69 );    sub public_fn { my $param = shift; return $private_hash{$param};    }  }  print public_fn("A");    # good:  prints 42  my $x = $private_hash{"A"};  # error: good, hash n

Re: Learning Python via a little word frequency program

2008-01-09 Thread Andrew Savige
Fredrik Lundh wrote: ># sort items on descending count >deco = sorted(freq.items(), key=lambda x: -x[1]) Neat. Is there any way to use sorted() with multiple sort keys? ... Given that the spec calls for sorting by _two_ keys: first by frequency (descending), then by name (ascending). To c

Learning Python via a little word frequency program

2008-01-09 Thread Andrew Savige
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = "freddy fred

Re: Split a string based on change of character

2007-07-28 Thread Andrew Savige
--- "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Using itertools: > > import itertools > > s = 'ABBBCC' > print [''.join(grp) for key, grp in itertools.groupby(s)] Nice. > Using re: > > import re > > pat = re.compile(r'((\w)\2*)') > print [t[0] for t in re.findall(pat, s)] Also nice. Esp

Split a string based on change of character

2007-07-28 Thread Andrew Savige
Python beginner here. For a string 'ABBBCC', I want to produce a list ['A', 'BBB', 'CC']. That is, break the string into pieces based on change of character. What's the best way to do this in Python? Using Python 2.5.1, I tried: import re s = re.split(r'(?<=(.))(?!\1)', 'ABBBCC') for e in s: pri