Why is os.stat so slow?

2009-06-16 Thread the_ricka
Hi all,
Fairly new to python but have programmed in other languages (C, Java)
before.  I was experimenting with a python program that needed to take
a directory tree and get the total disk usage of every file (and
subfolder) underneath it.  This solution also has to run on Windows
Server 2003 for work and it is accessing a NAS shared via CIFS.  A
sample folder I'm using contains about 26,000 subfolders and 435,000
files.  The original solution I came up with was elegant, but
extremely slow (compared to doing a right click in Windowsexplorer on
the folder tree and clicking properties).  It looked something like
this:
import os
folder = r'Z:\foldertree'
folder_size = 0
for (path, dirs, files) in os.walk(folder):
for file in files:
folder_size += os.path.getsize(os.path.join(path,file))

I profiled the above code and os.stat was taking up roughly 90% of the
time.  After digging around, I found some code in another post to use
win32api to use API calls to speed this up (if you are interested,
search for supper fast walk, yes super is misspelled).  To my
surprise, the average time is now about 1/7th of what it used to be.
I believe the problem is that my simple solution had to call os.stat
twice (once in the os.walk and once by me calling os.path.getsize) for
every file and folder in the tree.

I understand that os.stat can work on any OS.  However, the expense
should not be that dramatic of a difference (in my opinion).  Is there
an OS agnostic way to get this functionality to work faster?

Also, if I wanted to port this to Linux or some other OS, is os.stat
as expensive?  If so, are there other libraries (like win32api) to
assist in doing these operations faster?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: waling a directory with very many files

2009-06-16 Thread Hrvoje Niksic
Nick Craig-Wood  writes:

> It can be done properly with gccxml though which converts structures
> into ctypes definitions.

That sounds interesting.

> That said the dirent struct is specified by POSIX so if you get the
> correct types for all the individual members then it should be
> correct everywhere.  Maybe ;-)

AFAIK POSIX specifies the names and types of the members, but not
their order in the structure, nor alignment.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: : an integer is required

2009-06-16 Thread Ulrich Eckhardt
Dave Angel wrote:
> Ulrich Eckhardt wrote:
>> open() doesn't take a string as second parameter, see 'help(open)'.
>> Instead, it takes one of the integers which are defined as symbols in the
>> os module, see 'dir(os)'.
>
> [...]The second parameter to the builtin function open() is a string[...]
> Now perhaps you're referring to open() in some other module, such as
> os.open().

True, mixed those up, as perhaps did the OP.

> an unqualified  open() can only refer to the one in builltin.

...unless you explicitly imported an 'open' from somewhere else.

Thanks for the correction.

Uli

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

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


Re: Build problem on Solaris 9

2009-06-16 Thread Martin v. Löwis
> When I run make after successively running ./configure, I got the
> following Error message:
> ./Parser/asdl_c.py -c ./Python ./Parser/Python.asdl
> /usr/bin/env: No such file or directory
> make: *** [Python/Python-ast.c] Error 127
> 
> /usr/bin/env deos exist
> 
> Can anyone help me with this?

It's probably rather that "python" does not exist on the path,
which asdl_c.py requires.

touch Include/Python-ast.h Python/Python-ast.c

should work around.

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


Re: Exceptions and Object Destruction (was: Problem with apsw and garbage collection)

2009-06-16 Thread Steven D'Aprano
On Tue, 16 Jun 2009 16:45:43 +1200, Lawrence D'Oliveiro wrote:

> In message , Piet van Oostrum wrote:
> 
>> The exact time of the destruction of objects is an implementation
>> detail and should not be relied upon.
> 
> That may be true in Java and other corporate-herd-oriented languages,
> but we know that dynamic languages like Perl and Python make heavy use
> of reference-counting wherever they can. If it's easy to satisfy
> yourself that the lifetime of an object will be delimited in this way, I
> don't see why you can't rely upon it.

Reference counting is an implementation detail used by CPython but not 
IronPython or Jython. I don't know about the dozen or so other minor/new 
implementations, like CLPython, PyPy, Unladen Swallow or CapPython.

In other words, if you want to write *Python* code rather than CPython 
code, don't rely on ref-counting.




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


Re: Good books in computer science?

2009-06-16 Thread Joachim Strömbergson
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Aloha!

dads wrote:
> I remember someone earlier in the thread mentioning reading source
> code from good coders. I've been wanting to give this a go as it makes
> perfect sense, I suppose the standard library would be a good start.
> What would your recommendations be, something not too too hard, so I
> don't understand.

When people wants to know what (good) Python code looks like, I usually
point them to Trac:

http://trac.edgewall.org/

Trac is not only a good tool for development written in Python. Trac
also uses Trac to develop Trac (cudos for eating your own dogfood) and
Trac allows easy browsing of the source code.

I still consider myself a Python-n00b and my judgement might be all
wrong. But I believe that the Trac developers follows Pythonic code
rules and the result a prime example of what well written, well document
Python code looks like.

Check for yourself though at:
http://trac.edgewall.org/browser/trunk/trac

- --
Med vänlig hälsning, Yours

Joachim Strömbergson - Alltid i harmonisk svängning.

Kryptoblog - IT-säkerhet på svenska
http://www.strombergson.com/kryptoblog

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAko3RegACgkQZoPr8HT30QEMVwCgrNkOYMGFmhMYunwZqlTFpAkt
He8AoOEXIC/QXkRu+sHtzIz+1+JQZp2F
=o+g8
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Regarding GUI

2009-06-16 Thread abhishek goswami
Hi,
I have a very basic question about GUI programming as i am beginner into Python.

You are providing many enviroment for GUI programming in python. But could you 
guide me

which is better or frequently used


Abhishek Goswami

Chennai

Phone No -0996227099


  Explore and discover exciting holidays and getaways with Yahoo! India 
Travel http://in.travel.yahoo.com/-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding GUI

2009-06-16 Thread Banibrata Dutta
Kindly search the archives of this mailing list... this is a fairly FAQ, and
you should find a relevant thread which isn't older than 2-3 months, IIRC.

In brief, you have several options, as you've probably already found, most
one of the most popular is wxPython but then there are several other
alternatives, and best fit depends on your specific requirements,
constraints and background.

On Tue, Jun 16, 2009 at 2:05 PM, abhishek goswami wrote:

> Hi,
> I have a very basic question about GUI programming as i am beginner into
> Python.
>
> You are providing many enviroment for GUI programming in python. But could
> you guide me
>
> which is better or frequently used
>
>
> Abhishek Goswami
> Chennai
> Phone No -0996227099
> --
> Bollywood news, movie reviews, film trailers and more! Click 
> here.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
regards,
Banibrata
http://www.linkedin.com/in/bdutta
-- 
http://mail.python.org/mailman/listinfo/python-list


Need to know if a file as only ASCII charaters

2009-06-16 Thread Jorge
Hi there,
I'm making  a application that reads 3 party generated ASCII files, but some
times
the files are corrupted totally or partiality and I need to know if it's a
ASCII file with *nix line terminators.
In linux I can run the file command but the applications should run in
windows.

Any help will be great.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: On the property function

2009-06-16 Thread koranthala
On Jun 16, 9:19 am, Chris Rebert  wrote:
> On Mon, Jun 15, 2009 at 5:43 AM, Virgil Stokes wrote:
> > Does anyone have a good example (or examples) of when "property(...)" can be
> > useful?
>
> Erm, when you want to create a property (i.e. computed attribute).
>
> from __future__ import division
> class TimeDelta(object):
>     def __init__(self, secs):
>         self. seconds =secs
>
>     @property
>     def minutes(self):
>         return self.seconds/60
>
>     @property
>     def hours(self):
>         return self.minutes/60
>
>     @property
>     def days(self):
>         return self.hours/24
>
> td = TimeDelta(555)
> print td.days, "days", "=", td.hours, "hours", "=", td.minutes, "minutes"
>
> Cheers,
> Chris
> --http://blog.rebertia.com

To add a little more, this link _ 
http://dirtsimple.org/2004/12/python-is-not-java.html
_ might help. - Search for 'Getters and setters' and you will reach
the place where property is explained more.
-- 
http://mail.python.org/mailman/listinfo/python-list


Input problem

2009-06-16 Thread Prasoon
I am new to pythonand using python 2.6
I want to know when to use raw_input( ) and when to use input( )???

According to my interpretation one should use input( ) when entering
numbers etc and
raw_input( ) when a string is too be entered.

Correct me if I am wrong

Also if I want to enter two numbers 'a' and b such that while entering
them through the keyboard
there is a space between the two...

For example:
>>>Enter two numbers:
 .12 15

Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b'

I mean how to handle spaces???/

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


Re: On the property function

2009-06-16 Thread Lie Ryan
Chris Rebert wrote:
> On Mon, Jun 15, 2009 at 5:43 AM, Virgil Stokes wrote:
>> Does anyone have a good example (or examples) of when "property(...)" can be
>> useful?
> 
> Erm, when you want to create a property (i.e. computed attribute).
> 
> from __future__ import division
> class TimeDelta(object):
> def __init__(self, secs):
> self. seconds =secs
> 
> @property
> def minutes(self):
> return self.seconds/60
> 
> @property
> def hours(self):
> return self.minutes/60
> 
> @property
> def days(self):
> return self.hours/24
> 
> td = TimeDelta(555)
> print td.days, "days", "=", td.hours, "hours", "=", td.minutes, "minutes"
> 
> Cheers,
> Chris


Python's property is better than just that...

from __future__ import division
class TimeDelta(object):
def __init__(self, secs):
self. seconds =secs

