Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Alan Gauld
 1) the os.system module
 2a-d) os.popen, and popen2 popen3 and popen4
 3) the popen2 module
 4) the subprocess module

Take a look at the OS topic in my tutorial. The process control section
covers all of these calls and explains their differences. It also points out
that the latter is intended to replace all the others and shows examples.

 #1 is synchronous, which is what I want, but it doesn't seem to have any
 means to capture stdout.

Actually they are all synchronous if the command is not interactive.
If the command requires input before it completes then it will wait
for Python to supply it if using popen (in the same way that it would
from a user if using system).

 #s 2-4 look like they support capturing stdout, but they seem to be
 asynchronous, which is at the very least overkill for me.

output = popen(command).read()

isn't too hard is it?

 More detail, in case it matters:  the command line I need to execute is
 actually three commands, with pipes between them, i.e.,

  string xxx | grep zz | head -10

Just make sure its all within a string and Python doesn't care.
But in this case I'd use Pytthon to search for the string and get the
last 10 entries rather than grep/head...

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cannot Understand

2006-03-11 Thread Alan Gauld
 we don't all agree on everything, but that we can disagree politely and
 still be helpful.

One of the truly great things about Python is the user community, by 
far the most civilised that I've encountered in my 20 years or so of 
internet use.

Long may it continue,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread Kent Johnson
kevin parks wrote:
 I have several lists... and i would like to some times chose from one 
 list and for a while choose from a different list, etc.

You don't say what isn't working but I have a guess. The actual windex() 
function looks OK to me.

 def test():
 
   lst_a = [ 'red', 'green', 'blue', 'orange', 'violet', 'yellow', 
 'black', 'white' ]
   lst_b = ['Rottweiler', 'Beagle', 'Sheepdog', 'Collie', 'Boxer', 
 'Terrier', 'Bulldog', 'Chihuahua', 'Retriever', 'Collie', 'Dachshund', 
 'Doberman', 'Greyhound', 'Pug', 'Spaniel']
   lst_c = ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
   lst_d = ['fender', 'gibson', 'rickenbacker', 'guild', 'danelectro', 
 'gretsch', 'martin', 'ibanez']
   x = [('lst_a', .50), ('lst_b', .25), ('lst_c', .10),('lst_d', .15)]

x is list containing the *names* of the other lists. You need to keep 
references to the lists so you can pick from them.
x = [(lst_a, .50), (lst_b, .25), (lst_c, .10),(lst_d, .15)]

   i = 1
   while i  100:
   lst = windex(x)
   print i, lst,

with the change above this will print the list, not its name
   pick = random.choice(lst)
but this will work.

If you want to be able to print the name of the list then you could 
include both the name and the actual list in x:

x = [(('lst_a', lst_a), .50), etc...]

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode and regexes

2006-03-11 Thread Kent Johnson
Michael Broe wrote:
 Does Python support the Unicode-flavored class-specifications in  
 regular expressions, e.g. \p{L} ? It doesn't work in the following  
 code, any ideas?

 From http://www.unicode.org/unicode/reports/tr18/ I see that \p{L} is 
intended to select Unicode letters, and it is part of a large number of 
selectors based on Unicode character properties.

Python doesn't support this syntax. It has limited support for Unicode 
character properties as an extension of the \d, \D, \s, \S, \w and \W 
sequences. For example with
   numbers = re.compile(r'\d', re.UNICODE)

numbers will match any Unicode digit.

You can combine and difference the built-in categories to get more 
possibilities. This thread shows how to construct a regex that finds 
just Unicode letters:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/6ef6736581fecaeb/a49326cb48c408ee?q=unicode+character+classrnum=1#a49326cb48c408ee

Python does have built-in support for the Unicode character database in 
the unicodedata module, so for example you can look up the character 
class of a character. You can roll your own solution on top of this 
data. This thread shows how to build your own regex category directly 
from a property in unicodedata:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/fdfdec9a0649c540/471331f518fa680f?q=unicode+character+classrnum=2#471331f518fa680f

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Sorting and secondary sorting.

2006-03-11 Thread Liam Clarke
Hi all,

