Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Hugo Arts
On Thu, Jan 10, 2013 at 7:01 AM, Karim  wrote:

>
>
> Hello all,
>
> I want to run multiline shell command within python without using a
> command file but directly execute several lines of shell.
> I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but I
> want to know if it is posssible to avoid making file and execute
> shell lines of code directly.
>
>
Yes, this is very possible. Specify shell=True as an argument and you can
do anything you can do in a shell:

 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?Sl   09:14   0:08
/usr/bin/python2 /usr/bi
hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00 python
hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00 grep python

>>>

watch out though, accepting user input into the commands variable will lead
to shell injection, which can be a dangerous security vulnerability.

HTH,
Hugo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL or something to open EXIF Metadata with Python

2013-01-10 Thread Pete Forman
Tim Golden  writes:

> On 09/01/2013 14:45, Jose Trevino wrote:
>> I am trying to load the PIL module to manage exif metadata with
>> Python but have had no success. 
>
>
> Try pyexiv2:
>
>   http://tilloy.net/dev/pyexiv2/
>
> TJG

Or Hachoir

http://pypi.python.org/pypi/hachoir-metadata
https://bitbucket.org/haypo/hachoir/wiki/Home

-- 
Pete Forman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

2013-01-10 Thread Duncan Booth
Dave Cinege  wrote:

> You will notice that the code is disgusting simple. However I have
> found that this has completely changed the way I program in python.
> I've re-written some exiting programs using Thesaurus, and often
> relized 15-30% code reduction. Additionally I find the new code much
> easier to read. 

And here's the same code written without your class but maintaining as 
far as possible the same structure. I find my version far easier to read 
then your's with all your spurious 'g.' 'L.' prefixes.


-

#!python2.7
from textwrap import dedent

class Blob(object): pass

prog = Blob()
prog.VERSION = '1.0'# But isn't this so much cleaner?
prog.NAME = 'Thesaurus'

class TestClass:
no = 'Class'
way = 'this'

def main ():
tc = TestClass()
l = ['Some', 'objects']

# Here's how you should create output without a fight.
print dedent('''\
When programing python without {prog.NAME}, it is very
easy to access your {l[1]}.

{l[0]} people might say {prog.NAME} has no 
{tc.no}.''').format(prog=prog, l=l, tc=tc)

if hasattr(prog, 'VERSION'):
print 'But I challenge them to write code {tc.way} clean 
without it!'.format(**locals())

if __name__ == '__main__':
main()
-


-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

2013-01-10 Thread 88888 Dihedral
在 2013年1月10日星期四UTC+8下午7时34分23秒,Duncan Booth写道:
> Dave Cinege  wrote:
> 
> 
> 
> > You will notice that the code is disgusting simple. However I have
> 
> > found that this has completely changed the way I program in python.
> 
> > I've re-written some exiting programs using Thesaurus, and often
> 
> > relized 15-30% code reduction. Additionally I find the new code much
> 
> > easier to read. 
> 
> 
> 
> And here's the same code written without your class but maintaining as 
> 
> far as possible the same structure. I find my version far easier to read 
> 
> then your's with all your spurious 'g.' 'L.' prefixes.
> 
> 
> 
> 
> 
> -
> 
> 
> 
> #!python2.7
> 
> from textwrap import dedent
> 
> 
> 
> class Blob(object): pass
> 
> 
> 
> prog = Blob()
> 
> prog.VERSION = '1.0'  # But isn't this so much cleaner?
> 
> prog.NAME = 'Thesaurus'
> 
> 
> 
> class TestClass:
> 
>   no = 'Class'
> 
>   way = 'this'
> 
> 
> 
> def main ():
> 
>   tc = TestClass()
> 
>   l = ['Some', 'objects']
> 
> 
> 
>   # Here's how you should create output without a fight.
> 
>   print dedent('''\
> 
>   When programing python without {prog.NAME}, it is very
> 
>   easy to access your {l[1]}.
> 
>   
> 
>   {l[0]} people might say {prog.NAME} has no 
> {tc.no}.''').format(prog=prog, l=l, tc=tc)
> 
> 
> 
>   if hasattr(prog, 'VERSION'):
> 
>   print 'But I challenge them to write code {tc.way} clean 
> without it!'.format(**locals())
> 
> 
> 
> if __name__ == '__main__':
> 
>   main()
> 
> -
> 
> 
> 
> 
> 
> -- 
> 
> Duncan Booth http://kupuguy.blogspot.com

An object can accquire new properties and methods
in the run time without the limitations from 
the class definition of the object which belongs to.

This is a true OOP language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Karim

On 10/01/2013 09:31, Hugo Arts wrote:
On Thu, Jan 10, 2013 at 7:01 AM, Karim > wrote:




Hello all,

I want to run multiline shell command within python without using
a command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f
my_file.csh".split())* but I want to know if it is posssible to
avoid making file and execute
shell lines of code directly.


Yes, this is very possible. Specify shell=True as an argument and you 
can do anything you can do in a shell:


 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?Sl 09:14   0:08 
/usr/bin/python2 /usr/bi

hugo  6529  0.0  0.0  42408  7196 pts/0S+ 09:23   0:00 python
hugo  6559  0.0  0.0  10656  1128 pts/0S+ 09:28   0:00 grep python

>>>

watch out though, accepting user input into the commands variable will 
lead to shell injection, which can be a dangerous security vulnerability.


HTH,
Hugo


Many thanks Hugo. It makes my day!
In my case there are no possibilities for shell injection. It is 
internal to a class.


Regards
Karim

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


Re: Python-list Digest, Vol 112, Issue 79

2013-01-10 Thread Niklas Berliner
>
>
>
>
> -- Weitergeleitete Nachricht --
> From: Dave Angel 
> To: [email protected]
> Cc:
> Date: Thu, 10 Jan 2013 00:56:20 -0500
> Subject: Re: subprocess.Popen and multiprocessing fails to execute
> external program
> On 01/09/2013 11:08 PM, Niklas Berliner wrote:
> > I have a pipline that involves processing some data, handing the data to
> an
> > external program (t_coffee used for sequence alignments in
> bioinformatics),
> > and postprocessing the result. Since I have a lot of data, I need to run
> my
> > pipeline in parallel which I implemented using the multiprocessing module
> > following Doug Hellmanns blog (
> > http://blog.doughellmann.com/2009/04/pymotw-multiprocessing-part-1.html
> ).
> >
> > My pipeline works perfectly fine when I run it with the multiprocessing
> > implementation and one consumer, i.e. on one core. If I increase the
> number
> > of consumers, i.e. that multiple instances of my pipeline run in parallel
> > the external program fails with a core dump.
> >
>
> Could it be that the external program is not designed to have multiple
> simultaneous instances?  There are many such programs, some of which
> check for an existing process before allowing another one to get far.
>
> When using the multiprocessing module, always make sure your externals
> are well-behaved before looking for problems in your multi-code.
>
> To put it more strongly, a well-written program cannot easily be crashed
> by the parent that launched it.
>
>
> --
>
> DaveA
>
>