@property
def minutes(self):
return self.seconds
@minutes.setter
def minutes(self, value
self.seconds = 60 * value

@property
def hours(self):
return self.minutes/60
@hours.setter
def hours(self, value):
self.minutes = 60 * value

@property
def days(self):
return self.hours/24
@days.setter
def days(self, value):
self.hours = 24 * value
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Bearophile
Prasoon:
> I am new to pythonand using python 2.6
> I want to know when to use raw_input( ) and when to use input( )???

I think it's better to always use raw_input(), and then convert the
string to the data to work on.

Once in a while you may want to use input() to input expressions (like
formulas) in your program in a quick, simple and unsafe way...

In Python3+ they have removed input() and they have renamed raw_input
() as input(). You can have the functionality of 2.x input() with eval
(input()). (I think Python3 developers have taken the right choices
here).

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


Re: Input problem

2009-06-16 Thread Francesco Bochicchio
On 16 Giu, 11:32, Prasoon  wrote:
> I am new to pythonand using python 2.6
> I want to know when to use raw_input( ) and when to use input( )???
>
> According to my interpretation one should use input( ) when entering
> numbers etc and
> raw_input( ) when a string is too be entered.
>
> Correct me if I am wrong
>
You should almost always use raw_input and write your own code to
validate the
input and convert it. input (wich is roughly equivalent of veval
(raw:_input())
is officially considered a Bad Choice and as such has been changed in
Python 3.x
( that is, python 3.x 'input' is equivalent to python 2.x raw_input ).

P.S : if you are new to python and don't expect to use external
libraries for the next
months (one year?) you might consider to start directly with python
3.x.


> Also if I want to enter two numbers 'a' and b such that while entering
> them through the keyboard
> there is a space between the two...
>
> For example:>>>Enter two numbers:
>
>  .12 15
>
> Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b'
>
> I mean how to handle spaces???/


For instance: map( int, raw_input.split() ) splits the
input string using blanks as separator, then try to convert each piece
in an integer
and returns a list of integer. Of course if the input string is not a
list of integer
you get an exception.

You could also do:

a, b =  map( int, raw_input.split() )

but in this case you get an exception also if the input strings
cobntains less or more than two integers.

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


PSP Text Editor

2009-06-16 Thread Johnson Mpeirwe

Hi all,

Does anyone know of any good Python Server Pages text editor that can 
provide indentation, syntax highlighting, etc..


Thank you.

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


how to stop a function execution like...

2009-06-16 Thread Gaudha
Is there any built-in function to stop execution of a function similar
to stop the program execution by sys.exit?
In the example below, I want to skip statement 2... if the 'if'
condition is satisfied.
Don't advice me to put statement 2 in 'else' block. That's not my
intention.
May be this a simple task. Sorry to say I'm novice in Python,
gentlemen...

def funct :
if (.) : statement 1
statement 2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Prasoon
On Jun 16, 3:34 pm, Francesco Bochicchio  wrote:
> On 16 Giu, 11:32, Prasoon  wrote:> I am new to 
> pythonand using python 2.6
> > I want to know when to use raw_input( ) and when to use input( )???
>
> > According to my interpretation one should use input( ) when entering
> > numbers etc and
> > raw_input( ) when a string is too be entered.
>
> > Correct me if I am wrong
>
> You should almost always use raw_input and write your own code to
> validate the
> input and convert it. input (wich is roughly equivalent of veval
> (raw:_input())
> is officially considered a Bad Choice and as such has been changed in
> Python 3.x
> ( that is, python 3.x 'input' is equivalent to python 2.x raw_input ).
>
> P.S : if you are new to python and don't expect to use external
> libraries for the next
> months (one year?) you might consider to start directly with python
> 3.x.
>
> > Also if I want to enter two numbers 'a' and b such that while entering
> > them through the keyboard
> > there is a space between the two...
>
> > For example:>>>Enter two numbers:
>
> >  .12 15
>
> > Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b'
>
> > I mean how to handle spaces???/
>
> For instance: map( int, raw_input.split() ) splits the
> input string using blanks as separator, then try to convert each piece
> in an integer
> and returns a list of integer. Of course if the input string is not a
> list of integer
> you get an exception.
>
> You could also do:
>
> a, b =  map( int, raw_input.split() )
>
> but in this case you get an exception also if the input strings
> cobntains less or more than two integers.
>
> Ciao
> -
> FB

I think you meant

a, b = map( int, raw_input().split() )

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


Re: how to stop a function execution like...

2009-06-16 Thread Diez B. Roggisch
Gaudha wrote:

> Is there any built-in function to stop execution of a function similar
> to stop the program execution by sys.exit?
> In the example below, I want to skip statement 2... if the 'if'
> condition is satisfied.
> Don't advice me to put statement 2 in 'else' block. That's not my
> intention.

Why not? It's from all you tell us perfectly the right thing to do.

> May be this a simple task. Sorry to say I'm novice in Python,
> gentlemen...
> 
> def funct :
> if (.) : statement 1
> statement 2


def funct():
if ...:
   statement 1
   return
statement 2


would also work. But it is not really "better" than using else.

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


Re: Good books in computer science?

2009-06-16 Thread Aaron Watters
On Jun 14, 4:47 pm, dads  wrote:
> I'm wanting to purchase some of the titles that have been raised in
> this thread. When I look they are very expensive books which is
> understandable. Do you think getting earlier editions that are cheaper
> is a daft thing or should I fork out the extra £10-£30 to get the
> latest edition?

This is the best book ever written on computer science
and the first edition is free.

http://www.math.upenn.edu/~wilf/AlgComp3.html

  -- Aaron Watters
 http://aaron.oirt.rutgers.edu/myapp/amcharts/doc

===
less is more.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to stop a function execution like...

2009-06-16 Thread Gaudha
On Jun 16, 4:45 pm, "Diez B. Roggisch"  wrote:
> Gaudha wrote:
> > Is there any built-in function to stop execution of a function similar
> > to stop the program execution by sys.exit?
> > In the example below, I want to skip statement 2... if the 'if'
> > condition is satisfied.
> > Don't advice me to put statement 2 in 'else' block. That's not my
> > intention.
>
> Why not? It's from all you tell us perfectly the right thing to do.
>
> > May be this a simple task. Sorry to say I'm novice in Python,
> > gentlemen...
>
> > def funct :
> >     if (.) : statement 1
> >     statement 2
>
> def funct():
>     if ...:
>        statement 1
>        return
>     statement 2
>
> would also work. But it is not really "better" than using else.
>
> Diez

I considered 'return' as meant only for returning any value. Thank you
sir...
-- 
http://mail.python.org/mailman/listinfo/python-list


SIP v4.8.1 Released

2009-06-16 Thread Phil Thompson
SIP v4.8.1 has been released and can be downloaded from
http://www.riverbankcomputing.com/software/sip/.

SIP is a tool for generating Python modules that wrap C or C++ libraries.
It is similar to SWIG.  It is used to generate PyQt and PyKDE.

SIP is licensed under the Python License and runs on Windows, UNIX, Linux
and MacOS/X.  SIP requires Python v2.3 or later.

The main focus of this release is support for Python v3.

Other features of SIP include:

- extension modules are implemented as a single binary .pyd or .so file (no
  Python stubs)
- support for Python new-style classes
- the ability to specify the super-type and meta-type used to wrap
  instances
- generated modules are quick to import, even for large libraries
- thread support
- the ability to re-implement C++ abstract and virtual methods in Python
- the ability to define Python classes that derive from abstract C++
  classes
- the ability to spread a class hierarchy across multiple Python modules
- support for C++ namespaces
- support for C++ exceptions
- support for C++ operators
- an extensible build system written in Python that supports over 50
  platform/compiler combinations
- the generation of API files for IDEs that support autocompletion and call
  tips.
-- 
http://mail.python.org/mailman/listinfo/python-list


Tool for browsing python code

2009-06-16 Thread Lucas P Melo
Is there any tool for browsing python code? (I'm having a hard time 
trying to figure this out)

Anything like cscope with vim would be great.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tool for browsing python code

2009-06-16 Thread Stef Mientki

Lucas P Melo wrote:
Is there any tool for browsing python code? (I'm having a hard time 
trying to figure this out)

Anything like cscope with vim would be great.

maybe a good IDE like PyScripter would do ?

cheers,
Stef
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tool for browsing python code

2009-06-16 Thread Banibrata Dutta
On Tue, Jun 16, 2009 at 6:18 PM, Lucas P Melo  wrote:

> Is there any tool for browsing python code? (I'm having a hard time trying
> to figure this out)
> Anything like cscope with vim would be great.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

If you are not averse to GUI's, there are lot of options... IDLE,
PyScripter, BOA, PyDev(Eclipse)...
not sure if there are any "curses" base TUI's (!) for Python.

-- 
regards,
Banibrata
http://www.linkedin.com/in/bdutta
-- 
http://mail.python.org/mailman/listinfo/python-list


PyQt v4.5.1 Released (Python bindings for Qt)

2009-06-16 Thread Phil Thompson
PyQt v4.5.1 has been released and is available from
http://www.riverbankcomputing.com/software/pyqt/.

PyQt is a comprehensive set of bindings for the Qt application and UI
framework from Nokia.  It supports the same platforms as Qt (Windows,
Linux and MacOS/X).

The highlights of this release include:

- support for Python v3

- support for Qt v4.5

- a new Pythonic API for connecting signals and slots that doesn't
  require any knowledge of C++ data types

- support for the GTK+ theme engine.

Windows installers are provided for the GPL version of PyQt which contains
everything needed for PyQt development (including Qt, Qt Designer and
QScintilla) except Python itself.

PyQt v4 is implemented as a set of 18 extension modules containing over
400 classes and over 6,000 functions and methods.

QtCore
The non-GUI infrastructure including event loops, threads, i18n,
Unicode, signals and slots, user and application settings, mapped
files and shared memory.

QtDesigner
A set of classes that allow the Qt Designer GUI design tool to be
extended with PyQt.

QtGui
A rich collection of GUI widgets.

QtHelp
A set of classes for creating and viewing searchable documentation and
being able to integrate online help with PyQt applications.  It
includes the C++ port of the Lucene text search engine.

QtNetwork
A set of classes to support TCP and UDP socket programming and higher
level protocols (eg. HTTP, SSL).

QtOpenGL
A set of classes that allows PyOpenGL to render onto Qt widgets.

QtScript
A set of classes that implements a JavaScript interpreter.  Python
objects may be exposed in the interpreter as JavaScript objects.

QtScriptTools
A debugger for the JavaScript interpreter.

QtSql
A set of classes that implement SQL data models and interfaces to
industry standard databases.  The Windows installers include support
for SQLite, MySQL, PostgreSQL and ODBC.

QtSvg
A set of classes to render SVG files onto Qt widgets.

QtTest
A set of classes to automate unit testing of PyQt applications and
GUIs.

QtWebKit
This implements a web browser engine based on the WebKit engine used by
Apple's Safari browser.  It allows the methods and properties of Python
objects to be published and appear as JavaScript objects to scripts
embedded in HTML pages.

QtXML
A set of classes that implement DOM and SAX parsers.

QtXMLPatterns
A set of classes that implement XQuery and XPath support for XML and
custom data models.

QtAssistant
A set of classes that enables the Qt Assistant online help browser to
be integrated with an application.

QAxContainer
A set of classes for Windows that allows the integration of ActiveX
controls and COM objects.

phonon
A cross-platform multimedia framework that enables the use of audio and
video content in PyQt applications.  DirectX is used as the Windows
backend, QuickTime as the MacOS/X backend, and GStreamer as the Linux
backend.

DBus
PyQt includes dbus.mainloop.qt that allows the Qt event loop to be used
with the standard DBus Python bindings.

PyQt includes the pyuic4 utility which generates Python code to implement
user interfaces created with Qt Designer in the same way that the uic
utility generates C++ code.  It is also able to load Designer XML files
dynamically.

PyQt is available under the GPL and a commercial license.  Unlike Qt, PyQt
is not available under the LGPL.  The commercial PyQt license allows GPL
applications to be relicensed at any time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to stop a function execution like...

2009-06-16 Thread pdpi
On Jun 16, 12:45 pm, "Diez B. Roggisch"  wrote:
> Gaudha wrote:
> > Is there any built-in function to stop execution of a function similar
> > to stop the program execution by sys.exit?
> > In the example below, I want to skip statement 2... if the 'if'
> > condition is satisfied.
> > Don't advice me to put statement 2 in 'else' block. That's not my
> > intention.
>
> Why not? It's from all you tell us perfectly the right thing to do.
>

If I understood his post correctly, it's because he really wants to
exit the function early.
If that is the case, in his real situation rather than the tiny
example he posted, using the else clause would translate into:

def funct(params):
  if a:
something
  else:
rest of the function
goes here
and it goes on for a while
so you just burnt through
an indentation level needlessly

Now we can have a nice philosophical discussion about how using the
else version makes the execution outline more obvious :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread Dave Angel

Jorge wrote:

Hi there,
I'm making  a application that reads 3 party generated ASCII files, but some
times
the files are corrupted totally or partiality and I need to know if it's a
ASCII file with *nix line terminators.
In linux I can run the file command but the applications should run in
windows.

Any help will be great.

Thank you in advance.

  

So, which is the assignment:
  1) determine if a file has non-ASCII characters
  2) determine whether the line-endings are crlf or just lf

In the former case, look at translating the file contents to Unicode, 
specifying ASCII as source.  If it fails, you have non-ASCII
In the latter case, investigate the 'u' attribute of the mode parameter 
in the open() function.


You also need to ask yourself whether you're doing a validation of the 
file, or doing a "best guess" like the file command.



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


Re: how to stop a function execution like...

2009-06-16 Thread Dave Angel

Gaudha wrote:

On Jun 16, 4:45 pm, "Diez B. Roggisch"  wrote:
  

Gaudha wrote:


Is there any built-in function to stop execution of a function similar
to stop the program execution by sys.exit?
In the example below, I want to skip statement 2... if the 'if'
condition is satisfied.
Don't advice me to put statement 2 in 'else' block. That's not my
intention.
  

Why not? It's from all you tell us perfectly the right thing to do.



May be this a simple task. Sorry to say I'm novice in Python,
gentlemen...
  
def funct :

if (.) : statement 1
statement 2
  

def funct():
if ...:
   statement 1
   return
statement 2

would also work. But it is not really "better" than using else.

Diez



I considered 'return' as meant only for returning any value. Thank you
sir...

  
return with no arguments will return a value of None, same as falling 
off the end of the function.  That can be important to know, as the 
caller can therefore test for None.


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


Re: how to stop a function execution like...

2009-06-16 Thread mzdude
On Jun 16, 7:30 am, Gaudha  wrote:
> Is there any built-in function to stop execution of a function similar
> to stop the program execution by sys.exit?
> In the example below, I want to skip statement 2... if the 'if'
> condition is satisfied.
> Don't advice me to put statement 2 in 'else' block. That's not my
> intention.
> May be this a simple task. Sorry to say I'm novice in Python,
> gentlemen...
>
> def funct :
>     if (.) : statement 1
>     statement 2

sys.exit is a pretty harsh way to stop execution. It usually
means unable to continue. There is nothing that stops you
from putting that in a function.

Another possiblity would be
def funct :
   if(  ) :
  statement 1
  raise UserWarning, "precondition X in funct not met"

statement 2
...
statement n

Check out the try / except docs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Observer implementations

2009-06-16 Thread Mike C. Fletcher

Tobias Weber wrote:
...

No time to reinvent the wheel

I'd still need to know how to weakref a classmethod
  

See PyDispatcher for code to do this.

PyDispatcher, at least, is not abandoned, it would be more accurate to 
say "finished".  I use it in OpenGLContext (extensively), but I haven't 
had to change anything in a rather long time.  It pretty much just works.


Enjoy,
Mike

--

 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com

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


Re: persistent composites

2009-06-16 Thread Aaron Brady
On Jun 15, 4:56 pm, Aaron Brady  wrote:
> On Jun 15, 11:10 am, Paul Rubin  wrote:
>
> > Aaron Brady  writes:
> > > > A real-world application of persistent data structures can be found 
> > > > here:
> > > >http://stevekrenzel.com/persistent-list
>
> > > Jaime, thanks for the link.  I contacted its author.
>
> > You might also look atwww.couchdb.org.
>
> I'm not much for the interface.  But the back end might match what I'm
> doing.

Making the charitable interpretation that this was the extent of c-l-
py's support and enthusiasm for my idea, I will now go into mourning.
Death occurred at oh-eight-hundred.  Rest in peace, support &
enthusiasm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread pdpi
On Jun 16, 2:17 pm, Dave Angel  wrote:
> Jorge wrote:
> > Hi there,
> > I'm making  a application that reads 3 party generated ASCII files, but some
> > times
> > the files are corrupted totally or partiality and I need to know if it's a
> > ASCII file with *nix line terminators.
> > In linux I can run the file command but the applications should run in
> > windows.
>
> > Any help will be great.
>
> > Thank you in advance.
>
> So, which is the assignment:
>    1) determine if a file has non-ASCII characters
>    2) determine whether the line-endings are crlf or just lf
>
> In the former case, look at translating the file contents to Unicode,
> specifying ASCII as source.  If it fails, you have non-ASCII
> In the latter case, investigate the 'u' attribute of the mode parameter
> in the open() function.
>
> You also need to ask yourself whether you're doing a validation of the
> file, or doing a "best guess" like the file command.