I'm trying to think of a way to sort a list of dictionaries. In pseudo-code:

l = [ { host:foo, db:bob},
   { host:foo, db:dave},
   { host:fee, db:henry}
 ]

l.sort( key = lambda item: item[host], second_key = lambda item: item[db])

Which, if all went well, would give me -

l = [ { host:fee, db:henry}
   { host:foo, db:bob},
   { host:foo, db:dave},
]

So, I'm trying to sort and then do a secondary sort. I'd like to do it
Pythonically; currently I'm creating a Pysqlite db in memory and
sticking the data in a table, and selecting it back out with an ORDER
BY clause, and then reconstituting it into dictionaries in a list.

I get that overlooking something simple feeling on this one, so any
assistance welcomed.
I got lost with lists and lists of lists and joining lists back
together, so I cheated and went the SQL way.

Regards,

Liam Clarke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting and secondary sorting.

2006-03-11 Thread Karl Pflästerer
On 11 Mrz 2006, [EMAIL PROTECTED] wrote:

 I'm trying to think of a way to sort a list of dictionaries. In pseudo-code:

 l = [ { host:foo, db:bob},
{ host:foo, db:dave},
{ host:fee, db:henry}
  ]

 l.sort( key = lambda item: item[host], second_key = lambda item: item[db])

 Which, if all went well, would give me -

 l = [ { host:fee, db:henry}
{ host:foo, db:bob},
{ host:foo, db:dave},
 ]

 So, I'm trying to sort and then do a secondary sort. I'd like to do it
 Pythonically; currently I'm creating a Pysqlite db in memory and
 sticking the data in a table, and selecting it back out with an ORDER
 BY clause, and then reconstituting it into dictionaries in a list.

One easy way could be:

. l = [ { host:foo, db:bob},
.   { host:foo, db:dave},
.   { host:fee, db:henry}
. ]
 ... ...  l.sort(key=lambda d: (d['host'], d['db'])) 
. l
.[{'host': 'fee', 'db': 'henry'}, {'host': 'foo', 'db': 'bob'}, {'host': 'foo', 
'db': 'dave'}]

Or you write the above explicitly with DSU; decorate with a tuple of
host name and db name, sort and undecorate. 


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting and secondary sorting.

2006-03-11 Thread Kent Johnson
Liam Clarke wrote:
 Hi all,
 
 I'm trying to think of a way to sort a list of dictionaries. In pseudo-code:
 
 l = [ { host:foo, db:bob},
{ host:foo, db:dave},
{ host:fee, db:henry}
  ]
 
 l.sort( key = lambda item: item[host], second_key = lambda item: item[db])
 
 Which, if all went well, would give me -
 
 l = [ { host:fee, db:henry}
{ host:foo, db:bob},
{ host:foo, db:dave},
 ]
 

Just make a key that includes both of the values you want to sort on as 
a tuple:

l.sort( key = lambda item: (item[host], item[db]))

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] need help tracing a syntax error

2006-03-11 Thread Kermit Rose
def vmod(a , b ):

.r1 = b
.r2 = a
. m1 = 0
.m2 = 1


.q = Int(r1 / r2)

.r3 = r1 - q * r2
.m3 = m1 - q * m2


.while r3 != 0:
...r1 = r2
...m1 = m2
...r2 = r3
...m2 = m3
...q = Int(r1 / r2)
...r3 = r1 - r2 * q
...m3 = m1 - m2 * q

.If r2 == 1:

...If m2  0: 
.return( m2 + b)
...Else:
.return( m2 )


.Else:
...return( -r2 )
 



When I attempt to run this function from the shell run menu,

I get the message 

syntax error


and it highlightsr2  

in the line

.If r2 == 1:

However if I 
use the eval function in the shell, I get

 eval(factor30)
module 'factor30' from 'c:\math\factoring\factor30.pyc'
 

no error message.


and when I use help to look at the associated module,

this function is not listed.   It must be because of the syntax error.

 help(factor30)
Help on module factor30:

NAME
factor30

FILE
c:\math\factoring\factor30.py

FUNCTIONS
factor(z)

factor0(z)