Hi Dave,

the developers of the external program said that they are using the program
with multiple simultaneous instances. Also, when I execute multiple
simultaneous instances of the external program using a bash wrapper script
on my machine it works (the same wrapper scripts that fail when executed
through python).
Before asking here I have contacted the developers of the external program
but they couldn't help me any further.

Cheers,
Niklas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Matty Sarro
Have you looked a the pexpect class? It works like gangbusters, especially
if you're trying to run something with an interactive shell.

http://www.noah.org/wiki/pexpect


On Thu, Jan 10, 2013 at 9:25 AM, Karim  wrote:

>  On 10/01/2013 09:31, Hugo Arts wrote:
>
> On Thu, Jan 10, 2013 at 7:01 AM, Karim  wrote:
>
>>
>>
>> Hello all,
>>
>> I want to run multiline shell command within python without using a
>> command file but directly execute several lines of shell.
>> I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but
>> I want to know if it is posssible to avoid making file and execute
>> shell lines of code directly.
>>
>>
>  Yes, this is very possible. Specify shell=True as an argument and you
> can do anything you can do in a shell:
>
>   >>> commands = """echo hello
> ... echo hello | wc -l
> ... ps aux | grep python"""
> >>> b = subprocess.check_output(commands, shell=True)
> >>> print(b.decode('ascii'))
>  hello
> 1
> hugo  1255  1.0  0.6 777316 49924 ?Sl   09:14   0:08
> /usr/bin/python2 /usr/bi
> hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00 python
> hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00 grep
> python
>
>  >>>
>
>  watch out though, accepting user input into the commands variable will
> lead to shell injection, which can be a dangerous security vulnerability.
>
>  HTH,
> Hugo
>
>
> Many thanks Hugo. It makes my day!
> In my case there are no possibilities for shell injection. It is internal
> to a class.
>
> Regards
> Karim
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why BOM in logging message?

2013-01-10 Thread Roy Smith
In article ,
Roy Smith   wrote:
>In article ,
> Chris Angelico  wrote:
>
>> On Thu, Jan 10, 2013 at 9:54 AM, Roy Smith  wrote:
>> > What's weird is that two of the servers, and only those two, stick a
>> > BOM (Byte Order Mark) in front of the message they log.
>> 
>> Could it be this issue you're looking at?
>> 
>> http://bugs.python.org/issue14452
>> 
>> What are the exact Python versions in use? Are the two different
>> servers running an older revision of Py 2.7?
>> 
>> ChrisA
>
>It sounds like it might be it, but we're running 2.7.3 on all machines.

Well, this is fascinating.  It turns out that while all of our
machines report that they're running 2.7.3, they have two different
versions of /usr/lib/python2.7/logging/handlers.py!

-rw-r--r-- 1 root root 45076 Aug  1 05:39 /usr/lib/python2.7/logging/handlers.py
-rw-r--r-- 1 root root 45143 Apr 20  2012 /usr/lib/python2.7/logging/handlers.py

The April 24th version has the BOM code in SysLogHander.emit():

| # Message is a string. Convert to bytes as required by RFC 5424
|if type(msg) is unicode:
|msg = msg.encode('utf-8')
|if codecs:
|msg = codecs.BOM_UTF8 + msg
|msg = prio + msg

and the August 1st version doesn't:

|# Message is a string. Convert to bytes as required by RFC 5424
|if type(msg) is unicode:
|msg = msg.encode('utf-8')
|msg = prio + msg

Is it possible there's two different 2.7.3 builds that somehow got
packaged by Ubuntu at different times?  The pattern of which machines
have the August code and which have the April code correlates with
when we rolled out each server instance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why BOM in logging message?

2013-01-10 Thread Chris Angelico
On Fri, Jan 11, 2013 at 3:06 AM, Roy Smith  wrote:
> Well, this is fascinating.  It turns out that while all of our
> machines report that they're running 2.7.3, they have two different
> versions of /usr/lib/python2.7/logging/handlers.py!
>
> -rw-r--r-- 1 root root 45076 Aug  1 05:39 
> /usr/lib/python2.7/logging/handlers.py
> -rw-r--r-- 1 root root 45143 Apr 20  2012 
> /usr/lib/python2.7/logging/handlers.py

Ha, that would do it!

I don't have a corresponding system to compare against, but what
package is that file managed by?

$ dpkg -S /usr/lib/python2.7/logging/handlers.py

See if both systems show it as part of the same package, and if so, if
the package is at the same version on each. On my Maverick desktop, I
have 2.6, and the package is python2.6-minimal.

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


problems importing from /usr/lib/pyshared/

2013-01-10 Thread Harold
Dear all,

I recently upgraded my system from ubuntu 11.4 to 12.4 and since run into an 
issue when trying to import several packages in python2.7, e.g.

harold@ubuntu:~$ python -c 'import gtk'
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py", line 30, in 

import gobject as _gobject
  File "/usr/share/pyshared/gobject/__init__.py", line 26, in 
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
  File "/usr/share/pyshared/glib/__init__.py", line 22, in 
from glib._glib import *
ImportError: No module named _glib

the same, for example, with pysvn:

harold@ubuntu:~$ python -c 'import pysvn'
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/share/pyshared/pysvn/__init__.py", line 99, in 
import _pysvn_2_7
ImportError: No module named _pysvn_2_7

After playing around a bit, I realized that I can import said packages without 
problems, if I delete /usr/lib/pyshared from the python path:

>>> import sys
>>> sys.path
['', '/usr/share/pyshared', '/home/harold', '/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PIL', 
'/usr/lib/python2.7/dist-packages/gst-0.10', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', 
'/usr/lib/python2.7/dist-packages/ubuntuone-couch', 
'/usr/lib/python2.7/dist-packages/ubuntuone-installer', 
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', 
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
>>> del sys.path[1]
>>> import gtk
>>> import pysvn
>>> 

Is /usr/lib/pyshared supposed to be in the python path? If not, how can I 
ensure that it is not included? Where is PYTHONPATH initialized by default, 
anyway?

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


Re: Why BOM in logging message?

2013-01-10 Thread Roy Smith
>On Fri, Jan 11, 2013 at 3:06 AM, Roy Smith  wrote:
>> -rw-r--r-- 1 root root 45076 Aug  1 05:39 
>> /usr/lib/python2.7/logging/handlers.py
>> -rw-r--r-- 1 root root 45143 Apr 20  2012 
>> /usr/lib/python2.7/logging/handlers.py

Chris Angelico   wrote:
>$ dpkg -S /usr/lib/python2.7/logging/handlers.py

Yup, on some machines we've got 2.7.3-0ubuntu3, and on others,
2.7.3-0ubuntu3.1 of python2.7-minimal.  Details at:

https://launchpad.net/ubuntu/+source/python2.7/2.7.3-0ubuntu3.1

Well, I guess today is dist-upgrade day :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why BOM in logging message?

2013-01-10 Thread Chris Angelico
On Fri, Jan 11, 2013 at 3:40 AM, Roy Smith  wrote:
>>On Fri, Jan 11, 2013 at 3:06 AM, Roy Smith  wrote:
>>> -rw-r--r-- 1 root root 45076 Aug  1 05:39 
>>> /usr/lib/python2.7/logging/handlers.py
>>> -rw-r--r-- 1 root root 45143 Apr 20  2012 
>>> /usr/lib/python2.7/logging/handlers.py
>
> Chris Angelico   wrote:
>>$ dpkg -S /usr/lib/python2.7/logging/handlers.py
>
> Yup, on some machines we've got 2.7.3-0ubuntu3, and on others,
> 2.7.3-0ubuntu3.1 of python2.7-minimal.  Details at:
>
> https://launchpad.net/ubuntu/+source/python2.7/2.7.3-0ubuntu3.1

I love it when everything adds up! The message even cites the specific
change. It's like a cryptic crossword - once you get the answer, you
KNOW it's the answer because suddenly it all makes sense :)

Thanks for bringing a fun problem to solve! It's (unfortunately) a
refreshing change to read a post from someone who knows how to ask
smart questions.

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


average time calculation??

2013-01-10 Thread pmec
Hi there guys i've got a script that's suppose to find the average of two times 
as strings. The times are in minutes:seconds:milliseconds
i'm doing ok in printing the right minutes and seconds my problem is with the 
milliseconds.

Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 
00:02:00 and 00:03:00 will be 00:02:30

Can anyone help me out with this please. Here is the code that i have so far:

def lap_average(lap1, lap2): 

t1 = lap1.replace(":",'') 
t2 = lap2.replace(":",'')   

mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:] 
mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:] 

total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60 

millisec = (total_seconds * 1000)  
millisec = millisec / 2 

micro_x = millisec 
minutes = micro_x / (60*1000) 

micro_x = micro_x - minutes * (60*1000) 
seconds = micro_x / 1000 
micro_x = micro_x - seconds  

   print '%02d:%02d:%s' % (minutes, seconds, micro_x) 

lap_average('03:40:00', '05:20:00') 
lap_average('03:00:02', '02:00:00') 
lap_average('02:25:50', '06:50:75') 
lap_average('00:02:00', '00:03:00') 
lap_average('00:02:20', '00:04:40') 
lap_average('02:40:40', '03:30:30') 
lap_average('02:60:30', '60:40:40')

Thanks in Advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: average time calculation??

2013-01-10 Thread Oscar Benjamin
On 10 January 2013 17:50, pmec  wrote:
> Hi there guys i've got a script that's suppose to find the average of two 
> times as strings. The times are in minutes:seconds:milliseconds
> i'm doing ok in printing the right minutes and seconds my problem is with the 
> milliseconds.

You might find it easier to do this arithmetic if using datetime.time
objects from the datetime module:
http://docs.python.org/2/library/datetime.html

>
> Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 
> 00:02:00 and 00:03:00 will be 00:02:30
>
> Can anyone help me out with this please. Here is the code that i have so far:
>
> def lap_average(lap1, lap2):
>
> t1 = lap1.replace(":",'')
> t2 = lap2.replace(":",'')
>
> mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:]
> mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:]

Are these really hundredths? At the top you said it goes
minutes:seconds:milliseconds. A hundredth of a second is 10
milliseconds so you need to be clear about which one you want.

>
> total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 
> 60

What happened to the hundredths in the line above. Surely you wanted
to add 0.01 * hundredths there.

>
> millisec = (total_seconds * 1000)
> millisec = millisec / 2

In Python 2 this division will always round down if millisec is an
integer. Is that what you want?

>
> micro_x = millisec

Is the line above just there to confuse me? I thought millisec was a
good descriptive name. In this context micro would suggest
microseconds which would be 1000 times smaller.

> minutes = micro_x / (60*1000)

Wouldn't it have been easier to just get this from total_seconds?

> micro_x = micro_x - minutes * (60*1000)
> seconds = micro_x / 1000

This will behave differently in Python 3. Safest to use floor division // here:
http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division

> micro_x = micro_x - seconds

Surely that should be seconds*1000?

>
>print '%02d:%02d:%s' % (minutes, seconds, micro_x)

Until your done debugging, I suggest you print all of these values out
directly (without the format string):

print(minutes, seconds, microx)

>
> lap_average('03:40:00', '05:20:00')
> lap_average('03:00:02', '02:00:00')
> lap_average('02:25:50', '06:50:75')
> lap_average('00:02:00', '00:03:00')
> lap_average('00:02:20', '00:04:40')
> lap_average('02:40:40', '03:30:30')
> lap_average('02:60:30', '60:40:40')


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


Re: average time calculation??

2013-01-10 Thread Oscar Benjamin
Two quick corrections to what I wrote...

On 10 January 2013 18:13, Oscar Benjamin  wrote:
> On 10 January 2013 17:50, pmec  wrote:
>> Hi there guys i've got a script that's suppose to find the average of two 
>> times as strings. The times are in minutes:seconds:milliseconds
>> i'm doing ok in printing the right minutes and seconds my problem is with 
>> the milliseconds.
>
> You might find it easier to do this arithmetic if using datetime.time
> objects from the datetime module:
> http://docs.python.org/2/library/datetime.html

Actually  I meant datetime.timedelta objects.

>
>>
>> Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 
>> 00:02:00 and 00:03:00 will be 00:02:30
>>
>> Can anyone help me out with this please. Here is the code that i have so far:
>>
>> def lap_average(lap1, lap2):
>>
>> t1 = lap1.replace(":",'')
>> t2 = lap2.replace(":",'')
>>
>> mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:]
>> mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:]
>
> Are these really hundredths? At the top you said it goes
> minutes:seconds:milliseconds. A hundredth of a second is 10
> milliseconds so you need to be clear about which one you want.
>
>>
>> total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 
>> 60
>
> What happened to the hundredths in the line above. Surely you wanted
> to add 0.01 * hundredths there.
>
>>
>> millisec = (total_seconds * 1000)
>> millisec = millisec / 2
>
> In Python 2 this division will always round down if millisec is an
> integer. Is that what you want?
>
>>
>> micro_x = millisec
>
> Is the line above just there to confuse me? I thought millisec was a
> good descriptive name. In this context micro would suggest
> microseconds which would be 1000 times smaller.
>
>> minutes = micro_x / (60*1000)
>
> Wouldn't it have been easier to just get this from total_seconds?
>
>> micro_x = micro_x - minutes * (60*1000)
>> seconds = micro_x / 1000
>
> This will behave differently in Python 3. Safest to use floor division // 
> here:
> http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division
>
>> micro_x = micro_x - seconds
>
> Surely that should be seconds*1000?
>
>>
>>print '%02d:%02d:%s' % (minutes, seconds, micro_x)

