ANN: matplotlib-0.98.3 - plotting for python

2008-08-06 Thread [EMAIL PROTECTED]
matplotlib is a 2D plotting library for python for use in scripts,
applications, interactive shell work or web application servers.
matplotlib 0.98.3 is a major release but stable release which brings
many new features detailed below.

 Homepage: http://matplotlib.sourceforge.net/

 Downloads: 
http://sourceforge.net/project/showfiles.php?group_id=80706package_id=278194release_id=617552

 Screenshots: http://matplotlib.sourceforge.net/screenshots.html


Thanks to Charlie Moad for the release and for all the matplotlib
developers for the feature enhancements and bug fixes.

The following what's new summary is also online at
http://matplotlib.sourceforge.net/whats_new.html.

What's new
==

delaunay triangularization

 Jeffrey Whitaker has added support for gridding irregularly spaced
 data using the Matlab (TM) equivalent griddata function.  This is a
 long-standing feature request for matplotlib and a major
 enhancement.  matplotlib now ships with Robert Kern's delaunay
 triangularization code (BSD license), which supports the default
 griddata implementation, but there are some known corner cases where
 this routine fails.  As such, Jeff has provided a python wrapper to
 the NCAR natgrid routines, whose licensing terms are a bit murkier,
 for those who need bullet proof gridding routines.  If the NCAR
 toolkit is installed, griddata will detect it and use it.  See
 http://matplotlib.sf.net/matplotlib.mlab.html#-griddata for details.
 Thanks Robert and Jeff.

proper paths

 For the first time, matplotlib supports spline paths across
 backends, so you can pretty much draw anything.  See the
 http://matplotlib.sf.net/screenshots.html#path_patch_demo. Thanks to
 Michael Droettboom and http://www.stsci.edu (STScI).

better transformations

 In what has been described as open-heart surgery on matplotlib,
 Michael Droettboom, supported by http://www.stsci.edu (STSci) , has
 rewritten the transformation infrastructure from the ground up,
 which not only makes the code more intuitive, it supports custom
 user projections and scales.  See
 http://matplotlib.sf.net/doc/devel/add_new_projection.rst and the
 http://matplotlib.sf.net/matplotlib.transforms.html module
 documentation.

histogram enhancements

 hist (http://matplotlib.sf.net/matplotlib.pyplot.html#-hist) can
 handle 2D arrays and create side-by-side or stacked histograms, as
 well as cumulative filled and unfilled histograms; see
 http://matplotlib.sf.net/examples/pylab_examples/histogram_demo_extended.py

ginput function

 ginput (http://matplotlib.sf.net/matplotlib.pyplot.html#-ginput) is
 a blocking function for interactive use to get input from the user.
 A long requested feature submitted by Gael Varoquaux.  See
 http://matplotlib.sf.net/examples/pylab_examples/ginput_demo.py

wind barbs

 Ryan May has added support for wind barbs, which are popular among
 meterologists.  These are similar to direction fields or quiver
 plots but contain extra information about wind speed and other
 attributes.  See
 http://matplotlib.sf.net/examples/pylab_examples/barb_demo.py

external backends

 backend developers and users can now use custom backends outside the
 matplotlib tree, by using the special syntax
 module://my_backend for the backend setting in the rc
 file, the use directive, or in -d command line argument to
 pylab/pyplot scripts

findobj

 Introduced a recursive object search method to find all objects that
 meet some matching criterion, ef to find all text instances in a
 figure.  See
 http://matplotlib.sf.net/examples/pylab_examples/findobj_demo.py


saving transparent figures

 http://matplotlib.sf.net/matplotlib.pyplot.html#-savefig now
 supports a *transparent* keyword argument to set the figure an axes
 backgrounds transparent.  Useful when you want to embed matplotlib
 figures with transparent backgrounds into other documents

axes3d support removed

 Amid considerable controversy from the users, we decided to pull the
 experimental 3D support from matplotlib.  Although basic 3D support
 remains a goal, the 3D support we had was mainly orphaned, and we
 need a developer with interest to step up and maintain it.

mathtext outside matplotlib

 The mathtext support in matplotlib is very good, and some folks want
 to be able to use it outside of matplotlib figures.  We added some
 helper functions to get the mathtext rendered pixel buffer as a
 numpy array, with an example at
 http://matplotlib.sf.net/examples/api/mathtext_asarray.py


image optimizations

 enhancements to speed up color mapping and panning and zooming on
 dense images


better savefig

 http://matplotlib.sf.net/matplotlib.pyplot.html#-savefig now
 supports save to file handles (great for web app servers) or unicode
 filenames on all backends

record array functions

 some more helper functions to facilitate work with record arrays:
 http://matplotlib.sf.net/matplotlib.mlab.html#-rec_groupby,
 http://matplotlib.sf.net/matplotlib.mlab.html#-rec2txt,
 

Re: How to troubleshoot hanging script?

2008-08-06 Thread Alexandru Palade

Hi,

Just a thought, I'm not sure it's the best way.
You can start an alarm just before the line you *think* it's wrong and
cancel it after that. If it's activated then you probably have
pinpointed the location. Have a look at the signals module
http://docs.python.org/lib/module-signal.html, the alarm function.

Hope it helps.

kj wrote:

Hi!  I have a Pythonoob question.

I have a script that hangs indefinitely at random times; the only
thing to do at this point is to kill it.

I'm looking for suggestions on how to troubleshoot and debug the
problem.

I'm not even sure of where exactly the script is hanging, though
I suspect it is right around when it tries to read from a pipe
(generated by popen).  (I arrived at this guess by putting print
statements all over the place, but I'm not sure this is a very
reliable way to pinpoint the error.)

So the first thing I'd like to do is find out exactly where the
script is hanging.  It would be nice if I could just hit Ctrl-C
(which sends SIGINT from the terminal) when the script is hanging,
to kill it and get a backtrace, but when this script hangs it
becomes unresponsive to Ctrl-C!  The only way to kill it is with
something like

  % pkill -KILL my_script.py

or even

  % pkill -TERM my_script.py

...or -ABRT or -QUIT.  I tried to exploit this by adding this to the
script:

import signal

def term_handler(signum, frame):
raise KeyboardInterrupt

signal.signal(signal.SIGTERM, term_handler)

...but this did not help at all; in fact, after this addition, the script
no longer responded to pkill -TERM.

TIA!

Kynn
  


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


Re: How to troubleshoot hanging script?

2008-08-06 Thread Bruno Desthuilliers

kj a écrit :

Hi!  I have a Pythonoob question.

I have a script that hangs indefinitely at random times; the only
thing to do at this point is to kill it.

I'm looking for suggestions on how to troubleshoot and debug the
problem.

I'm not even sure of where exactly the script is hanging, though
I suspect it is right around when it tries to read from a pipe
(generated by popen).  (I arrived at this guess by putting print
statements all over the place, but I'm not sure this is a very
reliable way to pinpoint the error.)

So the first thing I'd like to do is find out exactly where the
script is hanging.  It would be nice if I could just hit Ctrl-C
(which sends SIGINT from the terminal) when the script is hanging,
to kill it and get a backtrace, but when this script hangs it
becomes unresponsive to Ctrl-C! 


Pretty often, this kind of behaviour is related to a catch-all (or at 
least a 'catch-too-much') except clause that doesn't re-raise.



wrt/ debugging, there's a command-line debugger named pdb in the stdlib.
--
http://mail.python.org/mailman/listinfo/python-list


Re: URLs and ampersands

2008-08-06 Thread Duncan Booth
Matthew Woodcraft [EMAIL PROTECTED] wrote:

 Gabriel Genellina wrote:
 Steven D'Aprano wrote:
 
 I have searched for, but been unable to find, standard library
 functions that escapes or unescapes URLs. Are there any such
 functions?
 
 Yes: cgi.escape/unescape, and xml.sax.saxutils.escape/unescape.
 
 I don't see a cgi.unescape in the standard library.
 
 I don't think xml.sax.saxutils.unescape will be suitable for Steven's
 purpose, because it doesn't process numeric character references (which
 are both legal and seen in the wild in /href/ attributes).
 

Here's the code I use. It handles decimal and hex entity references as well 
as all html named entities.

import re
from htmlentitydefs import name2codepoint
name2codepoint = name2codepoint.copy()
name2codepoint['apos']=ord(')

EntityPattern = re.compile('(?:#(\d+)|(?:#x([\da-fA-F]+))|([a-zA-Z]+));')
def decodeEntities(s, encoding='utf-8'):
def unescape(match):
code = match.group(1)
if code:
return unichr(int(code, 10))
else:
code = match.group(2)
if code:
return unichr(int(code, 16))
else:
code = match.group(3)
if code in name2codepoint:
return unichr(name2codepoint[code])
return match.group(0)

if isinstance(s, str):
s = s.decode(encoding)
return EntityPattern.sub(unescape, s)



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


Re: very large dictionary

2008-08-06 Thread Bruno Desthuilliers

Simon Strobl a écrit :
(snip)
 I would prefer to be able to use the same type of

scripts with data of all sizes, though.


Since computers have a limited RAM, this is to remain a wish. You can't 
obviously expect to deal with terabytes of data like you do with a 1kb 
text file.

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


Re: Python-URL! - weekly Python news and links (Jul 28)

2008-08-06 Thread Bruno Desthuilliers

Gabriel Genellina a écrit :

En Tue, 05 Aug 2008 06:28:33 -0300, Bruno Desthuilliers [EMAIL PROTECTED] 
escribió:


Gabriel Genellina a écrit :

QOTW:  Python's goals are to maximize opportunities for good
programming, which is quite different. - Bruno Desthuilliers, contrasting
Python with Java


I'm afraid I mostly (and approximatly) quoted somebody else here (just
don't ask me for a link to the original...).


I tried to creatively google for similar phrases, but no luck...

So be it... Perhaps someone will come up one day claiming paternity for 
this quote.

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


Re: Tkinter Entry widgets 'font' property (API ?) changed in Python 2.5.2 ?

2008-08-06 Thread Eric Brunel

On Wed, 06 Aug 2008 06:01:59 +0200, Atul [EMAIL PROTECTED] wrote:


Hi,

The snippet :

entryFontDescr = Entry()[font]
print self.entryFontDescr

On Windows XP it displays

{MS Sans Serif} 8

On Suse Linux 10.2 it used to display

TkTextFont 10

I upgraded to OpenSuse 11 and now it shows

TkTextFont

I used this snippet to obtain the default font size for an Entry
widget. Now with an OS upgrade, my code is broken.

The python version on the upgraded box is

~ python
Python 2.5.2 (r252:60911, Jun  6 2008, 23:32:27)
[GCC 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]] on
linux2
Type help, copyright, credits or license for more information.




I dont remember the exact version of Python on the earlier Suse 10.2
box

My questions:

1. Is this not an API change ? I looked up Python's release
documentation and didn't find any mention of the same.


Tkinter is a very thin wrapper over an embedded tcl/tk interpreter. So I  
guess the API change is caused by a tcl/tk version change, not by a Python  
one. You can check the version of the tcl/tk interpreter you're using from  
Python via:


root = Tkinter.Tk()
root.tk.eval('puts $tcl_patchLevel')
root.tk.eval('puts $tk_patchLevel')


2. How can I achieve what I want alternatively ?


I'd use this way:

import tkFont
entryFontDescr = Entry()[font]
entry_font = tkFont.Font(font=entryFontDescr)
print entry_font.actual()


Regards,
-- Atul


HTH
--
python -c print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])

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


Re: very large dictionary

2008-08-06 Thread Jake Anderson

Bruno Desthuilliers wrote:

Simon Strobl a écrit :
(snip)
 I would prefer to be able to use the same type of

scripts with data of all sizes, though.


Since computers have a limited RAM, this is to remain a wish. You 
can't obviously expect to deal with terabytes of data like you do with 
a 1kb text file.

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

You can, you just start off handling the multi GB case and your set.
databases are really easy, I often use them for manipulating pretty 
small amounts of data because its just an easy way to group and join etc.


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


python script help

2008-08-06 Thread Heather Cole
Hello all,

not sure if anyone will get this, I did register, but haven't yet received a 
confimation.

I am a 100% newbie when it comes to python script, but I think what I want to 
do is relatively straight forward. I have a table with many species and 
coordinate points, for each species, I want to make a buffer around the points, 
then generate random samples (within a polygon).

I know how to do all of these in ARCmap, but don't know how to automate it for 
my hundreds of species?

Any help or advice on how hard or easy it would be to write a script to do this 
would be greatly appreciated!

thanks!



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

Re: Locking around

2008-08-06 Thread Ulrich Eckhardt
Nikolaus Rath wrote:
 I need to synchronize the access to a couple of hundred-thousand
 files[1]. It seems to me that creating one lock object for each of the
 files is a waste of resources, but I cannot use a global lock for all
 of them either (since the locked operations go over the network, this
 would make the whole application essentially single-threaded even
 though most operations act on different files).

Just wondering, but at what time do you know what files are needed? If you
know that rather early, you could simply 'check out' the required files, do
whatever you want with them and then release them again. If one of the
requested files is marked as already in use, you simply wait (without
reserving the others) until someone releases files and then try again. You
could also wait for that precise file to be available, but that would
require that you already reserve the other files, which might unnecessarily
block other accesses.

Note that this idea requires that each access locks one set of files at the
beginning and releases them at the end, i.e. no attempts to lock files in
between, which would otherwise easily lead to deadlocks.

Further, you could distinguish between read-only and read-write access as an
optimisation.

 My idea is therefore to create and destroy per-file locks on-demand
 and to protect the creation and destruction by a global lock
 (self.global_lock). For that, I add a usage counter
 (wlock.user_count) to each lock, and destroy the lock when it reaches
 zero.