gcd(a, b)

ifprime(z)

ksqrt(j)

test(tv)

transfac(v)



Give me some hint for why this is happening.

The periods at the beginning of each line represents beginning spaces. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Terry Carroll
On Sat, 11 Mar 2006, Alan Gauld wrote:

 Take a look at the OS topic in my tutorial. The process control section
 covers all of these calls and explains their differences. It also points out
 that the latter is intended to replace all the others and shows examples.

Excellent; I'll check it out.  Your tutorials have been vey helpful to me 
in the past.

 output = popen(command).read()
 
 isn't too hard is it?

That's exactly the sort of thing I'm thinking of.

   string xxx | grep zz | head -10
 
 But in this case I'd use Pytthon to search for the string and get the
 last 10 entries rather than grep/head...

That was my initial thought, but the files are 1-3 gigabytes.  The string 
command takes 10-15  minutes to get through a file.  I've been using the 
pipe sequence manually, so I know it works, and only takes a second or two 
to run.

The Python program is for my own use, so I don't have to worry about 
whether and where string/grep/head are installed.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and unicode

2006-03-11 Thread Ferry Dave Jäckel
Hi Michael and Kent,

thanks to your tips I was able to solve my problems! It was quite easy at 
last.

For those interested and struggling with utf-8, ascii and unicode:

After knowing the right way of
   - string.decode() upon input (if in question)
   - string.encode() upon output (more often then not)
   where input and output are reading and writing to files, file-like 
   objects, databases... and functions of some not unicode-proof modules
I got rid of all calls to encode() and decode() I made by trial and error  
and which messed it all up. Now I have just a few calls to encode() and 
voilá! xml.sax seems to read and decode the utf-8 encoded xml-file 
perfectly right, so do ZipFile.read() and file.write() - no encding oder 
decoding.

To me it was very important to stress out that utf-8 ist *not* unicode, 
although I have already read about this topic (and you can read this advise 
often here at this list).

On my system sys.stdout and sys.stderr seem to have a utf-8 and a None 
encoding, respectively (Kubuntu Linux, python2.4, ipython and konsole as 
terminal).

The wrapper suggested by Kent
  sys.stdout = codecs.getwriter('utf-8')(sys.stdout, 'backslashreplace')
  sys.stderror = codecs.getwriter('ascii')(sys.stderror, 'backslashreplace')
solves all my output problems regarding debugging.

Thank you for your help!
  Dave

P.s.: The quotations in my signature are by chance, really. Normally I'm not 
the kind of guy believing in prevision... ;)

-- 
I never realized it before, but having looked that over I'm certain I'd 
rather
have my eyes burned out by zombies with flaming dung sticks than work on a
conscientious Unicode regex engine.
  -- Tim Peters, 3 Dec 1998


pgpwHHJ0xtmzY.pgp
Description: PGP signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] activestate

2006-03-11 Thread Danny Yoo


On Sat, 11 Mar 2006, kevin parks wrote:

 I noticed a couple days ago that the active state archive seems to have
 ceased. Is it going away?

Hi Kevin,

Can you check this again?  It appears to look ok to me:

http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help tracing a syntax error

2006-03-11 Thread Danny Yoo
 I get the message

 syntax error


 and it highlightsr2

 in the line

 .If r2 == 1:

Hi Kermit,