micro_x is in units of microseconds. Did you want microseconds or
hundredths of a second here in the output?

>
> Until your done debugging, I suggest you print all of these values out
> directly (without the format string):
>
> print(minutes, seconds, microx)
>
>>
>> lap_average('03:40:00', '05:20:00')
>> lap_average('03:00:02', '02:00:00')
>> lap_average('02:25:50', '06:50:75')
>> lap_average('00:02:00', '00:03:00')
>> lap_average('00:02:20', '00:04:40')
>> lap_average('02:40:40', '03:30:30')
>> lap_average('02:60:30', '60:40:40')
>
>
> Oscar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: average time calculation??

2013-01-10 Thread MRAB

On 2013-01-10 17:50, pmec wrote:

Hi there guys i've got a script that's suppose to find the average of two times 
as strings. The times are in minutes:seconds:milliseconds
i'm doing ok in printing the right minutes and seconds my problem is with the 
milliseconds.

Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 
00:02:00 and 00:03:00 will be 00:02:30


If the final 2 digits are milliseconds, then the second average would
be 00:02:500; if they're centiseconds (hundredths of a second), then
the second average would be 00:02:50.

If the average you give is correct ('00:02:30'), then that suggests
that they are in fact hours:minutes:seconds.


Can anyone help me out with this please. Here is the code that i have so far:

def lap_average(lap1, lap2):

 t1 = lap1.replace(":",'')
 t2 = lap2.replace(":",'')

 mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:]
 mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:]

 total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60

 millisec = (total_seconds * 1000)
 millisec = millisec / 2

 micro_x = millisec
 minutes = micro_x / (60*1000)

 micro_x = micro_x - minutes * (60*1000)
 seconds = micro_x / 1000
 micro_x = micro_x - seconds

print '%02d:%02d:%s' % (minutes, seconds, micro_x)

lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')

Thanks in Advance


I don't see the point of these 2 lines of removing the ':'; slice
'lap1' and 'lap2' instead:

mins1, secs1, hundreths1 = lap1[:2], lap1[3:5], lap1[6:]
mins2, secs2, hundreths2 = lap2[:2], lap2[3:5], lap2[6:]

Better yet, split on ':':

mins1, secs1, hundreths1 = lap1.split(':')
mins2, secs2, hundreths2 = lap2.split(':')

Incidentally, you're talking about milliseconds, but the names say
'hundreths'.

A useful function is 'divmod', which, as its name suggests, performs
both (integer) division and modulo in one step:

seconds, millisec = divmod(millisec, 1000)
minutes, seconds = divmod(seconds, 60)

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


How to change colors of multiple widgets after hovering in Tkinter

2013-01-10 Thread mountdoom12
Hello,

I´m trying to make a script, which will change the background and foreground 
color of widgets after hovering. 

-
from Tkinter import *

root=Tk()

Hover1=Button(root,text="Red color", bg="white")
Hover1.pack()

Hover2=Button(root,text="Yellow color", bg="white")
Hover2.pack()

Hover1.bind("",Hover1.configure(bg="red"))
Hover1.bind("",Hover1.configure(bg="white"))

Hover2.bind("",Hover2.configure(bg="yellow"))
Hover2.bind("",Hover2.configure(bg="white"))

root.mainloop()
-

but when I hover on any button, nothing happens, they stay white. I know I 
could use a function, but there would be two functions for every widget (1 for 
, 1 for ). I'd like to create a single function, which will recolor that widget 
I hover on and explain why this script is not doing what I want it to do.

I hope I described my problem well. Thanks for every answer.

PS: I would like to avoid classes.

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


Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Karim

On 10/01/2013 16:21, Matty Sarro wrote:
Have you looked a the pexpect class? It works like gangbusters, 
especially if you're trying to run something with an interactive shell.


http://www.noah.org/wiki/pexpect


On Thu, Jan 10, 2013 at 9:25 AM, Karim > wrote:


On 10/01/2013 09:31, Hugo Arts wrote:

On Thu, Jan 10, 2013 at 7:01 AM, Karim mailto:[email protected]>> wrote:



Hello all,

I want to run multiline shell command within python without
using a command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f
my_file.csh".split())* but I want to know if it is posssible
to avoid making file and execute
shell lines of code directly.


Yes, this is very possible. Specify shell=True as an argument and
you can do anything you can do in a shell:

 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?  Sl   09:14   0:08
/usr/bin/python2 /usr/bi
hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00
python
hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00
grep python

>>>

watch out though, accepting user input into the commands variable
will lead to shell injection, which can be a dangerous security
vulnerability.

HTH,
Hugo


Many thanks Hugo. It makes my day!
In my case there are no possibilities for shell injection. It is
internal to a class.

Regards
Karim


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




Thanks Matty!

I will have a look specially for interactive session.

Regards
Karim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change colors of multiple widgets after hovering in Tkinter

2013-01-10 Thread Peter Otten
[email protected] wrote:

> I´m trying to make a script, which will change the background and
> foreground color of widgets after hovering.

> but when I hover on any button, nothing happens, they stay white. I know I
> could use a function, but there would be two functions for every widget (1
> for , 1 for ). I'd like to create a single function, which will recolor
> that widget I hover on and explain why this script is not doing what I
> want it to do.
> 
> I hope I described my problem well. 

You did.

> from Tkinter import *
> 
> root=Tk()
> 
> Hover1=Button(root,text="Red color", bg="white")
> Hover1.pack()
> 
> Hover2=Button(root,text="Yellow color", bg="white")
> Hover2.pack()
> 
> Hover1.bind("",Hover1.configure(bg="red"))

This calls Hover1.configure(bg="red") once and binds the result of that 
method call (which is None) to the event. So the above line is equivalent to

Hover1.configure(bg="red")
Hover1.bind("", None)

You say you don't want to write a function, but that is really the correct 
aproach. Fortunately there is a way to create such a function on the fly:

def f(event):
Hover1.configure(bg="red")

can be written as

f = lambda event: Hover1.configure(bg="red")

With that your code becomes

Hover1.bind("", lambda event: Hover1.configure(bg="red"))
Hover1.bind("", lambda event: Hover1.configure(bg="white"))

and so on. In this specific case this doesn't have the desired effect 
because when the mouse enters a Button widget its background color changes 
to 'activebackground'. So you don't really need to bind the enter/leave 
events. Specify an activebackground instead when you create the buttons. For 
example:

Hover1 = Button(root, text="Red color", bg="white", activebackground="red")
Hover1.pack()



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


Re: RIse and fall of languages in 2012