[...code...]

  - Does that look like a proper solution, or does anyone have a better
one?

This should work, at least the idea is not flawed. However, I'd say there
are too many locks involved. Rather, you just need a simple flag and the
global lock. Further, you need a condition/event that tells waiting threads
that you released some of the files so that it should see again if the ones
it wants are available.

  - Did I overlook any deadlock possibilities?

The normal deadlock possibilities when multiple locks are involved apply,
you must make sure that they are always acquired in an order that prevents
two threads waiting for a resource held by the other.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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

Re: Why doesn't import work?

2008-08-06 Thread alex23
On Aug 5, 8:34 am, ssecorp [EMAIL PROTECTED] wrote:
 I have in Lib/site-packages a module named pdfminer. when I do import
 pdfminer it complains:

 so I apparently can't import a directory pdfminer. In the directory
 pdfminer there are 3 other directoriees and inside them python-files.

Are the 3 directories called pdflib, samples  tools?

 how would I import them?

The simple answer is: you -shouldn't- be.

PDFMiner is a set of tools -to be used from the command line- rather
than a PDF-handling library. You're not meant to unpack PDFMiner into
site-packages, instead you should unpack it to a temporary location
and run make. Please read the documentation on the PDFMiner site, it's
pretty clear that it's a suite of tools.

If you're after a library for dealing programmatically with PDF files,
try pyPDF:
http://pybrary.net/pyPdf/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to fix Invalid CSV File

2008-08-06 Thread Roel Schroeven

Ryan Rosario schreef:


Next time I am going to be much more careful. Tab delimited is
probably better for my purpose, but I can definitely see there being
issues with invisible tab characters and other weirdness.


No matter which delimiter you use, there will always be data that 
includes that delimiter, and you need some way to deal with it.