Next time, rather than describe the error message here in paraphrase,
please copy-and-paste it in.  Your paraphrase of the situation here hides
useful information.  We would rather that you give it to us straight and
unfiltered.  (Gosh, I sound like I'm in a bar or something.  *grin*)

I can see a problem, but I'm not sure if it's the only one.  Keywords are
case sensitive.  'If' is different than 'if'.  You're using 'If', which is
not a keyword, so what Python is seeing is essentially:

 some_variable_name another_variable_name

which is illegal syntax in Python.

Python can't tell that 'If' is meant to be the keyword 'if', so the
closest it can say is that something weird happened as it was reading up
to 'r2', so that's why the syntax error arrow is pointing at r2 rather
than the 'If'.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help tracing a syntax error

2006-03-11 Thread Alan Gauld
Hi,

It would help us a lot if you send us a cut n paste of the actual code, 
not retyped versions. Particularly for syntax erros since a single wrong 
character might be all that's wrong and when you retype it its OK.

I'm assuming you are retyping from the fact that you have uppercase 
keywords etc which aren't valid Python.

Also for the error merssage can you cut n paste the full error since 
they contain a lot of useful info, do not just give us a description of 
the error.

 def vmod(a , b ):
 .r1 = b
 .r2 = a
 . m1 = 0
 .m2 = 1
 .q = Int(r1 / r2)
 .r3 = r1 - q * r2
 .m3 = m1 - q * m2
 .while r3 != 0:
 ...r1 = r2
 ...m1 = m2
 ...r2 = r3
 ...m2 = m3
 ...q = Int(r1 / r2)
 ...r3 = r1 - r2 * q
 ...m3 = m1 - m2 * q
 .If r2 == 1:
 ...If m2  0: 
 .return( m2 + b)
 ...Else:
 .return( m2 )
 .Else:
 ...return( -r2 )

 I get the message 
 syntax error
 in the line
 
 .If r2 == 1:

 However if I 
 use the eval function in the shell, I get
 eval(factor30)
 module 'factor30' from 'c:\math\factoring\factor30.pyc'

Sorry, what is the connection between evaluating the string function30 
and the code above? function30 doesn't appear anywhere...

 and when I use help to look at the associated module,
 
 this function is not listed.   It must be because of the syntax error.

Which function?
Do you mean that the function you have written is in a file called 
function30.py?

If so then eval(function30) does not do anything with your function, 
it simply evaluated the name function30 and identifies that it is a module.

But this next bit seems to suggest that this is not what you have done.

 NAME
factor30
 
 FILE
c:\math\factoring\factor30.py


 Give me some hint for why this is happening.

I feel the same way. Can you send us real code with the full error message 
and explain where function30 fits into the picture. Preferrably posted as 
plain text. 

Otherwise I'm completely confused!

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Alan Gauld
 But in this case I'd use Pytthon to search for the string and get the
 last 10 entries rather than grep/head...

 That was my initial thought, but the files are 1-3 gigabytes.  The string
 command takes 10-15  minutes to get through a file.  I've been using the
 pipe sequence manually, so I know it works, and only takes a second or two
 to run.

But surely the string command will take just as long to run regardless of 
whether
you use pipes or read the output into Python? Are ytou saying that running
strings on its own takes 10  minutes but running it in a pipe takes only a 
few
seconds? What happens if you redirect the strings output to a file instead 
of
a pipe - MS DOS terminal windows are notoriously slow, it could be the
display update thats taking the trime... Just a suggestion. But 3GB files 
will
take a while on any system!

 The Python program is for my own use, so I don't have to worry about
 whether and where string/grep/head are installed.

Whatever works for you! :-)

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help tracing a syntax error