2013-01-10 Thread Michael Torrie
On 01/10/2013 12:23 AM, Steven D'Aprano wrote:
> "In general-purpose scripting languages, Python continues to grow slowly, 
> JavaScript and Ruby are treading water, and Perl continues its long 
> decline. According to Google trends, the number of searches for Perl is 
> 19% of what it was in 2004. Its declining role in open-source communities 
> further cements the perception that it's in an irretrievable tailspin. 
> One should always be careful pronouncing a language dead or dying, 
> because rare resurrections have occurred: JavaScript and Objective-C 
> being two stand-out cases. However, Perl is unlikely to see such a new 
> lease on life because of direct competition from Python, which is 
> considerably more popular (whereas Objective-C and JavaScript had no 
> direct equivalents when they came back)."
> 
> http://www.drdobbs.com/jvm/the-rise-and-fall-of-languages-in-2012/240145800
> 
> 
> And from the TIOBE Index, Python is steady at number 8:
> 
> http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

The TIOBE index is meaningless.  Since it's based on google searches,
one could probably guess that any language that is awkward and difficult
will require more searches to figure out how to use the thing.  Thus of
course C is top!  Especially if ranked by sarcastic queries like, "C
sucks," and "why does C suck so much."

Javascript is doing much more than just "treading water."  Javascript
may not be glamorous but it is *the* glue that makes the web run.  Funny
to see such a reputable journal make such an absurd statement.  I can
buy that Perl is in a slow decline.  Certainly I'd use Python for the
same tasks that people used to use Perl for.  In short I see no rise and
fall of languages in 2012.  Seems like business as usual, and the usual
suspects continue to get steady use.

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


Re: average time calculation??

2013-01-10 Thread pmec
Hi Oscar,

Thank you for your reply, and you are absolutely right, I meant hundredths of a 
second to be outputed
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: average time calculation??

2013-01-10 Thread pmec
Hi Oscar, again I do apologize for my beginner mistakes, I've changed the code 
taking in consideration some of your and MRAB suggestions.

Could you give me an example on how could I use the datetime.timedelta function 
in this particular case. 

This is my code:


def lap_average(lap1, lap2):

mins1, secs1, hundreths1 = lap1.split(":")
mins2, secs2, hundreths2 = lap2.split(":")

minutes = int(mins1) + int(mins2)
seconds = float(secs1) + float(secs2)
hundredths = int(6 * minutes + 1000 * seconds)
hundredths = hundredths // 2

print hundredths

lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00') #should output: 00:02:50
lap_average('00:02:20', '00:04:40') # 00:03:30
lap_average('02:40:40', '03:30:30') # etc
lap_average('02:60:30', '60:40:40')



Also I was a bit confused with what you said about :


"> total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60 

What happened to the hundredths in the line above. Surely you wanted 
to add 0.01 * hundredths there."


I thought the above was already the entire time as hundredths of second??
-- 
http://mail.python.org/mailman/listinfo/python-list


Organic Chemistry, 8th Ed by Wade, Jan Simek

2013-01-10 Thread kalvinmanual
I have solutions manuals to all problems and exercises in these textbooks. To 
get one in an electronic format contact me at: kalvinmanual(at)gmail(dot)com 
and let me know its title, author and edition. Please this service is NOT free.

instructor's solutions manual to OpenScape Voice V3.1R3 Test Configuration and 
connectivity Vol 2 , Application and Hardware Configuratio
instructor's solutions manual to Operating System Concepts, 6E, Silberschatz, 
Galvin, Gagne
instructor's solutions manual to Operating System Concepts, 7E, Silberschatz, 
Galvin, Gagne
instructor's solutions manual to Operating systems Internals and Design 
principles 4th Edition Stallings
instructor's solutions manual to Operating systems Internals and Design 
principles 5th Edition Stallings
instructor's solutions manual to Operations Management 5th Ed by Nigel Slack, 
Chambers, Johnston
instructor's solutions manual to Optical Fiber Communications 3rd E by Gerd 
Keiser
instructor's solutions manual to Optical Properties of Solids 2nd Ed by Mark Fox
instructor's solutions manual to Optics 4th Edition by Hecht E., Coffey M., 
Dolan P
instructor's solutions manual to Optimal Control Theory An Introduction By 
Donald E. Kirk
instructor's solutions manual to Optimal State Estimation Dan Simon
instructor's solutions manual to Optimization of Chemical Processes by Edgar
instructor's solutions manual to Options, Futures and Other Derivatives, 4E, by 
John Hull
instructor's solutions manual to Options, Futures and Other Derivatives, 5E, by 
John Hull
instructor's solutions manual to Options, Futures, and Other Derivatives 7th Ed 
by John C. Hull
instructor's solutions manual to Orbital Mechanics for Engineering Students 2nd 
ED by Curtis
instructor's solutions manual to Orbital Mechanics for Engineering Students by 
Curtis
instructor's solutions manual to ORDINARY DIFFERENTIAL EQUATIONS by Adkins, 
Davidson
instructor's solutions manual to Organic Chemistry - Clayden et.al. 
instructor's solutions manual to Organic Chemistry 2nd ed by Schore
instructor's solutions manual to Organic Chemistry 2nd Edition by Hornback
instructor's solutions manual to Organic Chemistry 5th Ed by Brown, Foote, 
Iverson, Ansyln
instructor's solutions manual to Organic Chemistry 7ed, McMurry
instructor's solutions manual to Organic Chemistry, 4E., by Carey, Atkins
instructor's solutions manual to Organic Chemistry, 5E., by Carey, Atkins
instructor's solutions manual to Organic Chemistry, 6 Ed by Wade, Jan Simek
instructor's solutions manual to Organic Chemistry, 8th Ed by Wade, Jan Simek
instructor's solutions manual to Parallel & Distributed Computation  Numerical 
Methods by Bertsekas & Tsitsiklis
instructor's solutions manual to Parallel Programming: Techniques and 
Applications Using Networked Workstations and Parallel Computers (2nd Ed., 
Barry Wilkinson & Michael Allen)
instructor's solutions manual to PARTIAL DIFFERENTIAL EQUATIONS WITH FOURIER 
SERIES and BOUNDARY VALUE PROBLEMS 2nd Edition by NAKHLE H. ASMAR (Students 
Solutions Manual)
instructor's solutions manual to Physical Basis of Biochemistry 2nd Ed by 
Bergethon, Hallock 
instructor's solutions manual to Physical Chemistry (7E, Peter Atkins & Julio 
de Paula)
instructor's solutions manual to Physical Chemistry by Prem Dhawan
instructor's solutions manual to Physical Chemistry by Thomas Engel & Philip 
Reid
instructor's solutions manual to Physics - Concept and Connections - Book Two 
by Brian Heimbecker, Igor Nowikow, et al
instructor's solutions manual to Physics - Concept and Connections -by Brian 
Heimbecker, Igor Nowikow, et al
instructor's solutions manual to Physics - Principles and Problems
instructor's solutions manual to Physics - Principles and Problems ( Glencoe )
instructor's solutions manual to Physics , Fifth Edition, Volume One (Halliday, 
Resnick, Krane)
instructor's solutions manual to Physics 7th ed by Paul E. Tippens
instructor's solutions manual to Physics 8 ED by Cutnell & Johnsen
instructor's solutions manual to Physics for Scientist and Engineers, 5E, by 
Tipler, Mosca
instructor's solutions manual to Physics For Scientists & Engineers 5th Ed 
(vol.I,vol.II) by Serway & Beichner
instructor's solutions manual to Physics For Scientists & Engineers 7th Ed. by 
Serway & Jewett
instructor's solutions manual to Physics For Scientists & Engineers Vol.1& 2 
3rd Ed. by Serway & Jewett
instructor's solutions manual to Physics For Scientists & Engineers Vol.1& 2 
4th Ed. by Serway & Jewett
instructor's solutions manual to Physics For Scientists & Engineers Vol.I 6th 
Ed. by Serway & Jewett
instructor's solutions manual to Physics For Scientists & Engineers Vol.II 6th 
Ed. by Serway & Jewett
instructor's solutions manual to Physics for Scientists & Engineers with Modern 
Physics 4th E by Douglas Giancoli
instructor's solutions manual to Physics For Scientists and Engineers 8th ed 
VOL.1 by Gordon, McGrew, Serway ( student solutions manual and study guide )
instructor's solutions manual t