>From your requisites, you're already assuming something that _should_
be ASCII, so it's easiest to check for ASCIIness at the binary level:

Open the file as binary
Loop at the bytes
  exit with error upon reading a byte outside the printable range
(32-126 decimal)
  or any of a number of lower-range exceptions (\n, \t -- not \r since
you want UNIX-style linefeeds)
exit with success if the loop ended cleanly

This supposes you're dealing strictly with ASCII, and not a full 8 bit
codepage, of course.
-- 
http://mail.python.org/mailman/listinfo/python-list


simple GUI for my application?

2009-06-16 Thread Filipe Teixeira
Hi, I'm really struggling to find the best GUI to make a simple
application.

I'm doing a program to load all the ini files in the current folder,
or the folder that the user chooses and list the specifics entries in
it.

So, the program would be like this:

Som tabs here like:
( Load | Edit | Options)

In the [ Load ] tab:
A folder tree in the left, and two labels or edit boxes showing some
specific entries in the ini file.

In the [ Edit ] tab:
really straight-forward, Edit boxes of the entries so the user can
edit

in the [ options ] tab:
More edits to specifie the default folder, etc.

Basically I will use a lot of edit boxes and some tabs, and a folder
tree, any tips so I can search in the right place?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple GUI for my application?

2009-06-16 Thread Tim Harig
On 2009-06-16, Filipe Teixeira  wrote:
> Hi, I'm really struggling to find the best GUI to make a simple
> application.

http://www.python.org/doc/faq/gui/
-- 
http://mail.python.org/mailman/listinfo/python-list


Funny xmlrpc / linux problem

2009-06-16 Thread Hans Müller

Hello,

I found a timing problem while playing with the xmlrpx stuff.

I created a very simple server, running on a network node on windows.
The client runs on windows or linux. It runs a very simple test function on the 
server
which just echos a passed string. The trip time is beeing measured.
When I use a 1024 char test string I get a trip time in the 10-20ms range on 
windows and linux.
But for small arguments the trip time explodes - why on earth ?!

May be this problem is here off topic and tcp/ip stack related.

Here is the very very simply test server:

#!/usr/bin/python
# -*- coding: cp1250 -*-
# xmlrpc test server

import SimpleXMLRPCServer
import xmlrpclib
import time

# the test function - justs echos the argument
def RunTest(buffer):
return buffer


print "Starting RPC Server..."

# Create server
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("node01", 1))
server.register_function(RunTest)

# Run the server's main loop
server.serve_forever()


This is the test client:

#!/usr/bin/python
# -*- coding: cp1250 -*-
# Test for Roundtrip Time xmlrpc calls

import SimpleXMLRPCServer
import xmlrpclib
import datetime

print "started..."


s = xmlrpclib.ServerProxy('http://laptvm:1')# place here your 
server address

for scale in (100240, 10240, 1024, 512, 256, 128, 64, 32, 16, 10, 1):
print "Checking with", scale
buffer = "0123456789" * scale
now = datetime.datetime.now()
for i in range(10):
res = s.RunTest(buffer)
if buffer != res:
print "data error"
later = datetime.datetime.now()
dt = later - now

print "time for 10 loops: %f" % (dt.seconds + (0.01 * 
dt.microseconds))



Some results from my tests here:

started...
Checking with 100240
time for 10 loops: 3.282000
Checking with 10240
time for 10 loops: 0.36
Checking with 1024
time for 10 loops: 0.078000
Checking with 512
time for 10 loops: 0.047000
Checking with 256
time for 10 loops: 0.046000
Checking with 128
time for 10 loops: 0.047000
Checking with 64
time for 10 loops: 1.985000 ' Whats this ?! - smaler packets, more time ?!
Checking with 32
time for 10 loops: 2.00
Checking with 16
time for 10 loops: 2.00
Checking with 10
time for 10 loops: 2.00
Checking with 1
time for 10 loops: 2.00



Tanks a lot,
Hans
--
http://mail.python.org/mailman/listinfo/python-list


Getting a processes' name?

2009-06-16 Thread Daniel Merboth (RIT Student)
Hello,

My college uses a program called AccessGrid for video conferencing, and it runs 
through pythonw.exe. It comes with a python script to kill all processes and 
exit the program. The problem is, the script kills everything related to 
pythonw.exe, including my open scripts. I'm looking for a way to get the name 
of the window or the processname, or something, to differentiate between the 
running versions of pythonw.exe.

This is critical to a script I'm writing that grabs a bunch of the computer's 
information, populates an HTML table with it, then uploads it to an internal 
webpage. The goal is to make my (and my co-worker's) job easier on AccessGrid 
node maintenance.  We use VNC to remotely access and having a table that 
automatically updates when a DNS or IP changes would be an incredible asset to 
us. The script also includes some basic information about AccessGrid, so if a 
staffmember emails us a node isn't working, we can quickly diagnose and repair 
the problem.

The scripts both overlap in getting the name of a running process.

I'm also kind of teaching myself python to write this. I have coding experience 
in java though.

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


Re: Funny xmlrpc / linux problem

2009-06-16 Thread Hans Müller

Small addition:

While tracing the network data I found the server to be the problem,
the answer to a request is beeing delayed by about 180ms - no idea why.

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


Re: simple GUI for my application?

2009-06-16 Thread Stef Mientki

Filipe Teixeira wrote:

Hi, I'm really struggling to find the best GUI to make a simple
application.

I'm doing a program to load all the ini files in the current folder,
or the folder that the user chooses and list the specifics entries in
it.

So, the program would be like this:

Som tabs here like:
( Load | Edit | Options)

In the [ Load ] tab:
A folder tree in the left, and two labels or edit boxes showing some
specific entries in the ini file.

In the [ Edit ] tab:
really straight-forward, Edit boxes of the entries so the user can
edit

in the [ options ] tab:
More edits to specifie the default folder, etc.

Basically I will use a lot of edit boxes and some tabs, and a folder
tree, any tips so I can search in the right place?
  

mayby this will do:
http://mientki.ruhosting.nl/data_www/pylab_works/pw_gui_support.html

cheers,
Stef
--
http://mail.python.org/mailman/listinfo/python-list


Python module problem under QNX

2009-06-16 Thread gnapalm
Hello,

I'm trying to solve an issue with custom Python module at QNX 6.4.0
and Python 2.5.2.
I have simple Python module (A.so) written in c++ which is linked
against some other c++ library (B.so) both build by autotools.

On Linux all works fine with PYTHONPATH pointed to A.so directory when
dynamic linker loads B.so afterwards.

On QNX there is no inter-library dependency support in libtools and
Python can not find symbols from B.so.

I'm not using dlopen in Python module and I would like to keep it that way.
What is the workaround for this issue?

Thanks in advance.

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


Re: Funny xmlrpc / linux problem

2009-06-16 Thread Hans Müller

Another addendum...

while running again the server code on a linux host,

the times are as expected.

started...
Checking with 100240
time for 10 loops: 2.844000
Checking with 10240
time for 10 loops: 0.39
Checking with 1024
time for 10 loops: 0.078000
Checking with 512
time for 10 loops: 0.063000
Checking with 256
time for 10 loops: 0.063000
Checking with 128
time for 10 loops: 0.062000
Checking with 64
time for 10 loops: 0.063000
Checking with 32
time for 10 loops: 0.063000
Checking with 16
time for 10 loops: 0.062000
Checking with 10
time for 10 loops: 0.063000
Checking with 1
time for 10 loops: 0.063000


The problem seems to be on the windows side.

Any ideas ?

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


Python so module dependency problem under QNX

2009-06-16 Thread gnapalm
Hello,

I'm trying to solve an issue with custom Python module at QNX 6.4.0
and Python 2.5.2.
I have simple Python module (A.so) written in c++ which is linked
against some other c++ library (B.so) both build by autotools.

On Linux all works fine with PYTHONPATH pointed to A.so directory when
dynamic linker loads B.so afterwards.

On QNX there is no inter-library dependency support in libtools and
Python can not find symbols from B.so.

I'm not using dlopen in Python module and I would like to keep it that way.
What is the workaround for this issue?

Thanks in advance.

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


Re: Funny xmlrpc / linux problem

2009-06-16 Thread Brian Quinlan

Hey Hans,

Try reversing the list of numbers and see if anything changes.

Cheers,
Brian

Hans Müller wrote:

Hello,

I found a timing problem while playing with the xmlrpx stuff.

I created a very simple server, running on a network node on windows.
The client runs on windows or linux. It runs a very simple test function 
on the server

which just echos a passed string. The trip time is beeing measured.
When I use a 1024 char test string I get a trip time in the 10-20ms 
range on windows and linux.

But for small arguments the trip time explodes - why on earth ?!

May be this problem is here off topic and tcp/ip stack related.

Here is the very very simply test server:

#!/usr/bin/python
# -*- coding: cp1250 -*-
# xmlrpc test server

import SimpleXMLRPCServer
import xmlrpclib
import time

# the test function - justs echos the argument
def RunTest(buffer):
return buffer


print "Starting RPC Server..."

# Create server
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("node01", 1))
server.register_function(RunTest)

# Run the server's main loop
server.serve_forever()


This is the test client:

#!/usr/bin/python
# -*- coding: cp1250 -*-
# Test for Roundtrip Time xmlrpc calls

import SimpleXMLRPCServer
import xmlrpclib
import datetime

print "started..."


s = xmlrpclib.ServerProxy('http://laptvm:1')# place here 
your server address


for scale in (100240, 10240, 1024, 512, 256, 128, 64, 32, 16, 10, 1):
print "Checking with", scale
buffer = "0123456789" * scale
now = datetime.datetime.now()
for i in range(10):
res = s.RunTest(buffer)
if buffer != res:
print "data error"
later = datetime.datetime.now()
dt = later - now

print "time for 10 loops: %f" % (dt.seconds + (0.01 * 
dt.microseconds))




Some results from my tests here:

started...
Checking with 100240
time for 10 loops: 3.282000
Checking with 10240
time for 10 loops: 0.36
Checking with 1024
time for 10 loops: 0.078000
Checking with 512
time for 10 loops: 0.047000
Checking with 256
time for 10 loops: 0.046000
Checking with 128
time for 10 loops: 0.047000
Checking with 64
time for 10 loops: 1.985000' Whats this ?! - smaler packets, more 
time ?!

Checking with 32
time for 10 loops: 2.00
Checking with 16
time for 10 loops: 2.00
Checking with 10
time for 10 loops: 2.00
Checking with 1
time for 10 loops: 2.00



Tanks a lot,
Hans


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


Re: Tool for browsing python code

2009-06-16 Thread D'Arcy J.M. Cain
On Tue, 16 Jun 2009 18:25:00 +0530
Banibrata Dutta  wrote:
> not sure if there are any "curses" base TUI's (!) for Python.

vi

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: persistent composites

2009-06-16 Thread Mike Kazantsev
On Tue, 16 Jun 2009 06:57:13 -0700 (PDT)
Aaron Brady  wrote:

> Making the charitable interpretation that this was the extent of c-l-
> py's support and enthusiasm for my idea, I will now go into mourning.
> Death occurred at oh-eight-hundred.  Rest in peace, support &
> enthusiasm.

I've read this thread from the beginning, being tempted to insert
remarks about shelve module or ORMs like SQLAlchemy, but that'd be
meaningless without the problem description, which I haven't seen
anywhere. Is it some trick idea like "let's walk on our heads"?

-- 
Mike Kazantsev // fraggod.net


signature.asc
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


sqlite3, OperationalError: no such column, shouldn't that ne a ProgrammingError?

2009-06-16 Thread Gabriel Rossetti

Hello everyone,

I get an OperationalError with sqlite3 if I put the wrong column name, 
but shouldn't that be a ProgrammingError instead? I read PEP 249 and it 
says :


"OperationalError

   Exception raised for errors that are related to the

   database's operation and not necessarily under the control
   of the programmer, e.g. an unexpected disconnect occurs,
   the data source name is not found, a transaction could not
   be processed, a memory allocation error occurred during
   processing, etc.  It must be a subclass of DatabaseError.
  
   ProgrammingError

   Exception raised for programming errors, e.g. table not

   found or already exists, syntax error in the SQL
   statement, wrong number of parameters specified, etc.  It
   must be a subclass of DatabaseError.
"

and to me it sounds more like a programming error than an operational 
error.


Thank you,
Gabriel
--
http://mail.python.org/mailman/listinfo/python-list


doctests and decorators

2009-06-16 Thread Eric Snow
Apparently there is a known issue with doctests, in which tests in
functions using externally defined decorators are ignored.  The
recommended fix is to update the order of checks in the _from_module
method of DocTestFinder in the doctest module.  The bug and fix are
discussed at the following URLs (and several places in this group):

http://bugs.python.org/issue1108
http://mail.python.org/pipermail/python-list/2007-September/627866.html

The fix implies that the inpect.getmodule function will find the
module of the function object and not of the decorator.  However, in
2.4 the inspect.getmodule function returns the module of the
decorator.  I have subsequently tested this in 2.5 and 2.6, and it
also returns the module of the decorator.  As such, the fix for
doctests does not work in my tests.  Below is the test code that I
used:



test1.py

def decorator(function):
def new_function(*args, **kwargs):
return function(*args, **kwargs)
return new_function

test2.py

import test1
import inspect

class Test(object):
@test1.decorator
def test2(self): pass

def run_tests():
test = Test()
test.test2()

print("Test is class, test is instance, test2 is method of Test
(has decorator)")
print("test's module:  %s" % inspect.getmodule(test))
print("Test's module:  %s" % inspect.getmodule(Test))
print("test.test2's module:%s" % inspect.getmodule
(test.test2))
print("Test.test2's module:%s" % inspect.getmodule
(Test.test2))
print("test.test2's func_name: %s" % test.test2.func_name)
print("Test.test2's func_name: %s" % Test.test2.func_name)

if __name__ == "__main__":
run_tests()



Here is the output that I got in 2.4, 2.5, and 2.6:

Test is class, test is instance, test2 is method of Test (has
decorator)
test's module:  
Test's module:  
test.test2's module:
Test.test2's module:
test.test2's func_name: new_function
Test.test2's func_name: new_function

If things were working right, then the module for test.test2 would be
the same as the module for test.  I must be missing something, as the
referenced discussion suggests a simple conclusion.  Any ideas?

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


ODE, GUI, plotter in Python

2009-06-16 Thread Ala
Hello everyone.

I am starting on implementing a simulator using python, and since it's
the first time I code in python would appreciate a few pointers:

The simulator will use a coupled ODE for the most part of the
simulation, I plan to use scipy. (Anything considered faster/better
than scipy for solving coupled ODEs? )

I plan for a GUI program with network graph plotting. I am leaning
towards using Qt for the GUI (internet forums seem to recommend it,
anyone got other preferences? )

Since the GUI application will contain few buttons and a plot, I am
planning to implement matplotlib into the GUI. But does anyone know if
matplotlib allows for interaction with the graph plot? (say for a
network simulation, allowing to right click on nodes and disable them
for instance, or alter some other properties of nodes and/or links
across them).

I am just starting out, hence I'd rather get some advice and experiment
a bit for my self as I go along.

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


Re: doctests and decorators

2009-06-16 Thread Eric Snow
On Jun 16, 9:59 am, Eric Snow  wrote:
> Apparently there is a known issue with doctests, in which tests in
> functions using externally defined decorators are ignored.  The
> recommended fix is to update the order of checks in the _from_module
> method of DocTestFinder in the doctest module.  The bug and fix are
> discussed at the following URLs (and several places in this group):
>
> http://bugs.python.org/issue1108http://mail.python.org/pipermail/python-list/2007-September/627866.html
>
> The fix implies that the inpect.getmodule function will find the
> module of the function object and not of the decorator.  However, in
> 2.4 the inspect.getmodule function returns the module of the
> decorator.  I have subsequently tested this in 2.5 and 2.6, and it
> also returns the module of the decorator.  As such, the fix for
> doctests does not work in my tests.  Below is the test code that I
> used:
>
> 
>
> test1.py
> 
> def decorator(function):
>     def new_function(*args, **kwargs):
>         return function(*args, **kwargs)
>     return new_function
>
> test2.py
> 
> import test1
> import inspect
>
> class Test(object):
>     @test1.decorator
>     def test2(self): pass
>
> def run_tests():
>     test = Test()
>     test.test2()
>
>     print("Test is class, test is instance, test2 is method of Test
> (has decorator)")
>     print("test's module:          %s" % inspect.getmodule(test))
>     print("Test's module:          %s" % inspect.getmodule(Test))
>     print("test.test2's module:    %s" % inspect.getmodule
> (test.test2))
>     print("Test.test2's module:    %s" % inspect.getmodule
> (Test.test2))
>     print("test.test2's func_name: %s" % test.test2.func_name)
>     print("Test.test2's func_name: %s" % Test.test2.func_name)
>
> if __name__ == "__main__":
>     run_tests()
>
> 
>
> Here is the output that I got in 2.4, 2.5, and 2.6:
>
> Test is class, test is instance, test2 is method of Test (has
> decorator)
> test's module:          
> Test's module:          
> test.test2's module:    
> Test.test2's module:    
> test.test2's func_name: new_function
> Test.test2's func_name: new_function
>
> If things were working right, then the module for test.test2 would be
> the same as the module for test.  I must be missing something, as the
> referenced discussion suggests a simple conclusion.  Any ideas?
>
> -eric

One work-around I found is the following change in example:



test1.py

def decorator(function):
def new_function(*args, **kwargs):
return function(*args, **kwargs)
new_function.__module__ = function.__module__
new_function.__doc__ = function.__doc__
new_function.__name__ = function.__name__
return new_function



However, this seems pretty lame.  The doctest module should be able to
figure out that the docstring belongs is there in the module.

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


Re: doctests and decorators

2009-06-16 Thread Christian Heimes
Eric Snow schrieb:
> Apparently there is a known issue with doctests, in which tests in
> functions using externally defined decorators are ignored.  The
> recommended fix is to update the order of checks in the _from_module
> method of DocTestFinder in the doctest module.  The bug and fix are
> discussed at the following URLs (and several places in this group):
> 
> http://bugs.python.org/issue1108
> http://mail.python.org/pipermail/python-list/2007-September/627866.html
> 
> The fix implies that the inpect.getmodule function will find the
> module of the function object and not of the decorator.  However, in
> 2.4 the inspect.getmodule function returns the module of the
> decorator.  I have subsequently tested this in 2.5 and 2.6, and it
> also returns the module of the decorator.  As such, the fix for
> doctests does not work in my tests.  Below is the test code that I
> used:

It's not an issue with the doctest module but with your code. You want
to use functools.wraps().

http://docs.python.org/library/functools.html#functools.wraps

Christian

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


Re: ODE, GUI, plotter in Python

2009-06-16 Thread Eduardo Lenz
Em Ter 16 Jun 2009, às 09:00:02, Ala escreveu:
> Hello everyone.
>
> I am starting on implementing a simulator using python, and since it's
> the first time I code in python would appreciate a few pointers:
>
> The simulator will use a coupled ODE for the most part of the
> simulation, I plan to use scipy. (Anything considered faster/better
> than scipy for solving coupled ODEs? )
>
> I plan for a GUI program with network graph plotting. I am leaning
> towards using Qt for the GUI (internet forums seem to recommend it,
> anyone got other preferences? )
>
> Since the GUI application will contain few buttons and a plot, I am
> planning to implement matplotlib into the GUI. But does anyone know if
> matplotlib allows for interaction with the graph plot? (say for a
> network simulation, allowing to right click on nodes and disable them
> for instance, or alter some other properties of nodes and/or links
> across them).
>
> I am just starting out, hence I'd rather get some advice and experiment
> a bit for my self as I go along.
>
> Thank you.


you should take a look at 
http://pyode.sourceforge.net/

and pygame.


[]'s
Eduardo.
-- 

 Eduardo Lenz Cardoso
 Dr.  Eng.
 Associate Professor
 
 State University of Santa Catarina
 Department of Mechanical Engineering
 89223-100 - Joinville-SC - Brasil

 Tel: +55 47 4009-7971 - Fax: +55 47 4009-7940
 E-mail: l...@joinville.udesc.br  
 -

-- 
Esta mensagem foi verificada pelo sistema de antivírus e
 acredita-se estar livre de perigo.

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


Re: doctests and decorators

2009-06-16 Thread Eric Snow
On Jun 16, 10:31 am, Christian Heimes  wrote:
> Eric Snow schrieb:
>
>
>
> > Apparently there is a known issue with doctests, in which tests in
> > functions using externally defined decorators are ignored.  The
> > recommended fix is to update the order of checks in the _from_module
> > method of DocTestFinder in the doctest module.  The bug and fix are
> > discussed at the following URLs (and several places in this group):
>
> >http://bugs.python.org/issue1108
> >http://mail.python.org/pipermail/python-list/2007-September/627866.html
>
> > The fix implies that the inpect.getmodule function will find the
> > module of the function object and not of the decorator.  However, in
> > 2.4 the inspect.getmodule function returns the module of the
> > decorator.  I have subsequently tested this in 2.5 and 2.6, and it
> > also returns the module of the decorator.  As such, the fix for
> > doctests does not work in my tests.  Below is the test code that I
> > used:
>
> It's not an issue with the doctest module but with your code. You want
> to use functools.wraps().
>
> http://docs.python.org/library/functools.html#functools.wraps
>
> Christian

Unfortunately, I am stuck on 2.4 for now, which does not have the
functools.

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


Re: sqlite3, OperationalError: no such column, shouldn't that ne a ProgrammingError?

2009-06-16 Thread John Machin
On Jun 17, 1:41 am, Gabriel Rossetti 
wrote:
> Hello everyone,
>
> I get an OperationalError with sqlite3 if I put the wrong column name,
> but shouldn't that be a ProgrammingError instead? I read PEP 249 and it
> says :
>
[snip]
> and to me it sounds more like a programming error than an operational
> error.

How about showing us the code you used and the exact error message and
traceback?


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


Re: Input problem

2009-06-16 Thread Scott David Daniels

Prasoon wrote:

What is the difference between

z=int(raw_input()) and z=eval(raw_input())(I thought them to be
the same in case of integers)


Note that you can (and probably should) provide a prompt as an arg to
input and raw_input.


I mean when an integer is entered in that case are they same and when
an integer in not entered,in that case how are they different?

In response to an input() call in Python 2.x, you can type

sys.exit()

Generally it gives your user enough rope to shoot himself in the foot.

And here is the code running under 3.0 and 3.1rc2 (release candidate #2)
a, b = (int(t) for t in input('Numbers: ').split())

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: ODE, GUI, plotter in Python

2009-06-16 Thread Diez B. Roggisch
> you should take a look at
> http://pyode.sourceforge.net/

I think he's talking about "ordinary differential equations". While these
are part of physics-simulations (and the ODE-libraries' name might be a
PUN), PyODE is not what the OP is after.

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


Re: cross platform method Re: How to get the total size of a local hard disk?

2009-06-16 Thread Nigel Rantor
Tim Harig wrote:
> 
> This is a joke.  Do not take it seriously.  I do not actually suggest
> anybody use this method to measure the size of their drive.  I do not take any
> responsibility for any damages incurred by using this method.  I will laugh
> at you if you do.  Offer not valid in AK, HI, Puero Rico, or U.S Virgin 
> Ilands.
> 

Like most jokes it's not really funny if you have to explain it.

But I appreciate that you're worried that anyone who would actually
follow the advice would also probably be rabidly litigious even if they
were one of those rare-breed of living brain-donors.

  n

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


strptime issue in multi-threaded application

2009-06-16 Thread Joe Holloway
We recently uplifted our web application to run on Python 2.6.2.
We've noticed on a couple occasions that calls into time.strptime have
failed with this exception:

ImportError: Failed to import _strptime because the import lockis
[sic] held by another thread.

I poked around the source code enough to realize that this is
apparently due to time.strptime using PyImport_ImportModuleNoBlock
which potentially raises an ImportError rather than waiting for the
"import lock" to be released [1].  This appears to have been
introduced as a workaround for other thread safety concerns [2].

Does this indicate that strptime and any other library function that
uses the non-blocking import call in this fashion are not thread safe?
 Is there an idiomatic way of dealing with this error in
multi-threaded applications?

Like I mentioned, it's only happened on a couple occasions because the
right conditions have to be in place, but something doesn't seem right
about it.  I thought I'd ask on the mailing list before going so far
as to open a ticket, but feel free to direct me there if that's the
appropriate place for this.

Thanks,
Joe

[1] http://www.python.org/doc/2.6/c-api/import.html#PyImport_ImportModuleNoBlock
[2] http://svn.python.org/view?view=rev&revision=59678
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Funny xmlrpc / linux problem

2009-06-16 Thread Hans Müller

Richard,

thanks a lot for your hint, that was completely new for me.
Nagle's optimisation is definitely a good idea in most cases.

By the way, do you have an idea how to access the underlying socket to modify 
the behavier
via the setsockopt function to disable Nagle's algorythm in my special case (i 
need speed for small
packets) ?

Thanks a lot

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


Re: TypeError: int argument required

2009-06-16 Thread Lie Ryan
Lawrence D'Oliveiro wrote:
> In message , Rhodri 
> James wrote:
> 
>> On Mon, 15 Jun 2009 01:33:50 +0100, Lawrence D'Oliveiro
>>  wrote:
>>
>>> Perl allows just about any printable character as a quote. I tried
>>> alternative quotes for many years, and decided making that choice was a
>>> waste of brain cells.
>>>
>>> So no, using alternative quotes does not make things more readable.
>> I find it odd that you consider qquoting less scalable than backslashes.
> 
> Backslashes are scalable because they can be nested to any depth, without 
> having to decide beforehand which quotes to use at which level. And yes, I 
> do write things like this:

Scalable for the computers, not the eye...

>> I also find it odd that you dislike two visuals stutters (at the start
>> and end of string) so much that you'll put up with a dozen visual
>> stutters in the string to avoid them.  Particular since my years of
>> Perl-bashing lead me to the opposite conclusion.
> 
> I find it odd you should think so.
> 

If found it odd that you think that is more readable and scalable than this:

out.write (
'''
function JSString(Str)
  {
var Result = '\"'
for (var i = 0; i < Str.length; ++i)
  {
var ThisCh = Str.charAt(i)
if (ThisCh == '\\')
  {
ThisCh = ''
  }
else if (ThisCh == '\"')
  {
ThisCh = '\\\"'
  }
else if (ThisCh == '\t')
  {
ThisCh = '\\t'
  }
else if (ThisCh == '\n')
  {
ThisCh = '\\n'
  } /*if*/
Result += ThisCh
  } /*for*/
return Result + '\"'
} /*JSString*/
'''
)

I might go even further:

out.write (
'''
function JSString(Str)
  {
const dq = '\"'
const slash = '\\'

var Result = dq
for (var i = 0; i < Str.length; ++i)
  {
var ThisCh = Str.charAt(i)
if (ThisCh == slash)
  {
ThisCh = slash + slash
  }
else if (ThisCh == dq)
  {
ThisCh = slash + dq
  }
else if (ThisCh == '\t')
  {
ThisCh = slash + 't'
  }
else if (ThisCh == '\n')
  {
ThisCh = slash + 'n'
  } /*if*/
Result += ThisCh
  } /*for*/
return Result + dq
} /*JSString*/
'''
)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sqlite3, OperationalError: no such column, shouldn't that ne a ProgrammingError?

2009-06-16 Thread Jon Clements
On Jun 16, 4:41 pm, Gabriel Rossetti 
wrote:
> Hello everyone,
>
> I get an OperationalError with sqlite3 if I put the wrong column name,
> but shouldn't that be a ProgrammingError instead? I read PEP 249 and it
> says :
>
> "        OperationalError
>
>             Exception raised for errors that are related to the
>             database's operation and not necessarily under the control
>             of the programmer, e.g. an unexpected disconnect occurs,
>             the data source name is not found, a transaction could not
>             be processed, a memory allocation error occurred during
>             processing, etc.  It must be a subclass of DatabaseError.
>
>         ProgrammingError
>
>             Exception raised for programming errors, e.g. table not
>             found or already exists, syntax error in the SQL
>             statement, wrong number of parameters specified, etc.  It
>             must be a subclass of DatabaseError.
> "
>
> and to me it sounds more like a programming error than an operational
> error.
>
> Thank you,
> Gabriel

I would agree. With v2.5.2 of Python, I'm getting OperationalError
from sqlite3 and ProgrammingError from the psycopg2 (for postgres)
library. (Trying to create a table that already exists, trying to
'create tabel' and trying to select from non-existant tables)

I don't have time to go through code but looking at
http://www.sqlite.org/c3ref/c_abort.html, it would appear that there's
enough error results to distinguish between Operational and
Programming exceptions. Perhaps result != SQLITE_OK raises
OperationalError, or I'm looking at the wrong C spec for the version
in various Python versions etc... Or perhaps there's some chosen
rationale... or, perhaps, for an embedded engine, no one cares how it
failed, it just did...


Jon







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


strange behavior with os.system

2009-06-16 Thread kmw
Hi,

I wanted to write a simple script (in 5 minutes or so) which replaces
the option '+1' given to the command 'sort' by '-k 2' and than runs
'sort' with the modified argument list. After two hours I am giving up
and ask you for help. This is what I tried (please excuse the verbose
code, it is due to my various efforts to understand the error):

#!/usr/bin/python
import sys, os, re
arguments = sys.argv[0]
for i in sys.argv[1:]:
arguments += " " + i
p = re.compile ( "(\+(\d+))" )
m = p.search ( arguments )
print type ( m )
m_list = list ( m.groups () )
print type ( m_list )
from1 = str ( m_list[0] )
to1 = "-k " + str ( int ( m_list[1] ) + 1 )
cmd1 = str ( arguments.replace ( from1, to1 ) )
print cmd1
os.system ( cmd1 )

Now, this is what I get (on three different machines with different
versions of python):


./sort -F -k 2 -e

Traceback (most recent call last):
  File "./sort", line 9, in 
m_list = list ( m.groups () )
AttributeError: 'NoneType' object has no attribute 'groups'

Please note the unrequested output of ''. The strange
thing about this all is the fact that  the whole thing works as
expected when typed  into the interpreter. I would be glad if anyone
could help.

Best regards,
Kay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strptime issue in multi-threaded application

2009-06-16 Thread MRAB

Joe Holloway wrote:

We recently uplifted our web application to run on Python 2.6.2.
We've noticed on a couple occasions that calls into time.strptime have
failed with this exception:

ImportError: Failed to import _strptime because the import lockis
[sic] held by another thread.

I poked around the source code enough to realize that this is
apparently due to time.strptime using PyImport_ImportModuleNoBlock
which potentially raises an ImportError rather than waiting for the
"import lock" to be released [1].  This appears to have been
introduced as a workaround for other thread safety concerns [2].

Does this indicate that strptime and any other library function that
uses the non-blocking import call in this fashion are not thread safe?
 Is there an idiomatic way of dealing with this error in
multi-threaded applications?

Like I mentioned, it's only happened on a couple occasions because the
right conditions have to be in place, but something doesn't seem right
about it.  I thought I'd ask on the mailing list before going so far
as to open a ticket, but feel free to direct me there if that's the
appropriate place for this.

Thanks,
Joe

[1] http://www.python.org/doc/2.6/c-api/import.html#PyImport_ImportModuleNoBlock
[2] http://svn.python.org/view?view=rev&revision=59678


A simple workaround might be to sleep a short time and then retry.
--
http://mail.python.org/mailman/listinfo/python-list


Re: doctests and decorators

2009-06-16 Thread Michele Simionato
On Jun 16, 6:39 pm, Eric Snow  wrote:
> On Jun 16, 10:31 am, Christian Heimes  wrote:
>
>
>
> > Eric Snow schrieb:
>
> > > Apparently there is a known issue with doctests, in which tests in
> > > functions using externally defined decorators are ignored.  The
> > > recommended fix is to update the order of checks in the _from_module
> > > method of DocTestFinder in the doctest module.  The bug and fix are
> > > discussed at the following URLs (and several places in this group):
>
> > >http://bugs.python.org/issue1108
> > >http://mail.python.org/pipermail/python-list/2007-September/627866.html
>
> > > The fix implies that the inpect.getmodule function will find the
> > > module of the function object and not of the decorator.  However, in
> > > 2.4 the inspect.getmodule function returns the module of the
> > > decorator.  I have subsequently tested this in 2.5 and 2.6, and it
> > > also returns the module of the decorator.  As such, the fix for
> > > doctests does not work in my tests.  Below is the test code that I
> > > used:
>
> > It's not an issue with the doctest module but with your code. You want
> > to use functools.wraps().
>
> >http://docs.python.org/library/functools.html#functools.wraps
>
> > Christian
>
> Unfortunately, I am stuck on 2.4 for now, which does not have the
> functools.
>
> -eric

But you can always use the decorator module: 
http://pypi.python.org/pypi/decorator
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange behavior with os.system

2009-06-16 Thread MRAB

kmw wrote:

Hi,

I wanted to write a simple script (in 5 minutes or so) which replaces
the option '+1' given to the command 'sort' by '-k 2' and than runs
'sort' with the modified argument list. After two hours I am giving up
and ask you for help. This is what I tried (please excuse the verbose
code, it is due to my various efforts to understand the error):

#!/usr/bin/python
import sys, os, re
arguments = sys.argv[0]
for i in sys.argv[1:]:
arguments += " " + i


Shorter:

arguments = " ".join(sys.argv)


p = re.compile ( "(\+(\d+))" )


This looks for a "+" followed by digits.


m = p.search ( arguments )
print type ( m )
m_list = list ( m.groups () )
print type ( m_list )
from1 = str ( m_list[0] )
to1 = "-k " + str ( int ( m_list[1] ) + 1 )
cmd1 = str ( arguments.replace ( from1, to1 ) )
print cmd1
os.system ( cmd1 )

Now, this is what I get (on three different machines with different
versions of python):


./sort -F -k 2 -e


No "+"s, so the p.search(arguments) returns None.



Traceback (most recent call last):
  File "./sort", line 9, in 
m_list = list ( m.groups () )
AttributeError: 'NoneType' object has no attribute 'groups'

Please note the unrequested output of ''. The strange
thing about this all is the fact that  the whole thing works as
expected when typed  into the interpreter. I would be glad if anyone
could help.



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


how to import a name from a module-path?

2009-06-16 Thread thebjorn
I'm storing the path to functions in a database and now I'd like to
get a reference so I can execute them.

I looked briefly at the imp module and got very confused...  Currently
I'm doing this:

  def import_object(path):
  module, obj = path.rsplit('.', 1)
  exec "from rootpkg.%s import %s as fn" % (module, obj)
  return fn

  function = import_object('mypackage.mymodule.myfunction')

this is happening in a trusted environment, so I'm not worried about
malicious code.

Are there more elegant ways of doing this (ie. without exec)?

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


mac text files & for line

2009-06-16 Thread Humberto
Greetings.

This is probably a v. basic question, but my apologies as I'm
relatively new w/ this.

But I am attempting to use for line to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line
feed? I've attempted the method using tr, but receive an
illegal byte sequence error when running the tr '\r' '\n' < file1.txt
> file2.txt command on my Mac.

Any help would be greatly appreciated and thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Funny xmlrpc / linux problem

2009-06-16 Thread Jeff McNeil
On Jun 16, 12:51 pm, Hans Müller  wrote:
> Richard,
>
> thanks a lot for your hint, that was completely new for me.
> Nagle's optimisation is definitely a good idea in most cases.
>
> By the way, do you have an idea how to access the underlying socket to modify 
> the behavier
> via the setsockopt function to disable Nagle's algorythm in my special case 
> (i need speed for small
> packets) ?
>
> Thanks a lot
>
> Hans

Something like this ought to work. SimpleXMLRPCServer uses
SocketServer.TCPServer to handle all of the network-layer stuff. Also,
ironically, see http://bugs.python.org/issue6192.

import socket
from SimpleXMLRPCServer import SimpleXMLRPCServer

class MyXMLServer(SimpleXMLRPCServer):
def server_bind(self):
self.socket.setsockopt(
socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
SimpleXMLRPCServer.server_bind(self)

s = MyXMLServer(('127.0.0.1', 8080))
print s.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)

HTH,

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


Re: mac text files & for line

2009-06-16 Thread MRAB

Humberto wrote:

Greetings.

This is probably a v. basic question, but my apologies as I'm
relatively new w/ this.

But I am attempting to use for line to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line
feed? I've attempted the method using tr, but receive an
illegal byte sequence error when running the tr '\r' '\n' < file1.txt

file2.txt command on my Mac.


Any help would be greatly appreciated and thanks!


Open the file with mode 'U' for universal newline support ('\n', '\r' or
'\r\n').
--
http://mail.python.org/mailman/listinfo/python-list


Re: doctests and decorators

2009-06-16 Thread Eric Snow
On Jun 16, 11:24 am, Michele Simionato 
wrote:
> On Jun 16, 6:39 pm, Eric Snow  wrote:
>
>
>
> > On Jun 16, 10:31 am, Christian Heimes  wrote:
>
> > > Eric Snow schrieb:
>
> > > > Apparently there is a known issue with doctests, in which tests in
> > > > functions using externally defined decorators are ignored.  The
> > > > recommended fix is to update the order of checks in the _from_module
> > > > method of DocTestFinder in the doctest module.  The bug and fix are
> > > > discussed at the following URLs (and several places in this group):
>
> > > >http://bugs.python.org/issue1108
> > > >http://mail.python.org/pipermail/python-list/2007-September/627866.html
>
> > > > The fix implies that the inpect.getmodule function will find the
> > > > module of the function object and not of the decorator.  However, in
> > > > 2.4 the inspect.getmodule function returns the module of the
> > > > decorator.  I have subsequently tested this in 2.5 and 2.6, and it
> > > > also returns the module of the decorator.  As such, the fix for
> > > > doctests does not work in my tests.  Below is the test code that I
> > > > used:
>
> > > It's not an issue with the doctest module but with your code. You want
> > > to use functools.wraps().
>
> > >http://docs.python.org/library/functools.html#functools.wraps
>
> > > Christian
>
> > Unfortunately, I am stuck on 2.4 for now, which does not have the
> > functools.
>
> > -eric
>
> But you can always use the decorator 
> module:http://pypi.python.org/pypi/decorator

Thanks to both of you.  Very helpful.

So in general should decorators always hide themselves?  I am guessing
not, otherwise this would already be part of their behavior.  Still,
is it the common case to camouflage the decorator like this?  If so, I
would expect it to be the default behavior of decorators.

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


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread Scott David Daniels

Dave Angel wrote:

Jorge wrote:

Hi there,
I'm making  a application that reads 3 party generated ASCII files, 
but some

times
the files are corrupted totally or partiality and I need to know if 
it's a

ASCII file with *nix line terminators.
In linux I can run the file command but the applications should run in
windows.

Any help will be great.

Thank you in advance.

  

So, which is the assignment:
  1) determine if a file has non-ASCII characters
  2) determine whether the line-endings are crlf or just lf