2006-03-11 Thread Alan Gauld
[CC'd to the list...]

 Also for the error merssage can you cut n paste the full error since
 they contain a lot of useful info, do not just give us a description of
 the error.

no error message was provided.
 It only highlighted the variable in the If statement.

But that's exactly what we need to see. Do not describe the error send
the actual message including the line that it highlights. There are often
subtle hints in those messages that help pinpoint the error, so simply
saying it was a syntax error and what was highlighted is not as useful
as sending us the actual text of the error.

In this case it wouldn't have made much difference since it was the
uppercase keywords, but in other scenarios it might make all the difference.
I can't emphasise this enough, including the complete actual error message
is one of the most useful things you can do to help us help you.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread kevin parks

On Mar 11, 2006, at 3:24 PM, [EMAIL PROTECTED] wrote:


 Message: 1
 Date: Sat, 11 Mar 2006 08:34:49 -0500
 From: Kent Johnson [EMAIL PROTECTED]
 Subject: Re: [Tutor] weighted choices from among many lists
 Cc: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 kevin parks wrote:
 I have several lists... and i would like to some times chose from one
 list and for a while choose from a different list, etc.

 You don't say what isn't working but I have a guess. The actual 
 windex()
 function looks OK to me.

yes, that part always worked fine... not the most robust thing, but it 
works.



 def test():

  lst_a = [ 'red', 'green', 'blue', 'orange', 'violet', 'yellow',
 'black', 'white' ]
  lst_b = ['Rottweiler', 'Beagle', 'Sheepdog', 'Collie', 'Boxer',
 'Terrier', 'Bulldog', 'Chihuahua', 'Retriever', 'Collie', 'Dachshund',
 'Doberman', 'Greyhound', 'Pug', 'Spaniel']
  lst_c = ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
  lst_d = ['fender', 'gibson', 'rickenbacker', 'guild', 'danelectro',
 'gretsch', 'martin', 'ibanez']
  x = [('lst_a', .50), ('lst_b', .25), ('lst_c', .10),('lst_d', .15)]

 x is list containing the *names* of the other lists. You need to keep
 references to the lists so you can pick from them.
   x = [(lst_a, .50), (lst_b, .25), (lst_c, .10),(lst_d, .15)]

i am an idiot... someone shoot me.. i guess i got so carried away with 
typing
the single quotes for the above lists that when i went to pack those up 
for windex
i didn't realize that i had needlessly typed 'lst_a', etc. Sometimes 
you just need a
fresh pair of eyes to pick up on that type of thing


  i = 1
  while i  100:
  lst = windex(x)
  print i, lst,

 with the change above this will print the list, not its name
  pick = random.choice(lst)
 but this will work.

 If you want to be able to print the name of the list then you could
 include both the name and the actual list in x:

   x = [(('lst_a', lst_a), .50), etc...]


That produces:
('lst_c', ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout'])
['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']

i am actually trying to get name of list, the choice like so:

lst_c
Bock

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode and regexes

2006-03-11 Thread Michael Broe
Thanks Kent, for breaking the bad news. I'm not angry, just terribly,  
terribly disappointed. :)

From http://www.unicode.org/unicode/reports/tr18/ I see that \p{L} is
intended to select Unicode letters, and it is part of a large number of
selectors based on Unicode character properties.

Yeah, that's the main cite, and yeah, a large, large number. The only  
sane way to use regexes with Unicode. Also see Friedl's 'Mastering  
Regular Expressions' Chapter 3: or actually, if you are a Python only  
person, don't: it will make you weep.

Python doesn't support this syntax. It has limited support for  
Unicode character properties [...].

Umm Earth to Python-guys, you *have heard* of Unicode, right? Call me  
crazy, but in this day and age, I assume a scripting language with  
regex support will implement standard Unicode conventions, unless  
there is a compelling reason not to. Very odd.

Back to Perl. Right now. Just kidding. Not. Sheesh. This is a big  
flaw in Python, IMHO. I never saw it coming.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Terry Carroll
On Sat, 11 Mar 2006, Alan Gauld wrote:

 But surely the string command will take just as long to run regardless
 of whether you use pipes or read the output into Python?

Surely it will!  Except that it doesn't.

 Are ytou saying that running strings on its own takes 10 minutes but
 running it in a pipe takes only a few seconds?

Yes, exactly.

Not any pipe, though; a pipe that is satisfied and closes down after just 
the first few hundred lines or so.

 What happens if you redirect the strings output to a file instead of a
 pipe

I haven't tried directing it to an actual file, but when directing it to 
nul: it's about 10-15 minutes; while directing it to the pipe is just a 
few seconds.

I gotta admit, this took me by surprise, too, but my guess is that once 
the head command is done, it closes the pipe it's reading from, which is 
being filled by grep; grep takes the hint and terminates, closing the pipe 
it's reading from, which is being filled by strings; and strings takes the 
hint and terminates, even though it hasn't gotten through the entire file.

And that all happens in a second or two.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting and secondary sorting.

2006-03-11 Thread Liam Clarke
Ahaha, thanks guys, I knew I was overlooking something.

Regards,

Liam Clarke

On 3/12/06, Kent Johnson [EMAIL PROTECTED] wrote:
 Liam Clarke wrote:
  Hi all,
 
  I'm trying to think of a way to sort a list of dictionaries. In pseudo-code:
 
  l = [ { host:foo, db:bob},
 { host:foo, db:dave},
 { host:fee, db:henry}
   ]
 
  l.sort( key = lambda item: item[host], second_key = lambda item: 
  item[db])
 
  Which, if all went well, would give me -
 
  l = [ { host:fee, db:henry}
 { host:foo, db:bob},
 { host:foo, db:dave},
  ]
 

 Just make a key that includes both of the values you want to sort on as
 a tuple:

 l.sort( key = lambda item: (item[host], item[db]))

 Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread Kent Johnson
kevin parks wrote:
From: Kent Johnson [EMAIL PROTECTED]
If you want to be able to print the name of the list then you could
include both the name and the actual list in x:

  x = [(('lst_a', lst_a), .50), etc...]
 
 
 
 That produces:
 ('lst_c', ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout'])
 ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
 
 i am actually trying to get name of list, the choice like so:
 
 lst_c
 Bock

Yes, you need to unpack the result of calling windex(). You so ably 
demonstrated unpacking the list in your for loop I thought you would 
figure it out :-)

Try
   lst_name, lst = windex(x)

then random.choice() and print.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Terry Carroll
On Sat, 11 Mar 2006, Terry Carroll wrote:

 I gotta admit, this took me by surprise, too, but my guess is that once 
 the head command is done, it closes the pipe it's reading from, which is 
 being filled by grep; grep takes the hint and terminates, closing the pipe 
 it's reading from, which is being filled by strings; and strings takes the 
 hint and terminates, even though it hasn't gotten through the entire file.

Just for the heack of it, I wrote a tiny Python echo program, and 
interposed it in the pipe between the strings and grep command:

 while 1:
 line = raw_input()
 print line

The command line now looks like this:

 strings 3193.DAT | python echo.py | grep Newsgroups: | head

(the .DAT file is an Agent newsgroup file; the idea here is that by 
grepping for the first few lines with Newsgroups:, I can tell what 
newsgroup the .DAT file is associated with.)

I get:

 strings 3193.DAT | python echo.py | grep Newsgroups: | head -10
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Newsgroups: comp.lang.python.announce
Traceback (most recent call last):
  File echo.py, line 3, in ?
print line
IOError: [Errno 22] Invalid argument
close failed: [Errno 22] Invalid argument

My guess is that the Invalid argument here was a write attempting to 
write to a closed file, the pipe.  The string and grep commands (or IO 
routines called by them) probably detect this and close gracefully; the 
end result being that a set of piped commands only lasts as long as the 
shortest-lived consumer, and the upstream producers shut down when they 
can no longer write to the pipe.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test code organization

2006-03-11 Thread Kent Johnson
Willi Richert wrote:
 Hi,
 
 for some time I try to find the best test code organization. I've come 
 up with the following solution, which I guess is not optimal. Please 
 comment.

OK I'll take a stab at it.
 
 In the code directory there is a special tests directory, which contains 
 all the unit test files. 

This is a common organization but I find I prefer to put the tests in 
the same directory as the modules under test. It makes the directory a 
bit more crowded but it keeps the modules and their tests close together.

 There is the file alltests.py which collects 
 all python test files and executes them:
 
 =
 import sys, unittest
 sys.path.append(.)
 sys.path.append(..)
 
 TEST_DIR = tests
 
 import glob
 testCases = [t.split(.)[0] for t in glob.glob1(TEST_DIR, *Tests.py)]
 print Found test case modules +, .join(testCases)
 print
 
 for t in testCases:
 exec(from %s import %s%(TEST_DIR, t))

You could probably replace this with __import__ (or my_import() from the 
doc page for __import__()) but maybe it's not worth the trouble.
 
 def suite():
 exec(suites = tuple([x.suite() for x in [%s]])%str(, 
 .join(testCases)))

I don't think you need exec here, you could use
   suites = [sys.modules.get(x).suite() for x in testCases]

 suite = unittest.TestSuite(suites)
 return suite
 
 
 if __name__ == '__main__':
 suite = suite()
 result = unittest.TextTestRunner(verbosity=2).run(suite)
 if result.wasSuccessful():
 sys.exit(0)
 else:
 sys.exit(1)
 ==
 
 
 For every class to test I create a ClassTests.py file which contains the 
 following code:
 Header:
 ==
 import sys
 sys.path.append(..)
 
 import unittest
 from Class... import *
 ==
 
 The footer parses the available test classes:
 ==
 def _testclasses():
 mod = sys.modules[__name__]
 return [getattr(mod, name) for name in dir(mod) if 
 name.endswith('TestCase')]
 
 def suite():
 return unittest.TestSuite([unittest.makeSuite(tc) for tc in 
 _testclasses()])

I think all the above footer could be replaced with
def suite():
   return 
unittest.defaultTestLoader.loadTestsFromModule(sys.modules.get(__name__))

 
 if __name__ == '__main__':
 unittest.main()
 ==
 
 
 What smalls badly is the sys.path.append() stuff every test file must 
 have. Improvements?

I usually run everything from the base directory of the project. On 
Windows, the current dir is added to sys.path automatically. (On linux 
apparently this is not true.) Then the test classes can use normal 
imports and there is no need to alter sys.path in each test class.

You might want to look at some of the other auto-discovery methods such 
as those included in nose and py.test, either for your own use or to see 
how they work.
http://somethingaboutorange.com/mrl/projects/nose/
http://codespeak.net/py/current/doc/test.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread kevin parks
 Yes, you need to unpack the result of calling windex(). You so ably
 demonstrated unpacking the list in your for loop I thought you would
 figure it out :-)

 Try