Re: average time calculation??

2013-01-10 Thread Ian Kelly
On Thu, Jan 10, 2013 at 1:31 PM, pmec  wrote:
> Hi Oscar, again I do apologize for my beginner mistakes, I've changed the 
> code taking in consideration some of your and MRAB suggestions.
>
> Could you give me an example on how could I use the datetime.timedelta 
> function in this particular case.

td1 = timedelta(minutes=mins1, seconds=secs1, milliseconds=hundredths1*10)
td2 = timedelta(minutes=mins2, seconds=secs2, milliseconds=hundredths2*10)
average = (td1 + td2) / 2
mins, secs = divmod(average.seconds, 60)
hundredths = average.microseconds // 1
print("{:02d}:{:02d}:{:02d}".format(mins, secs, hundredths))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RIse and fall of languages in 2012

2013-01-10 Thread John Ladasky
On Wednesday, January 9, 2013 11:23:51 PM UTC-8, Steven D'Aprano wrote:

> One should always be careful pronouncing a language dead or dying, 

No kidding!

https://www.google.com/#q=is+fortran+still+used

I usually use the query phrase "Why isn't Fortran dead yet?", but you get a 
better list of links with a less biased phrase.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RIse and fall of languages in 2012

2013-01-10 Thread Steven D'Aprano
On Thu, 10 Jan 2013 12:42:49 -0700, Michael Torrie wrote:

>> And from the TIOBE Index, Python is steady at number 8:
>> 
>> http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
> 
> The TIOBE index is meaningless.  Since it's based on google searches,
> one could probably guess that any language that is awkward and difficult
> will require more searches to figure out how to use the thing.  Thus of
> course C is top!  Especially if ranked by sarcastic queries like, "C
> sucks," and "why does C suck so much."

If you have a problem with TIOBE's methodology, feel free to come up with 
your own. Or take it up with them.

I dispute that TIOBE measures difficulty of language. If it did, Malbolge 
would likely be at the top of the list. Yes, there are sarcastic queries 
asking "C sucks", but that's just measurement error: 21,200 hits for "C 
sucks" versus 9,900,000 for "C programming". It's not as if there is any 
language, not even Python, that is so easy to use that nobody needs to 
write about it.


> Javascript is doing much more than just "treading water."

How do you know? What's *your* methodology for determining the popularity 
of a language?

* "But everybody knows that Javascript is super popular!!!"

* "All my friends are using Javascript."

* "I'm a web developer, and I use Javascript for my day job."

* "I counted 14 job adverts on Monster.com for Javascript devs last week, 
what more evidence does anyone need?"

* "I googled for `What's the most popular language?` and found a blog 
that says it's Javascript, that's good enough for me."

* "I have a gut feeling."

If you are going to criticise TIOBE's methodology, and then make your own 
claims for language popularity, you really need to demonstrate that your 
methodology is better.


> Javascript
> may not be glamorous but it is *the* glue that makes the web run.

And web development is a tiny fraction of all software development.



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


Re: Why BOM in logging message?

2013-01-10 Thread Terry Reedy

On 1/10/2013 11:06 AM, Roy Smith wrote:


Well, this is fascinating.  It turns out that while all of our
machines report that they're running 2.7.3, they have two different
versions of /usr/lib/python2.7/logging/handlers.py!

-rw-r--r-- 1 root root 45076 Aug  1 05:39 /usr/lib/python2.7/logging/handlers.py
-rw-r--r-- 1 root root 45143 Apr 20  2012 /usr/lib/python2.7/logging/handlers.py

The April 24th version has the BOM code in SysLogHander.emit():


The 'BOM' you are discussing here is not a true 2 or 4 byte 
byte-order-mark, but the pseudo-BOM (pseudo because a stream of single 
bytes has no byte order within the single bytes) that Micro$tupid adds 
(sometimes) to utf-8 encoded bytes to mark their encoding as utf-8 
rather than anything else. In otherwords, it is a non-(anti-)standard 
U(nicode)E(ncoding)M(ark). It is actually the utf-8 encoding of the 
2-byte BOM, and hence not a single mark! It is really confusing when 
people use 'BOM' to refer to a UEM sequence instead of a BOM.



| # Message is a string. Convert to bytes as required by RFC 5424
|if type(msg) is unicode:
|msg = msg.encode('utf-8')
|if codecs:
|msg = codecs.BOM_UTF8 + msg
|msg = prio + msg


2.7.3 was released in April.


and the August 1st version doesn't:

|# Message is a string. Convert to bytes as required by RFC 5424
|if type(msg) is unicode:
|msg = msg.encode('utf-8')
|msg = prio + msg


The issue referenced in an earlier messaged was to remove the UEM where 
it did not belong.



Is it possible there's two different 2.7.3 builds that somehow got
packaged by Ubuntu at different times?


As you discovered, Ubuntu sometimes updates a release with bug patches 
before we release a new official bugfix release. 2.7.4, with many 
bugfixes, is still to see the light of day.



The pattern of which machines
have the August code and which have the April code correlates with
when we rolled out each server instance.


Great detective work ;-).

--
Terry Jan Reedy

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


Re: RIse and fall of languages in 2012

2013-01-10 Thread Walter Hurry
On Thu, 10 Jan 2013 07:23:51 +, Steven D'Aprano wrote:

> "In general-purpose scripting languages, Python continues to grow
> slowly, JavaScript and Ruby are treading water, and Perl continues its
> long decline. According to Google trends, the number of searches for
> Perl is 19% of what it was in 2004. Its declining role in open-source
> communities further cements the perception that it's in an irretrievable
> tailspin.
> One should always be careful pronouncing a language dead or dying,
> because rare resurrections have occurred: JavaScript and Objective-C
> being two stand-out cases. However, Perl is unlikely to see such a new
> lease on life because of direct competition from Python, which is
> considerably more popular (whereas Objective-C and JavaScript had no
> direct equivalents when they came back)."

Why should we care? We use Python because it's powerful, easy, elegant  
and all the other things.
-- 
http://mail.python.org/mailman/listinfo/python-list


Probabilistic unit tests?

2013-01-10 Thread Nick Mellor
Hi,

I've got a unit test that will usually succeed but sometimes fails. An 
occasional failure is expected and fine. It's failing all the time I want to 
test for.

What I want to test is "on average, there are the same number of males and 
females in a sample, give or take 2%."

Here's the unit test code:
import unittest
from collections import counter

sex_count = Counter()
for contact in range(self.binary_check_sample_size):
p = get_record_as_dict()
sex_count[p['Sex']] += 1
self.assertAlmostEqual(sex_count['male'],
   sex_count['female'],
   delta=sample_size * 2.0 / 100.0)

My question is: how would you run an identical test 5 times and pass the group 
*as a whole* if only one or two iterations passed the test? Something like:

for n in range(5):
# self.assertAlmostEqual(...)
# if test passed: break
else:
self.fail()

(except that would create 5+1 tests as written!)

Thanks for any thoughts,

Best wishes,

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


Re: Probabilistic unit tests?

2013-01-10 Thread Roy Smith
In article ,
 Nick Mellor  wrote:

> Hi,
> 
> I've got a unit test that will usually succeed but sometimes fails. An 
> occasional failure is expected and fine. It's failing all the time I want to 
> test for.
> 
> What I want to test is "on average, there are the same number of males and 
> females in a sample, give or take 2%."
> [...]
> My question is: how would you run an identical test 5 times and pass the 
> group *as a whole* if only one or two iterations passed the test? Something 
> like:
> 
> for n in range(5):
> # self.assertAlmostEqual(...)
> # if test passed: break
> else:
> self.fail()

I would do something like:

def do_test_body():
   """Returns 1 if it passes, 0 if it fails"""

results = [do_test() for n in range(number_of_trials)]
self.assert(sum(results) > threshold)

That's the simple part.

The more complicated part is figuring out how many times to run the test 
and what an appropriate threshold is.  For that, you need to talk to a 
statistician.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Probabilistic unit tests?

2013-01-10 Thread Steven D'Aprano
On Thu, 10 Jan 2013 17:59:05 -0800, Nick Mellor wrote:

> Hi,
> 
> I've got a unit test that will usually succeed but sometimes fails. An
> occasional failure is expected and fine. It's failing all the time I
> want to test for.

Well, that's not really a task for unit testing. Unit tests, like most 
tests, are well suited to deterministic tests, but not really to 
probabilistic testing. As far as I know, there aren't really any good 
frameworks for probabilistic testing, so you're stuck with inventing your 
own. (Possibly on top of unittest.)


> What I want to test is "on average, there are the same number of males
> and females in a sample, give or take 2%."
> 
> Here's the unit test code:
> import unittest
> from collections import counter
> 
> sex_count = Counter()
> for contact in range(self.binary_check_sample_size):
> p = get_record_as_dict()
> sex_count[p['Sex']] += 1
> self.assertAlmostEqual(sex_count['male'],
>sex_count['female'],
>delta=sample_size * 2.0 / 100.0)

That's a cheap and easy way to almost get what you want, or at least what 
I think you should want.

Rather than a "Succeed/Fail" boolean test result, I think it is worth 
producing a float between 0 and 1 inclusive, where 0 is "definitely 
failed" and 1 is "definitely passed", and intermediate values reflect 
some sort of fuzzy logic score. In your case, you might look at the ratio 
of males to females. If the ratio is exactly 1, the fuzzy score would be 
1.0 ("definitely passed"), otherwise as the ratio gets further away from 
1, the score would approach 0.0:

if males <= females:
score = males/females
else:
score = females/males

should do it.

Finally you probabilistic-test framework could then either report the 
score itself, or decide on a cut-off value below which you turn it into a 
unittest failure.

That's still not quite right though. To be accurate, you're getting into 
the realm of hypotheses testing and conditional probabilities:

- if these random samples of males and females came from a population of 
equal numbers of each, what is the probability I could have got the 
result I did?

- should I reject the hypothesis that the samples came from a population 
with equal numbers of males and females?


Talk to a statistician on how to do this.


> My question is: how would you run an identical test 5 times and pass the
> group *as a whole* if only one or two iterations passed the test?
> Something like:
> 
> for n in range(5):
> # self.assertAlmostEqual(...)
> # if test passed: break
> else:
> self.fail()
> 
> (except that would create 5+1 tests as written!)


Simple -- don't use assertAlmostEqual, or any other of the unittest 
assertSomething methods. Write your own function to decide whether or not 
something passed, then count how many times it passed:

count = 0
for n in range(5):
count += self.run_some_test()  # returns 0 or 1, or a fuzzy score
if count < some_cut_off:
self.fail()


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


Re: How to modify this script?

2013-01-10 Thread Kurt Hansen

Kurt wrote:
>
> Spooky behavior. Yes, the green-apple-example also works for me with
> your new script, BUT ...!
>
> Try to copy the table content on this page:
> http://www.danacord.dk/frmsets/records/732-r.html
> which is a realistic scenario. That's whar I am doing these days.
>
> Pasting it into Gedit and running the snippet blanks the edit area (on
> MY Mac at least).
>
> And yes: I have pasted your code excatly and I've double-checked for
> linewraps. Everything is okay.

Chaouche replied:


Indeed, the console shows a traceback where data is misinterpreted,
maybe due to my triple protective quotes around $GEDIT_SELECTED_TEXT.
Try without them, like so (it worked for me) :


Yes!!! :-)

Of course it would be nice if the script could be developed to take into 
account some of the antics I have made in my tables over the years, but 
with this script the basic codes of rows and columns is formatted 
properly; refinements of some collapsed fields with with line breaks 
a.o. is up to me now ;-).


Thanks to you and others who have participated in this thread.
--
Venlig hilsen
Kurt Hansen
--
http://mail.python.org/mailman/listinfo/python-list


Re: RIse and fall of languages in 2012

2013-01-10 Thread Craig Yoshioka
At one point or another I'm pretty sure I've googled "_ sucks" for every 
language I've ever used- even the ones I like. ie: Python easily more than 
once. 

Craig reporting from the road
10550 N Torrey Pines Rd
La Jolla CA 92037
work: 858 784 9208
cell: 619 623 2233