In the former case, look at translating the file contents to Unicode, 
specifying ASCII as source.  If it fails, you have non-ASCII
In the latter case, investigate the 'u' attribute of the mode parameter 
in the open() function.


You also need to ask yourself whether you're doing a validation of the 
file, or doing a "best guess" like the file command.




Also, realize that ASCII is a 7-bit code, with printing characters all
greater than space, and very few people use delete ('\x7F'), so you
can define a function to determine if a file contains only printing
ASCII and a few control characters.  This one is False unless some ink
would be printed.

Python 3.X:
def ascii_file(name, controls=b'\t\n'):
ctrls = set(controls + b' ')
with open(name, 'rb') as f:
chars = set(f.read())
return min(chars) >= min(ctrls) ord('~') >= max(chars)
  ) and min(chars - ctrls) > ord(' ')

Python 2.X:
def ascii_file(name, controls='\t\n'):
ctrls = set(controls + ' ')
with open(name, 'rb') as f:
chars = set(f.read())
return min(chars) >= min(ctrls) and '~' >= max(chars
  ) and min(chars - ctrls) > ' '

For potentially more performance (at least on 2.X), you could do min
and max on the data read, and only do the set(data) if the min and
max are OK.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: waling a directory with very many files

2009-06-16 Thread thebjorn
On Jun 15, 6:56 am, Steven D'Aprano
 wrote:
> On Sun, 14 Jun 2009 22:35:50 +0200, Andre Engels wrote:
> > On Sun, Jun 14, 2009 at 6:35 PM, tom wrote:
> >> i can traverse a directory using os.listdir() or os.walk(). but if a
> >> directory has a very large number of files, these methods produce very
> >> large objects talking a lot of memory.
>
> >> in other languages one can avoid generating such an object by walking a
> >> directory as a liked list. for example, in c, perl or php one can use
> >> opendir() and then repeatedly readdir() until getting to the end of the
> >> file list. it seems this could be more efficient in some applications.
>
> >> is there a way to do this in python? i'm relatively new to the
> >> language. i looked through the documentation and tried googling but
> >> came up empty.
>
> > What kind of directories are those that just a list of files would
> > result in a "very large" object? I don't think I have ever seen
> > directories with more than a few thousand files...
>
> You haven't looked very hard :)
>
> $ pwd
> /home/steve/.thumbnails/normal
> $ ls | wc -l
> 33956
>
> And I periodically delete thumbnails, to prevent the number of files
> growing to hundreds of thousands.
>
> Steven

Not proud of this, but...:

[django] www4:~/datakortet/media$ ls bfpbilder|wc -l
 174197

all .jpg files between 40 and 250KB with the path stored in a database
field... *sigh*

Oddly enough, I'm a relieved that others have had similar folder sizes
(I've been waiting for this burst to the top of my list for a while
now).

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


Re: how to import a name from a module-path?

2009-06-16 Thread Gary Herron

thebjorn wrote:

I'm storing the path to functions in a database and now I'd like to
get a reference so I can execute them.

I looked briefly at the imp module and got very confused...  Currently
I'm doing this:

  def import_object(path):
  module, obj = path.rsplit('.', 1)
  exec "from rootpkg.%s import %s as fn" % (module, obj)
  return fn

  function = import_object('mypackage.mymodule.myfunction')

this is happening in a trusted environment, so I'm not worried about
malicious code.

Are there more elegant ways of doing this (ie. without exec)?
  


Yes.  Look at the __import__ builtin function, and if that's not enough 
functionality, look at the module names imp, which provides 
documentation starting with this bit:


DESCRIPTION
   This module provides the components needed to build your own
   __import__ function.



Both are available in Python2.x and Python3.x

Gary Herron




-- bjorn
  


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


Re: simple GUI for my application?

2009-06-16 Thread Mike Driscoll
On Jun 16, 9:20 am, Filipe Teixeira  wrote:
> Hi, I'm really struggling to find the best GUI to make a simple
> application.
>
> I'm doing a program to load all the ini files in the current folder,
> or the folder that the user chooses and list the specifics entries in
> it.
>
> So, the program would be like this:
>
> Som tabs here like:
> ( Load | Edit | Options)
>
> In the [ Load ] tab:
> A folder tree in the left, and two labels or edit boxes showing some
> specific entries in the ini file.
>
> In the [ Edit ] tab:
> really straight-forward, Edit boxes of the entries so the user can
> edit
>
> in the [ options ] tab:
> More edits to specifie the default folder, etc.
>
> Basically I will use a lot of edit boxes and some tabs, and a folder
> tree, any tips so I can search in the right place?

wxPython has all the widgets you've described built into it. They also
have a very helpful mailing list. However, you should try the various
toolkits and see which one makes the most sense to you.

When I was first looking at GUIs, I tried Tkinter first. But it just
couldn't replicate the stupid UIs I needed to reimplement, so I went
with wxPython. I've heard good things about pyQT. If you want the
ultimate look-and-feel for Windows, you should go with IronPython.

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


Re: doctests and decorators

2009-06-16 Thread Scott David Daniels

Eric Snow wrote:

...
One work-around I found is the following change in example:
test1.py

def decorator(function):
def new_function(*args, **kwargs):
return function(*args, **kwargs)
new_function.__module__ = function.__module__
new_function.__doc__ = function.__doc__
new_function.__name__ = function.__name__
return new_function

However, this seems pretty lame.  The doctest module should be able to
figure out that the docstring belongs is there in the module.


would the following look better?
import functools
...

def decorator(function):
@functools.wraps(function)
def new_function(*args, **kwargs):
return function(*args, **kwargs)
return new_function

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread norseman

Scott David Daniels wrote:

Dave Angel wrote:

Jorge wrote:

Hi there,
I'm making  a application that reads 3 party generated ASCII files, 
but some

times
the files are corrupted totally or partiality and I need to know if 
it's a

ASCII file with *nix line terminators.
In linux I can run the file command but the applications should run in
windows.


you are looking for a \x0D (the Carriage Return) \x0A (the Line feed) 
combination. If present you have Microsoft compatibility. If not you 
don't.  If you think High Bits might be part of the corruption, filter 
each byte with byte && \x7F  (byte AND'ed with hex 7F or 127 base 10) 
then check for the \x0D \x0A combination.
Run the test on a known text setup. Intel uses one order and the SUN and 
the internet another.  The BIG/Little ending confuses many. Intel 
reverses the order of multibyte numerics.  Thus - Small machine has big 
ego or largest byte value last. Big Ending.  Big machine has small ego. 
Little Ending.  Some coders get the 0D0A backwards, some don't.  You 
might want to test both.


(2^32)(2^24)(2^16(2^8)  4 bytes correct math order  little ending
Intel stores them (2^8)(2^16)(2^24)(2^32)   big ending
SUN/Internet stores them in correct math order.


Python will use \r\n (0D0A) and \n\r (0A0D) correctly.

HTH

Steve


Any help will be great.

Thank you in advance.

  

So, which is the assignment:
  1) determine if a file has non-ASCII characters
  2) determine whether the line-endings are crlf or just lf

In the former case, look at translating the file contents to Unicode, 
specifying ASCII as source.  If it fails, you have non-ASCII
In the latter case, investigate the 'u' attribute of the mode 
parameter in the open() function.


You also need to ask yourself whether you're doing a validation of the 
file, or doing a "best guess" like the file command.




Also, realize that ASCII is a 7-bit code, with printing characters all
greater than space, and very few people use delete ('\x7F'), so you
can define a function to determine if a file contains only printing
ASCII and a few control characters.  This one is False unless some ink
would be printed.

Python 3.X:
def ascii_file(name, controls=b'\t\n'):
ctrls = set(controls + b' ')
with open(name, 'rb') as f:
chars = set(f.read())
return min(chars) >= min(ctrls) ord('~') >= max(chars)
  ) and min(chars - ctrls) > ord(' ')

Python 2.X:
def ascii_file(name, controls='\t\n'):
ctrls = set(controls + ' ')
with open(name, 'rb') as f:
chars = set(f.read())
return min(chars) >= min(ctrls) and '~' >= max(chars
  ) and min(chars - ctrls) > ' '

For potentially more performance (at least on 2.X), you could do min
and max on the data read, and only do the set(data) if the min and
max are OK.

--Scott David Daniels
scott.dani...@acm.org


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


Re: how to import a name from a module-path?

2009-06-16 Thread thebjorn
On Jun 16, 7:43 pm, Gary Herron  wrote:
> thebjorn wrote:
> > I'm storing the path to functions in a database and now I'd like to
> > get a reference so I can execute them.
>
> > I looked briefly at the imp module and got very confused...  Currently
> > I'm doing this:
>
> >   def import_object(path):
> >       module, obj = path.rsplit('.', 1)
> >       exec "from rootpkg.%s import %s as fn" % (module, obj)
> >       return fn
>
> >   function = import_object('mypackage.mymodule.myfunction')
>
> > this is happening in a trusted environment, so I'm not worried about
> > malicious code.
>
> > Are there more elegant ways of doing this (ie. without exec)?
>
> Yes.  Look at the __import__ builtin function,
[...]

Thanks, that is much better:

   def import_object(path):
   module, obj = path.rsplit('.', 1)
   m = __import__(module, fromlist=['rootpkg'])
   return getattr(m, obj)

   function = import_object('mypackage.mymodule.myfunction')


> Gary Herron

Bjorn

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


Re: Measuring Fractal Dimension ?

2009-06-16 Thread Paul Rubin
Lawrence D'Oliveiro  writes:
> I don't think any countable set, even a countably-infinite set, can have a 
> fractal dimension. It's got to be uncountably infinite, and therefore 
> uncomputable.

I think the idea is you assume uniform continuity of the set (as
expressed by a parametrized curve).  That should let you approximate
the fractal dimension.

As for countability, remember that the reals are a separable metric
space, so the value of a continuous function any dense subset of the
reals (e.g. on the rationals, which are countable) completely
determines the function, iirc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple GUI for my application?

2009-06-16 Thread Tim Harig
On 2009-06-16, Mike Driscoll  wrote:
> On Jun 16, 9:20 am, Filipe Teixeira  wrote:
>> Hi, I'm really struggling to find the best GUI to make a simple
>> application.
[SNIP]
>> Basically I will use a lot of edit boxes and some tabs, and a folder
>> tree, any tips so I can search in the right place?
> When I was first looking at GUIs, I tried Tkinter first. But it just
> ultimate look-and-feel for Windows, you should go with IronPython.

IronPython is not a GUI toolkit per se.  It is a python implementation
build on top of .Net like Jython is built on top of Java.  I therefore has
access to the MFCs which can be used to create native Windows GUIs.  This
can also be done from Cpython using the pywin extensions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread MRAB

norseman wrote:

Scott David Daniels wrote:

Dave Angel wrote:

Jorge wrote:

Hi there,
I'm making  a application that reads 3 party generated ASCII files, 
but some

times
the files are corrupted totally or partiality and I need to know if 
it's a

ASCII file with *nix line terminators.
In linux I can run the file command but the applications should run in
windows.


you are looking for a \x0D (the Carriage Return) \x0A (the Line feed) 
combination. If present you have Microsoft compatibility. If not you 
don't.  If you think High Bits might be part of the corruption, filter 
each byte with byte && \x7F  (byte AND'ed with hex 7F or 127 base 10) 
then check for the \x0D \x0A combination.
Run the test on a known text setup. Intel uses one order and the SUN and 
the internet another.  The BIG/Little ending confuses many. Intel 
reverses the order of multibyte numerics.  Thus - Small machine has big 
ego or largest byte value last. Big Ending.  Big machine has small ego. 
Little Ending.  Some coders get the 0D0A backwards, some don't.  You 
might want to test both.



In an ASCII file endianness is irrelevant.

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


Re: Need to know if a file as only ASCII charaters

2009-06-16 Thread Scott David Daniels

norseman wrote:

Scott David Daniels wrote:

Dave Angel wrote:

Jorge wrote: ...
I'm making  a application that reads 3 party generated ASCII files, 
but some times the files are corrupted totally or partiality and 
I need to know if it's a ASCII file with *nix line terminators.

In linux I can run the file command but the applications should run in
windows.
you are looking for a \x0D (the Carriage Return) \x0A (the Line feed) 
combination. If present you have Microsoft compatibility. If not you 
don't.  If you think High Bits might be part of the corruption, filter 
each byte with byte && \x7F  (byte AND'ed with hex 7F or 127 base 10) 
then check for the \x0D \x0A combination.


Well  ASCII defines a \x0D as the return code, and \x0A as line feed.
It is unix that is wrong, not Microsoft (don't get me wrong, I know
Microsoft has often redefined what it likes invalidly).  If you
open the file with 'U', Python will return lines w/o the \r character
whether or not they started with it, equally well on both unix and
Microsoft systems.  Many moons ago the high order bit was used as a
parity bit, but few communication systems do that these days, so
anything with the high bit set is likely corruption.


 Intel uses one order and the SUN and  the internet another.  The

> BIG/Little ending confuses many. Intel reverses the order of multibyte
> numerics.  Thus- Small machine has big ego or largest byte value last.
> Big Ending.  Big machine has small ego.
Little Ending.  Some coders get the 0D0A backwards, some don't.  You 
might want to test both.

(2^32)(2^24)(2^16(2^8)  4 bytes correct math order  little ending
Intel stores them (2^8)(2^16)(2^24)(2^32)   big ending
SUN/Internet stores them in correct math order.
Python will use \r\n (0D0A) and \n\r (0A0D) correctly.


This is the most confused summary of byte sex I've ever read.
There is no such thing as "correct math order" (numbers are numbers).
The '\n\r' vs. '\r\n' has _nothing_ to do with little-endian vs.
big-endian.  By the way, there are great arguments for each order,
and no clear winner.  Network order was defined for sending numbers
across a wire, the idea was that you'd unpack them to native order
as you pulled the data off the wire.

The '\n\r' vs. '\r\n' differences harken back to the days when they were
format effectors (carriage return moved the carriage to the extreme
left, line feed advanced the paper).  You needed both to properly
position the print head.  ASCII uses the pair, and defined the effect
of each.  As ASCII was being worked out, MIT even defined a "line
starve" character to move up one line just as line feed went down one.
The order of the format effectors most used was '\r\n' because the
carriage return involved the most physical motion on many devices, and
the vertical motion time of the line feed could happen while the
carriage was moving.  After that, you often added padding bytes 
(typically ASCII NUL ('\x00') or DEL ('\x7F')) to allow the hardware

time to finish before you the did spacing and printing.

--Scott David Daniels
scott.dani...@acm.org

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


Re: Measuring Fractal Dimension ?

2009-06-16 Thread David C . Ullrich
On 15 Jun 2009 04:55:03 GMT, Steven D'Aprano
 wrote:

>On Sun, 14 Jun 2009 14:29:04 -0700, Kay Schluehr wrote:
>
>> On 14 Jun., 16:00, Steven D'Aprano
>>  wrote:
>> 
>>> Incorrect. Koch's snowflake, for example, has a fractal dimension of
>>> log 4/log 3 ? 1.26, a finite area of 8/5 times that of the initial
>>> triangle, and a perimeter given by lim n->inf (4/3)**n. Although the
>>> perimeter is infinite, it is countably infinite and computable.
>> 
>> No, the Koch curve is continuous in R^2 and uncountable. 
>
>I think we're talking about different things. The *number of points* in 
>the Koch curve is uncountably infinite, but that's nothing surprising, 
>the number of points in the unit interval [0, 1] is uncountably infinite. 
>But the *length* of the Koch curve is not, it's given by the above limit, 
>which is countably infinite (it's a rational number for all n).

No, the length of the perimeter is infinity, period. Calling it
"countably infinite" makes no sense.

You're confusing two different sorts of "infinity". A set has a
cardinality - "countably infinite" is the smallest infinite
cardinality.

Limits, as in calculus, as in that limit above, are not
cardinailities.

>
>> Lawrence is
>> right and one can trivially cover a countable infinite set with disks of
>> the diameter 0, namely by itself. The sum of those diameters to an
>> arbitrary power is also 0 and this yields that the Hausdorff dimension
>> of any countable set is 0.
>
>Nevertheless, the Hausdorff dimension (or a close approximation thereof) 
>can be calculated from the scaling properties of even *finite* objects. 
>To say that self-similar objects like broccoli or the inner surface of 
>the human lungs fails to nest at all scales is pedantically correct but 
>utterly pointless. If it's good enough for Benoît Mandelbrot, it's good 
>enough for me.

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


Re: doctests and decorators

2009-06-16 Thread Scott David Daniels

Eric Snow wrote:

In general should decorators always hide themselves?  I am guessing
not, otherwise this would already be part of their behavior.  Still,
is it the common case to camouflage the decorator like this?  If so, I
would expect it to be the default behavior of decorators.


The Python goal is "no magic".  So, if you want the stuff wrapped, you
do it (as the default traceback shows where the code actually goes).
It would be far more complicated to display the truth if decorators
defaulted to modifying the builtins, and you had to do magic to remove
that part of the decoration.  A decorator has _very_ simple semantics,
while anything that automatically copied attributes would have funny
semantics indeed for use by funny decorators like:

 abi = []
 def indexed(function):
 result = len(abi)
 abi.append(function)
 return result

 @indexed
 def first_function(...):
 ...

 @indexed
 def second_function(...):
 ...

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: strptime issue in multi-threaded application

2009-06-16 Thread Scott David Daniels

MRAB wrote:

Joe Holloway wrote:

We recently uplifted our web application to run on Python 2.6.2.
We've noticed on a couple occasions that calls into time.strptime have
failed with this exception:
[2] http://svn.python.org/view?view=rev&revision=59678


A simple workaround might be to sleep a short time and then retry.


Can you just import and use time.strptime once before you start the
threads?

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: simple GUI for my application?

2009-06-16 Thread Mike Driscoll
On Jun 16, 1:24 pm, Tim Harig  wrote:
> On 2009-06-16, Mike Driscoll  wrote:
>
> > On Jun 16, 9:20 am, Filipe Teixeira  wrote:
> >> Hi, I'm really struggling to find the best GUI to make a simple
> >> application.
> [SNIP]
> >> Basically I will use a lot of edit boxes and some tabs, and a folder
> >> tree, any tips so I can search in the right place?
> > When I was first looking at GUIs, I tried Tkinter first. But it just
> > ultimate look-and-feel for Windows, you should go with IronPython.
>
> IronPython is not a GUI toolkit per se.  It is a python implementation
> build on top of .Net like Jython is built on top of Java.  I therefore has
> access to the MFCs which can be used to create native Windows GUIs.  This
> can also be done from Cpython using the pywin extensions.

That is true...I was just referring to IronPython's ability to hook a
GUI created using Visual Studio easily. Going about it through pywin
and ctypes is probably above the OP's current needs...although I think
Greg Ewing's pyGUI wraps that stuff. I suppose the OP might find that
useful:

http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/

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


Re: mac text files & for line

2009-06-16 Thread Scott David Daniels

MRAB wrote:

Humberto wrote:

But I am attempting to use for line to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line
feed? I've attempted the method using tr, but receive an
illegal byte sequence error when running the tr '\r' '\n' < file1.txt

file2.txt command on my Mac.


Read
http://www.catb.org/~esr/faqs/smart-questions.html
Show code, inputs, and output and explain what you expected.

This could be one of dozens of problems.


Open the file with mode 'U' for universal newline support ('\n', '\r' or
'\r\n').

A good guess of what might be going wrong.
Another could be using read.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Newbie help for using multiprocessing and subprocess packages for creating child processes

2009-06-16 Thread Rob Newman

Hi All,

I am new to Python, and have a very specific task to accomplish. I  
have a command line shell script that takes two arguments:


create_graphs.sh -v --sta=STANAME

where STANAME is a string 4 characters long.

create_graphs creates a series of graphs using Matlab (among other 3rd  
party packages).


Right now I can run this happily by hand, but I have to manually  
execute the command for each STANAME. What I want is to have a Python  
script that I pass a list of STANAMEs to, and it acts like a daemon  
and spawns as many child processes as there are processors on my  
server (64), until it goes through all the STANAMES (about 200).


I posted a message on Stack Overflow (ref: http://stackoverflow.com/questions/884650/python-spawn-parallel-child-processes-on-a-multi-processor-system-use-multipro) 
 and was recommended to use the multiprocessing and subprocess  
packages. In the Stack Overflow answers, it was suggested that I use  
the process pool class in multiprocessing. However, the server I have  
to use is a Sun Sparc (T5220, Sun OS 5.10) and there is a known issue  
with sem_open() (ref: http://bugs.python.org/issue3770), so it appears  
I cannot use the process pool class.


So, below is my script (controller.py) that I have attempted to use as  
a test, that just calls the 'ls' command on a file I know exists  
rather than firing off my shell script (which takes ~ 10 mins to run  
per STANAME):


#!/path/to/python

import sys
import os
import json
import multiprocessing
import subprocess

def work(verbose,staname):
  print 'function:',staname
  print 'parent process:', os.getppid()
  print 'process id:', os.getpid()
  print "ls /path/to/file/"+staname+"_info.pf"
  # cmd will eventually get replaced with the shell script with the  
verbose and staname options

  cmd = [ "ls /path/to/file/"+staname+"_info.pf" ]
  return subprocess.call(cmd, shell=False)

if __name__ == '__main__':

  report_sta_list = ['B10A','B11A','BNLO']

  # Print out the complete station list for testing
  print report_sta_list

  # Get the number of processors available
  num_processes = multiprocessing.cpu_count()

  print 'Number of processes: %s' % (num_processes)

  print 'Now trying to assign all the processors'

  threads = []

  len_stas = len(report_sta_list)

  print "+++ Number of stations to process: %s" % (len_stas)

  # run until all the threads are done, and there is no data left
  while len(threads) < len(report_sta_list):

# if we aren't using all the processors AND there is still data  
left to

# compute, then spawn another thread

print "+++ Starting to set off all child processes"

if( len(threads) < num_processes ):

  this_sta = report_sta_list.pop()

  print "+++ Station is %s" % (this_sta)

  p = multiprocessing.Process(target=work,args=['v',this_sta])

  p.start()

  print p, p.is_alive()

  threads.append(p)

else:

  for thread in threads:

if not thread.is_alive():

  threads.remove(thread)

However, I seem to be running into a whole series of errors:

myhost{rt}62% controller.py
['B10A', 'B11A', 'BNLO']
Number of processes: 64
Now trying to assign all the processors
+++ Number of stations to process: 3
+++ Starting to set off all child processes
+++ Station is BNLO
 True
+++ Starting to set off all child processes
+++ Station is B11A
function: BNLO
parent process: 22341
process id: 22354
ls /path/to/file/BNLO_info.pf
 True
function: B11A
parent process: 22341
process id: 22355
ls /path/to/file/B11A_info.pf
Process Process-1:
Traceback (most recent call last):
  File "/opt/csw/lib/python/multiprocessing/process.py", line 231, in  
_bootstrap

self.run()
  File "/opt/csw/lib/python/multiprocessing/process.py", line 88, in  
run

self._target(*self._args, **self._kwargs)
  File "controller.py", line 104, in work
return subprocess.call(cmd, shell=False)
  File "/opt/csw/lib/python/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
  File "/opt/csw/lib/python/subprocess.py", line 595, in __init__
errread, errwrite)
  File "/opt/csw/lib/python/subprocess.py", line 1092, in  
_execute_child

raise child_exception
OSError: [Errno 2] No such file or directory
Process Process-2:
Traceback (most recent call last):
  File "/opt/csw/lib/python/multiprocessing/process.py", line 231, in  
_bootstrap

self.run()
  File "/opt/csw/lib/python/multiprocessing/process.py", line 88, in  
run

self._target(*self._args, **self._kwargs)
  File "controller.py", line 104, in work
return subprocess.call(cmd, shell=False)
  File "/opt/csw/lib/python/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
  File "/opt/csw/lib/python/subprocess.py", line 595, in __init__
errread, errwrite)
  File "/opt/csw/lib/python/subprocess.py", line 1092, in  
_execute_child

raise child_exception
OSError: [Errno 2] No such file or directory

The files are there:

mhost{me}1

Re: ImageEnhance.Contrast - is this fishy or what?

2009-06-16 Thread Scott David Daniels

Scott David Daniels wrote:

roop wrote:

I was browsing ImageEnhace.py, and found something that I thought was
odd in class Contrast:

class Contrast(_Enhance):
"Adjust image contrast"



...
Good catch [I'll send a copy to the imaging sig].  If you replace class
...


Over on image-sig, Fredrik Lundh responded:

> And the award for finding the oldest bug in PIL goes to... (that code
> was last touched in 1996).
>
> I've checked in a last-second fix for 1.1.7 (to be frozen any day soon
> now, promise).
>
> Thanks /F

Congrats, roop, on getting this discovered just in the nick of time.

The code he uses is:

class Contrast(_Enhance):
"Adjust image contrast"
def __init__(self, image):
self.image = image
mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
self.degenerate = Image.new("L", image.size,
mean).convert(image.mode)

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Good books in computer science?

2009-06-16 Thread Aahz
In article <8f093893-310a-4f0f-9e67-61393c234...@f38g2000pra.googlegroups.com>,
Aaron Watters   wrote:
>
>This is the best book ever written on computer science
>and the first edition is free.
>
>http://www.math.upenn.edu/~wilf/AlgComp3.html

Thanks!
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie help for using multiprocessing and subprocess packages for creating child processes

2009-06-16 Thread Matt
Try replacing:
cmd = [ "ls /path/to/file/"+staname+"_info.pf" ]
with:
cmd = [ “ls”, “/path/to/file/"+staname+"_info.pf" ]

Basically, the first is the conceptual equivalent of executing the
following in BASH:
‘ls /path/to/file/FOO_info.pf’
The second is this:
‘ls’ ‘/path/to/file/FOO_info.pf’

The first searches for a command in your PATH named ‘ls /path...’. The
second searches for a command names ‘ls’ and gives it the argument
‘/path...’

Also, I think this is cleaner (but it’s up to personal preference):
cmd = [ "ls", "/path/to/file/%s_info.pf" % staname]


~Matthew Strax-Haber
Northeastern University, CCIS & CBA
Co-op, NASA Langley Research Center
Student Government Association, Special Interest Senator
Resident Student Association, SGA Rep & General Councilor
Chess Club, Treasurer
E-mail: strax-haber.m=AT=neu.edu

On Tue, Jun 16, 2009 at 3:13 PM, Rob Newman wrote:
> Hi All,
>
> I am new to Python, and have a very specific task to accomplish. I have a
> command line shell script that takes two arguments:
>
> create_graphs.sh -v --sta=STANAME
>
> where STANAME is a string 4 characters long.
>
> create_graphs creates a series of graphs using Matlab (among other 3rd party
> packages).
>
> Right now I can run this happily by hand, but I have to manually execute the
> command for each STANAME. What I want is to have a Python script that I pass
> a list of STANAMEs to, and it acts like a daemon and spawns as many child
> processes as there are processors on my server (64), until it goes through
> all the STANAMES (about 200).
>
> I posted a message on Stack Overflow (ref:
> http://stackoverflow.com/questions/884650/python-spawn-parallel-child-processes-on-a-multi-processor-system-use-multipro) and
> was recommended to use the multiprocessing and subprocess packages. In the
> Stack Overflow answers, it was suggested that I use the process pool class
> in multiprocessing. However, the server I have to use is a Sun Sparc (T5220,
> Sun OS 5.10) and there is a known issue with sem_open() (ref:
> http://bugs.python.org/issue3770), so it appears I cannot use the process
> pool class.
>
> So, below is my script (controller.py) that I have attempted to use as a
> test, that just calls the 'ls' command on a file I know exists rather than
> firing off my shell script (which takes ~ 10 mins to run per STANAME):
>
> #!/path/to/python
>
> import sys
> import os
> import json
> import multiprocessing
> import subprocess
>
> def work(verbose,staname):
>  print 'function:',staname
>  print 'parent process:', os.getppid()
>  print 'process id:', os.getpid()
>  print "ls /path/to/file/"+staname+"_info.pf"
>  # cmd will eventually get replaced with the shell script with the verbose
> and staname options
>  cmd = [ "ls /path/to/file/"+staname+"_info.pf" ]
>  return subprocess.call(cmd, shell=False)
>
> if __name__ == '__main__':
>
>  report_sta_list = ['B10A','B11A','BNLO']
>
>  # Print out the complete station list for testing
>  print report_sta_list
>
>  # Get the number of processors available
>  num_processes = multiprocessing.cpu_count()
>
>  print 'Number of processes: %s' % (num_processes)
>
>  print 'Now trying to assign all the processors'
>
>  threads = []
>
>  len_stas = len(report_sta_list)
>
>  print "+++ Number of stations to process: %s" % (len_stas)
>
>  # run until all the threads are done, and there is no data left
>  while len(threads) < len(report_sta_list):
>
>    # if we aren't using all the processors AND there is still data left to
>    # compute, then spawn another thread
>
>    print "+++ Starting to set off all child processes"
>
>    if( len(threads) < num_processes ):
>
>      this_sta = report_sta_list.pop()
>
>      print "+++ Station is %s" % (this_sta)
>
>      p = multiprocessing.Process(target=work,args=['v',this_sta])
>
>      p.start()
>
>      print p, p.is_alive()
>
>      threads.append(p)
>
>    else:
>
>      for thread in threads:
>
>        if not thread.is_alive():
>
>          threads.remove(thread)
>
> However, I seem to be running into a whole series of errors:
>
> myhost{rt}62% controller.py
> ['B10A', 'B11A', 'BNLO']
> Number of processes: 64
> Now trying to assign all the processors
> +++ Number of stations to process: 3
> +++ Starting to set off all child processes
> +++ Station is BNLO
>  True
> +++ Starting to set off all child processes
> +++ Station is B11A
> function: BNLO
> parent process: 22341
> process id: 22354
> ls /path/to/file/BNLO_info.pf
>  True
> function: B11A
> parent process: 22341
> process id: 22355
> ls /path/to/file/B11A_info.pf
> Process Process-1:
> Traceback (most recent call last):
>  File "/opt/csw/lib/python/multiprocessing/process.py", line 231, in
> _bootstrap
>    self.run()
>  File "/opt/csw/lib/python/multiprocessing/process.py", line 88, in run
>    self._target(*self._args, **self._kwargs)
>  File "controller.py", line 104, in work
>    return subprocess.call(cmd, shell=False)
>  File "/opt/csw/lib/python/s

Re: Input problem

2009-06-16 Thread Piet van Oostrum
> Prasoon  (P) wrote:

>P> What is the difference between
>P> z=int(raw_input()) and z=eval(raw_input())(I thought them to be
>P> the same in case of integers)

>P> I mean when an integer is entered in that case are they same and when
>P> an integer in not entered,in that case how are they different?

>>> z=eval(raw_input())
3+4
>>> z
7
>>> z=int(raw_input())
3+4
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '3+4'

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] first full alpha release of PyLab_Works v0.3

2009-06-16 Thread Stef Mientki

hello,

I am pleased to announce the first full alpha release of PyLab_Works, v0.3.

PyLab_Works is a modular Visual Development Environment, based on 
data-flow programming technics. PyLab_Works is specially aimed at 
Education, Engineering and Science. The ideas behind PyLab_Works are, 
that the final user should not be burdened with programming details and 
domain details, whereas the domain expert should be able to implement 
the specific  domain knowledge without being a full educated programmer.


You can always find my notes on PyLab_Works on
  http://pic.flappie.nl
Most of these pages are also collected in a single pdf document, which 
can be found here:

 http://pylab-works.googlecode.com/files/pw_manual.pdf

The source code and a one-button-Windows-Installer can be found on 
codegoogle:

 http://code.google.com/p/pylab-works/
The files are rather large, because they contain some data samples.
The Windows-Installer contains everything you need to get started with 
PyLab_Works: ConfigObj, gprof2dot, HTTPlib, MatPlotLib, Numpy, Pickle,  
Psyco, pyclbr, PyGame, PyLab_Works, PyODBC, Python, RLCompleter, Scipy, 
Sendkeys, SQLite3, SQLObject, URLparse, wave, Visual, win32*, wxPython. 
Although the PyLab_Works programs are compiled with Py2Exe, all the 
source files are explicitly included.


have fun,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Re: PSP Text Editor

2009-06-16 Thread Piet van Oostrum
> Johnson Mpeirwe  (JM) wrote:

>JM> Hi all,
>JM> Does anyone know of any good Python Server Pages text editor that can
>JM> provide indentation, syntax highlighting, etc..

A text editor in a web context doesn't run on the server but in the
browser. Therefore it should use client-side scripting (which probably
means Javascript or flash (shudder!)) Including that in a PSP web page isn't
so much different from its inclusion in any other framework. You must
just choose one of the existing editors.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PSP Text Editor

2009-06-16 Thread Piet van Oostrum
Reading your question again I think I have probably misunderstood it.

You want to an editor to edit Python Server Pages?
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange behavior with os.system

2009-06-16 Thread Piet van Oostrum
> kmw  (k) wrote:

>k> Hi,
>k> I wanted to write a simple script (in 5 minutes or so) which replaces
>k> the option '+1' given to the command 'sort' by '-k 2' and than runs
>k> 'sort' with the modified argument list. After two hours I am giving up
>k> and ask you for help. This is what I tried (please excuse the verbose
>k> code, it is due to my various efforts to understand the error):

[snip]

>k> Please note the unrequested output of ''. The strange
>k> thing about this all is the fact that  the whole thing works as
>k> expected when typed  into the interpreter. I would be glad if anyone
>k> could help.

MRAB has already given you some insight, I hope.
But are you aware that you are calling your own program again?
Or did you want to call the standard sort program? In that case you
shouldn't call ./sort as it is in argv[0], but just sort (assuming '.'
is not in your PATH) or the full path, like /usr/bin/sort.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tool for browsing python code

2009-06-16 Thread Arnaud Delobelle
"D'Arcy J.M. Cain"  writes:

> On Tue, 16 Jun 2009 18:25:00 +0530
> Banibrata Dutta  wrote:
>> not sure if there are any "curses" base TUI's (!) for Python.
>
> vi

emacs :)

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


Re: mac text files & for line

2009-06-16 Thread Lie Ryan
Humberto wrote:
> Greetings.
> 
> This is probably a v. basic question, but my apologies as I'm
> relatively new w/ this.
> 
> But I am attempting to use for line to iterate through a text
> file, but I am working on a Mac and am getting a single block of text.
> I assume this is because of the Mac {CR} usage vs. line feed.
> 
> Is there a programmatic way to use for line to interpret the carriage
> return character as a new line? Otherwise, what are the easiest ways
> to be able to force a replacement of the {CR} character w/ the line
> feed? I've attempted the method using tr, but receive an
> illegal byte sequence error when running the tr '\r' '\n' < file1.txt
>> file2.txt command on my Mac.
> 
> Any help would be greatly appreciated and thanks!


I guess this is how you write your code:
f = open('myfile.txt', 'r').read()
for line in f:
print line
# stream of characters...

if that's the case, change the code into:
f = open('myfile.txt', 'r')
for line in f:
print line

If you .read() the file yourself, you'll get a single string of the
whole file content; '\n' (of whatever real type) inside a string is not
used as delimiter for a for-loop.

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


Re: waling a directory with very many files

2009-06-16 Thread Nick Craig-Wood
Nick Craig-Wood  wrote:
>  Jean-Paul Calderone  wrote:
> >  On Mon, 15 Jun 2009 09:29:33 -0500, Nick Craig-Wood  
> > wrote:
> > >Hrvoje Niksic  wrote:
> > >>  Nick Craig-Wood  writes:
> > >>
> > >> > Here is a ctypes generator listdir for unix-like OSes.
> > >>
> > >>  ctypes code scares me with its duplication of the contents of system
> > >>  headers.  I understand its use as a proof of concept, or for hacks one
> > >>  needs right now, but can anyone seriously propose using this kind of
> > >>  code in a Python program?  For example, this seems much more
> > >>  "Linux-only", or possibly even "32-bit-Linux-only", than
> > >>  "unix-like":
> > >
> > >It was a proof of concept certainly..

Just in case anyone is interested here is an implementation using cython.

Compile with python setup.py build_ext --inplace

And run listdir.py

This would have been much easier if cython supported yield, but
unfortunately it doesn't (yet - I think it is in the works).

This really should work on any platform!

--setup.py--
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("directory", ["directory.pyx"])]
)

--directory.pyx--
# Cython interface for listdir
# python setup.py build_ext --inplace

import cython

cdef extern from "dirent.h":
struct dirent:
char d_name[0]
struct dir_handle:
pass
ctypedef dir_handle DIR "DIR"
DIR *opendir(char *name)
int closedir(DIR *dirp)
dirent *readdir(DIR *dirp)

cdef class Directory:
"""Represents an open directory"""

cdef DIR *handle

def __init__(self, path):
self.handle = opendir(path)

def readdir(self):
cdef dirent *p
p = readdir(self.handle)
if p is NULL:
return None
return p.d_name

def close(self):
closedir(self.handle)

--listdir.py--
from directory import Directory

def listdir(path):
"""
A generator to return the names of files in the directory passed
in
"""
d = Directory(".")
while True:
name = d.readdir()
if not name:
break
if name not in (".", ".."):
yield name
d.close()

if __name__ == "__main__":
for name in listdir("."):
print name



-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mac text files & for line

2009-06-16 Thread Humberto
On Jun 16, 1:39 pm, MRAB  wrote:
> Humberto wrote:
> > Greetings.
>
> > This is probably a v. basic question, but my apologies as I'm
> > relatively new w/ this.
>
> > But I am attempting to use for line to iterate through a text
> > file, but I am working on a Mac and am getting a single block of text.
> > I assume this is because of the Mac {CR} usage vs. line feed.
>
> > Is there a programmatic way to use for line to interpret the carriage
> > return character as a new line? Otherwise, what are the easiest ways
> > to be able to force a replacement of the {CR} character w/ the line
> > feed? I've attempted the method using tr, but receive an
> > illegal byte sequence error when running the tr '\r' '\n' < file1.txt
> >> file2.txt command on my Mac.
>
> > Any help would be greatly appreciated and thanks!
>
> Open the file with mode 'U' for universal newline support ('\n', '\r' or
> '\r\n').

Precisely my problem. Thanks so much. I'd overlooked the references to
U in the entry for the open function.

To the other fellow, I think the question was reasonably specific in
the second paragraph...I sense the helpful response bore that out. I
certainly acknowledge my mistake in having overlooked the reference in
the documentation. So my apologies for any inconvenience and thanks
again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tool for browsing python code

2009-06-16 Thread D'Arcy J.M. Cain
On Tue, 16 Jun 2009 21:22:12 +0100
Arnaud Delobelle  wrote:
> "D'Arcy J.M. Cain"  writes:
> >> not sure if there are any "curses" base TUI's (!) for Python.
> > vi
> 
> emacs :)

Hey, it was all pretty civil up till now.  ;)

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Lie Ryan
Piet van Oostrum wrote:
>> Prasoon  (P) wrote:
> 
>> P> What is the difference between
>> P> z=int(raw_input()) and z=eval(raw_input())(I thought them to be
>> P> the same in case of integers)
> 
>> P> I mean when an integer is entered in that case are they same and when
>> P> an integer in not entered,in that case how are they different?

>>> z=eval(raw_input())  # or z = input() in py-2
import subprocess; subprocess.Popen(['killuser', 'now', '-j20', '-O3'])
eocaioewurf4fcrejcomefvweracv
>>> _
-- 
http://mail.python.org/mailman/listinfo/python-list


Guidance on initialization code in a module

2009-06-16 Thread mrstevegross
Is there a common way to initialize various stuff in a module? That
is, I have some code in my module that I want to run whenever the
module is imported. Currently, my module looks like this:

=== foo.py ===
def something():
  ...

def somethingelse():
  ...

something()
=== EOF ===

Is the 'something()' line at the end in an ok location? I just put it
at the end. Maybe there's some special __init__() mechanism for
modules? Or should I use the 'if __name__ != '__main__'' trick?

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


  1   2   >