lst_name, lst = windex(x)

 then random.choice() and print.


Thanks Kent i got it, just another brain-fart on my side...

I was already doing something like:

lst_name, lst = windex(x)
pick = random.choice(lst)

But instead of:
print i, lst_name, pick

I was idiotically still printing:
print i, lst, pick

now matter how you unpack it all... you are still gonna get lst if that 
is what
is in your print statement... grrr

of course the another way i would be a use a dictionary and us the list 
name as
a key... the list as a value...

Thanks,

kevin

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problems with numbers in my python code

2006-03-11 Thread Smith
| From: sjw28 
| 
| Basically, I have a code with is almost finished but I've having
| difficultly 
| with the last stage of the process. I have a program that gets assigns
| different words with a different value via looking them up in a
| dictionary: 
| 
| eg if THE is in the writing, it assigns 0.965
| 
| and once the whole passage is read it returns all the numbers in the
| format 
| as follows:
| 
| ['0.965', '1.000', '0.291', '1.000', '0.503']
| 
| However, I can't seem to get the program to treat the numbers as
| numbers. If 
| I put them in the dictionary as 'THE' = int(0.965) the program
| returns 1.0 
| and if I put 'THE' = float(0.965) it returns 0.9655549 or
| something 
| similar. Neither of these are right! I basically need to access each
| item in 
| the string as a number, because for my last function I want to
| multiply them 
| all together by each other.
| 
| I have tried two bits of code for this last bit, but neither are
| working (I'm not sure about the first one but the second one should
| work I think if 
| I could figure out how to return the values as numbers):
| 
| 1st code
| value = codons[x] * codons[x+1]
| x = (int)
| x = 0
| 
You defined x after you tried to use it and that was the error message: x was 
not defined yet in the first line of your code.  Also, what were you trying to 
do with x = (int)?