On Jan 10, 2013, at 3:32 PM, Steven D'Aprano 
 wrote:

> On Thu, 10 Jan 2013 12:42:49 -0700, Michael Torrie wrote:
> 
>>> And from the TIOBE Index, Python is steady at number 8:
>>> 
>>> http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
>> 
>> The TIOBE index is meaningless.  Since it's based on google searches,
>> one could probably guess that any language that is awkward and difficult
>> will require more searches to figure out how to use the thing.  Thus of
>> course C is top!  Especially if ranked by sarcastic queries like, "C
>> sucks," and "why does C suck so much."
> 
> If you have a problem with TIOBE's methodology, feel free to come up with 
> your own. Or take it up with them.
> 
> I dispute that TIOBE measures difficulty of language. If it did, Malbolge 
> would likely be at the top of the list. Yes, there are sarcastic queries 
> asking "C sucks", but that's just measurement error: 21,200 hits for "C 
> sucks" versus 9,900,000 for "C programming". It's not as if there is any 
> language, not even Python, that is so easy to use that nobody needs to 
> write about it.
> 
> 
>> Javascript is doing much more than just "treading water."
> 
> How do you know? What's *your* methodology for determining the popularity 
> of a language?
> 
> * "But everybody knows that Javascript is super popular!!!"
> 
> * "All my friends are using Javascript."
> 
> * "I'm a web developer, and I use Javascript for my day job."
> 
> * "I counted 14 job adverts on Monster.com for Javascript devs last week, 
> what more evidence does anyone need?"
> 
> * "I googled for `What's the most popular language?` and found a blog 
> that says it's Javascript, that's good enough for me."
> 
> * "I have a gut feeling."
> 
> If you are going to criticise TIOBE's methodology, and then make your own 
> claims for language popularity, you really need to demonstrate that your 
> methodology is better.
> 
> 
>> Javascript
>> may not be glamorous but it is *the* glue that makes the web run.
> 
> And web development is a tiny fraction of all software development.
> 
> 
> 
> -- 
> Steven
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Over 30 types of variables available in python ?

2013-01-10 Thread Rick Johnson
> On 1-7-2013 2:53:26 AM UTC-6, chaouche yacine wrote:
> 
> Thanks for all your comments. It appears to me that there
> is a slight confusion between types and classes then, plus
> other entities (protocols ?)

The only "confusion" stems from improper terminology. "Class" is the worst 
possible word to describe: /"the code written by a programmer to define an 
object"/. 

This epidemic of transforming words improperly is not only a python problem (as 
you well know). I believe it is high time we stop designing languages by 
propagating foolish terminology simply because we have no will to break the 
"status quo". 

And everybody needs to help push this cause along by refusing to use the word 
"class" and only use the words "object definition". This is something we can 
all do WITHOUT modifying the python source. This is how you get the snowball 
rolling. However, if we do this for long enough, language designers will start 
to realize that "class" is NOT the proper terminology and MAYBE they will 
consider terminology a bit more. Well, a boy can dream...

We MUST separate the idea of: "an object that lives in memory" from the: "code 
that defines the object" AND we must do so by wielding intuitive terminology. 

"Just because person X decides to jump off a bridge, that action does not 
impose person Y to blindly follow"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change colors of multiple widgets after hovering in Tkinter

2013-01-10 Thread Rick Johnson
On Thursday, January 10, 2013 1:13:38 PM UTC-6, Peter Otten wrote:
> mountdoom wrote:
> > I´m trying to make a script, which will change the background and
> > foreground color of widgets after hovering.

Peter's advice is spot on except you may want ALL widgets to change colors on 
 and  events. If you want all widgets use the "w.bind_all" method 
instead of "w.bind". Also check out the "w.bind_class" method to confine 
bindings to one particular class of widget (like a Tkinter.Button).
-- 
http://mail.python.org/mailman/listinfo/python-list


PyWart: Module access syntax

2013-01-10 Thread Rick Johnson

Python's module/package access uses dot notation. 

  mod1.mod2.mod3.modN

Like many warts of the language, this wart is not so apparent when first 
learning the language. The dot seems innocently sufficient, however, in truth 
it is woefully inadequate! Observe:

 name1.name2.name3.name4.name5

Can you tell me which names are modules, which are classes, and which are 
methods/functions? Wait, i know the argument you'll use:

  """I can decipher from spelling! Modules use all lowercase, classes use 
initial uppercase, and methods use words_sep_by_underscore. I'm so smart!"""

Indeed. But that's only *_IF_* everybody used the same style guide. And as we 
know many people don't follow the style guide (not to mention the style guide 
has issues!) And since style is NOT enforced, we suffer the unintuitive syntax 
nightmare! The solution is obvious by using proper syntax. 

 import lib:gui:tkinter:dialogs.SimpleDialog as Blah

You /could/ use two colons:
 
 import lib::gui::tkinter::dialogs.SimpleDialog as Blah

...and i think this syntax does help to separate the identifiers more clearly, 
but since these imports can be quite long, i prefer the single colon myself.
-- 
http://mail.python.org/mailman/listinfo/python-list


PyWart: Import resolution order

2013-01-10 Thread Rick Johnson

Python's import resolution order is terrible.[1]

The fact that Python looks in the stdlib _first_ is not a good idea. It would 
seem more intuitive for a custom "math" module (living in the current 
directory) to /override/ the stlib "math" module. The proper order is as 
follows:

1. Current package or directory
2. stdlib
3. under the bed
4. who cares at this point

Look, if you want to keep you foolish imports starting from the stdlib, fine, 
just create a switch so we can change it to "package/directory" if we like. 

If /only/ the Python gods had placed all stdlib modules in a package named, i 
don't know, "lib" then none of this would be an issue. You could happily have a 
stdlib::math and a mylib::math, and when you import either module your code 
will reflect that path explicitly. Hmm, this last sentence reminded me of 
something??? Oh yes -> "Explicit is better than implicit".


[1]: Yes, yes, i know about sys.path.insert. *puke*!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyWart: Import resolution order

2013-01-10 Thread Chris Angelico
On Fri, Jan 11, 2013 at 5:13 PM, Rick Johnson
 wrote:
> The fact that Python looks in the stdlib _first_ is not a good idea. It would 
> seem more intuitive for a custom "math" module (living in the current 
> directory) to /override/ the stlib "math" module. The proper order is as 
> follows:
>
> 1. Current package or directory
> 2. stdlib
> 3. under the bed
> 4. who cares at this point

Why is it better to import from the current directory first? Windows
has that policy for executable commands; Unix, on the other hand,
requires that you put an explicit path for anything that isn't in the
standard search path. Which of these options is the more likely to
produce security holes and/or unexpected behaviour?

Welcome back to the list, Rick. Got any demonstrable code for Python 4000 yet?

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