I prefer the approach that esr suggests in The Art of Unix Programming 
(http://www.catb.org/~esr/writings/taoup/html/ch05s02.html): define a 
delimiter (preferably but necessary one that doesn't occur frequently in 
your data) and an escape character. On output, escape all occurrences of 
delimiter and escape character in your data. On input, you can trivially 
and unambiguously distinguish delimiters in the data from delimiters 
between data, and unescape everything.


Cheers,
Roel

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

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


Re: Print statement isn't showing up?

2008-08-06 Thread Lie
On Aug 6, 2:28 am, Timothy Grant [EMAIL PROTECTED] wrote:
 On Tue, Aug 5, 2008 at 9:09 AM, Robert Dailey [EMAIL PROTECTED] wrote:
  Hi,

  I have the following code:

  def ReplaceExternalWithCopy( localDir, remoteDir ):
      print Removing external local directory:, localDir
      rmdirs( localDir )
      vfxrepo.copy( remoteDir, localDir )

  I noticed that the print statement above does not show up before
  vfxrepo.copy() is called. the copy() function (as well as the rmdirs()
  function) are very long file-system calls that take up to 5 minutes. I
  should see a print statement before these are executed, but I do not.
  Instead it shows up *after* the last 2 lines of code have completed. Is
  there something broken about this?

 My guess is that the output is getting buffered and the buffer doesn't
 get flushed until sometime after the function executes.

 --
 Stand Fast,
 tjg. [Timothy Grant]

Are you calling this function from inside, say, doctests-watched
comments.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Locking around

2008-08-06 Thread Nikolaus Rath
Ulrich Eckhardt [EMAIL PROTECTED] writes:
 Nikolaus Rath wrote:
 I need to synchronize the access to a couple of hundred-thousand
 files[1]. It seems to me that creating one lock object for each of the
 files is a waste of resources, but I cannot use a global lock for all
 of them either (since the locked operations go over the network, this
 would make the whole application essentially single-threaded even
 though most operations act on different files).

 Just wondering, but at what time do you know what files are needed?

As soon as I have read a client request. Also, I will only need one
file per request, not multiple.

 If you know that rather early, you could simply 'check out' the
 required files, do whatever you want with them and then release them
 again. If one of the requested files is marked as already in use,
 you simply wait (without reserving the others) until someone
 releases files and then try again. You could also wait for that
 precise file to be available, but that would require that you
 already reserve the other files, which might unnecessarily block
 other accesses.

 Note that this idea requires that each access locks one set of files at the
 beginning and releases them at the end, i.e. no attempts to lock files in
 between, which would otherwise easily lead to deadlocks.

I am not sure that I understand your idea. To me this sounds exactly
like what I'm already doing, just replace 'check out' by 'lock' in
your description... Am I missing something?

 My idea is therefore to create and destroy per-file locks on-demand
 and to protect the creation and destruction by a global lock
 (self.global_lock). For that, I add a usage counter
 (wlock.user_count) to each lock, and destroy the lock when it reaches
 zero.
 [...code...]

  - Does that look like a proper solution, or does anyone have a better
one?

 This should work, at least the idea is not flawed. However, I'd say
 there are too many locks involved. Rather, you just need a simple
 flag and the global lock. Further, you need a condition/event that
 tells waiting threads that you released some of the files so that it
 should see again if the ones it wants are available.

I have to agree that this sounds like an easier implementation. I just
have to think about how to do the signalling. Thanks a lot!


Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

soap call through firewall

2008-08-06 Thread Edwin . Madari
hi,

any hints/pointers appreciated if you have succeeded in making a soap call 
through a firewall. 
other than this  http://www.ibm.com/developerworks/xml/library/x-tipfire.html 
cannot find much.

thanks in advance
Edwin


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.

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

SOAPpy how to

2008-08-06 Thread Edwin . Madari
unable to get past local proxy server with SOAPpy client. In the code below 
using 'thproxy' or 'httpproxy' variable for http_proxy fails.

from SOAPpy import WSDL

proxyuser='..'
proxypass='..
httpproxy=a.b.c.com:1234
theproxy='http://'+proxyuser+':'+proxypass+'@'+httpproxy

wsdl='sample.wsdl'
#soap service provided by the soap server defined in the wsdl
server = WSDL.Proxy(wsdl, http_proxy=httpproxy)  #fails with 
SOAPpy.Errors.HTTPError: HTTPError 407 Proxy Authentication required
#server = WSDL.Proxy(wsdl, http_proxy=theproxy) #fails with socket.gaierror: 
(7, 'getaddrinfo failed')

a = server.aTestMethod( arg1, arg2, )
print a

any suggestions or hints any one..

thanks in advance
Edwin



The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.

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

Re: Locking around

2008-08-06 Thread Nikolaus Rath
Nikolaus Rath [EMAIL PROTECTED] writes:
 This should work, at least the idea is not flawed. However, I'd say
 there are too many locks involved. Rather, you just need a simple
 flag and the global lock. Further, you need a condition/event that
 tells waiting threads that you released some of the files so that it
 should see again if the ones it wants are available.

 I have to agree that this sounds like an easier implementation. I
 just have to think about how to do the signalling. Thanks a lot!

Here's the code I use now. I think it's also significantly easier to
understand (cv is a threading.Condition() object and cv.locked_keys a
set()).

def lock_s3key(s3key):
cv = self.s3_lock

try:
# Lock set of locked s3 keys (global lock)
cv.acquire()

# Wait for given s3 key becoming unused
while s3key in cv.locked_keys:
cv.wait()

# Mark it as used (local lock)
cv.locked_keys.add(s3key)
finally:
# Release global lock
cv.release()


def unlock_s3key(s3key):
cv = self.s3_lock

try:
# Lock set of locked s3 keys (global lock)
cv.acquire()

# Mark key as free (release local lock)
cv.locked_keys.remove(s3key)

# Notify other threads
cv.notify()

finally:
# Release global lock
cv.release()


Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

Re: Locking around

2008-08-06 Thread Carl Banks
On Aug 4, 9:30 am, Nikolaus Rath [EMAIL PROTECTED] wrote:
 Hello,

 I need to synchronize the access to a couple of hundred-thousand
 files[1]. It seems to me that creating one lock object for each of the
 files is a waste of resources, but I cannot use a global lock for all
 of them either (since the locked operations go over the network, this
 would make the whole application essentially single-threaded even
 though most operations act on different files).

 My idea is therefore to create and destroy per-file locks on-demand
 and to protect the creation and destruction by a global lock
 (self.global_lock). For that, I add a usage counter
 (wlock.user_count) to each lock, and destroy the lock when it reaches
 zero.
[snip]
 My questions:

  - Does that look like a proper solution, or does anyone have a better
one?


You need the per-file locks at all if you use a global lock like
this.  Here's a way to do it using threading.Condition objects.  I
suspect it might not perform so well if there is a lot of competition
for certain keys but it doesn't sound like that's the case for you.
Performance and robustness improvements left as an exercise.  (Note:
I'm not sure where self comes from in your examples so I left it out
of mine.)


global_lock = threading.Condition()
locked_keys = set()

def lock_s3key(s3key):
global_lock.acquire()
while s3key in locked_keys:
global_lock.wait()
locked_keys.add(s3key)
global_lock.release()

def unlock_s3key(s3key):
global_lock.acquire()
locked_keys.remove(s3key)
global_lock.notifyAll()
global_lock.release()



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


Re: Locking around

2008-08-06 Thread Carl Banks
On Aug 6, 6:34 am, Nikolaus Rath [EMAIL PROTECTED] wrote:
 Nikolaus Rath [EMAIL PROTECTED] writes:
  This should work, at least the idea is not flawed. However, I'd say
  there are too many locks involved. Rather, you just need a simple
  flag and the global lock. Further, you need a condition/event that
  tells waiting threads that you released some of the files so that it
  should see again if the ones it wants are available.

  I have to agree that this sounds like an easier implementation. I
  just have to think about how to do the signalling. Thanks a lot!

 Here's the code I use now. I think it's also significantly easier to
 understand (cv is a threading.Condition() object and cv.locked_keys a
 set()).

     def lock_s3key(s3key):
         cv = self.s3_lock

         try:
             # Lock set of locked s3 keys (global lock)
             cv.acquire()

             # Wait for given s3 key becoming unused
             while s3key in cv.locked_keys:
                 cv.wait()

             # Mark it as used (local lock)
             cv.locked_keys.add(s3key)
         finally:
             # Release global lock
             cv.release()

     def unlock_s3key(s3key):
         cv = self.s3_lock

         try:
             # Lock set of locked s3 keys (global lock)
             cv.acquire()

             # Mark key as free (release local lock)
             cv.locked_keys.remove(s3key)

             # Notify other threads
             cv.notify()

         finally:
             # Release global lock
             cv.release()

Freaky... I just posted nearly this exact solution.

I have a couple comments.  First, the call to acquire should come
before the try block.  If the acquire were to fail, you wouldn't want
to release the lock on cleanup.

Second, you need to change notify() to notifyAll(); notify alone won't
cut it.  Consider what happens if you have two threads waiting for
keys A and B respectively.  When the thread that has B is done, it
releases B and calls notify, but notify happens to wake up the thread
waiting on A.  Thus the thread waiting on B is starved.


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


Help with mechanize

2008-08-06 Thread Neal Becker
I'm trying to use mechanize to read for a M$ mail server.  I can get past the 
login page OK using:

import mechanize

b = mechanize.Browser()
b.open 
('https://mail.hughes.com/owa/auth/logon.aspx?url=https://mail.hughes.com/OWA/reason=0')
b.select_form(nr=0)
b['username']='myname'
b['password']='password'
b.submit()

Now it seems if I read b.links() I can see links to my mail.  My question is, 
how do I now actually get the contents of this page?




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


Accessing tree nodes from the cgi

2008-08-06 Thread Noorhan Abbas
Hello,
I wonder if anyone knows how to sort out this problem for me! I have a Yhaoo 
tree view control created using javascript and I don't know how to get the node 
selected from within my python cgi?  
Anyone can help please?
Nora


  __
Not happy with your email address?.
Get the one you really want - millions of new email addresses available now at 
Yahoo! http://uk.docs.yahoo.com/ymail/new.html--
http://mail.python.org/mailman/listinfo/python-list

Re: Help with mechanize

2008-08-06 Thread Wojtek Walczak
Dnia Wed, 06 Aug 2008 07:16:37 -0400, Neal Becker napisa�(a):
 I'm trying to use mechanize to read for a M$ mail server.  I can get past the 
 login page OK using:
...
 Now it seems if I read b.links() I can see links to my mail.  My question is, 
 how do I now actually get the contents of this page?


Have you tried follow_link() method?
In your case it should be something like:

response = b.follow_link(b.links()[0]) # I suppose links()
   # returns a list or tuple
print response.info() # headers
print response.read() # body

IIRC, it's described in the documentation.

-- 
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list

Re: sys.ps1 with formatting (linux)

2008-08-06 Thread Hugo

Hi all,

My apologies for resurrecting an old thread, but I couldn't find the 
answer on the list and others might still have the same problem.


On Mon Jul 23 22:33:22 CEST 2007, Jon Dobson wrote (reformatted):

I'm trying to set sys.ps1 and sys.ps2 with some formatting using:

sys.ps1=\033[1m\033[32mspy\033[0m

 sys.ps2=\033[1m\033[32m   .\033[0m


I get the colored prompt(s) as you might expect, but I'm getting some

 strange behavior with wrapping.  Once the interactive command gets long
 enough to wrap, it wraps back to the same line (overwriting the
 beginning).  It doesn't break anything - the command gets interpreted
 correctly, it's just an ugly way to work.

After a couple of hours of having the same problem, I found out that you 
should surround the unprintable characters with \001 and \002:


sys.ps1=\001\033[1m\033[32m\002spy\001\033[0m\002
sys.ps2=\001\033[1m\033[32m\002   .\001\033[0m\002

Solution found in: http://hg.secdev.org/scapy/raw-file/tip/scapy.py:
 ^A and ^B delimit invisible caracters for readline to count right

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


enhancing decorator signatures

2008-08-06 Thread Diez B. Roggisch
Hi,

I'm using Michele S's decorator-module to create decorators with matching
signatures, for better error-catching.

However, I now want to enrich the signature of a generic wrapper so that the
new function will accept more parameters (keyword only). These additional
parameters are consumed by the wrapper and not passed to the decorated
function.

So something like this would be cool:

@enriched_decorator(bar)
def some_decorator(f, **args, **kwargs):
bar = kwargs.pop(bar)
return f(**args, **kwargs)

Anybody has done something like this?

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


ANN: matplotlib-0.98.3 - plotting for python

2008-08-06 Thread [EMAIL PROTECTED]
matplotlib is a 2D plotting library for python for use in scripts,
applications, interactive shell work or web application servers.
matplotlib 0.98.3 is a major but stable release which brings
many new features detailed below.


 Homepage: http://matplotlib.sourceforge.net/

 Downloads: 
http://sourceforge.net/project/showfiles.php?group_id=80706package_id=278194release_id=617552

 Screenshots: http://matplotlib.sourceforge.net/screenshots.html


Thanks to Charlie Moad for the release and for all the matplotlib
developers for the feature enhancements and bug fixes.

The following what's new summary is also online at
http://matplotlib.sourceforge.net/whats_new.html.

What's new
==

delaunay triangularization

 Jeffrey Whitaker has added support for gridding irregularly spaced
 data using the Matlab (TM) equivalent griddata function.  This is a
 long-standing feature request for matplotlib and a major
 enhancement.  matplotlib now ships with Robert Kern's delaunay
 triangularization code (BSD license), which supports the default
 griddata implementation, but there are some known corner cases where
 this routine fails.  As such, Jeff has provided a python wrapper to
 the NCAR natgrid routines, whose licensing terms are a bit murkier,
 for those who need bullet proof gridding routines.  If the NCAR
 toolkit is installed, griddata will detect it and use it.  See
 http://matplotlib.sf.net/matplotlib.mlab.html#-griddata for details.
 Thanks Robert and Jeff.

proper paths

 For the first time, matplotlib supports spline paths across
 backends, so you can pretty much draw anything.  See the
 http://matplotlib.sf.net/screenshots.html#path_patch_demo. Thanks to
 Michael Droettboom and http://www.stsci.edu (STScI).

better transformations

 In what has been described as open-heart surgery on matplotlib,
 Michael Droettboom, supported by http://www.stsci.edu (STSci) , has
 rewritten the transformation infrastructure from the ground up,
 which not only makes the code more intuitive, it supports custom
 user projections and scales.  See
 http://matplotlib.sf.net/doc/devel/add_new_projection.rst and the
 http://matplotlib.sf.net/matplotlib.transforms.html module
 documentation.

histogram enhancements

 hist (http://matplotlib.sf.net/matplotlib.pyplot.html#-hist) can
 handle 2D arrays and create side-by-side or stacked histograms, as
 well as cumulative filled and unfilled histograms; see
 http://matplotlib.sf.net/examples/pylab_examples/histogram_demo_extended.py

ginput function

 ginput (http://matplotlib.sf.net/matplotlib.pyplot.html#-ginput) is
 a blocking function for interactive use to get input from the user.
 A long requested feature submitted by Gael Varoquaux.  See
 http://matplotlib.sf.net/examples/pylab_examples/ginput_demo.py

wind barbs

 Ryan May has added support for wind barbs, which are popular among
 meterologists.  These are similar to direction fields or quiver
 plots but contain extra information about wind speed and other
 attributes.  See
 http://matplotlib.sf.net/examples/pylab_examples/barb_demo.py

external backends

 backend developers and users can now use custom backends outside the
 matplotlib tree, by using the special syntax
 module://my_backend for the backend setting in the rc
 file, the use directive, or in -d command line argument to
 pylab/pyplot scripts

findobj

 Introduced a recursive object search method to find all objects that
 meet some matching criterion, ef to find all text instances in a
 figure.  See
 http://matplotlib.sf.net/examples/pylab_examples/findobj_demo.py


saving transparent figures

 http://matplotlib.sf.net/matplotlib.pyplot.html#-savefig now
 supports a *transparent* keyword argument to set the figure an axes
 backgrounds transparent.  Useful when you want to embed matplotlib
 figures with transparent backgrounds into other documents

axes3d support removed

 Amid considerable controversy from the users, we decided to pull the
 experimental 3D support from matplotlib.  Although basic 3D support
 remains a goal, the 3D support we had was mainly orphaned, and we
 need a developer with interest to step up and maintain it.

mathtext outside matplotlib

 The mathtext support in matplotlib is very good, and some folks want
 to be able to use it outside of matplotlib figures.  We added some
 helper functions to get the mathtext rendered pixel buffer as a
 numpy array, with an example at
 http://matplotlib.sf.net/examples/api/mathtext_asarray.py


image optimizations

 enhancements to speed up color mapping and panning and zooming on
 dense images


better savefig

 http://matplotlib.sf.net/matplotlib.pyplot.html#-savefig now
 supports save to file handles (great for web app servers) or unicode
 filenames on all backends

record array functions

 some more helper functions to facilitate work with record arrays:
 http://matplotlib.sf.net/matplotlib.mlab.html#-rec_groupby,
 http://matplotlib.sf.net/matplotlib.mlab.html#-rec2txt,
 

Re: Limits of Metaprogramming

2008-08-06 Thread Wilson
On Aug 4, 9:23 pm, castironpi [EMAIL PROTECTED] wrote:
 On Aug 4, 1:57 pm, Wilson [EMAIL PROTECTED] wrote:

  On Aug 4, 6:49 pm, castironpi [EMAIL PROTECTED] wrote:

   Two, if all your methods will have uniform signatures and closures,
   you can store class methods as only their co_code objects:

C.g.im_func.func_code.co_code

   'd\x00\x00S'

   And fabricate them dynamically into full live types as needed.

  Thanks for your comments and advice. This second option intrigues me;
  could you elaborate further, I don't follow you...

  Thanks Paul

 Depending on the complexity of the functions, a code string could be
 all you need to store to determine (redetermine) a function's
 behavior.  For something moderately simple,

 def trans1( self, prev, trans ):
         if prev== 0 and trans== 'a':
                 return 1
         if prev== 1 and trans== 'b':
                 return 0
         return prev

 I found you need to store code.co_nlocals, code.co_code, and
 code.co_consts, to distinguish from a blank stub.  With extra
 variables, I needed code.co_names and code.co_varnames too.  To
 recreate a code object completely, you need 12 variables (14 to
 include closures), some of which are composite objects and would need
 to be pickled to be stored.

 Then you can build a new code object, then a new function object, then
 a new method object, then you can call it.  Instead of a module of
 code, perhaps you could have a datafile containing only these values,
 up to twelve per record, one record for each different function you
 have.

 Here is the benefit:

 newcode= dupecode( oldcode, codet1 )
 newfun= FunctionType( newcode, {} )
 stub.stub= MethodType( newfun, stub )
 prev= stub.stub( prev, trans )
 print prev

 You can loop over these five lines, re-loading function data with
 'dupecode', executing it, then reloading the next one, and you have a
 different function.  Additions to your database of functions would
 start in source first (unless you construct each parameter, in
 particular co_code, by hand, which you may want), then get compiled,
 then go in.

 Here is the complete constructor for a code object:

  help(_)

 Help on code object:

 class code(object)
  |  code(argcount, nlocals, stacksize, flags, codestring, constants,
 names,
  |        varnames, filename, name, firstlineno, lnotab[, freevars[,
 cellvars]])

 Here's the constructor in Python 3.0:

 class code(object)
  |  code(argcount, kwonlyargcount nlocals, stacksize, flags,
 codestring,
  |        constants, names, varnames, filename, name, firstlineno,
  |        lnotab[, freevars[, cellvars]])

 I defined Stub.stub like this:

 class Stub:
         def stub( self, prev, trans ):
                 return prev
 stub= Stub( )

 You need imports from the types module:

 from types import MethodType, FunctionType, CodeType

 And here is 'dupecode', which currently only replaces five of the old
 function's members with new ones:

 def dupecode( old, new ):

         newcode= CodeType( old.co_argcount, new.co_nlocals, old.co_stacksize,
                 old.co_flags,
                 new.co_code,
                 new.co_consts, new.co_names,
                 new.co_varnames, old.co_filename, old.co_name, 
 old.co_firstlineno,
                 old.co_lnotab )

         return newcode

Still don't really understand this so I'm going to admit defeat.
Thanks all for your advice... Very much appreciated!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Locking around

2008-08-06 Thread Nikolaus Rath
Carl Banks [EMAIL PROTECTED] writes:
 Freaky... I just posted nearly this exact solution.

 I have a couple comments.  First, the call to acquire should come
 before the try block.  If the acquire were to fail, you wouldn't want
 to release the lock on cleanup.

 Second, you need to change notify() to notifyAll(); notify alone won't
 cut it.  Consider what happens if you have two threads waiting for
 keys A and B respectively.  When the thread that has B is done, it
 releases B and calls notify, but notify happens to wake up the thread
 waiting on A.  Thus the thread waiting on B is starved.

You're right. Thanks for pointing it out.

Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

Find class of an instance?

2008-08-06 Thread Neal Becker
Sounds simple, but how, given an instance, do I find the class?

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


Re: Find class of an instance?

2008-08-06 Thread Heiko Wundram
Am Mittwoch, den 06.08.2008, 08:44 -0400 schrieb Neal Becker:
 Sounds simple, but how, given an instance, do I find the class?

inst.__class__

For example:

Python 2.5.2 (r252:60911, Aug  5 2008, 03:26:50)
[GCC 4.3.1] on linux2
Type help, copyright, credits or license for more information.
 x = hello
 x.__class__
type 'str'


--- Heiko.

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


Best practise implementation for equal by value objects

2008-08-06 Thread Slaunger
Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?

Example:

class Age:

def __init__(self, an_age):
self.age = an_age

def __eq__(self, obj):
self.age == obj.age

def __repr__(self):
return self.__class__.__name__ + \
   (%r) % self.age

age_ten = Age(10)
print repr(age_ten)
print eval(repr(age_ten))
print eval(repr(age_ten)).age

Running this gives

Age(10)
Age(10)
10

Exactly as I want to.

The problem arises when the Age class is iomported into another module
in another package as then there is a package prefix and the above
implementation of __repr__ does not work.

I have then experimented with doing somthing like

def __repr__(self):
return self.__module__ + '.' + self.__class__.__name__ +
(%r) % self.age

This seems to work when called from the outside, but not from the
inside of the module. That is, if I rerun the script above the the
module name prefixed to the representation I get the following error

Traceback (most recent call last):
  File valuetest.py, line 15, in module
print eval(repr(age_ten))
__main__.Age(10)
  File string, line 1, in module
NameError: name '__main__' is not defined

This is pretty annoying.

My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?

This is something I plan to reuse for many different Value classes, so
I would like to get it robust.

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


How to create python codecs?

2008-08-06 Thread yrogirg
Actually, I need utf-8 to utf-8 encoding which would change the text
to another keyboard layout (e.g. from english to russian ghbdtn -
привет) and would not affect other symbols.

I`m totally new to python and to more or less advanced programming. I
couldn`t find the answer to the question anywhere.

I`ve tried create simple utf to utf codec for some symbols but it
doesn`t work. Here it is.



import codecs

### Codec APIs

class Codec(codecs.Codec):

def encode(self,input,errors='strict'):
return codecs.charmap_encode(input,errors,encoding_table)

def decode(self,input,errors='strict'):
return codecs.charmap_decode(input,errors,decoding_table)

class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
return codecs.charmap_encode(input,self.errors,encoding_table)
[0]

class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
return codecs.charmap_decode(input,self.errors,decoding_table)
[0]

class StreamWriter(Codec,codecs.StreamWriter):
pass

class StreamReader(Codec,codecs.StreamReader):
pass

### encodings module API

def getregentry():
return codecs.CodecInfo(
name='rulayout',
encode=Codec().encode,
decode=Codec().decode,
incrementalencoder=IncrementalEncoder,
incrementaldecoder=IncrementalDecoder,
streamreader=StreamReader,
streamwriter=StreamWriter,
)


### Decoding Table

decoding_table = (
u'\u'   #  u'\u' - NULL
u'\u0001'   #  u'\u0001' - START OF HEADING
u'\u0002'   #  u'\u0002' - START OF TEXT
u'\u0003'   #  u'\u0003' - END OF TEXT
u'\u0004'   #  u'\u0004' - END OF TRANSMISSION
u'\u0005'   #  u'\u0005' - ENQUIRY
u'\u0006'   #  u'\u0006' - ACKNOWLEDGE
u'\u0007'   #  u'\u0007' - BELL
u'\u0008'   #  u'\u0008' - BACKSPACE
u'\u0009'   #  u'\u0009' - HORIZONTAL TABULATION
u'\u000A'   #  u'\u000A' - LINE FEED
u'\u000B'   #  u'\u000B' - VERTICAL TABULATION
u'\u000C'   #  u'\u000C' - FORM FEED
u'\u000D'   #  u'\u000D' - CARRIAGE RETURN
u'\u000E'   #  u'\u000E' - SHIFT OUT
u'\u000F'   #  u'\u000F' - SHIFT IN
u'\u0010'   #  u'\u0010' - DATA LINK ESCAPE
u'\u0011'   #  u'\u0011' - DEVICE CONTROL ONE
u'\u0012'   #  u'\u0012' - DEVICE CONTROL TWO
u'\u0013'   #  u'\u0013' - DEVICE CONTROL THREE
u'\u0014'   #  u'\u0014' - DEVICE CONTROL FOUR
u'\u0015'   #  u'\u0015' - NEGATIVE ACKNOWLEDGE
u'\u0016'   #  u'\u0016' - SYNCHRONOUS IDLE
u'\u0017'   #  u'\u0017' - END OF TRANSMISSION BLOCK
u'\u0018'   #  u'\u0018' - CANCEL
u'\u0019'   #  u'\u0019' - END OF MEDIUM
u'\u001A'   #  u'\u001A' - SUBSTITUTE
u'\u001B'   #  u'\u001B' - ESCAPE
u'\u001C'   #  u'\u001C' - FILE SEPARATOR
u'\u001D'   #  u'\u001D' - GROUP SEPARATOR
u'\u001E'   #  u'\u001E' - RECORD SEPARATOR
u'\u001F'   #  u'\u001F' - UNIT SEPARATOR
u'\u0020'   #  u'\u0020' - SPACE
u'\u0021'   #  u'\u0021' - EXCLAMATION MARK
u'\u0022'   #  u'\u0022' - QUOTATION MARK
u'\u0023'   #  u'\u0023' - NUMBER SIGN
u'\u0024'   #  u'\u0024' - DOLLAR SIGN
u'\u0025'   #  u'\u0025' - PERCENT SIGN
u'\u0026'   #  u'\u0026' - AMPERSAND
u'\u0027'   #  u'\u0027' - APOSTROPHE
u'\u0028'   #  u'\u0028' - LEFT PARENTHESIS
u'\u0029'   #  u'\u0029' - RIGHT PARENTHESIS
u'\u002A'   #  u'\u002A' - ASTERISK
u'\u002B'   #  u'\u002B' - PLUS SIGN
u'\u002C'   #  u'\u002C' - COMMA
u'\u002D'   #  u'\u002D' - HYPHEN-MINUS
u'\u002E'   #  u'\u002E' - FULL STOP
u'\u002F'   #  u'\u002F' - SOLIDUS
u'\u0030'   #  u'\u0030' - DIGIT ZERO
u'\u0031'   #  u'\u0031' - DIGIT ONE
u'\u0032'   #  u'\u0032' - DIGIT TWO
u'\u0033'   #  u'\u0033' - DIGIT THREE
u'\u0034'   #  u'\u0034' - DIGIT FOUR
u'\u0035'   #  u'\u0035' - DIGIT FIVE
u'\u0036'   #  u'\u0036' - DIGIT SIX
u'\u0037'   #  u'\u0037' - DIGIT SEVEN
u'\u0038'   #  u'\u0038' - DIGIT EIGHT
u'\u0039'   #  u'\u0039' - DIGIT NINE
u'\u003A'   #  u'\u003A' - COLON
u'\u003B'   #  u'\u003B' - SEMICOLON
u'\u003C'   #  u'\u003C' - LESS-THAN SIGN
u'\u003D'   #  u'\u003D' - EQUALS SIGN
u'\u003E'   #  u'\u003E' - GREATER-THAN SIGN
u'\u003F'   #  u'\u003F' - QUESTION MARK
u'\u0040'   #  u'\u0040' - COMMERCIAL AT
u'\u0041'   #  u'\u0041' - LATIN CAPITAL LETTER A
u'\u0042'   #  u'\u0042' - LATIN 

Re: Is there a faster way to do this?

2008-08-06 Thread Boris Borcic

Is your product ID always the 3rd and last item on the line ?
Else your output won't separate IDs.

And how does

output = open(output_file,'w')
for x in set(line.split(',')[2] for line in open(input_file)) :
output.write(x)
output.close()

behave ?


[EMAIL PROTECTED] wrote:

I have a csv file containing product information that is 700+ MB in
size. I'm trying to go through and pull out unique product ID's only
as there are a lot of multiples. My problem is that I am appending the
ProductID to an array and then searching through that array each time
to see if I've seen the product ID before. So each search takes longer
and longer. I let the script run for 2 hours before killing it and had
only run through less than 1/10 if the file.

Heres the code:
import string

def checkForProduct(product_id, product_list):
for product in product_list:
if product == product_id:
return 1
return 0


input_file=c:\\input.txt
output_file=c:\\output.txt
product_info = []
input_count = 0

input = open(input_file,r)
output = open(output_file, w)

for line in input:
break_down = line.split(,)
product_number = break_down[2]
input_count+=1
if input_count == 1:
product_info.append(product_number)
output.write(line)
output_count = 1
if not checkForProduct(product_number,product_info):
product_info.append(product_number)
output.write(line)
output_count+=1

output.close()
input.close()
print input_count
print output_count
--
http://mail.python.org/mailman/listinfo/python-list



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


Re: Locking around

2008-08-06 Thread MRAB
On Aug 6, 1:33 pm, Nikolaus Rath [EMAIL PROTECTED] wrote:
 Carl Banks [EMAIL PROTECTED] writes:
  Freaky... I just posted nearly this exact solution.

  I have a couple comments.  First, the call to acquire should come
  before the try block.  If the acquire were to fail, you wouldn't want
  to release the lock on cleanup.

  Second, you need to change notify() to notifyAll(); notify alone won't
  cut it.  Consider what happens if you have two threads waiting for
  keys A and B respectively.  When the thread that has B is done, it
  releases B and calls notify, but notify happens to wake up the thread
  waiting on A.  Thus the thread waiting on B is starved.

 You're right. Thanks for pointing it out.

There's also less chance of deadlock if the files are always locked in
the same order, ie if you sort the files by, say, name, don't lock a
file if one earlier in the sorted list is already locked.
--
http://mail.python.org/mailman/listinfo/python-list


More like a shell command.

2008-08-06 Thread Bill
Is there anyway I can extend python to accept a command
which looks more like shell syntax than a function call.

I want to be able to do this:

if blah :
MyCommand  Arg1  Arg2

as opposed to this:

if blah :
MyCommand(Arg1,Arg2)

or this:

if blah :
x(MyCommand  Arg1  Arg2)

Of source, I would like to do this by writing a module (or through
some other run-time hook) as opposed to editing the Python source
code.

Thanks in advance
(unless you are just a Python nut who is flaming to tell me that I
should not want this.  :-)  )

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


Re: Find class of an instance?

2008-08-06 Thread Hugo

Neal Becker schreef:

Sounds simple, but how, given an instance, do I find the class?


I always do that with .__class__, not sure whether it is the best way:

 class A:
...  pass
...
 a = A()
 a.__class__
class __main__.A at 0xb7f01fbc
 a.__class__ == A
True
--
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: matplotlib-0.98.3 - plotting for python

2008-08-06 Thread bearophileHUGS
jdh2358:
 delaunay triangularization
[and more amazing things]

I'm impressed, it's growing very well, congratulations, I use it now
and then. I know people in University that use Python only/mostly
because of matplotlib.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary to tree format (hopefully simple)

2008-08-06 Thread Adam Powell
Thanks very much for this, very concise!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Find class of an instance?

2008-08-06 Thread [EMAIL PROTECTED]
On 6 août, 15:52, Bruno Desthuilliers bruno.
[EMAIL PROTECTED] wrote:
 Heiko Wundram a écrit :

  Am Mittwoch, den 06.08.2008, 08:44 -0400 schrieb Neal Becker:
  Sounds simple, but how, given an instance, do I find the class?

  inst.__class__

 Works for new-style classes only. The generic way to go is to use
 type(obj).

 op
 Note that in both cases, what you'll get back is the class object, not
 the class name
 /op

My I should shut up and check my facts :( Please everybody ignore
this brain fart.


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


Has anyone used the Python-MMS libraries recently ? feedback / consultancy required

2008-08-06 Thread Ade Bamigboye
Hi 
 
We would like to talk with anyone who has recently used the Python-MMS
libraries with the aim of creating a prototype SMIL to MMS tool.
 

Regards Ade 
 
CareTeamR - monitoring, managing, supporting patients
 
Wireless Matters Limited
Tel  : +44 844 736 5330
Mobile : +44 7768 356150
Skype : ade-bamigboye
www.wireless-matters.com http://www.wireless-matters.com/ 
Registered in England at 39 Alma Road, St Albans AL1 3AT (Company No:
5379258)
 
--
http://mail.python.org/mailman/listinfo/python-list

Re: More like a shell command.

2008-08-06 Thread Mike Driscoll
On Aug 6, 9:38 am, Bill [EMAIL PROTECTED] wrote:
 Is there anyway I can extend python to accept a command
 which looks more like shell syntax than a function call.

 I want to be able to do this:

     if blah :
         MyCommand  Arg1  Arg2

 as opposed to this:

     if blah :
         MyCommand(Arg1,Arg2)

 or this:

     if blah :
         x(MyCommand  Arg1  Arg2)

 Of source, I would like to do this by writing a module (or through
 some other run-time hook) as opposed to editing the Python source
 code.

 Thanks in advance
 (unless you are just a Python nut who is flaming to tell me that I
 should not want this.  :-)  )

 Bill

I'm not aware of any way to do this without modifying the original
source in some fundamental way. You may be able to use some
metaprogramming techniques, like decorators, to achieve this, although
I'm not thinking of anything clever at the moment.

---
Mike Driscoll

Blog:   http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: More like a shell command.

2008-08-06 Thread Miki
Hello,

 Is there anyway I can extend python to accept a command
 which looks more like shell syntax than a function call.

 I want to be able to do this:

     if blah :
         MyCommand  Arg1  Arg2

 as opposed to this:

     if blah :
         MyCommand(Arg1,Arg2)

 or this:

     if blah :
         x(MyCommand  Arg1  Arg2)

 Of source, I would like to do this by writing a module (or through
 some other run-time hook) as opposed to editing the Python source
 code.
You might want to have a look at ipython, they do something like that
but for the command interpreter and not the compiler.

The other option will be to write a compiler from your syntax to valid
Python syntax. However this my not be simple since you need to know
the context. e.g. is f(a b c) calling f in the string a b c or
f(a, b, c)?

HTH,
--
Miki [EMAIL PROTECTED]
http://pythonwise.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: More like a shell command.

2008-08-06 Thread Richie Hindle
[Bill]
 Is there anyway I can extend python to accept a command
 which looks more like shell syntax than a function call.

 I want to be able to do this:

 if blah :
 MyCommand  Arg1  Arg2

As a general rule, if Python gives you a syntax error then you can't
achieve what you want without modifying Python's grammar (and turning
it into something that isn't Python any more).

-- 
Richie Hindle
[EMAIL PROTECTED]
Search your Visual Studio projects instantly with Entrian Source Search,
our powerful Source Code Search add-in: http://entrian.com/source-search
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psycho question

2008-08-06 Thread David C. Ullrich
In article [EMAIL PROTECTED],
 Erik Max Francis [EMAIL PROTECTED] wrote:

 David C. Ullrich wrote:
 
  Just heard about Psycho. I've often wondered why someone
  doesn't make something that does exactly what Psycho does - keen.
  
  Silly question: It's correct, is it not, that Psycho doesn't
  actually modify the Python installation, except by adding a
  module or two (so that code not using Psycho is absolutely
  unaffected)?
 
 That's correct.  Hi, David!

Thanks. If I can get it installed and it works as advertised
this means I can finally (eventually) finish the process of
dumping MS Windows: the only reason I need it right now is for
the small number of Delphi programs I have for which straight
Python is really not adequate. Been not looking forward to 
learning some C or Objective C (or whatever that Mac thing
is) - if I can just accelerate a few Python routines that'll
be great.

Tentatively a very happy camper. See ya.

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Re: More like a shell command.

2008-08-06 Thread Thor
Maybe this module would work fine:

http://docs.python.org/lib/module-cmd.html

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


Re: Locking around

2008-08-06 Thread Tobiah
On Mon, 04 Aug 2008 15:30:51 +0200, Nikolaus Rath wrote:

 Hello,
 
 I need to synchronize the access to a couple of hundred-thousand
 files[1]. It seems to me that creating one lock object for each of the
 files is a waste of resources, but I cannot use a global lock for all
 of them either (since the locked operations go over the network, this
 would make the whole application essentially single-threaded even
 though most operations act on different files).

Do you think you could use an SQL database on the network to
handle the locking?  I was thinking of a table with one row
per file.  If the lock field is clear, you could update with a unique
ID, and query back to make sure that it is still yours before
accessing the file.

Hey, maybe the files themselves should go into blobs.


** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex question

2008-08-06 Thread Tobiah
On Tue, 05 Aug 2008 15:55:46 +0100, Fred Mangusta wrote:

 Chris wrote:
 
 Doesn't work for his use case as he wants to keep periods marking the
 end of a sentence.

Doesn't it?  The period has to be surrounded by digits in the
example solution, so wouldn't periods followed by a space
(end of sentence) always make it through?



** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list


Re: enhancing decorator signatures

2008-08-06 Thread castironpi
On Aug 6, 7:16 am, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 Hi,

 I'm using Michele S's decorator-module to create decorators with matching
 signatures, for better error-catching.

 However, I now want to enrich the signature of a generic wrapper so that the
 new function will accept more parameters (keyword only). These additional
 parameters are consumed by the wrapper and not passed to the decorated
 function.

 So something like this would be cool:

 @enriched_decorator(bar)
 def some_decorator(f, **args, **kwargs):
     bar = kwargs.pop(bar)
     return f(**args, **kwargs)

 Anybody has done something like this?

 Diez

Diez,

notfound= object()
@enriched_decorator(bar)
def some_decorator(f, *args, **kwargs):
bar = kwargs.pop(bar,notfound)
return f(*args, **kwargs)

Then you would declare your function:

@some_decorator
def funA( x, y, z ):
  do( x, y )
  make( z )

And be able to call it:

funA( 0, 1, 2, bar= 'what' )

so some_decorator consumes 'bar' and preserves x, y, and z.  It's
working fine.  What did you want to ask about?

--Roggisch has plonked me in the past.  Can someone reply to this
message to the group so they'll see it?--
--
http://mail.python.org/mailman/listinfo/python-list


Re: More like a shell command.

2008-08-06 Thread castironpi
On Aug 6, 9:38 am, Bill [EMAIL PROTECTED] wrote:
 Is there anyway I can extend python to accept a command
 which looks more like shell syntax than a function call.

 I want to be able to do this:

     if blah :
         MyCommand  Arg1  Arg2

 as opposed to this:

     if blah :
         MyCommand(Arg1,Arg2)

 or this:

     if blah :
         x(MyCommand  Arg1  Arg2)

 Of source, I would like to do this by writing a module (or through
 some other run-time hook) as opposed to editing the Python source
 code.

 Thanks in advance
 (unless you are just a Python nut who is flaming to tell me that I
 should not want this.  :-)  )

 Bill

Bill,

You'll need to decide on a grammar you want to use beforehand.  What
do you do with:

 if f 0 and g

?

Does it translate to:

if f( 0 ) and g( )

or

if f( 0 and g )

?  If every line starts with its one and only function, and the
parameters exhaust the rest of the line, and you don't allow nested
expressions, you can run a preprocessor, then call Python.  (Those
three conditions are limiting.)  Here's the steps for each line.

1. strip leading tabs
2. compile( line, 'none', 'exec' ) to check for SyntaxError
3. if found:
3a. replace first space with '('
3b. replace remaining space with ','
3c. add trailing ')'
3d. replace leading tabs

Then just run:

python preprocessor.py -filename-
python -filename-.pppy

Have a look at code.py and codeop.py as well and the
InteractiveInterpreter class.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Limits of Metaprogramming

2008-08-06 Thread castironpi
On Aug 6, 7:24 am, Wilson [EMAIL PROTECTED] wrote:
 On Aug 4, 9:23 pm, castironpi [EMAIL PROTECTED] wrote:



  On Aug 4, 1:57 pm, Wilson [EMAIL PROTECTED] wrote:

   On Aug 4, 6:49 pm, castironpi [EMAIL PROTECTED] wrote:

Two, if all your methods will have uniform signatures and closures,
you can store class methods as only their co_code objects:

 C.g.im_func.func_code.co_code

'd\x00\x00S'

And fabricate them dynamically into full live types as needed.

   Thanks for your comments and advice. This second option intrigues me;
   could you elaborate further, I don't follow you...

   Thanks Paul

  Depending on the complexity of the functions, a code string could be
  all you need to store to determine (redetermine) a function's
  behavior.  For something moderately simple,

  def trans1( self, prev, trans ):
          if prev== 0 and trans== 'a':
                  return 1
          if prev== 1 and trans== 'b':
                  return 0
          return prev

  I found you need to store code.co_nlocals, code.co_code, and
  code.co_consts, to distinguish from a blank stub.  With extra
  variables, I needed code.co_names and code.co_varnames too.  To
  recreate a code object completely, you need 12 variables (14 to
  include closures), some of which are composite objects and would need
  to be pickled to be stored.

 Still don't really understand this so I'm going to admit defeat.
 Thanks all for your advice... Very much appreciated!

I was describing an alternative to storing functions in a way that
wasn't in serial in plain text.  It was off-topic from state-machine
transitions.

Can you start with this?

# state, input, next state
transitions= [
  ( 0, 'a', 1 ),
  ( 1, 'a', 2 ),
  ( 2, 'a', 0 ),
  ( 0, 'b', 0 ),
  ( 1, 'b', 0 ),
  ( 2, 'b', 2 )
]

What further?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psycho question

2008-08-06 Thread bearophileHUGS
David C. Ullrich:
 Thanks. If I can get it installed and it works as advertised
 this means I can finally (eventually) finish the process of
 dumping MS Windows: the only reason I need it right now is for
 the small number of Delphi programs I have for which straight
 Python is really not adequate. Been not looking forward to
 learning some C or Objective C (or whatever that Mac thing
 is) - if I can just accelerate a few Python routines that'll
 be great.

To have better performance with Psyco you need low-level style code,
generally not lazy, etc, and adopt some programming conventions, so
you may have to rewrite your routines for max speed.

If some of your routines are too much slow there are many ways in
Python to write faster modules, like Cython, Weave, Inline, Swig, SIP,
ShedSkin, etc. For bioinformatics purposes I have found that Pyd + D
language is good for me (I have tried Pyrex too few times, but I have
lost my patience trying to track down in a jungle of ugly auto-
generated C code where some reference count updates happen. Writing D
code is hugely faster/better for me. Even writing a C extension for
Python from scratch may be better for me because there aren't hidden
things happening everywhere. I presume other people don't share this
problems of mine because there are lot of people using Cython now).

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Using an DTD not specified in XML file for validation

2008-08-06 Thread Brian Quinlan

Hey,

I'm trying to figure out how I can validate an XML file using a DTD that 
isn't specified in the XML file.


My code so far is:

from xml import sax
from xml.sax import sax2exts

parser = sax2exts.XMLValParserFactory.make_parser()

parser.setContentHandler(handler)
parser.setErrorHandler(handler)

parser.parse(xml_file)

And this works fine if the DTD is specified in the XML file i.e errors 
are generated for non-compliant entities. But I would like to force the 
file to be valid according to one other DTD file that is not referenced 
in the XML file.


Anyone know how to do this?

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


Monitor and compare two log files in real time

2008-08-06 Thread m
I have a script I would like to write but I am not sure of where to
start / approach. Perhaps someone could help direct me in the right
direction. Any advice is appreciated.

I would like to write a python script that monitors two log files.
If a certain string, lets say string1 shows up in logfile-A, I want to
check if that same string shows up in log file-B within 8 minutes. If
it does not show up within 8 minutes, send an email ( using sendmail
or postfix).

Do you have any suggestions?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Any tips on Python web development on Mac OS

2008-08-06 Thread Bruno Desthuilliers

Tim Greening-Jackson a écrit :

Bruno Desthuilliers wrote:

Tim Greening-Jackson a écrit :
(snip)



Depends on what your site is doing.


There are all *sorts* of things I would like it to do, but am not 
dogmatic about any of them. For example, having various people being 
able to login to it securely to shuttle files between ourselves would be 
useful. As would webmail access. And various robot functionality...


Ok, so this is more a collection of web applications than a 
content-oriented site. FWIW, there are existing open-source solutions 
(not necessarily written in Python...) for at least the first two features.


The exercise is more to see what Python can do to help me develop 
websites and get used to some sort of proper development framework,


Ok. Then to answer one of your initial questions, yes, having a look at 
frameworks like Django, Turbogears or Pylons might be a good idea. But 
note that these are frameworks, not applications like iWeb.


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


Re: looking for IDE advice or workflow tips

2008-08-06 Thread Bruno Desthuilliers

[EMAIL PROTECTED] a écrit :

I'm a novice developer at best and often work with the R statistical
programming language. I use an editor called TINN-R which allows me to
write a script, then highlight a few lines and send them to the
interpreter. I am using pythonwin and it lacks this funtionality (that
I can tell) and when I copy and paste lines into the interpreter only
the first line is evaluated and the rest appears as returned text.

Is there an editor that allows me to send a few lines out of many
lines of code at a time?


emacs + python-mode.


or

How does one check small blocks of code without typing them each time,
running an entire script (with other code) or creating a small script
for every code block?



For example say lines 1-100 work fine and now I'm working on lines
101-105. Should I create a small script with just those lines?


You may want to learn more about functions.

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


RE: Monitor and compare two log files in real time

2008-08-06 Thread Reedick, Andrew


 -Original Message-
 From: [EMAIL PROTECTED] [mailto:python-
 [EMAIL PROTECTED] On Behalf Of m
 Sent: Wednesday, August 06, 2008 1:25 PM
 To: python-list@python.org
 Subject: Monitor and compare two log files in real time
 
 I have a script I would like to write but I am not sure of where to
 start / approach. Perhaps someone could help direct me in the right
 direction. Any advice is appreciated.
 
 I would like to write a python script that monitors two log files.
 If a certain string, lets say string1 shows up in logfile-A, I want to
 check if that same string shows up in log file-B within 8 minutes. If
 it does not show up within 8 minutes, send an email ( using sendmail
 or postfix).
 
 Do you have any suggestions?
 --

Google on python tail to get a python implementation of the unix tail
command.  The rest should be easy. 

http://code.activestate.com/recipes/157035/



*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


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


Re: Monitor and compare two log files in real time

2008-08-06 Thread Shawn Milochik
Can you be more specific? That will also help you write your
requirements, which will lead to your pseudo code and then your code.

Do you want to search for a a pre-defined string (or set of strings),
or just look for anything matching a pattern to appear in the first
file? Related question: Can that string appear anywhere in the second
file, or does it have to appear in a certain pattern (at the beginning
of a line, followed by a colon or the word error or something?

Do the log entries have timestamps, or will you be capturing the time
your script found the string in file 1? What will you do if the script
must be restarted and forgets what it found in the past eight
minutes?

If you're feeling ambitious, write some code. Figure out how to:

1. Read a file.

2. Locate the value(s) you are looking for and retain them somehow.

Once you can do those two things, you can easily read the second file
and seek the values you already found. A few lines to handle the
eight-minute rule, and you're almost done.

Post back when you have some code written.

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


Re: Locking around

2008-08-06 Thread Nikolaus Rath
Tobiah [EMAIL PROTECTED] writes:
 On Mon, 04 Aug 2008 15:30:51 +0200, Nikolaus Rath wrote:

 Hello,
 
 I need to synchronize the access to a couple of hundred-thousand
 files[1]. It seems to me that creating one lock object for each of the
 files is a waste of resources, but I cannot use a global lock for all
 of them either (since the locked operations go over the network, this
 would make the whole application essentially single-threaded even
 though most operations act on different files).

 Do you think you could use an SQL database on the network to
 handle the locking?

Yeah, I could. It wouldn't even have to be over the network (I'm
synchronizing access from within the same program). But I think that
is even more resource-wasteful than my original idea.

 Hey, maybe the files themselves should go into blobs.

Nope, not possible.  They're on Amazon S3.

Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

Re: Find class of an instance?

2008-08-06 Thread Nikolaus Rath
Neal Becker [EMAIL PROTECTED] writes:
 Sounds simple, but how, given an instance, do I find the class?


It does not only sound simple. When 'inst' is your instance, then

  inst.__class__

or

  type(inst) 

is the class.

Best,


   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

virtual IPs

2008-08-06 Thread leo davis
Good Day!
I have set up virtual IPs on my Ubuntu client machine  assigned IPs 
192.168.12.3 - eth0
192.168.12.4 - eth0:1
192.168.12.5 - eth0:2
192.168.12.6 - eth0:3
I have written python code to send multiple HTTP requests to my web server to 
load test it.When I check the logs on the apache server ,i find that the HTTP 
'GET' request is sent from the client IP 192.168.12.3.Is there any way that I 
can send the http requests from the other IPs on the virtual interfaces?
  [code]def send(self, url):
    conn = httplib.HTTPConnection(url)
    try:
    conn.request('GET', url)
    body = conn.getresponse().read()[/code]    
This code works fine,but since the apache default configuration doesnt allow a 
large number of simultaneous requests from single host,the code fails to load 
the server to my expectation.(Ofcourse I can change the default 
configuration,but wish to try something else)Hence was wondering whether I 
could  possibly send 'get' requests randomly from the virtual IPs? I could then 
have a list -.txt file of virtual IPs,the python script could fetch it and send 
the HTTP requets (in a loop)..Appreciate if anyone could suggest a way to 
do this.I am new to python...thanks in advance

CHeers
David


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

Calculate sha1 hash of a binary file

2008-08-06 Thread LaundroMat
Hi -

I'm trying to calculate unique hash values for binary files,
independent of their location and filename, and I was wondering
whether I'm going in the right direction.

Basically, the hash values are calculated thusly:

f = open('binaryfile.bin')
import hashlib
h = hashlib.sha1()
h.update(f.read())
hash = h.hexdigest()
f.close()

A quick try-out shows that effectively, after renaming a file, its
hash remains the same as it was before.

I have my doubts however as to the usefulness of this. As f.read()
does not seem to read until the end of the file (for a 3.3MB file only
a string of 639 bytes is being returned, perhaps a 00-byte counts as
EOF?), is there a high danger for collusion?

Are there better ways of calculating hash values of binary files?

Thanks in advance,

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


Re: Best practise implementation for equal by value objects

2008-08-06 Thread Terry Reedy



Slaunger wrote:

Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?


The CPython implementation gives up on that goal and simply prints 
modname.classname object at address for at least two reasons ;-).


1. In general, it require fairly sophisticated analysis of __init__ to 
decide what representation of what attributes to include and decide if 
the goal is even possible.  If an attribute is an instance of a user 
class, then *its* __init__ needs to be analyzed.  If an attribute is a 
module, class, or function, there is no generic evaluable representation.


2. Whether eval(repr(x)) even works (returns an answer) depends on 
whether the name bindings in the globals and locals passed to eval 
(which by default are the globals and locals of the context of the eval 
call) match the names used in the repr.  You discovered that to a first 
approximation, this depends on whether the call to repr comes from 
within or without the module containing the class definition.  But the 
situation is far worse.  Consider 'import somemod as m'.  Even if you 
were able to introspect the call and determine that it did not come from 
somemod**, prepending 'somemod.' to the repr *still* would not work. 
Or, the call to repr could come from one context, the result saved and 
passed to another context with different name bindings, and the eval 
call made there.  So an repr that can be eval'ed in any context is hopeless.


If this is a practical rather than theoretical question, then use your 
first repr version that uses the classes definition name and only eval 
the result in a context that has that name bound to the class object.


from mymod import Age
#or
import mymod
Age = mymod.Age

#in either case
eval(repr(Age(10))) == Age(10)


class Age:

def __init__(self, an_age):
self.age = an_age

def __eq__(self, obj):
self.age == obj.age

def __repr__(self):
return self.__class__.__name__ + \
   (%r) % self.age

**
While such introspection is not part of the language, I believe one 
could do it in CPython, but I forgot the details.  There have been 
threads like 'How do I determine the caller function' with answers to 
that question, and I presume the module of the caller is available also.


Terry Jan Reedy

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


Re: SMTP via GMAIL

2008-08-06 Thread mmm
On Aug 5, 12:18 am, Tim Roberts [EMAIL PROTECTED] wrote:

 But when using smtp.gmail.com as the server I learned that any
 @gmail.com address in the  Cc: text block would
 receive mail even if I changed the code to have the RECEIVERS list to
 ignore the CC addresses or not include the gmail address in the CC
 list as below

 Interesting.  If true, that is incorrect behavior.

I ran some more tests and now I am pretty sure

  session.sendmail(SENDER, RECEIVERS, BODY)

is only sending to the RECEIVERS list, ignoring the Cc: field in the
body (as it should)

What fooled me is that I was using my own gmail account (i.e.,
[EMAIL PROTECTED]) as a Cc: field and not putting it in the RECEIVERS
list.  It seems Gmail creates two links (or copies?) to the message:
(1) as it is stored in the SENT box (as it should since the message
was sent by my gmail account) and (2) another in my INBOX because the
mail reading software reads the Cc: field.

Other smtp servers such as comcast do not create the stored SENT mail
and hence behave different in terms of how they treat Cc: fields of
the same account ([EMAIL PROTECTED] in this case).

Most important, using another gmail account (not [EMAIL PROTECTED]) as a
Cc: field does not create another sent message (outside of what is in
the RECEIVERS field).

Sorry for confusion, and I do appreciate the tips as I now see how
almost anything To:, Cc:, Bcc: combination can be handled by a proper
RECEIVERS list.


Below is python code that can be used by anyone that wants to test
what I did  (just fill in the SMTPuser and password variables) and
then check you gmail inbox


import sys, os, glob, datetime, time
import smtplib
## Parameters for SMTP session
port=587
SMTPserver=  'smtp.gmail.com'
SMTPuser= '[EMAIL PROTECTED]'
pw= 'fill in here'
SENDER= SMTPuser

## Message details
FROM=  SENDER
TO= '[EMAIL PROTECTED]'
CC=FROM
##RECEIVERS= (TO, CC)  ##proper way to send to both TO and CC
RECEIVERS= (TO,)  ## ignore the CC address

subject= 'Test 1a'
message='*** Email test  *** '

print 'Starting SMTP mail session on %s as  %s ' %
(SMTPserver,SMTPuser)
session = smtplib.SMTP(SMTPserver,port)
session.set_debuglevel(0)  # set debug level to 1 to see details
session.ehlo(SMTPuser)  # say hello
session.starttls()  # TLS needed
session.ehlo(SMTPuser)  # say hello again, not sure why
session.login(SMTPuser, pw)

##Create HEADER + MESSAGE
HEADER= 'From: %s\r\n' % FROM
HEADER= HEADER + 'To: %s\r\n' % TO
HEADER= HEADER + 'Cc: %s\r\n' % CC
HEADER= HEADER + 'Subject: %s\r\n' % subject
BODY= HEADER + '\r\n' + message
print BODY

SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)  ## send email

session.close()







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


Run program from within Python

2008-08-06 Thread frankrentef
Greetings all...

Newbie to Python... need help with opening a file from within
Python... see the following code.


import popen2
stdout, stdin = popen2.popen2('c:\test\OpenProgram.exe 1 1')
keygen = stdout.read()
print The keygen value is: %s % keygen


from the command line if I execute OpenProgram.exe 1 1 a number is
returned.  (1 1 are required to return the value needed.) Ultimately
I want to take that number and apply it to another script, but the
program is not running.

Suggestions?


NEWBIE to Python..
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best practise implementation for equal by value objects

2008-08-06 Thread John Krukoff

On Wed, 2008-08-06 at 05:50 -0700, Slaunger wrote:
 Hi,
 
 I am new here and relatively new to Python, so be gentle:
 
 Is there a recommended generic implementation of __repr__ for objects
 equal by value to assure that eval(repr(x)) == x independet of which
 module the call is made from?
 
 Example:
 
 class Age:
 
 def __init__(self, an_age):
 self.age = an_age
 
 def __eq__(self, obj):
 self.age == obj.age
 
 def __repr__(self):
 return self.__class__.__name__ + \
(%r) % self.age
 
 age_ten = Age(10)
 print repr(age_ten)
 print eval(repr(age_ten))
 print eval(repr(age_ten)).age
 
 Running this gives
 
 Age(10)
 Age(10)
 10
 
 Exactly as I want to.
 
 The problem arises when the Age class is iomported into another module
 in another package as then there is a package prefix and the above
 implementation of __repr__ does not work.
 
 I have then experimented with doing somthing like
 
 def __repr__(self):
 return self.__module__ + '.' + self.__class__.__name__ +
 (%r) % self.age
 
 This seems to work when called from the outside, but not from the
 inside of the module. That is, if I rerun the script above the the
 module name prefixed to the representation I get the following error
 
 Traceback (most recent call last):
   File valuetest.py, line 15, in module
 print eval(repr(age_ten))
 __main__.Age(10)
   File string, line 1, in module
 NameError: name '__main__' is not defined
 
 This is pretty annoying.
 
 My question is: Is there a robust generic type of implementation of
 __repr__ which I can use instead?
 
 This is something I plan to reuse for many different Value classes, so
 I would like to get it robust.
 
 Thanks,
 Slaunger
 --
 http://mail.python.org/mailman/listinfo/python-list

Are you really sure this is what you want to do, and that a less tricky
serialization format such as that provided by the pickle module wouldn't
work for you?

-- 
John Krukoff [EMAIL PROTECTED]
Land Title Guarantee Company

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


Re: Calculate sha1 hash of a binary file

2008-08-06 Thread Tim Golden

LaundroMat wrote:

Hi -

I'm trying to calculate unique hash values for binary files,
independent of their location and filename, and I was wondering
whether I'm going in the right direction.

Basically, the hash values are calculated thusly:

f = open('binaryfile.bin')
import hashlib
h = hashlib.sha1()
h.update(f.read())
hash = h.hexdigest()
f.close()

A quick try-out shows that effectively, after renaming a file, its
hash remains the same as it was before.

I have my doubts however as to the usefulness of this. As f.read()
does not seem to read until the end of the file (for a 3.3MB file only
a string of 639 bytes is being returned, perhaps a 00-byte counts as
EOF?), is there a high danger for collusion?


Guess: you're running on Windows?

You need to open binary files by using open (filename, rb)
to indicate that Windows shouldn't treat certain characters --
specifically character 26 -- as special.

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


Parsing of a file

2008-08-06 Thread Tommy Grav

I have a file with the format

Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames  
5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames  
5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames  
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames  
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames  
5 Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames  
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames  
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames  
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames  
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames  
5 Set 10


I would like to parse this file by extracting the field id, ra, dec  
and mjd for each line. It is
not, however, certain that the width of each value of the field id,  
ra, dec or mjd is the same
in each line. Is there a way to do this such that even if there was a  
line where Ra=** and

MJD= was swapped it would be parsed correctly?

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


Re: Psycho question

2008-08-06 Thread David C. Ullrich
In article 
[EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 David C. Ullrich:
  Thanks. If I can get it installed and it works as advertised
  this means I can finally (eventually) finish the process of
  dumping MS Windows: the only reason I need it right now is for
  the small number of Delphi programs I have for which straight
  Python is really not adequate. Been not looking forward to
  learning some C or Objective C (or whatever that Mac thing
  is) - if I can just accelerate a few Python routines that'll
  be great.
 
 To have better performance with Psyco you need low-level style code,
 generally not lazy, etc, and adopt some programming conventions, so
 you may have to rewrite your routines for max speed.

Thanks. I would have guessed that I'd want low-level style code;
that's the sort of thing I have in mind. In fact the only thing
that seems likely to come up right now is looping through an
array of bytes, modifying them. The plan is to use the array
module first to convert a string or a list to an array, outside
the accelerated part, then maybe do something like

for j in range(len(bytes)/3):
  g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
  bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g

then convert back to a list or string or whatever outside
the accelerated function.

Surely something like _that_ is exactly what Psyco is going
to do well with, yes? (Ok, we're talking about image processing,
in cases where I can't figure out how to get PIL to do whatever
directly. So sometimes there will be double loops

for row in range(width):
  for col in range(height):
do_something[row*width + col]

but at least for the things I can think of right now it
shouldn't get much worse than that.)

The things you mention below sound very interesting - I'm
going to try Psyco first because unless I'm missing something
I won't have to learn how to use it. Someday when it turns out
to be not good enough I'll be in touch...

 If some of your routines are too much slow there are many ways in
 Python to write faster modules, like Cython, Weave, Inline, Swig, SIP,
 ShedSkin, etc. For bioinformatics purposes I have found that Pyd + D
 language is good for me (I have tried Pyrex too few times, but I have
 lost my patience trying to track down in a jungle of ugly auto-
 generated C code where some reference count updates happen. Writing D
 code is hugely faster/better for me. Even writing a C extension for
 Python from scratch may be better for me because there aren't hidden
 things happening everywhere. I presume other people don't share this
 problems of mine because there are lot of people using Cython now).
 
 Bye,
 bearophile

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Books to begin learning Python

2008-08-06 Thread Edward Cormier
Which computer books are the best to begin learning Python 2.5 with? 
I've heard that Learning Python 3rd Edition is a good choice - can 
anyone give any more advice on this?


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


Re: Run program from within Python

2008-08-06 Thread Mike Driscoll
On Aug 6, 2:42 pm, frankrentef [EMAIL PROTECTED] wrote:
 Greetings all...

 Newbie to Python... need help with opening a file from within
 Python... see the following code.

 import popen2
 stdout, stdin = popen2.popen2('c:\test\OpenProgram.exe 1 1')
 keygen = stdout.read()
 print The keygen value is: %s % keygen

 from the command line if I execute OpenProgram.exe 1 1 a number is
 returned.  (1 1 are required to return the value needed.) Ultimately
 I want to take that number and apply it to another script, but the
 program is not running.

 Suggestions?

 NEWBIE to Python..

If you're using Python 2.4+, popen2 is deprecated. I recommend reading
up on the subprocess module instead. Here's a couple links:

http://blog.doughellmann.com/2007/07/pymotw-subprocess.html
http://docs.python.org/lib/module-subprocess.html

The first one also explains how to communicate with a process you
opened.

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


Re: Parsing of a file

2008-08-06 Thread Mike Driscoll
On Aug 6, 1:55 pm, Tommy Grav [EMAIL PROTECTED] wrote:
 I have a file with the format

 Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames  
 5 Set 1
 Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames  
 5 Set 2
 Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames  
 5 Set 3
 Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames  
 5 Set 4
 Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames  
 5 Set 5
 Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames  
 5 Set 6
 Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames  
 5 Set 7
 Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames  
 5 Set 8
 Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames  
 5 Set 9
 Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames  
 5 Set 10

 I would like to parse this file by extracting the field id, ra, dec  
 and mjd for each line. It is
 not, however, certain that the width of each value of the field id,  
 ra, dec or mjd is the same
 in each line. Is there a way to do this such that even if there was a  
 line where Ra=** and
 MJD= was swapped it would be parsed correctly?

 Cheers
    Tommy

I'm sure Python can handle this. Try the PyParsing module or learn
Python regular expression syntax.

http://pyparsing.wikispaces.com/

You could probably do it very crudely by just iterating over each line
and then using the string's find() method.

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


Re: Books to begin learning Python

2008-08-06 Thread Mike Driscoll
On Aug 6, 2:56 pm, Edward Cormier [EMAIL PROTECTED] wrote:
 Which computer books are the best to begin learning Python 2.5 with?
 I've heard that Learning Python 3rd Edition is a good choice - can
 anyone give any more advice on this?

 Thanks.

There's lots of good books to read, including a few online ones. A lot
of people like Dive Into Python (http://diveintopython.org/). If you
want LOTS of information and some good code examples, Lutz's
Programming Python 3rd Ed is great. Chun (Core Python Programming)
has a book that's almost as large, but it's more text than examples.

If you want just short snippets of code to learn from, try the Python
Cookbook series or just go to the site those books are based on:
http://code.activestate.com/recipes/langs/python/

Python Power! and Beginning Python are good too with the latter
having some interesting projects at the end. There are a lot of other
topical Python books on XML parsing, web programming, Win32, Tkinter,
wxPython and even SqlAlchemy!

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


Re: Parsing of a file

2008-08-06 Thread Shawn Milochik

 I would like to parse this file by extracting the field id, ra, dec and mjd
 for each line. It is
 not, however, certain that the width of each value of the field id, ra, dec
 or mjd is the same
 in each line. Is there a way to do this such that even if there was a line

Regular expressions will do the trick nicely.

 where Ra=** and
 MJD= was swapped it would be parsed correctly?


Yes, but you'll probably have to look at each line three times or
split it into a list and check the elements.


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



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


Re: Run program from within Python

2008-08-06 Thread giltay
On Aug 6, 3:42 pm, frankrentef [EMAIL PROTECTED] wrote:
 stdout, stdin = popen2.popen2('c:\test\OpenProgram.exe 1 1')

What Mike said about subprocess.

Also, in regular Python strings, \t means a tab character.  You need
to replace \ with \\ in the programme path ('c:\\test\\OpenProgram.exe
1 1') or use a raw string (r'c:\test\OpenProgram.exe 1 1').  (The r
informs the Python parser that backslashes are to be used veratim, not
as special code.  If you're using Windows, raw strings make for fewer
headaches when dealing with file paths.)

Geoff G-T

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


Re: Run program from within Python

2008-08-06 Thread frankrentef
THNX for the links... lotta reading for the newbie!


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


Re: Psycho question

2008-08-06 Thread David C. Ullrich
In article [EMAIL PROTECTED],
 David C. Ullrich [EMAIL PROTECTED] wrote:

 In article 
 [EMAIL PROTECTED],
  [EMAIL PROTECTED] wrote:
 
  David C. Ullrich:
   Thanks. If I can get it installed and it works as advertised
   this means I can finally (eventually) finish the process of
   dumping MS Windows: the only reason I need it right now is for
   the small number of Delphi programs I have for which straight
   Python is really not adequate. Been not looking forward to
   learning some C or Objective C (or whatever that Mac thing
   is) - if I can just accelerate a few Python routines that'll
   be great.
  
  To have better performance with Psyco you need low-level style code,
  generally not lazy, etc, and adopt some programming conventions, so
  you may have to rewrite your routines for max speed.
 
 Thanks. I would have guessed that I'd want low-level style code;
 that's the sort of thing I have in mind. In fact the only thing
 that seems likely to come up right now is looping through an
 array of bytes, modifying them. The plan is to use the array
 module first to convert a string or a list to an array, outside
 the accelerated part, then maybe do something like
 
 for j in range(len(bytes)/3):
   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
 
 then convert back to a list or string or whatever outside
 the accelerated function.
 
 Surely something like _that_ is exactly what Psyco is going
 to do well with, yes? 

teehee. Downloaded Psyco. The install actually worked.
Tried exactly what's above with a list of 3 million ints.
Didn't time it carefully, seemed to take about two seconds.
Ran it again, in case the second run would be faster for some reason.
Second was about the same.

Said import psyco, etc. Ran the routine again, it returned
in _no_ time, perceptually.

This is so cool. Gonna find out whether a decorator that
returns the accelerated function works, just for the fun
of deciding what the name should be: @cool? @wheee?
@wow? @dontblinkyoullmissit?

(Ok, we're talking about image processing,
 in cases where I can't figure out how to get PIL to do whatever
 directly. So sometimes there will be double loops
 
 for row in range(width):
   for col in range(height):
 do_something[row*width + col]
 
 but at least for the things I can think of right now it
 shouldn't get much worse than that.)
 
 The things you mention below sound very interesting - I'm
 going to try Psyco first because unless I'm missing something
 I won't have to learn how to use it. Someday when it turns out
 to be not good enough I'll be in touch...
 
  If some of your routines are too much slow there are many ways in
  Python to write faster modules, like Cython, Weave, Inline, Swig, SIP,
  ShedSkin, etc. For bioinformatics purposes I have found that Pyd + D
  language is good for me (I have tried Pyrex too few times, but I have
  lost my patience trying to track down in a jungle of ugly auto-
  generated C code where some reference count updates happen. Writing D
  code is hugely faster/better for me. Even writing a C extension for
  Python from scratch may be better for me because there aren't hidden
  things happening everywhere. I presume other people don't share this
  problems of mine because there are lot of people using Cython now).
  
  Bye,
  bearophile

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing of a file

2008-08-06 Thread Stefan Behnel
Shawn Milochik wrote:
 I would like to parse this file by extracting the field id, ra, dec and mjd
 for each line. It is
 not, however, certain that the width of each value of the field id, ra, dec
 or mjd is the same
 in each line. Is there a way to do this such that even if there was a line
 
 Regular expressions will do the trick nicely.
 
 where Ra=** and
 MJD= was swapped it would be parsed correctly?

 
 Yes, but you'll probably have to look at each line three times or
 split it into a list and check the elements.

You can use named groups in a single regular expression.

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


Re: Psycho question

2008-08-06 Thread Erik Max Francis

David C. Ullrich wrote:


Thanks. I would have guessed that I'd want low-level style code;
that's the sort of thing I have in mind. In fact the only thing
that seems likely to come up right now is looping through an
array of bytes, modifying them. The plan is to use the array
module first to convert a string or a list to an array, outside
the accelerated part, then maybe do something like

for j in range(len(bytes)/3):
  g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
  bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g


If len(bytes) is large, you might want to use `xrange`, too.  `range` 
creates a list which is not really what you need.


--
Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM, Y!M erikmaxfrancis
  You and I / We've seen it all / Chasing our hearts' desire
   -- The Russian and Florence, _Chess_
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing of a file

2008-08-06 Thread John Machin
On Aug 7, 6:02 am, Mike Driscoll [EMAIL PROTECTED] wrote:
 On Aug 6, 1:55 pm, Tommy Grav [EMAIL PROTECTED] wrote:



  I have a file with the format

  Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
  5 Set 1
  Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
  5 Set 2
  Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
  5 Set 3
  Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
  5 Set 4
  Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
  5 Set 5
  Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
  5 Set 6
  Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
  5 Set 7
  Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
  5 Set 8
  Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
  5 Set 9
  Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
  5 Set 10

  I would like to parse this file by extracting the field id, ra, dec
  and mjd for each line. It is
  not, however, certain that the width of each value of the field id,
  ra, dec or mjd is the same
  in each line. Is there a way to do this such that even if there was a
  line where Ra=** and
  MJD= was swapped it would be parsed correctly?

  Cheers
 Tommy

 I'm sure Python can handle this. Try the PyParsing module or learn
 Python regular expression syntax.

 http://pyparsing.wikispaces.com/

 You could probably do it very crudely by just iterating over each line
 and then using the string's find() method.


Perhaps you and the OP could spend some time becoming familiar with
built-in functions and str methods. In particular, str.split is your
friend:

C:\junktype tommy_grav.py
# Look, Ma, no imports!

guff = \
Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
5 Set 1
Field f31448: MJD=53370.06811620123 Dec=+79:39:43.9 Ra=20:24:58.13
Frames 5 Set
2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
5 Set 5

Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
5 Set 10



is_angle = {
'ra': True,
'dec': True,
'mjd': False,
}

def convert_angle(text):
deg, min, sec = map(float, text.split(':'))
return (sec / 60. + min) / 60. + deg

def parse_line(line):
t = line.split()
assert t[0].lower() == 'field'
assert t[1].startswith('f')
assert t[1].endswith(':')
field_id = t[1].rstrip(':')
rdict = {}
for f in t[2:]:
parts = f.split('=')
if len(parts) == 2:
key = parts[0].lower()
value = parts[1]
assert key not in rdict
if is_angle[key]:
rvalue = convert_angle(value)
else:
rvalue = float(value)
rdict[key] = rvalue
return field_id, rdict['ra'], rdict['dec'], rdict['mjd']

for line in guff.splitlines():
line = line.strip()
if not line:
continue
field_id, ra, dec, mjd = parse_line(line)
print field_id, ra, dec, mjd


C:\junktommy_grav.py
f29227 20.396261 67.5 53370.0679769
f31448 20.416147 79.662194 53370.0681162
f31226 20.412639 78.445889 53370.0682386
f31004 20.418133 77.229694 53370.0683602
f30782 20.431094 76.0135 53370.0684821
f30560 20.450506 74.797306 53370.068604
f30338 20.4756527778 73.58 53370.0687262
f30116 20.506028 72.364889 53370.0688489
f29894 20.541261 71.148611 53370.0689707
f29672 20.5810805556 69.932389 53370.0690935

Cheers,
John

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


Re: Parsing of a file

2008-08-06 Thread bearophileHUGS
Using something like PyParsing is probably better, but if you don't
want to use it you may use something like this:

raw_data = 
Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
5 Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
5 Set 10

# from each line extract the fields: id, ra, dec, mjd
# even if they are swapped

data = []
for line in raw_data.lower().splitlines():
if line.startswith(field):
parts = line.split()
record = {id: int(parts[1][1:-1])}
for part in parts[2:]:
if = in part:
title, field = part.split(=)
record[title] = field
data.append(record)
print data

-

Stefan Behnel:
You can use named groups in a single regular expression.

Can you show how to use them in this situation when fields can be
swapped?

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psycho question

2008-08-06 Thread bearophileHUGS
Erik Max Francis:
 If len(bytes) is large, you might want to use `xrange`, too.  `range`
 creates a list which is not really what you need.

That's right for Python, but Psyco uses normal loops in both cases,
you can time this code in the two situations:

def foo1(n):
count = 0
for i in range(n):
count += 1
print count

def foo2(n):
count = 0
for i in xrange(n):
count += 1
print count

import psyco; psyco.full()
N = 1
#foo1(N)
foo2(N)

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing of a file

2008-08-06 Thread John Machin
On Aug 7, 7:06 am, John Machin [EMAIL PROTECTED] wrote:
 On Aug 7, 6:02 am, Mike Driscoll [EMAIL PROTECTED] wrote:



  On Aug 6, 1:55 pm, Tommy Grav [EMAIL PROTECTED] wrote:

   I have a file with the format

   Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
   5 Set 1
   Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
   5 Set 2
   Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
   5 Set 3
   Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
   5 Set 4
   Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
   5 Set 5
   Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
   5 Set 6
   Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
   5 Set 7
   Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
   5 Set 8
   Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
   5 Set 9
   Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
   5 Set 10

   I would like to parse this file by extracting the field id, ra, dec
   and mjd for each line. It is
   not, however, certain that the width of each value of the field id,
   ra, dec or mjd is the same
   in each line. Is there a way to do this such that even if there was a
   line where Ra=** and
   MJD= was swapped it would be parsed correctly?

   Cheers
  Tommy

  I'm sure Python can handle this. Try the PyParsing module or learn
  Python regular expression syntax.

 http://pyparsing.wikispaces.com/

  You could probably do it very crudely by just iterating over each line
  and then using the string's find() method.

 Perhaps you and the OP could spend some time becoming familiar with
 built-in functions and str methods. In particular, str.split is your
 friend:

 C:\junktype tommy_grav.py
 # Look, Ma, no imports!

 guff = \
 Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
 5 Set 1
 Field f31448: MJD=53370.06811620123 Dec=+79:39:43.9 Ra=20:24:58.13
 Frames 5 Set
 2
 Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
 5 Set 3
 Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
 5 Set 4
 Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
 5 Set 5

 Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
 5 Set 6
 Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
 5 Set 7
 Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
 5 Set 8
 Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
 5 Set 9
 Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
 5 Set 10

 

 is_angle = {
 'ra': True,
 'dec': True,
 'mjd': False,
 }

 def convert_angle(text):
 deg, min, sec = map(float, text.split(':'))
 return (sec / 60. + min) / 60. + deg

 def parse_line(line):
 t = line.split()
 assert t[0].lower() == 'field'
 assert t[1].startswith('f')
 assert t[1].endswith(':')
 field_id = t[1].rstrip(':')
 rdict = {}
 for f in t[2:]:
 parts = f.split('=')
 if len(parts) == 2:
 key = parts[0].lower()
 value = parts[1]
 assert key not in rdict
 if is_angle[key]:
 rvalue = convert_angle(value)
 else:
 rvalue = float(value)
 rdict[key] = rvalue
 return field_id, rdict['ra'], rdict['dec'], rdict['mjd']

 for line in guff.splitlines():
 line = line.strip()
 if not line:
 continue
 field_id, ra, dec, mjd = parse_line(line)
 print field_id, ra, dec, mjd

 C:\junktommy_grav.py
 f29227 20.396261 67.5 53370.0679769
 f31448 20.416147 79.662194 53370.0681162
 f31226 20.412639 78.445889 53370.0682386
 f31004 20.418133 77.229694 53370.0683602
 f30782 20.431094 76.0135 53370.0684821
 f30560 20.450506 74.797306 53370.068604
 f30338 20.4756527778 73.58 53370.0687262
 f30116 20.506028 72.364889 53370.0688489
 f29894 20.541261 71.148611 53370.0689707
 f29672 20.5810805556 69.932389 53370.0690935

 Cheers,
 John

Slightly less ugly:

C:\junkdiff tommy_grav.py tommy_grav_2.py
18,23d17
 is_angle = {
 'ra': True,
 'dec': True,
 'mjd': False,
 }

27a22,27
 converter = {
 'ra': convert_angle,
 'dec': convert_angle,
 'mjd': float,
 }

41,44c41
 if is_angle[key]:
 rvalue = convert_angle(value)
 else:
 rvalue = float(value)
---
 rvalue = converter[key](value)
--
http://mail.python.org/mailman/listinfo/python-list


cross-compilation

2008-08-06 Thread Roumen Petrov

Hi list members,

It seems to me that this is discussed many times in the past but without 
 progress. As I understand in general there is no objections and 
preferred cross-compilation has to be based on distutils (scons was 
rejected).


So I would like to cross-compile from linux(build system) to the 
mingw(host system) and note some(many) problems:


- the configure script don't support cross-compilation:
  The first is lack of macro AC_CANONICAL_HOST.
  Next is AC_TRY_RUN with two arguments.

- native mingw build isn't supported well:
  As example updated ifdefs from issue 1412448 aren't in the code. The 
posixmodule.c is required and I expect native build to fail. May be same 
for pwdmodule.c.


- the build process may require python installed on build system and the 
distutils may be isn't aware that cross-compilation is requested. In 
general isn't possible to run on build platform executable just build 
for host platform. Linux plus emulator is an exception.


What about to start to resolve issues step by step ?

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


random numbers according to user defined distribution ??

2008-08-06 Thread Alex
Hi everybody,

I wonder if it is possible in python to produce random numbers
according to a user defined distribution?
Unfortunately the random module does not contain the distribution I
need :-(

Many thanks

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


Re: random numbers according to user defined distribution ??

2008-08-06 Thread Dominic van Berkel
On Thursday 07 August 2008 00:02, Alex [EMAIL PROTECTED] wrote:

 Hi everybody,
 
 I wonder if it is possible in python to produce random numbers
 according to a user defined distribution?
 Unfortunately the random module does not contain the distribution I
 need :-(
 
 Many thanks
 
  axel
I'm not aware of any module with that specific function, but it's
algorithmically not too complex I'd think. If you're writing an application
that does this I'll assume that you have a basic gist of how to implement
it ;). It's been a while since I messed with the subject, so I'm not
getting any further than graphs in my head right now. Good luck!

-- 
Dominic van Berkel
Bi-la Kaifa
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyprocessing/multiprocessing for x64?

2008-08-06 Thread pigmartian
Interesting, I see Christian's responses to Benjamin, but not Benjamin's 
posts themselves.


Anyways, the question remains: will multiprocessing be supported for the 
x64 platform when it's released in 2.6?


pigmartian wrote:
I recently learned (from I response on this newsgroup to an earlier 
query) of the processing module for working with subprocesses in a 
similar manner to threading.  For what I needed to do, it worked great 
--- until I tried to run my code on an x64 box, for which that module 
isn't available*.  So, I'm just wondering if when processing is renamed 
to multiprocessing and included in the standard lib for 2.6, will x64 be 
supported?



~Scott

*yes, yes, I know.  download the source and compile it myself.

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


python equivalent for this perl soap client

2008-08-06 Thread Edwin . Madari
use SOAP::Lite;
use Data::Dumper;

$ENV{HTTP_proxy} = my_proxy_server_not_soap_proxy_server;
$ENV{HTTP_proxy_user} = ; #set correct value
$ENV{HTTP_proxy_pass} = ; #set correct value

my $soap = SOAP::Lite -service('file:./local_file_copy_of_wsdl.wsdl');
my $som = $soap-soapMethod(method, args, as, required);
print Dumper($som);

although above perl code (yes it works!), connects to the soap server through 
the http proxy with proper credentials, I would rather do it python. has any 
out there succeeded in making a soap request through firewall using wsdl 
something like below

from SOAPpy import WSDL
server = WSDL.Proxy('./local_file_copy_of_wsdl.wsdl')
res = server.soapMethod(method, args, as, required)

tried every which way but cannot get it to work. any hints, suggestions 
appreciated.

thanks in advance
Edwin




The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.

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

Adding Microsoft objects in Boa Constuctor Palette

2008-08-06 Thread Sid K
This is what I wanted to do:

Add Microsoft Active objects like Excel sheets and Word files to the
Palette in Boa Constructor. There is a User tab in the GUI builder
menu, but I'm not sure how to use/enable it.

1. Does anyone know how to do this?
2. Is anyone aware of any work that is currently going on to support
adding Microsoft objects in Boa.
3. Does any other Python GUI creator/editor exist out there that can
do this?
4. How would one add an excel sheet to a wx.Panel in general w/ or w/o
Boa?

Looking forward to replies...

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


Re: Parsing of a file

2008-08-06 Thread Henrique Dante de Almeida
On Aug 6, 3:55 pm, Tommy Grav [EMAIL PROTECTED] wrote:
 I have a file with the format

 Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames  
 5 Set 1
 Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames  
 5 Set 2
 Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames  
 5 Set 3
 Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames  
 5 Set 4
 Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames  
 5 Set 5
 Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames  
 5 Set 6
 Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames  
 5 Set 7
 Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames  
 5 Set 8
 Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames  
 5 Set 9
 Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames  
 5 Set 10

 I would like to parse this file by extracting the field id, ra, dec  
 and mjd for each line. It is
 not, however, certain that the width of each value of the field id,  
 ra, dec or mjd is the same
 in each line. Is there a way to do this such that even if there was a  
 line where Ra=** and
 MJD= was swapped it would be parsed correctly?

 Cheers
    Tommy

 Did you consider changing the file format in the first place, so that
you don't have to do any contortions to parse it ?

 Anyway, here is a solution with regular expressions (I'm a beginner
with re's in python, so, please correct it if wrong and suggest better
solutions):

import re
s = Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690
Frames 5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
5 Set 5
Field f30560: Dec=+74:47:50.3 Ra=20:27:01.82 MJD=53370.06860400 Frames
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
5 Set 10

s = s.split('\n')
r = re.compile(r'Field (\S+): (?:(?:Ra=(\S+) Dec=(\S+))|(?:Dec=(\S+)
Ra=(\S+))) MJD=(\S+)')
for i in s:
match = r.findall(i)
field = match[0][0]
Ra = match[0][1] or match[0][4]
Dec = match[0][2] or match[0][3]
MJD = match[0][5]
print field, Ra, Dec, MJD
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using an DTD not specified in XML file for validation

2008-08-06 Thread Ben Finney
Brian Quinlan [EMAIL PROTECTED] writes:

 I'm trying to figure out how I can validate an XML file using a DTD
 that isn't specified in the XML file.

When your inention is to start a new discussion, you could compose a
new message, *not* reply to an existing message. Your message here is
now part of an existing thread of discussion, yet is confusingly
unrelated in its content, and will not be noticed by most readers.

-- 
 \  “Whatever you do will be insignificant, but it is very |
  `\important that you do it.” —Mahatma Gandhi |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

Re: Parsing of a file

2008-08-06 Thread Paul McGuire
On Aug 6, 3:14 pm, Shawn Milochik [EMAIL PROTECTED] wrote:
 Regular expressions will do the trick nicely.


Or just use str.split, and create dicts using dict(list_of_tuples)
constructor.

This code creates a single dict for the input lines, keyed by id.
Each value contains elements labeled 'id', 'ra', and 'mjd'.

-- Paul


data = \
Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690
Frames   5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620
Frames   5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860
Frames   5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020
Frames   5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210
Frames   5 Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400
Frames   5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620
Frames   5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890
Frames   5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070
Frames   5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350
FramesSet 10 .splitlines()

d = dict(
(rec.split()[1][:-1],
 dict([('id',rec.split()[1][:-1])] +
  [map(str.lower,f.split('='))
  for f in rec.split()[2:5]] ) )
 for rec in data
)
print d.keys()

for id in d.keys():
print d[id].items()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using an DTD not specified in XML file for validation

2008-08-06 Thread Edwin . Madari
can you edit the xml and add the dtd/scheama ?

.Edwin

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Ben Finney
Sent: Wednesday, August 06, 2008 7:07 PM
To: python-list@python.org
Subject: Re: Using an DTD not specified in XML file for validation


Brian Quinlan [EMAIL PROTECTED] writes:

 I'm trying to figure out how I can validate an XML file using a DTD
 that isn't specified in the XML file.

When your inention is to start a new discussion, you could compose a
new message, *not* reply to an existing message. Your message here is
now part of an existing thread of discussion, yet is confusingly
unrelated in its content, and will not be noticed by most readers.

-- 
 \  Whatever you do will be insignificant, but it is very |
  `\important that you do it. -Mahatma Gandhi |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list



The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.


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


benchmark

2008-08-06 Thread Jack
I know one benchmark doesn't mean much but it's still disappointing to see 
Python as one of the slowest languages in the test:

http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/
 


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


Re: benchmark

2008-08-06 Thread Jake Anderson

Jack wrote:
I know one benchmark doesn't mean much but it's still disappointing to see 
Python as one of the slowest languages in the test:


http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/ 



--
http://mail.python.org/mailman/listinfo/python-list
  
Something to note though, The python version is ~ half the length of the 
rest of them ;-

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


Re: Tkinter fullscreen with Mac OS X

2008-08-06 Thread C Martin
On Jul 28, 6:43 pm, Guilherme Polo [EMAIL PROTECTED] wrote:

 You could try this, supposing tl is a toplevel:

 tl.tk.call(::tk::unsupported::MacWindowStyle, style, tl._w, plain, 
 none)


I tried this (although, my tl is actually a tk instance):

self.tk.call(::tk::unsupported::MacWindowStyle, style, self.tk._w,
plain, none)

and get this message:

Traceback (most recent call last):
  File ./ProgramCountdown.py, line 120, in module
control = Controller(tk)
  File ./ProgramCountdown.py, line 37, in __init__
self.tk.call(::tk::unsupported::MacWindowStyle, style,
self.tk._w, plain, none)
_tkinter.TclError: bad class: should be alert, moveableAlert, modal,
moveableModal, floating, help, or document

Any thoughts? Can you do something similar on the tk instance itself?

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


os.system question

2008-08-06 Thread Kevin Walzer

 import os
 foo = os.system('whoami')
kevin
 print foo
0


The standard output of the system command 'whoami' is my login name. Yet 
the value of the 'foo' object is '0,' not 'kevin.' How can I get the 
value of 'kevin' associated with foo?


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


A question about string and float number

2008-08-06 Thread Wei Guo
Hi all,

I am new of python. Could anyone help me a question as below?

Is there any function that can judge a string s is a float number or not?
FOr example, if s = '1.232' or s='1e+10', then it returns true, otherwise,
it will return false.

isdigit() in string doesn't work. float() will throw an exception and I just
need true or false as result.

Thanks a lot in advance,

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

Re: os.system question

2008-08-06 Thread Steven D'Aprano
On Wed, 06 Aug 2008 21:07:40 -0400, Kevin Walzer wrote:

 import os
   foo = os.system('whoami')
 kevin
   print foo
 0
  
  
 The standard output of the system command 'whoami' is my login name. Yet
 the value of the 'foo' object is '0,' not 'kevin.' How can I get the
 value of 'kevin' associated with foo?


That's because os.system captures the return code of the system call, 
which is 0 in this case because whoami succeeded. Meanwhile whoami 
printed its result to standard output, as normal.

What you want is os.popen('whoami', 'r').read()

Also look at the popen2 module.



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


Re: random numbers according to user defined distribution ??

2008-08-06 Thread Steven D'Aprano
On Wed, 06 Aug 2008 15:02:37 -0700, Alex wrote:

 Hi everybody,
 
 I wonder if it is possible in python to produce random numbers according
 to a user defined distribution? Unfortunately the random module does not
 contain the distribution I need :-(


This is a strange question. Of course you can -- just write a function to 
do so! Here's some easy ones to get you started:

from __future__ import division
import random, maths

def unbounded_rand(p=0.5):
Return a random integer between 0 and infinity.
if not (0  p = 1):
raise ValueError
n = 0
while random.random()  p:
n += 1
return n

def pseudonorm():
Return a random float with a pseudo-normal distribution.

The probability distribution is centered at 0 and bounded 
by -1 and +1.

return (sum([random.random() for i in range(6)])-3)/3

def triangular(min=0, max=1, mode=0.5):
Return a random float in the range (min, max) inclusive
with a triangular histogram, and the peak at mode.

u = random.random()
if u = (mode-min)/(max-min):
return min + math.sqrt(u*(max-min)*(mode-min))
else:
return max - math.sqrt((1-u)*(max-min)*(max-mode))

def linear():
Return a random float with probability density 
function pdf(x)=2x.

return math.sqrt(random.random())



There's no general way to create a random function for an arbitrary 
distribution. I don't think there's a general way to *describe* an 
arbitrary random distribution. However, there are some mathematical 
techniques you can use to generate many different distributions. Google 
on transformation method and rejection method.

If you have a specific distribution you are interested in, and you need 
some help, please ask.



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


Re: benchmark

2008-08-06 Thread bearophileHUGS
On Aug 7, 2:05 am, Jack [EMAIL PROTECTED] wrote:
 I know one benchmark doesn't mean much but it's still disappointing to see
 Python as one of the slowest languages in the test:

 http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-p...

That Python code is bad, it contains range() instead of xrange, the
big loop is in the main code instead of inside a function, uses ==
None, etc. That person can try this (with Psyco), I have changed very
little, the code is essentially the same:


import time, psyco
from psyco.classes import __metaclass__

class Person:
def __init__(self, count):
self.count = count
self.prev = None
self.next = None
def shout(self, shout, deadif):
if shout  deadif:
return shout + 1
self.prev.next = self.next
self.next.prev = self.prev
return 1

class Chain:
def __init__(self, size):
self.first = None
last = None
for i in xrange(size):
current = Person(i)
if self.first is None:
self.first = current
if last is not None:
last.next = current
current.prev = last
last = current
self.first.prev = last
last.next = self.first
def kill(self, nth):
current = self.first
shout = 1
while current.next != current:
shout = current.shout(shout, nth)
current = current.next
self.first = current
return current


def main():
ITER = 10
start = time.time()
for i in xrange(ITER):
chain = Chain(40)
chain.kill(3)
end = time.time()
print 'Time per iteration = %s microseconds ' % ((end - start) *
100 / ITER)

psyco.full()
main()

us = microseconds
On my PC (that seems similar to his one) this version needs about 38.9
us/iter instead of 189.

On my PC the Java version takes 1.17 us, while the C++ version (with
MinGW 4.2.1) takes 9.8 us.
A raw D translation needs 14.34 us, while a cleaned up (that uses
structs, no getters/setters) needs 4.67 us.
I don't know why my C++ is so much slow (doing the same things to the C
++ version doesn't change its running time much).

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.system question

2008-08-06 Thread Larry Wang
os.system() simply executes the command in a subshell, and returns the 
command's exit status which in your case is '0'. If you need to capture the 
stdout, stderr, etc. stuff, subprocess module is preferred which offers more 
powerful functionalities over os.system().


Nessus

Kevin Walzer [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

 import os
 foo = os.system('whoami')
kevin
 print foo
0


The standard output of the system command 'whoami' is my login name. Yet 
the value of the 'foo' object is '0,' not 'kevin.' How can I get the value 
of 'kevin' associated with foo?


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com 


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


  1   2   >