[cut]
| 
| Code 2 - the most likely code
| prod = 1
| for item in (codons): prod *= item
| prod

This line is not necessary. In the interactive window typing a variable name 
will cause the value to be shown to you. But in a script it does nothing. The 
next line (print prod) is the way to see the product.

Also, what is item in Code 2 above: something like 'THE' or something like 
'0.965'? If you did something like putting the values in a dictionary (named 
something like 'values') then you should have something like

prod *= float(values[item])

instead of what you have above.

| print prod
| 
| Gives this error message:
| Traceback (most recent call last):
|  File C:\Python24\code2, line 90, in -toplevel-
|for item in (codons): prod *= item
| TypeError: can't multiply sequence by non-int
| 
| 
| Can anyone help me solve this problem?
| Thanks.

Others have mentioned why 0.965 doesn't look exactly like that when you view it 
in repr() form and they have told you where you can read more about this and 
what other module you might use to avoid this problem.

HOWEVER, I don't think you need the decimal module for what you are doing. You 
have 17 digits of precision in the floating point numbers you are using. The 
numerical value that python is using for 0.965 is only 0.3 
units off from the true value. When you get done calculating your product I am 
guessing that you don't need 17 digits of precision (since your codon values 
only had 3 digits of precision). If you want a nice representation of your 
final answer showing maybe 4 of the leading digits, consider using the fpformat 
option, sci:

 ll=['0.965', '1.000', '0.291', '1.000', '0.503']
 p=1
 for li in ll:
...  p*=float(li)
...  
 print p
0.141249945
 from fpformat import sci
 sci(0.141249945,3) #rounds to 3rd digit after the first non-zero one
'1.412e-001'
 float(_) #makes it a floating point number (a little ugly)
0.14119
 str(_) #rounds it to 12 digits of precision (or 1 part per trillion)
'0.1412'
 print str(float(sci(0.141249945,3))) #putting it all together
0.1412

Chris


What are you going to do with your product when you are done computing it?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread Terry Carroll
On Sat, 11 Mar 2006, Terry Carroll wrote:

 Just for the heack of it, I wrote a tiny Python echo program, and 
 interposed it in the pipe between the strings and grep command:
 
  while 1:
  line = raw_input()
  print line
 
 The command line now looks like this:
 
  strings 3193.DAT | python echo.py | grep Newsgroups: | head

Actually, to break it down to its simplest, with the python program:

 for i in range(1,1):
 print line, i

I get:

 writeabunch.py | head -5
line 1
line 2
line 3
line 4
line 5
Traceback (most recent call last):
  File C:\Agent-files\Giganews\writeabunch.py, line 2, in ?
print line, i
IOError: (0, 'Error')
close failed: [Errno 22] Invalid argument

Same thing, but a far simpler scenario.

Interestingly, if I use a low enough range, say range(1,500), it simply 
runs to completion; I assume that 500 lines in the form of line n\n fit 
within the first written buffer.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-11 Thread w chun
ok, while we're on the subject, i thought i should ask a question for
once(!).  it's been awhile since i played with pipes, and i ran into
this problem on the same day as this post!

if i'm in the shell and playing with a 2-way game with the 'tr' command like so:

$ tr '[a-z]' '[A-Z]'
us
US
ca
CA
de
DE
$

i can interactively give the cmd some input via stdin and get
something out of stdout... giving and taking until i press ^D to
terminate the program.

however, when i try this using subprocess/popen2, i find that i can't
do the same interaction, i.e. writing to tr's stdin, reading from tr's
stdout, ad nauseum.  it seems that i have to give tr all of my input,
then close tr's stdin pipe in order to read anything.  (any attempts
at reading before closing results in a blocked OS call, whether it's
using the FD itself or os.read() on its fileno().  the only way for it
to work as i described above is like this:

 p = subprocess.Popen(('tr', '[a-z]', '[A-Z]'),
stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
 po, pi = p.stdin, p.stdout
 po.write('us\n')
 po.write('ca\n')
 po.write('de\n')
 po.close()
 pi.readlines()
['US\n', 'CA\n', 'DE\n']
 pi.close()

i was hoping to do somethign more on the lines of:

 po.write('us\n')
 pi.read() # or pi.readline()
'US\n'
 po.write('ca\n')
 pi.read()
'CA\n'

but to no avail.  neither sending po.write(chr(4)) nor po.flush() seem
to work.  i basically can tell whether a read() will block (or not)
using select, as in:

 from select import select
 sel = select([pi], [], [pi], 0)
 sel
([], [], []) --- MEANS IT WILL BLOCK
or
([open file 'fdopen', mode 'r' at 0x39ac80], [], []) -- WON'T BLOCK

is there anyway to do more asynchronous I/O where i can more or less
interact with the other procses rather than what it's doing now?

thanks you guys... i love the tutors!
-wesley
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor