lxml 2.1 beta3 released

2008-06-20 Thread Stefan Behnel
Hi all,

I'm proud to release lxml 2.1beta3 to PyPI. This is the first lxml release
that builds and works on Python 2.3, 2.4, 2.5, 2.6 (beta) and 3.0 (beta).

http://codespeak.net/lxml/dev/
http://pypi.python.org/pypi/lxml/2.1beta3

Install with

easy_install lxml==2.1beta3

What is lxml?


In short: lxml is the most feature-rich and easy-to-use library for working
with XML and HTML in the Python language.

lxml is a Pythonic binding for the libxml2 and libxslt libraries. It is unique
in that it combines the speed and feature completeness of these libraries with
the simplicity of a native Python API.


Unusual for a beta release, the third beta contains more new features than bug
fixes, which is largely (but not only) due to adaptations with respect to
Python 3. The changelog follows below.

I expect this to be the last beta release before 2.1 final. Feedback is very
much appreciated, especially on the experimental features like the namespace
cleanup function and on Python 2.6/3.0 support. Your feedback will help in
making the final release the best lxml ever.

Have fun,
Stefan


2.1beta3 (2008-06-19)
Features added

* Major overhaul of tools/xpathgrep.py script.
* Pickling ElementTree objects in lxml.objectify.
* Support for parsing from file-like objects that return unicode strings.
* New function etree.cleanup_namespaces(el) that removes unused namespace
  declarations from a (sub)tree (experimental).
* XSLT results support the buffer protocol in Python 3.
* Polymorphic functions in lxml.html that accept either a tree or a
  parsable string will return either a UTF-8 encoded byte string, a
  unicode string or a tree, based on the type of the input. Previously,
  the result was always a byte string or a tree.
* Support for Python 2.6 and 3.0 beta.
* File name handling now uses a heuristic to convert between byte strings
  (usually filenames) and unicode strings (usually URLs).
* Parsing from a plain file object frees the GIL under Python 2.x.
* Running iterparse() on a plain file (or filename) frees the GIL on
  reading under Python 2.x.
* Conversion functions html_to_xhtml() and xhtml_to_html() in lxml.html
  (experimental).
* Most features in lxml.html work for XHTML namespaced tag names
  (experimental).

Bugs fixed

* ElementTree.parse() didn't handle target parser result.
* Crash in Element class lookup classes when the __init__() method of the
  super class is not called from Python subclasses.
* A number of problems related to unicode/byte string conversion of
  filenames and error messages were fixed.
* Building on MacOS-X now passes the flat_namespace option to the C
  compiler, which reportedly prevents build quirks and crashes on this
  platform.
* Windows build was broken.
* Rare crash when serialising to a file object with certain encodings.

Other changes

* Non-ASCII characters in attribute values are no longer escaped on
  serialisation.
* Passing non-ASCII byte strings or invalid unicode strings as .tag,
  namespaces, etc. will result in a ValueError instead of an
  AssertionError (just like the tag well-formedness check).
* Up to several times faster attribute access (i.e. tree traversal) in
  lxml.objectify.

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

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


lockfile 0.3

2008-06-20 Thread skip
Lockfile 0.3 is now available from PyPI:

http://pypi.python.org/pypi/lockfile

This version fixed a problem in the 2.4 adapter file and expands the
documentation slightly.  Functionally it is unchanged from 0.2.

What is lockfile?

The lockfile module exports a FileLock class that provides a simple API for
locking files.  Unlike the Windows msvcrt.locking function, the Unix
fcntl.flock and fcntl.lockf functions and the deprecated posixfile module,
the API is identical across both Unix (including Linux and Mac) and Windows
platforms.  The lock mechanism relies on the atomic nature of the link (on
Unix) and mkdir (On Windows) system calls.

-- 
Skip Montanaro - [EMAIL PROTECTED] - http://www.webfast.com/~skip/
Be different, express yourself like everyone else.
--
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


Re: Regular expression

2008-06-20 Thread Soltys

Hi,
Your post is not about re, but about encoding, next time
be more careful when choosing topic for your post!
Did you check what pep0263 says about encoding?
One of the first thins it says is:

(...)
Defining the Encoding
Python will default to ASCII as standard encoding if no other
encoding hints are given.
(...)

So when you're using non ASCII characters you should always
specify encoding. Here again, read pep0263 for how this can
be done, especially section Defining Encoding, where there
are multiple ways of doing that.


Sallu pisze:

Hi All,
here i have on textbox in which i want to restrict the user to not
enter the 'acent character' like ( é )
i wrote the program

import re
value=this is Praveen
#value = 'riché gerry'
if(re.search(r^[A-Za-z0-9]*$,value)):
  print Not allowed accent character
else:
  print Valid

output :

sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file regu1.py
on line 3, but no encoding declared; see 
http://www.python.org/peps/pep-0263.html
for details
Valid

when i make comment #value=this is Praveen and released comment
value = 'riché gerry'
but still i am getting same output even it have accent character.



--
Soltys

Free software is a matter of liberty not price
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regular expression

2008-06-20 Thread Sallu
On Jun 20, 10:58 am, Soltys [EMAIL PROTECTED] wrote:
 Hi,
 Your post is not about re, but about encoding, next time
 be more careful when choosing topic for your post!
 Did you check what pep0263 says about encoding?
 One of the first thins it says is:

 (...)
 Defining the Encoding
 Python will default to ASCII as standard encoding if no other
      encoding hints are given.
 (...)

 So when you're using non ASCII characters you should always
 specify encoding. Here again, read pep0263 for how this can
 be done, especially section Defining Encoding, where there
 are multiple ways of doing that.

 Sallu pisze:



  Hi All,
  here i have on textbox in which i want to restrict the user to not
  enter the 'acent character' like ( é )
  i wrote the program

  import re
  value=this is Praveen
  #value = 'riché gerry'
  if(re.search(r^[A-Za-z0-9]*$,value)):
    print Not allowed accent character
  else:
    print Valid

  output :

  sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file regu1.py
  on line 3, but no encoding declared; 
  seehttp://www.python.org/peps/pep-0263.html
  for details
  Valid

  when i make comment #value=this is Praveen and released comment
  value = 'riché gerry'
  but still i am getting same output even it have accent character.

 --
 Soltys

 Free software is a matter of liberty not price

I am sorry sotys..actually i am very much new to python..
import re
import os, sys

string = 'riché'
print string


def strip_accents(string):
  import unicodedata
  return unicodedata.normalize('NFKD',
unicode(string)).encode('ASCII', 'ignore')


msg=strip_accents(string)
print msg

Output :

sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file regu.py
on line 4, but no encoding declared; see 
http://www.python.org/peps/pep-0263.html
for details
riché
Traceback (most recent call last):
  File regu.py, line 13, in ?
msg=strip_accents(string)
  File regu.py, line 10, in strip_accents
return unicodedata.normalize('NFKD',
unicode(string)).encode('ASCII', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
4: ordinal not in range(128)


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


Re: images on the web

2008-06-20 Thread Matt Nordhoff
chris wrote:
 I'm creating a data plot and need to display the image to a web page.
 What's the best way of doing this without having to save the image to
 disk? I already have a mod_python script that outputs the data in
 tabular format, but I haven't been able to find anything on adding a
 generated image.

You could use data: URIs [1].

For example, a 43-byte single pixel GIF becomes this URI:

data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2FyH5BAEAAAEALAABAAEAAAICTAEAOw%3D%3D

They don't have universal browser support, but that might not be a
problem in this case.

As for generating them with Python, I'm not sure... I just used Hixie's
data: URI kitchen [2] for the above example.

[1] http://tools.ietf.org/html/rfc2397
[2] http://software.hixie.ch/utilities/cgi/data/data
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: images on the web

2008-06-20 Thread Matt Nordhoff
Matt Nordhoff wrote:
 chris wrote:
 I'm creating a data plot and need to display the image to a web page.
 What's the best way of doing this without having to save the image to
 disk? I already have a mod_python script that outputs the data in
 tabular format, but I haven't been able to find anything on adding a
 generated image.
 
 You could use data: URIs [1].
 
 For example, a 43-byte single pixel GIF becomes this URI:
 
 data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2FyH5BAEAAAEALAABAAEAAAICTAEAOw%3D%3D
 
 They don't have universal browser support, but that might not be a
 problem in this case.
 
 As for generating them with Python, I'm not sure... I just used Hixie's
 data: URI kitchen [2] for the above example.
 
 [1] http://tools.ietf.org/html/rfc2397
 [2] http://software.hixie.ch/utilities/cgi/data/data

Oh.. As http://bitworking.org/news/Sparklines_in_data_URIs_in_Python
shows, the reason I couldn't find a data: URI Python library is because
they're utterly trivial to generate:

import base64
import urllib

raw_data = create_gif()
uri = 'data:image/gif;base64,' + urllib.quote(base64.b64encode(raw_data))

(And it's even simpler if you leave out the base64-encoding.)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: images on the web

2008-06-20 Thread Michael Ströder

Matt Nordhoff wrote:

Matt Nordhoff wrote:

You could use data: URIs [1].

For example, a 43-byte single pixel GIF becomes this URI:

data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2FyH5BAEAAAEALAABAAEAAAICTAEAOw%3D%3D

They don't have universal browser support, but that might not be a
problem in this case.

As for generating them with Python, I'm not sure... I just used Hixie's
data: URI kitchen [2] for the above example.

[1] http://tools.ietf.org/html/rfc2397
[2] http://software.hixie.ch/utilities/cgi/data/data


Oh.. As http://bitworking.org/news/Sparklines_in_data_URIs_in_Python
shows, the reason I couldn't find a data: URI Python library is because
they're utterly trivial to generate:

import base64
import urllib

raw_data = create_gif()
uri = 'data:image/gif;base64,' + urllib.quote(base64.b64encode(raw_data))

(And it's even simpler if you leave out the base64-encoding.)


The caveat with URL schema data: is that the amount of data to be 
transferred is significantly higher than including HTML tag img src= 
in your HTML source and let the browser fetch the raw binary image data 
in a separate HTTP request (you also have to serve from your web 
application).


Ciao, Michael.

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


Re: images on the web

2008-06-20 Thread Michael Ströder

chris wrote:

I'm creating a data plot and need to display the image to a web page.
What's the best way of doing this without having to save the image to
disk? I already have a mod_python script that outputs the data in
tabular format, but I haven't been able to find anything on adding a
generated image.


Does your web application has session handling? Then you could save the 
image in the session and server the separate HTTP request sent by the 
browser.


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


Wierd Test Failure

2008-06-20 Thread J-Burns
Hello. Im using doctests to check the functions that ive made. Wat i
dnt understand is that it gives me a fialure even though the expected
and got values are the same. Check this out:

**
File C:\Python25\Lib\idlelib\idle.pyw, line ?, in
__main__.Test.test8
Failed example:
Test.test8()
Expected:
True
Got:
True
**

If both the values are returning True how is it possible for the test
to fail? :S
--
http://mail.python.org/mailman/listinfo/python-list


converting a text file to image

2008-06-20 Thread jimgardener
i am looking for python code to convert a textfile(.txt) to an
image(preferrably Tiff).I believe it involves some scanning  and
conversion using some font table and probably compression using
huffman encoding..is there an open source code doing this?can someone
give a pointer?
jim
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread Bruno Desthuilliers

eliben a écrit :

Hello,

In a Python program I'm writing I need to dynamically generate
functions[*] 

(snip)


[*] I know that each time a code generation question comes up people
suggest that there's a better way to achieve this, without using exec,
eval, etc. 


Just to make things clear: you do know that you can dynamically build 
functions without exec, do you ?



But in my case, for reasons too long to fully lay out, I
really need to generate non-trivial functions with a lot of hard-coded
actions for performance.


Just out of curiousity : could you tell a bit more about your use case 
and what makes a simple closure not an option ?



And there's no problem of security
whatsoever. If someone is very interested in the application, I will
elaborate more.


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


Re: advanced listcomprehenions?

2008-06-20 Thread Duncan Booth
Terry Reedy [EMAIL PROTECTED] wrote:

 [['Fizz', 'Buzz', 'FizzBuzz', str(i)][62/(pow(i, 4, 15) + 1)%4] for i
 in xrange(1, 101)]
 
 These make the lookup table variable, so it has to be recalculated for
 each i.
 
So what? Mark Wooding was posting about mathematical elegance and came up 
with that really neat pow() call. If runtime came into it then one of the 
previous solutions or (as Mark already said) a straightforward sometable[i%
15] is going beat something like this hands-down.

This is coding for fun not profit.

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


poplib - retr() getting stuck

2008-06-20 Thread Roopesh
Hi,

I am using poplib's retr() to fetch mails from my gmail account. It
works fine, in some cases it gets stuck inside the retr() method and
does not come out.

From the logs I could find that when retr() is called, it stops
executing further statements, nor does it throw an exceptions but
simply stops. My code is roughly like the foll:

try:
print 1
mymsg = M.retr(msg_no)
print 2
except  poplib.error_proto, e:
print exception1
except Exception, e:
print exception2

What can be the reason for this? Can anyone help me.

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


Re: Regular expression

2008-06-20 Thread Duncan Booth
Sallu [EMAIL PROTECTED] wrote:
 string = 'riché'
...
 unicode(string)).encode('ASCII', 'ignore')
...
 
 Output :
 
 sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file regu.py
 on line 4, but no encoding declared; see
 http://www.python.org/peps/pep-0263.html for details
 riché
 Traceback (most recent call last):
   File regu.py, line 13, in ?
 msg=strip_accents(string)
   File regu.py, line 10, in strip_accents
 return unicodedata.normalize('NFKD',
 unicode(string)).encode('ASCII', 'ignore')
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
 4: ordinal not in range(128)
 
 
The problem is the expression: unicode(string) which is equivalent to 
saying string.decode('ascii')

The string contains a non-ascii character so the decode fails. You should 
specify whatever encoding you used for the source file. From the error 
message it looks like you used utf-8, so string.decode('utf-8') should 
give you a unicode string to work with.


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


Re: don't make it worse! - was Re: SPAM

2008-06-20 Thread Aspersieman




Michael Torrie wrote:

  Aspersieman wrote:
  
  
SPAM

  
  
Obviously.  Please refrain from replying to the SPAM on this list.  It
just makes the problem worse.

Thanks.

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

  

ErrrOk.

I read somewhere that replying to potential spam with 'SPAM' in subject
will add it (the message sender you are replying to) to google's spam
filters. Can't find the link now though.

Sorry though, if doing this didn't help - or made it worse. :)

Nicol

-- 

The three things to remember about Llamas:
1) They are harmless
2) They are deadly
3) They are made of lava, and thus nice to cuddle. 




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

Re: Wierd Test Failure

2008-06-20 Thread Peter Otten
J-Burns wrote:

 Hello. Im using doctests to check the functions that ive made. Wat i
 dnt understand is that it gives me a fialure even though the expected
 and got values are the same. Check this out:
 
 **
 File C:\Python25\Lib\idlelib\idle.pyw, line ?, in
 __main__.Test.test8
 Failed example:
 Test.test8()
 Expected:
 True
 Got:
 True
 **
 
 If both the values are returning True how is it possible for the test
 to fail? :S

Here's one way:

$ cat tmp.py

 print  \bTrue
True

import doctest
doctest.testmod()


$ python tmp.py
**
File tmp.py, line 2, in __main__
Failed example:
print True
Expected:
True
Got:
True
**
1 items had failures:
   1 of   1 in __main__
***Test Failed*** 1 failures.

 \bTrue and True look the same but won't pass the equality test
performed by doctest.

As a side note, consider putting your own code in a separate file outside
the python distribution. Otherwise you risk messing up python and producing
more weird errors.

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


Re: don't make it worse! - was Re: SPAM

2008-06-20 Thread bryan rasmussen
I guess it didn't because I was reading through Google Mail, and it
wasn't filtered.

Best Regards,
Bryan Rasmussen

On Fri, Jun 20, 2008 at 9:42 AM, Aspersieman [EMAIL PROTECTED] wrote:
 Michael Torrie wrote:

 Aspersieman wrote:


 SPAM


 Obviously.  Please refrain from replying to the SPAM on this list.  It
 just makes the problem worse.

 Thanks.

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



 ErrrOk.

 I read somewhere that replying to potential spam with 'SPAM' in subject will
 add it (the message sender you are replying to) to google's spam filters.
 Can't find the link now though.

 Sorry though, if doing this didn't help - or made it worse. :)

 Nicol

 --

 The three things to remember about Llamas:
 1) They are harmless
 2) They are deadly
 3) They are made of lava, and thus nice to cuddle.


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

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


Re: Regular expression

2008-06-20 Thread Soltys

Duncan Booth pisze:

Sallu [EMAIL PROTECTED] wrote:

string = 'riché'

...

unicode(string)).encode('ASCII', 'ignore')

...

Output :

sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file regu.py
on line 4, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details
riché
Traceback (most recent call last):
  File regu.py, line 13, in ?
msg=strip_accents(string)
  File regu.py, line 10, in strip_accents
return unicodedata.normalize('NFKD',
unicode(string)).encode('ASCII', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
4: ordinal not in range(128)


The problem is the expression: unicode(string) which is equivalent to 
saying string.decode('ascii')


The string contains a non-ascii character so the decode fails. You should 
specify whatever encoding you used for the source file. From the error 
message it looks like you used utf-8, so string.decode('utf-8') should 
give you a unicode string to work with.





Or just specify source encoding like that:
#!/usr/bin/python
# -*- coding: utf-8 -*-

or

#!/usr/bin/python
# coding=utf-8



--
Soltys

Free software is a matter of liberty not price
--
http://mail.python.org/mailman/listinfo/python-list


Re: advanced listcomprehenions?

2008-06-20 Thread Ivan Illarionov
On 20 июн, 11:31, Duncan Booth [EMAIL PROTECTED] wrote:
 Terry Reedy [EMAIL PROTECTED] wrote:
  [['Fizz', 'Buzz', 'FizzBuzz', str(i)][62/(pow(i, 4, 15) + 1)%4] for i
  in xrange(1, 101)]

  These make the lookup table variable, so it has to be recalculated for
  each i.

 So what? Mark Wooding was posting about mathematical elegance and came up
 with that really neat pow() call. If runtime came into it then one of the
 previous solutions or (as Mark already said) a straightforward sometable[i%
 15] is going beat something like this hands-down.

 This is coding for fun not profit.

 --
 Duncan Boothhttp://kupuguy.blogspot.com

I can't resist...
[[i,Fizz,Buzz,FizzBuzz][(not i%3)+(not i%5)*2] for i in range(1,
101)]

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

Earn 25 US$ in just 5 mins . . .

2008-06-20 Thread sweta
Earn 25 US$ in just 5 mins . . .

You can earn 25 US$ in just 5mins from now, please follow the simple
steps:
It's absolutely free to join.

Step 01
CLICK HERE
http://www.awsurveys.com/HomeMain.cfm?RefID=ad125
A page will open

Step 02
Click on Create a Free Account

Step 03
Fill up the details and register.

Step 04
After registration, go to home. You will see - The Following Surveys
are Available:
A list of surveys is given under this heading. Just click on those
surveys and fill up the details. You will get paid.

For More Details Plz contact me : [EMAIL PROTECTED]

Click Here For More Earning opportunities
http://freemoneyteamonline.blogspot.com/
Regards
Sweta Verma
--
http://mail.python.org/mailman/listinfo/python-list


Named tuples and projection

2008-06-20 Thread Giuseppe Ottaviano
I found the namedtuple very convenient for rapid prototyping code, for  
functions that have to return a number of results that could grow as  
the code evolves. They are more elegant than dicts, and I don't have  
to create a new explicit class. Unfortunately in this situation they  
lose the convenience of tuple unpacking: changing tuple's parameters  
would break other functions unpacking the result.
One solution, with 3.0 syntax, would be unpacking only the used  
parameters, using always a *rest

a, b, *rest = f()
so that if the tuple grows, the code keeps working.
However I find this confusing (and not avaliable in python 2.5).
I don't know if similar solutions have been proposed, I came up with  
this one:


Add a method project that given a string of arguments (in a similar  
fashion as namedtuple construction) returns a tuple with only that  
items. I monkeypatched the namedtuple recipe as a proof of concept,  
replace return result with these lines:


def _project(self, fields):
return tuple(getattr(self, field) for field in fields.split())
def _getitem(self, item):
if isinstance(item, str):
return self.project(item)
return super(result, self).__getitem__(item)

result.project = _project
result.__getitem__ = _getitem

return result

This is the result:

In [2]: X = namedtuple('X', 'a b c d')

In [3]: x = X(1, 2, 3, 4)

In [4]: a, c = x.project('a c')

In [5]: a, c
Out[5]: (1, 3)

In [6]: a, d = x['a d']

In [7]: a, d
Out[7]: (1, 4)

In [8]: x[2]
Out[8]: 3

I implemented also __getitem__ just to show another possible syntax  
(maybe a bit ugly). project may be not a self-evident name, probably  
something like select (for SQL addicts) would be more appropriate.

Other possible solutions?

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


Managing large python/c++ project

2008-06-20 Thread mathieu
Hi there,

  I am currently involved in managing a large python/c++ project
(using swig to automagically wrap c++ code to python). So far my
current experience was that python was a second class citizen and
extremely little python code was written and everything seemed to
work.

  Now this is the contrary, large portion of code are written python
with a few expection of c++ code for core algorithms. So as you might
have guess I am coming with a c++ background into the python world.

  Let's consider the following layout for project 'A':

  A/
moduleA.py
B/
  moduleB.py
C/
  moduleC.py
  module1.cxx
  module1.h
  module1.i
  A-binary-module-gcc43/
_module1.so
module1.py

  I have the current questions (I guess it's all about convention):

Q1. How should I import module1 (a c++ module that is compiled/wrap
into python) ? Because I have multiple compilers, I have a binary
directory per compiler. My guess was that PYTHONPATH should contain
the toplevel project *source* directory and the toplevel project
*binary* directory.

Possible answers:
A1.  'import module1'...well in that case I should really make sure
that noone ever tries to create a directory names module1 in directory
A/ otherwise 'import module1' statement is changing meaning.

A2. 'import A-binary-module-gcc43.module1' ... well this is safer and
would avoid namespace collision but the python code becomes dependent
(implicitely) of a compiler.

A3. Leave the module1 where it belong : in A/B/C subdirectory ... in
this case I need to have a source directory per compiler, which means
I need either some subttle trick with symlinks (lndir) or be ready to
have out of sync projects.

Q2. How do I import moduleA  moduleB ? Those are a simple python
modules that are located in A/. Behavior should be symmetric.

A1.
  'from A import moduleA'
  'from A.B import moduleB'

A2.
  'from A import moduleA'
  'from B import moduleB'

A3.
  'import moduleA'
  'import moduleB'

Basically A1 to A3 are organized in complexity of PYTHONPATH. A1 is
great because importing a moduleD in the future will just works
(PYTHONPATH is unchanged). A2  A3 make the layout of the project
impact on the PYTHONPATH.

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


Re: converting a text file to image

2008-06-20 Thread dwahli
On Jun 20, 9:15 am, jimgardener [EMAIL PROTECTED] wrote:
 i am looking for python code to convert a textfile(.txt) to an
 image(preferrably Tiff).I believe it involves some scanning  and
 conversion using some font table and probably compression using
 huffman encoding..is there an open source code doing this?can someone
 give a pointer?
 jim

Do you have a text file (toto.txt) and you want to render it as an
image ?
Never done that but my first thought is to use PIL (http://
www.pythonware.com/products/pil/).

Examples:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/483756
http://blog.foozia.com/blog/2007/nov/21/rendering-text-image-using-django-template-tag
--
http://mail.python.org/mailman/listinfo/python-list


Strange re problem

2008-06-20 Thread TYR
OK, this ought to be simple. I'm parsing a large text file (originally
a database dump) in order to process the contents back into a SQLite3
database. The data looks like this:

'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';

which goes on for another 308 lines. As keen and agile minds will no
doubt spot, the rows are separated by a ; so it should be simple to
parse it using a regex. So, I establish a db connection and cursor,
create the table, and open the source file.

Then we do this:

f = file.readlines()
biglist = re.split(';', f)

and then iterate over the output from re.split(), inserting each set
of values into the db, and finally close the file and commit
transactions. But instead, I get this error:

Traceback (most recent call last):
  File converter.py, line 12, in module
biglist = re.split(';', f)
  File /usr/lib/python2.5/re.py, line 157, in split
return _compile(pattern, 0).split(string, maxsplit)
TypeError: expected string or buffer

Is this because the lat and long values are integers rather than
strings? (If so, any ideas?)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pattern Matching Over Python Lists

2008-06-20 Thread MRAB
On Jun 20, 1:45 am, Chris [EMAIL PROTECTED] wrote:
 On Jun 17, 1:09 pm, [EMAIL PROTECTED] wrote:

  Kirk Strauser:

   Hint: recursion.  Your general algorithm will be something like:

  Another solution is to use a better (different) language, that has
  built-in pattern matching, or allows to create one.

  Bye,
  bearophile

 Btw, Python's stdlib includes a regular expression library. I'm not
 sure if you're trolling or simply unaware of it, but I've found it
 quite adequate for most tasks.

bearophile was talking about matching lists and tuples, not matching
strings.

Python's regular expression module works with characters in strings,
but the same approach can be applied to items in lists and tuples.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pyparsing performance boost using Python 2.6b1

2008-06-20 Thread dwahli
On Jun 19, 12:40 pm, Paul McGuire [EMAIL PROTECTED] wrote:
 I just ran my pyparsing unit tests with the latest Python 2.6b1
 (labeled internally as Python 2.6a3 - ???), and the current 1.5.0
 version of pyparsing runs with no warnings or regressions.

 I was pleasantly surprised by the improved performance.  The most
 complex parser I have is the Verilog parser, and I have just under 300
 sample input files, so the test gets a chance to run over a range of
 code and source inputs.  Here are the lines/second parsing for the
 Verilog data (higher numbers are better):

  Python V2.5.1   Python V2.6b1
 base
  209.2                307.0

 with packrat optimization enabled
  349.8                408.0

 This is a huge percentage improvement, anywhere from 15-50%!  I do not
 know what it is about 2.6 that runs so much faster, but given that
 packratting achieves somewhat less improvement, I would guess that the
 2.6 performance comes from some optimization in making function calls,
 or in GC of local variables when functions are completed (since
 packratting is a form of internal memoization of parse expressions,
 which thereby avoids duplicate calls to parsing functions).

 Using psyco typically gives another 30-50% performance improvement,
 but there is no psyco available for 2.6 yet, so I skipped those tests
 for now.

 -- Paul

Tests was made on Windows ?
Just try pybench on 2.6 and 2.5, and 2.6 is 17% better than 2.5 !
Very nice !

But I suspect the new compiler with Profile Guided Optimization (PGO)
to be the main cause of this nice boost...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strange re problem

2008-06-20 Thread Peter Otten
TYR wrote:

 OK, this ought to be simple. I'm parsing a large text file (originally
 a database dump) in order to process the contents back into a SQLite3
 database. The data looks like this:
 
 'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
 Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
 Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
 Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
 36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';
 
 which goes on for another 308 lines. As keen and agile minds will no
 doubt spot, the rows are separated by a ; so it should be simple to
 parse it using a regex. So, I establish a db connection and cursor,
 create the table, and open the source file.
 
 Then we do this:
 
 f = file.readlines()
 biglist = re.split(';', f)
 
 and then iterate over the output from re.split(), inserting each set
 of values into the db, and finally close the file and commit
 transactions. But instead, I get this error:
 
 Traceback (most recent call last):
   File converter.py, line 12, in module
 biglist = re.split(';', f)
   File /usr/lib/python2.5/re.py, line 157, in split
 return _compile(pattern, 0).split(string, maxsplit)
 TypeError: expected string or buffer
 
 Is this because the lat and long values are integers rather than
 strings? (If so, any ideas?)

No, the result of f.readlines() is a list, but re.split() expects a string
as the second parameter.

f = file.read()
biglist = re.split(;, f)

should work if the file fits into memory, but you don't need regular
expressions here:

biglist = file.read().split(;)

is just as good -- or bad, if your data contains any ; characters.

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


Re: Strange re problem

2008-06-20 Thread Mel
TYR wrote:

 OK, this ought to be simple. I'm parsing a large text file (originally
 a database dump) in order to process the contents back into a SQLite3
 database. The data looks like this:
 
 'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
 Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
 Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
 Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
 36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';
 
 which goes on for another 308 lines. As keen and agile minds will no
 doubt spot, the rows are separated by a ; so it should be simple to
 parse it using a regex. So, I establish a db connection and cursor,
 create the table, and open the source file.
 
 Then we do this:
 
 f = file.readlines()
 biglist = re.split(';', f)
 
 and then iterate over the output from re.split(), inserting each set
 of values into the db, and finally close the file and commit
 transactions. But instead, I get this error:
 
 Traceback (most recent call last):
   File converter.py, line 12, in module
 biglist = re.split(';', f)
   File /usr/lib/python2.5/re.py, line 157, in split
 return _compile(pattern, 0).split(string, maxsplit)
 TypeError: expected string or buffer

(untested) Try f=file.read()

readlines gives you a list of lines.

Mel.

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


Re: Simple Python class questions

2008-06-20 Thread John Dann
Many thanks for the further comments:

On Thu, 19 Jun 2008 21:24:31 -0400, Terry Reedy [EMAIL PROTECTED]
wrote:

  def __init__(self):
  Try
  Import serial # the pyserial library

The import should be at module level.  You only want to do it once, not 
for every link.  And if the import fails, you should find out right 
away.  

Yes I was wondering about that, but I wasn't clear about when 'body'
code (ie not contained within a def block) in the module might run
under Python. So it seemed to be safer to place the import statement
inside the 'constructor' to get the earliest warning of non-visibility
of pyserial. But you seem to be implying that the body code will run
when the class is instantiated - have I understood that right? It
surely doesn't run when the module containing the class is imported
into the main module - does it?? It would certainly make life easier
to place the import in the body of the module.

I think that some of the other points hinge on the this one, so let me
get my understanding straight on that first!


I guess you learned by now why cut/paste/edit-down is superior to 
re-typing ;-)

Well I know what you mean, but actually in this instance my Python
environment is a non-networked laptop , so no easy way to cut and
paste to a networked PC! (Actually the laptop does have an Ethernet
chip in but bizarrely the driver somehow manages to kill my ADSL
connection at the exchange or ISP, which takes hours to reset so I
take care not to use this option. But learning Linux/Python is a
useful role for this otherwise defunct PC)

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


Re: Simple Python class questions

2008-06-20 Thread cokofreedom

 Yes I was wondering about that, but I wasn't clear about when 'body'
 code (ie not contained within a def block) in the module might run
 under Python. So it seemed to be safer to place the import statement
 inside the 'constructor' to get the earliest warning of non-visibility
 of pyserial. But you seem to be implying that the body code will run
 when the class is instantiated - have I understood that right? It
 surely doesn't run when the module containing the class is imported
 into the main module - does it?? It would certainly make life easier
 to place the import in the body of the module.

Without insulting your intelligence I would advise looking at a few
python tutorials, not so much for the programming technique, but
rather how to think pythonic. A good one for using when coming from a
previous programming language is
Dive Into Python. http://www.diveintopython.org/

It does not deal with Serial specifically, but shows good examples of
practises in Python. Might be of benefit to you, and is very easy and
quick to read. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread eliben
On Jun 20, 9:17 am, Bruno Desthuilliers bruno.
[EMAIL PROTECTED] wrote:
 eliben a écrit : Hello,

  In a Python program I'm writing I need to dynamically generate
  functions[*]

 (snip)

  [*] I know that each time a code generation question comes up people
  suggest that there's a better way to achieve this, without using exec,
  eval, etc.

 Just to make things clear: you do know that you can dynamically build
 functions without exec, do you ?


Yes, but the other options for doing so are significantly less
flexible than exec.

  But in my case, for reasons too long to fully lay out, I
  really need to generate non-trivial functions with a lot of hard-coded
  actions for performance.

 Just out of curiousity : could you tell a bit more about your use case
 and what makes a simple closure not an option ?

Okay.

I work in the field of embedded programming, and one of the main uses
I have for Python (and previously Perl) is writing GUIs for
controlling embedded systems. The communication protocols are usually
ad-hoc messages (headear, footer, data, crc) built on top of serial
communication (RS232).

The packets that arrive have a known format. For example (YAMLish
syntax):

packet_length: 10
fields:
  - name: header
offset: 0
length: 1
  - name: time_tag
offset: 1
length: 1
transform: val * 2048
units: ms
  - name: counter
offset: 2
length: 4
bytes-msb-first: true
  - name: bitmask
offset: 6
length: 1
bit_from: 0
bit_to: 5
...

This is a partial capability display. Fields have defined offsets and
lengths, can be only several bits long, can have defined
transformations and units for convenient display.

I have a program that should receive such packets from the serial port
and display their contents in tabular form. I want the user to be able
to specify the format of his packets in a file similar to above.

Now, in previous versions of this code, written in Perl, I found out
that the procedure of extracting field values from packets is very
inefficient. I've rewritten it using a dynamically generated procedure
for each field, that does hard coded access to its data. For example:

def get_counter(packet):
  data = packet[2:6]
  data.reverse()
  return data

This gave me a huge speedup, because each field now had its specific
function sitting in a dict that quickly extracted the field's data
from a given packet.

Now I'm rewriting this program in Python and am wondering about the
idiomatic way to use exec (in Perl, eval() replaces both eval and exec
of Python).

Eli





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


Execute a script on a remote machine

2008-06-20 Thread srinivasan srinivas
Hi,
My requirement is i have to execute a python script on a remote machine as a 
subprocess from a python script and to get the subprocess pid of the process 
running the script. Is there anyway to do that??
I have used subprocess.popen() method to do that. I have done as following:
executable = '/usr/bin/rsh'
args = [executable, hostname, scriptname]
pid = subprocess.popen(args)
It returned the pid of rsh. But i am interested in the pid of the process 
running the script.
Can anyone help me out here?
Thanks,
Srini


  Unlimited freedom, unlimited storage. Get it now, on 
http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strange re problem

2008-06-20 Thread John Machin
On Jun 20, 9:01 pm, TYR [EMAIL PROTECTED] wrote:
 OK, this ought to be simple. I'm parsing a large text file (originally
 a database dump) in order to process the contents back into a SQLite3
 database. The data looks like this:

 'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
 Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
 Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
 Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
 36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';

 which goes on for another 308 lines.

308 lines or 308 rows? Another way of asking the same question: do you
have line terminators like \n or \r\n or \r in your file? If so, you
will need to do something like this:

rows = open('myfile', 'rb').read().replace('\r\n', '').split(';')

 As keen and agile minds will no
 doubt spot, the rows are separated by a ; so it should be simple to
 parse it using a regex. So, I establish a db connection and cursor,
 create the table, and open the source file.

 Then we do this:

 f = file.readlines()
 biglist = re.split(';', f)

 and then iterate over the output from re.split(), inserting each set
 of values into the db,

Where we left off, you had a list of rows. Each row will be a string
like:
'AAB','AU',-26.75,141,'Arrabury, Queensland,
Australia','?','?'

How do you propose to parse that string into a set of values?  Can
you rely there being data commas only in the 5th field, or do you need
a general solution? What if (as Peter remarked) there is a ';' in the
data? What if there's a ' in the data (think O'Hare)?

 and finally close the file and commit
 transactions. But instead, I get this error:

 Traceback (most recent call last):
   File converter.py, line 12, in module
 biglist = re.split(';', f)
   File /usr/lib/python2.5/re.py, line 157, in split
 return _compile(pattern, 0).split(string, maxsplit)
 TypeError: expected string or buffer

 Is this because the lat and long values are integers rather than
 strings? (If so, any ideas?)

At the stage where it blew up, you didn't even have rows, let alone
fields, let alone worries about converting your lat and long fields
from string to float (not integer!).

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


Re: Simple Python class questions

2008-06-20 Thread Duncan Booth
John Dann [EMAIL PROTECTED] wrote:

 Yes I was wondering about that, but I wasn't clear about when 'body'
 code (ie not contained within a def block) in the module might run
 under Python. So it seemed to be safer to place the import statement
 inside the 'constructor' to get the earliest warning of non-visibility
 of pyserial. But you seem to be implying that the body code will run
 when the class is instantiated - have I understood that right? It
 surely doesn't run when the module containing the class is imported
 into the main module - does it?? It would certainly make life easier
 to place the import in the body of the module.

Python starts executing at the top of your main script and then proceeds 
line by line down until it falls off the bottom. Various things can 
divert it from this straightforward progression towards the end of the 
script, some of them such as if/for/while/raise or function calls are 
obvious, but the less obvious ones include:

import somemodule (or 'from somemodule import something')

if 'somemodule' has not previously been imported this will find the 
module and execute the lines of code in the module from top to bottom 
just as for the main script. When it falls off the bottom of the module 
it returns to the import statement, assigns the module or the imported 
attributes to a name or names in the calling namespace (yes, an import 
is just a highly specialised assignment statement), and then continues 
with the next statement.

If somemodule has already started being imported anywhere in the program 
then the import simply returns immediately and does the assignment. 
(This can be a cause of confusion if you try to import modules 
recursively as it is perfectly possible that the imported module has not 
yet finished executing, so it may not yet have all the classes and 
functions you expect).

class somename(bases):
somecode

A 'class' statement in Python is just executable code. The body of the 
class is executed from top to bottom but in a new namespace. When 
execution falls off the bottom of the class body a new class object is 
created from that namespace and the base classes. The new class object 
is then assigned to 'somename' (i.e. a class statement is a specialised 
assignment statement). Then execution then procedes with the next 
statement.

def functionname(arg1, arg2=default):
somecode

A 'def' statement in Python is also a specialised assignment statement: 
unlike 'class' it doesn't immediately execute the code in the body, but 
it does evaluate any default argument values when it encounters the 
'def'. Then it creates a new function object from the code in the body 
and the default arguments (and a few other things such as the argument 
specification and the function name). Then it continues with the next 
statement.

global name

The 'global' statement is not executed at runtime. It is the only Python 
statement which is purely compile time.

Once you understand this it should be much clearer: everything except 
global is executed when it is encountered following the normal rules for 
program flow, and all the ways of creating module, classes, and 
functions simply execute some code and then do an assignment (and so if 
you wish you can later overwrite the values they assigned).

If you wish to do something special when an import fails then you simply 
put try:..except: around the import at the top level in a module and 
handle it there: you don't need to put either the import or the handler 
inside a function.

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


Re: Pyparsing performance boost using Python 2.6b1

2008-06-20 Thread John Machin
On Jun 19, 8:40 pm, Paul McGuire [EMAIL PROTECTED] wrote:
 I just ran my pyparsing unit tests with the latest Python 2.6b1
 (labeled internally as Python 2.6a3 - ???),

Hi, Paul. If it says 2.6a3, that's what it is. Look at the thread of
replies to Barry Warsaw's announcement of 2.6b1 ... [from memory]
there was a delay expected before MvL would make a Windows msi
available, and in the meantime the download page would point to the
alpha version.

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


Re: Noob: finding my way around the docs...

2008-06-20 Thread kj
In [EMAIL PROTECTED] Matimus [EMAIL PROTECTED] writes:

If you are in the interpreter and you type: help(foo.bar.baz) you get
the embeded documentation.

I usually go straight to the `global module index` http://docs.python.org/m=
odindex.html

Thanks!

kynn
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no output from xml.dom.ext.PrettyPrint?

2008-06-20 Thread kj
In [EMAIL PROTECTED] John Machin [EMAIL PROTECTED] writes:

On Jun 20, 7:17 am, kj [EMAIL PROTECTED] wrote:
 OK, the following should work but doesn't, and I can't figure out
 why:

  from xml.marshal.generic import dumps
  dumps( ( 1, 2.0, 'foo', [3,4,5] ) )

 '?xml 
 version=1.0?marshaltupleint1/intfloat2.0/floatstringfoo/stringlist
  id=i2int3/intint4/intint5/int/list/tuple/marshal'

  from xml.dom.ext import PrettyPrint
  PrettyPrint( dumps( ( 1, 2.0, 'foo', [3,4,5] ) ) )
  import sys
  PrettyPrint( dumps( ( 1, 2.0, 'foo', [3,4,5] ) ), sys.stdout )

 Why am I seeing no output from PrettyPrint?


You need to ask whoever you got your xml package from. In standard-
issue Python 2.5.2, there is an xml package with xml.dom, but it
contains no xml.dom.ext nor an xml.marshal.

Hmmm!?  OK.  Thanks!

Kynn
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strange re problem

2008-06-20 Thread TYR
How do you propose to parse that string into a set of values?  Can
you rely there being data commas only in the 5th field, or do you need
a general solution? What if (as Peter remarked) there is a ';' in the
data? What if there's a ' in the data (think O'Hare)?

My plan was to be pointlessly sarcastic.
--
http://mail.python.org/mailman/listinfo/python-list


ISO dict = xml converter

2008-06-20 Thread kj


Hi.  Does anyone know of a module that will take a suitable Python
dictionary and return the corresponding XML structure?

In Perl I use XML::Simple's handy XMLout function:

  use XML::Simple 'XMLout';
  my %h = ( 'Foo' = +{
'Bar' = +{
'Baz' = [ { 'meenie' = 3 },
   { 'meenie' = 7 } ],
'eenie' = 4,
  },
'minie' = 1,
'moe' = 2,
  } );

  print XMLout( \%h, KeepRoot = 1, KeyAttr = undef );
  __END__
Foo minie=1 moe=2
  Bar eenie=4
Baz meenie=3 /
Baz meenie=7 /
  /Bar
/Foo

Is there a Python module that can do a similar conversion from
a Python dict to an XML string?

(FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
not produce an output anywhere similar to the one illustrated
above.)

TIA!

Kynn

-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strange re problem

2008-06-20 Thread John Machin
On Jun 20, 10:33 pm, TYR [EMAIL PROTECTED] wrote:
 How do you propose to parse that string into a set of values?  Can
 you rely there being data commas only in the 5th field, or do you need
 a general solution? What if (as Peter remarked) there is a ';' in the
 data? What if there's a ' in the data (think O'Hare)?

 My plan was to be pointlessly sarcastic.

You misunderstand. My questions covered several of the problems often
encountered by newbies. I am offering help. If my suspicion that you
would need further help was incorrect, please forgive me.
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread Peter Otten
eliben wrote:

 Additionally, I've found indentation to be a problem in such
 constructs. Is there a workable way to indent the code at the level of
 build_func, and not on column 0 ?

exec if 1: + code.rstrip()

Peter

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


Accent character problem

2008-06-20 Thread Sallu
Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
  print Valid
else:
  print Not allowed special characters

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
 (because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message Not allowed
special characters
Please help me out.
Thanks



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


Re: ISO dict = xml converter

2008-06-20 Thread Stefan Behnel
kj wrote:
 Hi.  Does anyone know of a module that will take a suitable Python
 dictionary and return the corresponding XML structure?
 
 In Perl I use XML::Simple's handy XMLout function:
 
   use XML::Simple 'XMLout';
   my %h = ( 'Foo' = +{
 'Bar' = +{
 'Baz' = [ { 'meenie' = 3 },
{ 'meenie' = 7 } ],
 'eenie' = 4,
   },
 'minie' = 1,
 'moe' = 2,
   } );
 
   print XMLout( \%h, KeepRoot = 1, KeyAttr = undef );
   __END__
 Foo minie=1 moe=2
   Bar eenie=4
 Baz meenie=3 /
 Baz meenie=7 /
   /Bar
 /Foo
 
 Is there a Python module that can do a similar conversion from
 a Python dict to an XML string?

This is so trivial to do in ElementTree that I wouldn't expect there to be a
special package for this. If you write the estimated 15 lines of code
yourself, you can even tweak it into exactly the structure you want.

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


Re: pyinotify issue

2008-06-20 Thread Sebastian lunar Wiesner
AndreH [EMAIL PROTECTED]:

 On Jun 17, 12:11 pm, AndreH [EMAIL PROTECTED] wrote:
 On Jun 13, 3:39 pm, AndreH [EMAIL PROTECTED] wrote:



  Good day,

  I just installed pyinotify on my gentoo box.

  When I test the library through pyinotify.pv -v /tmp under root,
  everything works great, but when I try the same thing under my local
  user account, I receive the following error:
  Error: cannot watch . (WD=-1)

  Not very helpful. I've tried VERBOSE=True mode, but it doens't provide
  any additional information.

  I also tried it for a directory in my home folder just to be sure it's
  not a permission problem, but no luck.

  Any ideas?

  Regards,
  Andre

 Ok I ended up solving my problem.

 pyinotify is just a wrapper for the c lib, inotif.h. Installing the
 inotify-tools package allows one to do better troubleshooting.

 First, my kernel version was too old and did not allow inotify to be
 executed at user-level. I bumped my kernel up to 2.6.24 and enabled
 the user-level execution flag.

 Then pyinotify worked once and failed for all consecutive retries.
 inotifwatch said that my maximum number of user watches was maxed
 out and that I should increase it under /proc/sys/fs/inotify/
 max_user_watches.

 Something must be wrong, since the max_user_watches was set to 8192. I
 played around with this setting (sysctl -w
 fs.inotify.max_user_watches=16843), pyinotify.py and inotifywatch, and
 finally came the conclusion that pyinotify 0.7.0 was buggy. I got hold
 of 0.7.1 which seems to have fixed this problem. Hopefully, I'm not
 speaking too soon.
 
 I spoke too soon.
 
 pyinotify still seems to max out my number of user watches... I get
 this message when I run inotifywatch after a pyinotify operation:
 
 Establishing watches...
 Failed to watch .; upper limit on inotify watches reached!
 Please increase the amount of inotify watches allowed per user via `/
 proc/sys/fs/inotify/max_user_watches'.

inotify itself doesn't have support for recursive watches.  If you are
recursively watching a single directory, each subdirectory is added as
single, separate watch.  If you're watching large directories for changes,
it may well be, that there is no bug in inotify, but you're really hitting
the watch limit (which is one of the reasons, why stuff like locate can't
be implemented on top of the inotify mechanism).  Still, it sounds very
unlikely, that there are directories containing more than 8192
sub-directories, but did you consider this?

-- 
Freedom is always the freedom of dissenters.
  (Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex \b behaviour in python

2008-06-20 Thread Walter Cruz
 I'm surprised that splitting on \b doesn't work as expected, so it
 might be that re.split has been defined only to split on one or more
 characters. Is it something that should it be 'fixed'?

Thats's my main doubt: is this a bug or not?

[]'s
- Walter
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread George Sakkis
On Jun 20, 8:03 am, eliben [EMAIL PROTECTED] wrote:
 On Jun 20, 9:17 am, Bruno Desthuilliers bruno.

 [EMAIL PROTECTED] wrote:
  eliben a écrit : Hello,

   In a Python program I'm writing I need to dynamically generate
   functions[*]

  (snip)

   [*] I know that each time a code generation question comes up people
   suggest that there's a better way to achieve this, without using exec,
   eval, etc.

  Just to make things clear: you do know that you can dynamically build
  functions without exec, do you ?

 Yes, but the other options for doing so are significantly less
 flexible than exec.

   But in my case, for reasons too long to fully lay out, I
   really need to generate non-trivial functions with a lot of hard-coded
   actions for performance.

  Just out of curiousity : could you tell a bit more about your use case
  and what makes a simple closure not an option ?

 Okay.

 I work in the field of embedded programming, and one of the main uses
 I have for Python (and previously Perl) is writing GUIs for
 controlling embedded systems. The communication protocols are usually
 ad-hoc messages (headear, footer, data, crc) built on top of serial
 communication (RS232).

 The packets that arrive have a known format. For example (YAMLish
 syntax):

 packet_length: 10
 fields:
   - name: header
 offset: 0
 length: 1
   - name: time_tag
 offset: 1
 length: 1
 transform: val * 2048
 units: ms
   - name: counter
 offset: 2
 length: 4
 bytes-msb-first: true
   - name: bitmask
 offset: 6
 length: 1
 bit_from: 0
 bit_to: 5
 ...

 This is a partial capability display. Fields have defined offsets and
 lengths, can be only several bits long, can have defined
 transformations and units for convenient display.

 I have a program that should receive such packets from the serial port
 and display their contents in tabular form. I want the user to be able
 to specify the format of his packets in a file similar to above.

 Now, in previous versions of this code, written in Perl, I found out
 that the procedure of extracting field values from packets is very
 inefficient. I've rewritten it using a dynamically generated procedure
 for each field, that does hard coded access to its data. For example:

 def get_counter(packet):
   data = packet[2:6]
   data.reverse()
   return data

 This gave me a huge speedup, because each field now had its specific
 function sitting in a dict that quickly extracted the field's data
 from a given packet.

It's still not clear why the generic version is so slower, unless you
extract only a few selected fields, not all of them. Can you post a
sample of how you used to write it without exec to clarify where the
inefficiency comes from ?

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


Re: An idiom for code generation with exec

2008-06-20 Thread Raymond Hettinger
On Jun 20, 5:03 am, eliben [EMAIL PROTECTED] wrote:
 I've rewritten it using a dynamically generated procedure
 for each field, that does hard coded access to its data. For example:

 def get_counter(packet):
   data = packet[2:6]
   data.reverse()
   return data

 This gave me a huge speedup, because each field now had its specific
 function sitting in a dict that quickly extracted the field's data
 from a given packet.

 Now I'm rewriting this program in Python and am wondering about the
 idiomatic way to use exec (in Perl, eval() replaces both eval and exec
 of Python).

FWIW, when I had a similar challenge for dynamic coding, I just
generated a py file and then imported it.  This technique was nice
because can also work with Pyrex or Psyco.

Also, the code above can be simplified to:  get_counter = lambda
packet: packet[5:1:-1]

Since function calls are expensive in python, you can also gain speed
by parsing multiple fields at a time:

   header, timetag, counter = parse(packet)


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


Re: Accent character problem

2008-06-20 Thread Mark Tolonen


Sallu [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
  print Valid
else:
  print Not allowed special characters

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
 (because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message Not allowed
special characters
Please help me out.
Thanks


The match function only matches the pattern at the start of a string.  Use 
search instead.  Printing the results of a successful match will help you 
debug the problems also.


   matchobj = richre.match(value)
   if matchobj:
   print matchobj.group()
   else:
   print 'no match'

As written you will only get a successful match if your string starts with 
a-zA-Z0-9, which is why #1 and #3 print 'Valid'.


You also should declare the encoding of the file and consider using Unicode 
strings.


-Mark

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


Re: py2exe application add-ons

2008-06-20 Thread Alex Gusarov
Thanks everybody, yes, I use 'exec' for files.
And freeze modules - thanks too, I almost forgot this opportunity.

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


Re: Strange re problem

2008-06-20 Thread Paul McGuire
On Jun 20, 6:01 am, TYR [EMAIL PROTECTED] wrote:
 OK, this ought to be simple. I'm parsing a large text file (originally
 a database dump) in order to process the contents back into a SQLite3
 database. The data looks like this:

 'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
 Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
 Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
 Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
 36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';

 which goes on for another 308 lines. As keen and agile minds will no
 doubt spot, the rows are separated by a ; so it should be simple to
 parse it using a regex. So, I establish a db connection and cursor,
 create the table, and open the source file.

Using pyparsing, you can skip all that what happens if there is a
semicolon or comma inside a quoted string? noise, and get the data in
a trice.  If you add results names (as I've done in the example), then
loading each record into your db should be equally simple.

Here is a pyparsing extractor for you.  The parse actions already do
the conversions to floats, and stripping off of quotation marks.

-- Paul

data = 
'AAA','PF',-17.4167,-145.5,'Anaa, French Polynesia','Pacific/
Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
Australia','?','?';'AAC','EG',31.1333,33.8,'Al Arish,
Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
36.8333,8,'Annaba','Africa/Algiers','Rabah Bitat';
.splitlines()
data = .join(data)

from pyparsing import *

num = Regex(r'-?\d+(\.\d+)?')
num.setParseAction(lambda t: float(t[0]))
qs = sglQuotedString.setParseAction(removeQuotes)
CMA = Suppress(',')
SEMI = Suppress(';')
dataRow = qs(field1) + CMA + qs(field2) + CMA + \
num(long) + CMA + num(lat) + CMA + qs(city) + CMA + \
qs(tz) + CMA + qs(field7) + SEMI

for dr in dataRow.searchString(data):
print dr.dump()
print dr.city,dr.long,dr.lat

Prints:

['AAA', 'PF', -17.4166998, -145.5, 'Anaa, French Polynesia',
'Pacific/ Tahiti', 'Anaa']
- city: Anaa, French Polynesia
- field1: AAA
- field2: PF
- field7: Anaa
- lat: -145.5
- long: -17.416667
- tz: Pacific/ Tahiti
Anaa, French Polynesia -17.416667 -145.5
['AAB', 'AU', -26.75, 141.0, 'Arrabury, Queensland, Australia', '?',
'?']
- city: Arrabury, Queensland, Australia
- field1: AAB
- field2: AU
- field7: ?
- lat: 141.0
- long: -26.75
- tz: ?
Arrabury, Queensland, Australia -26.75 141.0
['AAC', 'EG', 31.1332999, 33.797, 'Al Arish,
Egypt', 'Africa/Cairo', 'El Arish International']
- city: Al Arish, Egypt
- field1: AAC
- field2: EG
- field7: El Arish International
- lat: 33.8
- long: 31.13
- tz: Africa/Cairo
Al Arish, Egypt 31.13 33.8
['AAE', 'DZ', 36.8333002, 8.0, 'Annaba', 'Africa/Algiers',
'Rabah Bitat']
- city: Annaba
- field1: AAE
- field2: DZ
- field7: Rabah Bitat
- lat: 8.0
- long: 36.83
- tz: Africa/Algiers
Annaba 36.83 8.0
--
http://mail.python.org/mailman/listinfo/python-list


Re: Accent character problem

2008-06-20 Thread Diez B. Roggisch

Sallu schrieb:

Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
  print Valid
else:
  print Not allowed special characters

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
 (because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message Not allowed
special characters
Please help me out.


You only test the *first* character. You need to modify your rex using a 
+, like this:


richre=re.compile(r'[a-zA-Z0-9]+')

Only then it will match the whole word.

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


Re: Named tuples and projection

2008-06-20 Thread MRAB
On Jun 20, 9:38 am, Giuseppe Ottaviano [EMAIL PROTECTED] wrote:
 I found the namedtuple very convenient for rapid prototyping code, for  
 functions that have to return a number of results that could grow as  
 the code evolves. They are more elegant than dicts, and I don't have  
 to create a new explicit class. Unfortunately in this situation they  
 lose the convenience of tuple unpacking: changing tuple's parameters  
 would break other functions unpacking the result.
 One solution, with 3.0 syntax, would be unpacking only the used  
 parameters, using always a *rest
 a, b, *rest = f()
 so that if the tuple grows, the code keeps working.
 However I find this confusing (and not avaliable in python 2.5).
 I don't know if similar solutions have been proposed, I came up with  
 this one:

[snip]
Provided you don't change the order of the items in the tuple, you can
just use slicing:

a, b = f()[ : 2]
--
http://mail.python.org/mailman/listinfo/python-list


py2exe, PyQT, QtWebKit and jpeg problem

2008-06-20 Thread Carbonimax
hello

I have a problem with py2exe and QtWebKit :
I make a program with a QtWebKit view.
If I launch the .py directly, all images (jpg, png) are displayed but
if I compile it with py2exe I have only png images. No jpg !
No error message, nothing.

Have you a solution ? Thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: converting a text file to image

2008-06-20 Thread Larry Bates

jimgardener wrote:

i am looking for python code to convert a textfile(.txt) to an
image(preferrably Tiff).I believe it involves some scanning  and
conversion using some font table and probably compression using
huffman encoding..is there an open source code doing this?can someone
give a pointer?
jim


You can use PIL to do this.  I have a small class that I wrote that does this 
using PIL.  Supports background TIF image (e.g. form).  I used it to generate 
TIFF images of a client's Invoices for them.  If you are interested, drop me an 
email and I'll forward it to you.  It does also depend on netpbm because PIL 
doesn't support outputting of compressed TIFF images.


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


Re: Simple Python class questions

2008-06-20 Thread Lie
On Jun 19, 10:49 pm, Ulrich Eckhardt [EMAIL PROTECTED] wrote:
 Lie wrote:
  I think it's not that hard to see that it's just a pseudo code

 ...in comms.py I have: ... actually explicitly says that it is actual code
 from a file.

 *shrug*

 Uli


I'm not sure how you think saying 'in comms.py I have:' is an explicit
declaration that it is the very code in his comms.py, on contrary, he
said: '...provide some outline code as an illustration: ', but let's
stop polluting this thread.
--
http://mail.python.org/mailman/listinfo/python-list


Re: images on the web

2008-06-20 Thread chris
On Jun 20, 1:52 am, Michael Ströder [EMAIL PROTECTED] wrote:
 Matt Nordhoff wrote:
  Matt Nordhoff wrote:
  You could use data: URIs [1].

  For example, a 43-byte single pixel GIF becomes this URI:

  data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2FyH5BAEAAAEALAABAAEAAAICTAEAOw%3D%3D

  They don't have universal browser support, but that might not be a
  problem in this case.

  As for generating them with Python, I'm not sure... I just used Hixie's
  data: URI kitchen [2] for the above example.

  [1] http://tools.ietf.org/html/rfc2397
  [2] http://software.hixie.ch/utilities/cgi/data/data

  Oh.. As http://bitworking.org/news/Sparklines_in_data_URIs_in_Python
  shows, the reason I couldn't find a data: URI Python library is because
  they're utterly trivial to generate:

  import base64
  import urllib

  raw_data = create_gif()
  uri = 'data:image/gif;base64,' + urllib.quote(base64.b64encode(raw_data))

  (And it's even simpler if you leave out the base64-encoding.)

 The caveat with URL schema data: is that the amount of data to be
 transferred is significantly higher than including HTML tag img src=
 in your HTML source and let the browser fetch the raw binary image data
 in a separate HTTP request (you also have to serve from your web
 application).

 Ciao, Michael.

This sounds like the way I want to go, it's just a matter of figuring
it out. Is it just a matter of putting a function call in an img tag?

 I'll give the URI thing a try, too,
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread Bruno Desthuilliers

eliben a écrit :

On Jun 20, 9:17 am, Bruno Desthuilliers bruno.
[EMAIL PROTECTED] wrote:

eliben a écrit : Hello,


In a Python program I'm writing I need to dynamically generate
functions[*]

(snip)


[*] I know that each time a code generation question comes up people
suggest that there's a better way to achieve this, without using exec,
eval, etc.

Just to make things clear: you do know that you can dynamically build
functions without exec, do you ?



Yes, but the other options for doing so are significantly less
flexible than exec.


Let's see...


But in my case, for reasons too long to fully lay out, I
really need to generate non-trivial functions with a lot of hard-coded
actions for performance.

Just out of curiousity : could you tell a bit more about your use case
and what makes a simple closure not an option ?


Okay.

I work in the field of embedded programming, and one of the main uses
I have for Python (and previously Perl) is writing GUIs for
controlling embedded systems. The communication protocols are usually
ad-hoc messages (headear, footer, data, crc) built on top of serial
communication (RS232).


ok


The packets that arrive have a known format. For example (YAMLish
syntax):

packet_length: 10
fields:
  - name: header
offset: 0
length: 1
  - name: time_tag
offset: 1
length: 1
transform: val * 2048
units: ms
  - name: counter
offset: 2
length: 4
bytes-msb-first: true
  - name: bitmask
offset: 6
length: 1
bit_from: 0
bit_to: 5
...

This is a partial capability display. Fields have defined offsets and
lengths, can be only several bits long, can have defined
transformations and units for convenient display.


ok


I have a program that should receive such packets from the serial port
and display their contents in tabular form. I want the user to be able
to specify the format of his packets in a file similar to above.


ok


Now, in previous versions of this code, written in Perl, I found out
that the procedure of extracting field values from packets is very
inefficient. I've rewritten it using a dynamically generated procedure
for each field, that does hard coded access to its data. For example:

def get_counter(packet):
  data = packet[2:6]
  data.reverse()
  return data

This gave me a huge speedup, because each field now had its specific
function sitting in a dict that quickly extracted the field's data
from a given packet.


ok. So if I get it right, you build the function's code as a string 
based on the YAML specification.


If so, well, I can't think of anything really better[1]  - at least *if* 
dynamically generated procedures are really better performance wise, 
which may *or not* be the case in Python.


[1] except using compile to build a code object with the function's 
body, then instanciate a function object using this code, but I'm not 
sure whether it will buy you much more performance-wise. I'd personnaly 
prefer this because I find it more explicit and readable, but YMMV.



Now I'm rewriting this program in Python and am wondering about the
idiomatic way to use exec (in Perl, eval() replaces both eval and exec
of Python).


Well... So far, the most pythonic way to use exec is to avoid using it - 
unless it's the right tool for the job !-)



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


Tkinter canvas drag/drop obstacle

2008-06-20 Thread Peter Pearson
Tkinter makes it very easy to drag jpeg images around on a
canvas, but I would like to have a target change color when
the cursor dragging an image passes over it.  I seem to be
blocked by the fact that the callbacks that might tell the
target that the mouse has entered it (Enter, Any-Enter,
even Motion) aren't called if the mouse's button is down.
What am I missing?  Have I failed to find the right Tkinter
document?  Is Tkinter the wrong tool for this job?  Thanks.

-- 
To email me, substitute nowhere-spamcop, invalid-net.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Execute a script on a remote machine

2008-06-20 Thread Gerhard Häring

srinivasan srinivas wrote:

Hi,
My requirement is i have to execute a python script on a remote machine as a 
subprocess from a python script and to get the subprocess pid of the process 
running the script. Is there anyway to do that??
I have used subprocess.popen() method to do that. I have done as following:
executable = '/usr/bin/rsh'
args = [executable, hostname, scriptname]
pid = subprocess.popen(args)
It returned the pid of rsh. But i am interested in the pid of the process 
running the script.
Can anyone help me out here?


Using os.getpid() you can find out the pid of the script and communicate 
it back to the caller.


-- Gerhard

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


Python is behavior

2008-06-20 Thread michalis . avraam
I am not certain why this is the case, but...

 a = 256
 b = 256
 a is b
True

 a = 257
 b = 257
 a is b
False

Can anyone explain this further? Why does it happen? 8-bit integer
differences?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Named tuples and projection

2008-06-20 Thread Giuseppe Ottaviano



[snip]
Provided you don't change the order of the items in the tuple, you can
just use slicing:

a, b = f()[ : 2]


Yes, this is what you would normally do with tuples. But i find this  
syntax very implicit and awkward. Also, you cannot skip elements, so  
you often end up with things like

a, _, b = f()[:3]

The use case I posted is just the most recent that I stumbled upon.  
But often a project function would have saved me a lot of typing.  
Consider for example when named tuples are used for rows in an ORM (or  
attributes in XML):


for person in people:
name, age, address = person['name age address']
# code






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


Re: py2exe, PyQT, QtWebKit and jpeg problem

2008-06-20 Thread Phil Thompson
On Fri, 20 Jun 2008 08:04:57 -0700 (PDT), Carbonimax [EMAIL PROTECTED]
wrote:
 hello
 
 I have a problem with py2exe and QtWebKit :
 I make a program with a QtWebKit view.
 If I launch the .py directly, all images (jpg, png) are displayed but
 if I compile it with py2exe I have only png images. No jpg !
 No error message, nothing.
 
 Have you a solution ? Thank you.

At a guess, the JPEG support is implemented as a Qt plugin which you are
not including.

Phil

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


Re: Python is behavior

2008-06-20 Thread Jean-Paul Calderone

On Fri, 20 Jun 2008 09:31:57 -0700 (PDT), [EMAIL PROTECTED] wrote:

I am not certain why this is the case, but...


a = 256
b = 256
a is b

True


a = 257
b = 257
a is b

False

Can anyone explain this further? Why does it happen? 8-bit integer
differences?


http://mail.python.org/pipermail/python-list/2001-November/113994.html

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


Re: Tkinter canvas drag/drop obstacle

2008-06-20 Thread Guilherme Polo
On Fri, Jun 20, 2008 at 1:11 PM, Peter Pearson [EMAIL PROTECTED] wrote:
 Tkinter makes it very easy to drag jpeg images around on a
 canvas, but I would like to have a target change color when
 the cursor dragging an image passes over it.  I seem to be
 blocked by the fact that the callbacks that might tell the
 target that the mouse has entered it (Enter, Any-Enter,
 even Motion) aren't called if the mouse's button is down.
 What am I missing?  Have I failed to find the right Tkinter
 document?  Is Tkinter the wrong tool for this job?  Thanks.


I believe the only way to achieve this is binding Motion to the
entire canvas, then checking if the x, y coords are inside the
target.

 --
 To email me, substitute nowhere-spamcop, invalid-net.
 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python is behavior

2008-06-20 Thread George Sakkis
On Jun 20, 12:31 pm, [EMAIL PROTECTED] wrote:
 I am not certain why this is the case, but...

  a = 256
  b = 256
  a is b

 True

  a = 257
  b = 257
  a is b

 False

 Can anyone explain this further? Why does it happen? 8-bit integer
 differences?

No, implementation-dependent optimization (caching). For all we know,
the next python version may cache up to 1024 or it may turn off
caching completely; do not rely on it. More generally, do not use 'is'
when you really mean '=='.

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


Re: Python is behavior

2008-06-20 Thread michalis . avraam
On Jun 20, 9:38 am, Jean-Paul Calderone [EMAIL PROTECTED] wrote:
 On Fri, 20 Jun 2008 09:31:57 -0700 (PDT), [EMAIL PROTECTED] wrote:
 I am not certain why this is the case, but...

  a = 256
  b = 256
  a is b
 True

  a = 257
  b = 257
  a is b
 False

 Can anyone explain this further? Why does it happen? 8-bit integer
 differences?

 http://mail.python.org/pipermail/python-list/2001-November/113994.html

 Jean-Paul

Thank you for this Jean-Paul. I did know about the identity of
objects, but my curiosity is based on the 256 number. Are the 2^8
integers cached due to the internal loops, or is there any other
specific reason? Is this something that can be controlled?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python is behavior

2008-06-20 Thread michalis . avraam
On Jun 20, 9:42 am, George Sakkis [EMAIL PROTECTED] wrote:
 On Jun 20, 12:31 pm, [EMAIL PROTECTED] wrote:



  I am not certain why this is the case, but...

   a = 256
   b = 256
   a is b

  True

   a = 257
   b = 257
   a is b

  False

  Can anyone explain this further? Why does it happen? 8-bit integer
  differences?

 No, implementation-dependent optimization (caching). For all we know,
 the next python version may cache up to 1024 or it may turn off
 caching completely; do not rely on it. More generally, do not use 'is'
 when you really mean '=='.

 George

Thank you George. I am very curious about some of these internal
Python things that I keep stumbling upon through friends. And thank
you for all the help!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple and safe evaluator

2008-06-20 Thread bvdp

Aahz wrote:

In article [EMAIL PROTECTED],
Simon Forman  [EMAIL PROTECTED] wrote:

FWIW, I got around to implementing a function that checks if a string
is safe to evaluate (that it consists only of numbers, operators, and
( and )).  Here it is. :)


What's safe about 1000 ** 1000?


Guess it depends on your definition of safe. I think that in most cases 
folks looking for safe are concerned about a malicious interjection of 
a command like rm * ... your example hangs the system for a long time 
and eventually will error out when it runs out of memory, but (probably) 
doesn't cause data corruption.


It would be nice if in a future version of Python we could have a 
safe/limited eval() ... which would limit the resources.

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


Re: An idiom for code generation with exec

2008-06-20 Thread Dan Yamins
On Fri, Jun 20, 2008 at 3:17 AM, Bruno Desthuilliers
[EMAIL PROTECTED] wrote:

Just to make things clear: you do know that you can dynamically build
 functions without exec, do you ?


Actually, I don't know how to do this, but would like to.   Can you point me
to a place where I can read more about how  (and to a discussion of the
flaws of the exec-based approach)?


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

Re: Python is behavior

2008-06-20 Thread George Sakkis
On Jun 20, 12:45 pm, [EMAIL PROTECTED] wrote:
 On Jun 20, 9:42 am, George Sakkis [EMAIL PROTECTED] wrote:



  On Jun 20, 12:31 pm, [EMAIL PROTECTED] wrote:

   I am not certain why this is the case, but...

a = 256
b = 256
a is b

   True

a = 257
b = 257
a is b

   False

   Can anyone explain this further? Why does it happen? 8-bit integer
   differences?

  No, implementation-dependent optimization (caching). For all we know,
  the next python version may cache up to 1024 or it may turn off
  caching completely; do not rely on it. More generally, do not use 'is'
  when you really mean '=='.

  George

 Thank you George. I am very curious about some of these internal
 Python things that I keep stumbling upon through friends. And thank
 you for all the help!

As far it's plain curiosity it's ok, but it's a small implementation
detail you shouldn't rely on. There's nothing magic about 256, just
the size decided for 2.5. If you tried it on 2.4 you'd get:

Python 2.4.2 (#1, Mar  8 2006, 13:24:00)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
Type help, copyright, credits or license for more information.
 a=99
 b=99
 a is b
True
 a=100
 b=100
 a is b
False

I was more surprised by the following:

Python 2.5.1 (r251:54863, May  8 2007, 14:46:30)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type help, copyright, credits or license for more information.
 a= 123456; b=123456; a is b
True

For some reason, stacking multiple statements reuses the same object.

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


Re: Python is behavior

2008-06-20 Thread Jean-Paul Calderone

On Fri, 20 Jun 2008 10:07:56 -0700 (PDT), George Sakkis [EMAIL PROTECTED] 
wrote:

On Jun 20, 12:45 pm, [EMAIL PROTECTED] wrote:

On Jun 20, 9:42 am, George Sakkis [EMAIL PROTECTED] wrote:



 On Jun 20, 12:31 pm, [EMAIL PROTECTED] wrote:

  I am not certain why this is the case, but...

   a = 256
   b = 256
   a is b

  True

   a = 257
   b = 257
   a is b

  False

  Can anyone explain this further? Why does it happen? 8-bit integer
  differences?

 No, implementation-dependent optimization (caching). For all we know,
 the next python version may cache up to 1024 or it may turn off
 caching completely; do not rely on it. More generally, do not use 'is'
 when you really mean '=='.

 George

Thank you George. I am very curious about some of these internal
Python things that I keep stumbling upon through friends. And thank
you for all the help!


As far it's plain curiosity it's ok, but it's a small implementation
detail you shouldn't rely on. There's nothing magic about 256, just
the size decided for 2.5. If you tried it on 2.4 you'd get:

Python 2.4.2 (#1, Mar  8 2006, 13:24:00)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
Type help, copyright, credits or license for more information.

a=99
b=99
a is b

True

a=100
b=100
a is b

False

I was more surprised by the following:

Python 2.5.1 (r251:54863, May  8 2007, 14:46:30)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type help, copyright, credits or license for more information.

a= 123456; b=123456; a is b

True

For some reason, stacking multiple statements reuses the same object.



This is because using the ; puts the statements into the same compilation
unit as each other.  So secretly an integer object is created for 123456
and then a and b are both given a reference to it.  This is a different
mechanism than the other case, where the builtin integer cache causes the
literal 100 to refer to the same object each time it is evaluated.

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


need to parse html to microsoft access table

2008-06-20 Thread Ahmed, Shakir
I need help to parse html file into Microsoft Access database table.
Right now I could parse it in a csv file but the way it is parsing is
not that I want and I could not import the list into access table as the
information is parsing one after another and it is not a row column
format. 

 

 

Any help is highly appreciated.

 

Thanks in advance

sk

 

 

 

 

 

Name:



 

Registered Office Address:

Xx 

 

 

 

 

 

 

Xxxx

 

 

Telephone:

(xx

 

 

Fax:

(xxx) xxx 

 

 

E-Mail:

[EMAIL PROTECTED] 

 

Website:

 

 

 

 

Status:

X

 

 

County(ies):

x 

 

 

Local Governing Authority:

Xxx 

 

 

Function(s):

X

X

Date Established:

6/15/1953

 

 

 

Creation Documents:

Xxxx

 

Statutory Authority:

 

 

Board Selection:

x

 

 

 

Authority to Issue Bonds:

x

 

 

 

Revenue Source:

 x

 

 

Most Recent Update:



 

 

 

Creation Method:

xxx 

 

 

 

 

 

 

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

Re: How do i : Python Threads + KeyboardInterrupt exception

2008-06-20 Thread Rhamphoryncus
On Jun 19, 11:09 pm, Brendon Costa [EMAIL PROTECTED] wrote:
  If only the main thread can receive KeyboardInterrupt, is there any
  reason why you couldn't move the functionality of the Read thread into
  the main thread? It looks like it's not doing any work, just waiting
  for the Proc thread to finish.

  You could start the Proc thread, do the current Read thread
  functionality until the interrupt occurs, put the apporpriate message
  in the queue, and then wait for the Proc thread to finish.

 It is already doing that. You will notice that the Proc() function is
 called by a threading.Thread instance so Proc() is running in a
 thread, but the Read() function is being called by the main thread
 right after this. It DOES work with the Ctrl + C, but i can find no
 way at all of closing down the script from within the Proc() thread.

 The relevant bit of code is:
 t = MyThread(Proc, queue, sys.stderr, None)
 Read(queue, sys.stdin, sys.stderr)

 In the end, the problem is that i am trying to multiplex IO and other
 operations. In UNIX i would use select with the input file descriptor
 and an anonymous pipe for the extra commands to achieve this without
 any threads, but this script needs to run primarily on a windows box
 and i would like to use it on UNIX too. I thought i could use threads
 to achieve the IO Multiplexing in python, but it seems not or at least
 not simply.

 How do people usually manage IO multiplexing (not just using sockets)
 cross platform in python?

 I only need to multiplex two sources really:
 * Input file or stdin
 * A input event queue
    This will have messages injected from various locations: timers,
 the processing thread itself, and possibly from a single GUI thread at
 a later point in time.

 Though i can foresee that at some point in the future i may also need
 to multiplex those two above and some sockets (For a server with a few
 clients).

 I was thinking of looking at some asynchronous IO libraries for python
 on Windows + UNIX, any suggestions (Must include more than just
 sockets)?

They either use an event-driven library.. or they use a timeout of
around 1 second.  1 second will definitely waste power on laptops (and
desktops), but it *works*.

python-safethread has this fixed - any lowlevel trickery needed is
done for you - but it's not ported to windows yet.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Execute a script on a remote machine

2008-06-20 Thread srinivasan srinivas
This is ok. 
Is there any other way to find it out?
Thanks,
Srini

- Original Message 
From: Gerhard Häring [EMAIL PROTECTED]
To: python-list@python.org
Sent: Friday, 20 June, 2008 10:03:30 PM
Subject: Re: Execute a script on a remote machine

srinivasan srinivas wrote:
 Hi,
 My requirement is i have to execute a python script on a remote machine as a 
 subprocess from a python script and to get the subprocess pid of the process 
 running the script. Is there anyway to do that??
 I have used subprocess.popen() method to do that. I have done as following:
 executable = '/usr/bin/rsh'
 args = [executable, hostname, scriptname]
 pid = subprocess.popen(args)
 It returned the pid of rsh. But i am interested in the pid of the process 
 running the script.
 Can anyone help me out here?

Using os.getpid() you can find out the pid of the script and communicate 
it back to the caller.

-- Gerhard

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



  Save all your chat conversations. Find them online at 
http://in.messenger.yahoo.com/webmessengerpromo.php
--
http://mail.python.org/mailman/listinfo/python-list


how to export functions by name for ctype

2008-06-20 Thread rych
I'm on Windows with VS2005 testing ctypes on a very simple dll
I create a test.dll project which exports a function fntest(). I don't
touch anything in the autogenerated source and build it. I can load
the dll but can't access the function by its name fntest. Only by
ordinal number or calling getattr with ?fntest@@YAHXZ. How do I
export functions by name? It's probably rather a VS2005 question, but
I'm a bit disappointed ctypes doesn't work with a default export
convention.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter canvas drag/drop obstacle

2008-06-20 Thread Matimus
On Jun 20, 9:11 am, Peter Pearson [EMAIL PROTECTED] wrote:
 Tkinter makes it very easy to drag jpeg images around on a
 canvas, but I would like to have a target change color when
 the cursor dragging an image passes over it.  I seem to be
 blocked by the fact that the callbacks that might tell the
 target that the mouse has entered it (Enter, Any-Enter,
 even Motion) aren't called if the mouse's button is down.
 What am I missing?  Have I failed to find the right Tkinter
 document?  Is Tkinter the wrong tool for this job?  Thanks.

 --
 To email me, substitute nowhere-spamcop, invalid-net.

I have used a combination of Motion and B1-Motion. You might also
throw in a Button-1 event to keep track of whether or not the mouse
button was down when it entered the widget or not.

Depending on what you really want to do though, you might take
advantage of the 'active' state:

import Tkinter as tk

can = tk.Canvas()
can.pack(fill=tk.BOTH, expand=True)

can.create_rectangle(
10,10,100,100,
fill=black,
activewidth=5,
activeoutline=blue
)

can.mainloop()

The 'active*' options take effect when the mouse is on top of that
item.

If all you are _really_ interested in is a visual indicator, this
should work for you. Note that there is also a disabled state. I only
discovered this by looking at the options available and guessing.

 from pprint import pprint
 import Tkinter as tk
 can = tk.Canvas()
 can.pack(fill=tk.BOTH, expand=True)
 r = can.create_rectangle(10,10,100,100)
 pprint(can.itemconfig(r))
{'activedash': ('activedash', '', '', '', ''),
 'activefill': ('activefill', '', '', '', ''),
 'activeoutline': ('activeoutline', '', '', '', ''),
 'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
 'activestipple': ('activestipple', '', '', '', ''),
 'activewidth': ('activewidth', '', '', '0.0', '0.0'),
 'dash': ('dash', '', '', '', ''),
 'dashoffset': ('dashoffset', '', '', '0', '0'),
 'disableddash': ('disableddash', '', '', '', ''),
 'disabledfill': ('disabledfill', '', '', '', ''),
 'disabledoutline': ('disabledoutline', '', '', '', ''),
 'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
 'disabledstipple': ('disabledstipple', '', '', '', ''),
 'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
 'fill': ('fill', '', '', '', ''),
 'offset': ('offset', '', '', '0,0', '0,0'),
 'outline': ('outline', '', '', 'black', 'black'),
 'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
 'outlinestipple': ('outlinestipple', '', '', '', ''),
 'state': ('state', '', '', '', ''),
 'stipple': ('stipple', '', '', '', ''),
 'tags': ('tags', '', '', '', ''),
 'width': ('width', '', '', '1.0', '1.0')}

The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So
if you want to make your canvas items look different when they are
disabled, set the disabled* options and set 'state' to 'disabled'.

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


Re: ISO dict = xml converter

2008-06-20 Thread Paddy
On Jun 20, 1:37 pm, kj [EMAIL PROTECTED] wrote:
 Hi.  Does anyone know of a module that will take a suitable Python
 dictionary and return the corresponding XML structure?

 In Perl I use XML::Simple's handy XMLout function:

   use XML::Simple 'XMLout';
   my %h = ( 'Foo' = +{
                         'Bar' = +{
                                     'Baz' = [ { 'meenie' = 3 },
                                                { 'meenie' = 7 } ],
                                     'eenie' = 4,
                                   },
                         'minie' = 1,
                         'moe' = 2,
                       } );

   print XMLout( \%h, KeepRoot = 1, KeyAttr = undef );
   __END__
 Foo minie=1 moe=2
   Bar eenie=4
     Baz meenie=3 /
     Baz meenie=7 /
   /Bar
 /Foo

 Is there a Python module that can do a similar conversion from
 a Python dict to an XML string?

 (FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
 not produce an output anywhere similar to the one illustrated
 above.)

 TIA!

 Kynn

 --
 NOTE: In my address everything before the first period is backwards;
 and the last period, and everything after it, should be discarded.

Try:
  http://pyxml.sourceforge.net/topics/howto/node26.html

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


Re: Simple Python class questions

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

 On Jun 19, 7:21 pm, Ulrich Eckhardt [EMAIL PROTECTED] wrote:
  John Dann wrote:
   Let's say I define the class in a module called comms.py. The class
   isn't really going to inherit from any other class (except presumably
   in the most primitive base-class sense, which is presumably automatic
   and implicit in using the class keyword). Let's call the class
   serial_link. So in comms.py I have:
 
   class serial_link:
       def __init__(self):
           Try
               Import serial # the pyserial library
 
  Stop, this can't work. Other than VB, Python actually is case sensitive, so
  you must write 'try' and not 'Try' and also 'import' and not 'Import'.
  Further, many (all?) statements that cause an indention are usually
  terminated with a colon, so like with 'class ..:' and 'def ..:' you also
  must use 'try:' and not just 'try'. Fix all these and try again, I guess
  this will already help a lot.
 [...]
 
  Uli
 
  --
  Sator Laser GmbH
  Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
 
 I think it's not that hard to see that it's just a pseudo code

I would have _thought_ it wasn't hard to see that if a person
says he's totally new to the language, and even explicitly says
that the problem could be syntax errors, then he shouldn't
post pseudo code. How in the world is pseudo code going to
allow people to help him fix his syntax?

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

Re: Python is behavior

2008-06-20 Thread Gary Herron

[EMAIL PROTECTED] wrote:

On Jun 20, 9:38 am, Jean-Paul Calderone [EMAIL PROTECTED] wrote:
  

On Fri, 20 Jun 2008 09:31:57 -0700 (PDT), [EMAIL PROTECTED] wrote:


I am not certain why this is the case, but...
  

a = 256
b = 256
a is b


True
  

a = 257
b = 257
a is b


False
  
Can anyone explain this further? Why does it happen? 8-bit integer

differences?
  

http://mail.python.org/pipermail/python-list/2001-November/113994.html

Jean-Paul



Thank you for this Jean-Paul. I did know about the identity of
objects, but my curiosity is based on the 256 number. Are the 2^8
integers cached due to the internal loops, or is there any other
specific reason? Is this something that can be controlled?
  


Python provides no way to change that number, but of course you can 
always fiddle with the source code and recompile.   The actual value is 
a trade off (like any caching scheme) of cache-space versus efficiency 
gains.   The value has changed at least once in recent versions of Python.


Gary Herron


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


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


Re: An idiom for code generation with exec

2008-06-20 Thread eliben
 FWIW, when I had a similar challenge for dynamic coding, I just
 generated a py file and then imported it.  This technique was nice
 because can also work with Pyrex or Psyco.


I guess this is not much different than using exec, at the conceptual
level. exec is perhaps more suitable when you really need just one
function at a time and not a whole file of related functions.

 Also, the code above can be simplified to:  get_counter = lambda
 packet: packet[5:1:-1]


OK, but that was just a demonstration. The actual functions are
complex enough to not fit into a single expression.

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


Re: Execute a script on a remote machine

2008-06-20 Thread Gerhard Häring
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

srinivasan srinivas wrote:
 Is there any other way rather than communicating back to the caller?

No, the remote PID isn't magically transferred via RSH. The remote
script must communicate the PID back.

Just writing it remotely as first line and on the client side reading
the first line via the subprocess module could do the trick.

- -- Gerhard
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIXAZFdIO4ozGCH14RAoGIAJ4jiEc/389uzBPkRWVMQJnJSEOM6ACfQ0ra
UXbXtTl6StW62Xq5MyD7+Yw=
=S4XS
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-20 Thread eliben
 [1] except using compile to build a code object with the function's
 body, then instanciate a function object using this code, but I'm not
 sure whether it will buy you much more performance-wise. I'd personnaly
 prefer this because I find it more explicit and readable, but YMMV.


How is compiling more readable than exec - doesn't it require an extra
step ? You generate code dynamically anyway.

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


Re: An idiom for code generation with exec

2008-06-20 Thread eliben
On Jun 20, 3:19 pm, George Sakkis [EMAIL PROTECTED] wrote:
 On Jun 20, 8:03 am, eliben [EMAIL PROTECTED] wrote:



  On Jun 20, 9:17 am, Bruno Desthuilliers bruno.

  [EMAIL PROTECTED] wrote:
   eliben a écrit : Hello,

In a Python program I'm writing I need to dynamically generate
functions[*]

   (snip)

[*] I know that each time a code generation question comes up people
suggest that there's a better way to achieve this, without using exec,
eval, etc.

   Just to make things clear: you do know that you can dynamically build
   functions without exec, do you ?

  Yes, but the other options for doing so are significantly less
  flexible than exec.

But in my case, for reasons too long to fully lay out, I
really need to generate non-trivial functions with a lot of hard-coded
actions for performance.

   Just out of curiousity : could you tell a bit more about your use case
   and what makes a simple closure not an option ?

  Okay.

  I work in the field of embedded programming, and one of the main uses
  I have for Python (and previously Perl) is writing GUIs for
  controlling embedded systems. The communication protocols are usually
  ad-hoc messages (headear, footer, data, crc) built on top of serial
  communication (RS232).

  The packets that arrive have a known format. For example (YAMLish
  syntax):

  packet_length: 10
  fields:
- name: header
  offset: 0
  length: 1
- name: time_tag
  offset: 1
  length: 1
  transform: val * 2048
  units: ms
- name: counter
  offset: 2
  length: 4
  bytes-msb-first: true
- name: bitmask
  offset: 6
  length: 1
  bit_from: 0
  bit_to: 5
  ...

  This is a partial capability display. Fields have defined offsets and
  lengths, can be only several bits long, can have defined
  transformations and units for convenient display.

  I have a program that should receive such packets from the serial port
  and display their contents in tabular form. I want the user to be able
  to specify the format of his packets in a file similar to above.

  Now, in previous versions of this code, written in Perl, I found out
  that the procedure of extracting field values from packets is very
  inefficient. I've rewritten it using a dynamically generated procedure
  for each field, that does hard coded access to its data. For example:

  def get_counter(packet):
data = packet[2:6]
data.reverse()
return data

  This gave me a huge speedup, because each field now had its specific
  function sitting in a dict that quickly extracted the field's data
  from a given packet.

 It's still not clear why the generic version is so slower, unless you
 extract only a few selected fields, not all of them. Can you post a
 sample of how you used to write it without exec to clarify where the
 inefficiency comes from ?

 George

The generic version has to make a lot of decisions at runtime, based
on the format specification.
Extract the offset from the spec, extract the length. Is it msb-
first ? Then reverse. Are specific bits required ? If so, do bit
operations. Should bits be reversed ? etc.

A dynamically generated function doesn't have to make any decisions -
everything is hard coded in it, because these decisions have been done
at compile time. This can save a lot of dict accesses and conditions,
and results in a speedup.

I guess this is not much different from Lisp macros - making decisions
at compile time instead of run time and saving performance.

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


Re: At long last...

2008-06-20 Thread Carl Banks
On Jun 19, 11:26 pm, Dan Bishop [EMAIL PROTECTED] wrote:
 On Jun 19, 9:24 pm, Carl Banks [EMAIL PROTECTED] wrote:



  On Jun 19, 10:17 pm, Terry Reedy [EMAIL PROTECTED] wrote:

   Carl Banks wrote:
Tuples will have an index method in Python 2.6.

I promise I won't indiscriminately use tuples for homogenous data.
Honest.  Scout's honor.  Cross my heart.

   Use them as you want.  This change came about because .index was
   included in the 3.0 Sequence ABC (abstract base class) and tuple was
   included as a sequence, so  something had to give.  The result was
   tuple getting the full suite of immutable sequence methods.  And then
   there was no good reason to not backport ;-).

  The last time I needed index on a tuple was in fact for partially non-
  homogenous data.  I forget why, but I needed to treat arguments after
  a certain value different from the front arguments.  So I wanted to do
  something like:

  def something(*args):
      firstspecial = args.index(0)

  'Cept I couldn't.

 Why didn't you just use a list inside the tuple?

I don't remember, but knowing myself I was probably implementing some
sort of declarative function that I would call 200 times with hand-
entered data to build some kind of dataset.

Normally I would not design functions like that, but when ease of
data entry is the predominant concern I will do all kinds of hacky
stuff.


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


Re: How to request data from a lazily-created tree structure ?

2008-06-20 Thread méchoui
On Jun 17, 10:54 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
  Do you know if there is such XPath engine that can be applied to a DOM-
  like structure ?

 No. But I toyed with the idea to write one :)

  One way would be to take an XPath engine from an existing XML engine
  (ElementTree, or any other), and see what APIs it calls... and see if
  we cannot create a DOM-like structure that has the same API. Duck
  typing, really...

 Why can't you create a *real* DOM?

 Diez

I don't know what real means, in fact. In python, being a real sg
is all about having the same interface, right? May be I did not
undertand what you meant.

I cannot load all the data in memory before I request it, because it
would take too long. If using XPath-like tools requires that I load
the data in memory, I'd better create my own algorithm instead. It
will be much faster.

What I mean it: if I have a XPath engine that works well on a specific
DOM-like structure... may be I can create my own DOM-lile structure to
fool the XPath engine; so that I can use it on my own structure.
--
http://mail.python.org/mailman/listinfo/python-list


How do I create a new Node using pulldom?

2008-06-20 Thread [EMAIL PROTECTED]

I'm using xml.dom.pulldom to parse through an XML file. I use
expandNode() to scrutinize certain blocks of it that I'm interested
in.


Once I find a block of XML in the input file that I'm interested in, I
need to add my own block MyTag./MyTag to the pulldom tree I'm
building in memory.

The documentation on PullDom is worse than atrocious. It is simply non-
existant. I can't even find a simple explanation of what the functions
are named and what arguments they take. Sheesh.


When I have a node N of the tree, I think that I can use
N.appendChild() to do what I want (just guessing from the function
name which I can see).

appendChild takes 1 argument -- a new node.

But I don't know how to create such a node.
Can someone out there please post a code fragment showing how to
create a pulldom node? The simpler and more commented it is the
better.

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


sublassing as a verb

2008-06-20 Thread davidj411
docs on urllib module say this about the FancyUrlOpener:
class FancyURLopener( ...)

FancyURLopener subclasses URLopener providing default handling
for ...

does that mean the FancyURLopener is a subclass of URLopener?
--
http://mail.python.org/mailman/listinfo/python-list


Re: sublassing as a verb

2008-06-20 Thread [EMAIL PROTECTED]
On 20 juin, 22:34, davidj411 [EMAIL PROTECTED] wrote:
 docs on urllib module say this about the FancyUrlOpener:
 class FancyURLopener( ...)

 FancyURLopener subclasses URLopener providing default handling
 for ...

 does that mean the FancyURLopener is a subclass of URLopener?

You could easily find out by yourself, you know ?-)

Python 2.5.1 (r251:54863, Apr  6 2008, 17:20:35)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.2)] on linux2
Type help, copyright, credits or license for more information.
 from urllib import URLopener, FancyURLopener
 issubclass(FancyURLopener, URLopener)
True

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


Re: An idiom for code generation with exec

2008-06-20 Thread George Sakkis
On Jun 20, 3:44 pm, eliben [EMAIL PROTECTED] wrote:
 On Jun 20, 3:19 pm, George Sakkis [EMAIL PROTECTED] wrote:



  On Jun 20, 8:03 am, eliben [EMAIL PROTECTED] wrote:

   On Jun 20, 9:17 am, Bruno Desthuilliers bruno.

   [EMAIL PROTECTED] wrote:
eliben a écrit : Hello,

 In a Python program I'm writing I need to dynamically generate
 functions[*]

(snip)

 [*] I know that each time a code generation question comes up people
 suggest that there's a better way to achieve this, without using exec,
 eval, etc.

Just to make things clear: you do know that you can dynamically build
functions without exec, do you ?

   Yes, but the other options for doing so are significantly less
   flexible than exec.

 But in my case, for reasons too long to fully lay out, I
 really need to generate non-trivial functions with a lot of hard-coded
 actions for performance.

Just out of curiousity : could you tell a bit more about your use case
and what makes a simple closure not an option ?

   Okay.

   I work in the field of embedded programming, and one of the main uses
   I have for Python (and previously Perl) is writing GUIs for
   controlling embedded systems. The communication protocols are usually
   ad-hoc messages (headear, footer, data, crc) built on top of serial
   communication (RS232).

   The packets that arrive have a known format. For example (YAMLish
   syntax):

   packet_length: 10
   fields:
 - name: header
   offset: 0
   length: 1
 - name: time_tag
   offset: 1
   length: 1
   transform: val * 2048
   units: ms
 - name: counter
   offset: 2
   length: 4
   bytes-msb-first: true
 - name: bitmask
   offset: 6
   length: 1
   bit_from: 0
   bit_to: 5
   ...

   This is a partial capability display. Fields have defined offsets and
   lengths, can be only several bits long, can have defined
   transformations and units for convenient display.

   I have a program that should receive such packets from the serial port
   and display their contents in tabular form. I want the user to be able
   to specify the format of his packets in a file similar to above.

   Now, in previous versions of this code, written in Perl, I found out
   that the procedure of extracting field values from packets is very
   inefficient. I've rewritten it using a dynamically generated procedure
   for each field, that does hard coded access to its data. For example:

   def get_counter(packet):
 data = packet[2:6]
 data.reverse()
 return data

   This gave me a huge speedup, because each field now had its specific
   function sitting in a dict that quickly extracted the field's data
   from a given packet.

  It's still not clear why the generic version is so slower, unless you
  extract only a few selected fields, not all of them. Can you post a
  sample of how you used to write it without exec to clarify where the
  inefficiency comes from ?

  George

 The generic version has to make a lot of decisions at runtime, based
 on the format specification.
 Extract the offset from the spec, extract the length. Is it msb-
 first ? Then reverse. Are specific bits required ? If so, do bit
 operations. Should bits be reversed ? etc.

So you are saying that for example if do_reverse: data.reverse() is
*much* slower than data.reverse() ? I would expect that checking the
truthness of a boolean would be negligible compared to the reverse
itself. Did you try converting all checks to identity comparisons with
None ? I mean replacing every if compile_time_condition: in a loop
with

compile_time_condition = compile_time_condition or None
for i in some_loop:
if compile_time_condition is None:
...

It's hard to believe that the overhead of identity checks is
comparable (let alone much higher) to the body of the loop for
anything more complex than pass.

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


Re: An idiom for code generation with exec

2008-06-20 Thread [EMAIL PROTECTED]
On 20 juin, 21:41, eliben [EMAIL PROTECTED] wrote:
  [1] except using compile to build a code object with the function's
  body, then instanciate a function object using this code, but I'm not
  sure whether it will buy you much more performance-wise. I'd personnaly
  prefer this because I find it more explicit and readable, but YMMV.

 How is compiling more readable than exec -

Using compile and function(), you explicitely instanciate a new
function object, while using exec you're relying on a side effect.

 doesn't it require an extra
 step ?

Well... Your way:

   d = {}
  exec code in globals(), d
  return d['foo']

My way:

  return function(compile(code, 'string', 'exec'), globals())

As far as I'm concern, it's two steps less - but YMMV, of course !-)

  You generate code dynamically anyway.

Yes, indeed. Which may or not be the right thing to do here, but this
is a different question (and one I can't actually answer).

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


Re: Tkinter canvas drag/drop obstacle

2008-06-20 Thread Matimus
On Jun 20, 11:10 am, Matimus [EMAIL PROTECTED] wrote:
 On Jun 20, 9:11 am, Peter Pearson [EMAIL PROTECTED] wrote:

  Tkinter makes it very easy to drag jpeg images around on a
  canvas, but I would like to have a target change color when
  the cursor dragging an image passes over it.  I seem to be
  blocked by the fact that the callbacks that might tell the
  target that the mouse has entered it (Enter, Any-Enter,
  even Motion) aren't called if the mouse's button is down.
  What am I missing?  Have I failed to find the right Tkinter
  document?  Is Tkinter the wrong tool for this job?  Thanks.

  --
  To email me, substitute nowhere-spamcop, invalid-net.

 I have used a combination of Motion and B1-Motion. You might also
 throw in a Button-1 event to keep track of whether or not the mouse
 button was down when it entered the widget or not.

 Depending on what you really want to do though, you might take
 advantage of the 'active' state:

 import Tkinter as tk

 can = tk.Canvas()
 can.pack(fill=tk.BOTH, expand=True)

 can.create_rectangle(
         10,10,100,100,
         fill=black,
         activewidth=5,
         activeoutline=blue
         )

 can.mainloop()

 The 'active*' options take effect when the mouse is on top of that
 item.

 If all you are _really_ interested in is a visual indicator, this
 should work for you. Note that there is also a disabled state. I only
 discovered this by looking at the options available and guessing.

  from pprint import pprint
  import Tkinter as tk
  can = tk.Canvas()
  can.pack(fill=tk.BOTH, expand=True)
  r = can.create_rectangle(10,10,100,100)
  pprint(can.itemconfig(r))

 {'activedash': ('activedash', '', '', '', ''),
  'activefill': ('activefill', '', '', '', ''),
  'activeoutline': ('activeoutline', '', '', '', ''),
  'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
  'activestipple': ('activestipple', '', '', '', ''),
  'activewidth': ('activewidth', '', '', '0.0', '0.0'),
  'dash': ('dash', '', '', '', ''),
  'dashoffset': ('dashoffset', '', '', '0', '0'),
  'disableddash': ('disableddash', '', '', '', ''),
  'disabledfill': ('disabledfill', '', '', '', ''),
  'disabledoutline': ('disabledoutline', '', '', '', ''),
  'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
  'disabledstipple': ('disabledstipple', '', '', '', ''),
  'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
  'fill': ('fill', '', '', '', ''),
  'offset': ('offset', '', '', '0,0', '0,0'),
  'outline': ('outline', '', '', 'black', 'black'),
  'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
  'outlinestipple': ('outlinestipple', '', '', '', ''),
  'state': ('state', '', '', '', ''),
  'stipple': ('stipple', '', '', '', ''),
  'tags': ('tags', '', '', '', ''),
  'width': ('width', '', '', '1.0', '1.0')}

 The 'state' option can be set to 'normal', 'hidden' or 'disabled'. So
 if you want to make your canvas items look different when they are
 disabled, set the disabled* options and set 'state' to 'disabled'.

 Matt

I appologize. I didn't actually test this before posting the code, but
if you have the mouse button down before entering an item on the
canvas, even the active state doesn't seem apply. So, well, I hope
someone finds this information useful, but I guess it isn't going to
solve the original posters issue.

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


[ANN] Virtual Worlds Open Grid Protocols testing library in Python (pyogp)

2008-06-20 Thread Lawson English
A new open source project has been started by Linden Lab, makers of the 
Second Life™ virtual worlds, and the Second Life Architecture Working 
Group (AWG) to test LL's proposed virtual worlds Open Grid Protocols 
(OGP) that will allow any virtual world to support multi-world login, 
between-world teleport and other transportation mechanisms, as well as 
asset/property and currency sharing between worlds.



https://wiki.secondlife.com/w/index.php?title=AWG_Test_Harness

The code is released to all comers in Python under an Apache v2 
agreement, although contributions require the signing of Linden Lab's 
developer contribution agreement (giving LL equal copyrights to the 
original contributer--a boilerplate LL corporate requirement that likely 
is redundant given the nature of Apache v2).


svn: http://svn.secondlife.com/trac/linden/browser/projects/2008/pyogp.

Second Life contribution agreement: 
http://secondlifegrid.net.s3.amazonaws.com/docs/SLVcontribution_agmt.pdf


irc: irc://irc.freenode.net/#pyogp

AWG homepage: https://wiki.secondlife.com/wiki/AWG

AWG discussion group homepage: 
https://wiki.secondlife.com/wiki/Category:AW_Groupies


AWG-related pages, irc channels and forums: 
https://wiki.secondlife.com/wiki/Category:AW_Groupies#External_Resources



The near-term AWG goal is to create a set of open standard protocols that:

1) Enable third parties to run servers that connect to the Second Life 
Grid platform
2) Scale the Second Life Grid architecture to support the 
industry-projected situation in the next 10 years, where virtual worlds 
will comprise at least 60 million regions, 2 billion users and in-world 
concurrency of 50-100 million residents.


https://wiki.secondlife.com/wiki/SLGOGP_Draft_1
https://wiki.secondlife.com/wiki/SLGOGP_Teleport_Strawman

The long-term goal is to design a set of protocols and standards that 
will enable almost any virtual world of any kind to plug into this 
system to varying degrees, from support for a universal avatar to full 
support for all Second Life features and/or the ability to inform 
universal 3D viewers what features are supported (or not) in any given 
virtual world.



More info on pyogp and the AWG can be obtained on the website or via irc.


Lawson English
AKA Saijana Kuhn, AWGroupies admin



















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


Re: An idiom for code generation with exec

2008-06-20 Thread [EMAIL PROTECTED]
On 20 juin, 21:44, eliben [EMAIL PROTECTED] wrote:
 On Jun 20, 3:19 pm, George Sakkis [EMAIL PROTECTED] wrote:

(snip)

  It's still not clear why the generic version is so slower, unless you
  extract only a few selected fields, not all of them. Can you post a
  sample of how you used to write it without exec to clarify where the
  inefficiency comes from ?

  George

 The generic version has to make a lot of decisions at runtime, based
 on the format specification.
 Extract the offset from the spec, extract the length.

import operator

transformers = []
transformers.append(operator.itemgetter(slice(format.offset,format.offset
+format.length)))

 Is it msb-
 first ? Then reverse.

if format.msb_first:
transformer.append(reverse)

 Are specific bits required ? If so, do bit
 operations.

etc Python functions are objects, you can define your own callable
(ie: function like) types, you can define anonymous single-expression
functions using lambda, functions are closures too so they can carry
the environment they were defined in, implementing partial application
(using either closures or callable objects) is trivial (and is in the
stdlib functools module since 2.5 FWIW), well... Defining a sequence
of transormer functionals is not a problem neither. And applying it to
your data bytestring is just trivial:

def apply_transformers(data, transormers) :
for transformer in transformers:
data = transformer(data)
return data

... and is not necessarily that bad performance-wide (here you'd have
to benchmark both solutions to know for sure).

 A dynamically generated function doesn't have to make any decisions -

No, but neither does a sequence of callable objects. The decisions are
taken where you have the necessary context, and applied somewhere
else. Dynamically generating/compiling code is one possible solution,
but not the only one.


 I guess this is not much different from Lisp macros
The main difference is that Lisp macro are not built as raw string,
but as first class objects. I've so found this approach more flexible
and way easier to maintain, but here again, YMMV.

Anyway, even while (as you may have noticed by now) I'm one of these
there's-a-better-way-than-eval-exec peoples, I'd think you may
(depending on benchmarks with both solutions and real-life data) have
a valid use case here - and if you encapsulate this part correctly,
you can alway start with your current solution (so you make it work),
then eventually switch implementation later if it's worth the extra
effort...


Just my 2 cents. Truth is that as long as it works and is
maintainable, then who cares...
--
http://mail.python.org/mailman/listinfo/python-list


inheritance question...

2008-06-20 Thread Hamish McKenzie

I have this class:

class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=TOL ):
   print tolerance


shortened for clarity obviously.  so I want to subclass this class like
so:

class BigVector(Vector)
 TOL = 100


for example if I was working with large vectors which I knew would never
be very close hence the large tolerance.  this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.


which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__eq__( otherVec )


prints 100 instead of 1e-5?

does this question make sense?  not sure how clearly I'm phrasing my
question...  any of you guys python experts?


I *could* do this, but its ugly:

class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=None ):
   if tolerance is None: tolerance = self.TOL
   print tolerance


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


Re: inheritance question...

2008-06-20 Thread Guilherme Polo
On Fri, Jun 20, 2008 at 6:19 PM, Hamish McKenzie
[EMAIL PROTECTED] wrote:

 I have this class:

 class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=TOL ):
   print tolerance


 shortened for clarity obviously.  so I want to subclass this class like
 so:

 class BigVector(Vector)
 TOL = 100


 for example if I was working with large vectors which I knew would never
 be very close hence the large tolerance.  this doesn't work however -
 the TOL class variable, while overridden in BigVector, is still using
 the Vector.TOL variable in the __eq__ method.


 which kinda makes sense to a certain degree, but how do I get the
 behaviour where doing:

 BigVector().__eq__( otherVec )

No, don't do this. Just do avector == othervector



 prints 100 instead of 1e-5?

 does this question make sense?  not sure how clearly I'm phrasing my
 question...  any of you guys python experts?


 I *could* do this, but its ugly:

 class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=None ):
   if tolerance is None: tolerance = self.TOL
   print tolerance


class Vector(object):
TOL = 1e-5
def __eq__(self, other):
   print self.TOL


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




-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe, PyQT, QtWebKit and jpeg problem

2008-06-20 Thread David Boddie
On Friday 20 June 2008 17:24, Phil Thompson wrote:

 On Fri, 20 Jun 2008 08:04:57 -0700 (PDT), Carbonimax
 [EMAIL PROTECTED] wrote:

 I have a problem with py2exe and QtWebKit :
 I make a program with a QtWebKit view.
 If I launch the .py directly, all images (jpg, png) are displayed but
 if I compile it with py2exe I have only png images. No jpg !
 No error message, nothing.
 
 Have you a solution ? Thank you.
 
 At a guess, the JPEG support is implemented as a Qt plugin which you are
 not including.

Yes, that would appear to the be most obvious cause. See here for another
report about this:

http://lists.trolltech.com/qt4-preview-feedback/2008-03/msg00064.html

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


Re: How do I create a new Node using pulldom?

2008-06-20 Thread Paul Boddie
On 20 Jun, 22:17, [EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:
 I'm using xml.dom.pulldom to parse through an XML file. I use
 expandNode() to scrutinize certain blocks of it that I'm interested
 in.

Right. This is the thing which differentiates pulldom from traditional
DOM implementations, which I'll discuss below.

 Once I find a block of XML in the input file that I'm interested in, I
 need to add my own block MyTag./MyTag to the pulldom tree I'm
 building in memory.

Understood.

 The documentation on PullDom is worse than atrocious. It is simply non-
 existant. I can't even find a simple explanation of what the functions
 are named and what arguments they take. Sheesh.

Sheesh, indeed. I think the authors assumed a familiarity with the DOM
APIs, but they're regarded as being somewhat old school by many
these days, so it's possible that you aren't too familiar with the
PyXML/DOM style of the API employed.

 When I have a node N of the tree, I think that I can use
 N.appendChild() to do what I want (just guessing from the function
 name which I can see).

 appendChild takes 1 argument -- a new node.

Correct. I think that you also need to have expanded the part of the
document where the node will be placed. For example:

  stream = xml.dom.pulldom.parseString(s)
  for event, node in stream:
  if some condition:
  stream.expandNode(node)

 But I don't know how to create such a node.

This is a traditional DOM activity, but it's easy to be disoriented if
you don't already have a document object (which has the necessary
methods). However, such an object is readily available:

  doc = node.ownerDocument

You can then create a node using the usual create* methods. For
example:

  element = doc.createElement(new)

And then you can use appendChild on the original node:

  node.appendChild(element)

Note that since the document under node has been expanded, subsequent
nodes pulled from the stream will start with an END_ELEMENT event
involving the node concerned.

 Can someone out there please post a code fragment showing how to
 create a pulldom node? The simpler and more commented it is the
 better.

Try this:

  import xml.dom.pulldom
  s = testnodexxx/node/test

  # Start parsing.
  stream = xml.dom.pulldom.parseString(s)

  # Process each event.
  for event, node in stream:

  # Do the addition of an element within node.
  if event == xml.dom.pulldom.START_ELEMENT and \
  node.localName == node:

  # Have to expand first.
  stream.expandNode(node)

  # Get the document and create the element.
  doc = node.ownerDocument
  element = doc.createElement(new)

  # Add the element; see what we've produced.
  node.appendChild(element)
  print node.toxml()

Hope this helps!

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


Time-out for regular expressions...

2008-06-20 Thread [EMAIL PROTECTED]
I'm doing a lot of regular expressions these days.  Sometimes when I'm
crafting them I mess up, and make them too complicated.  So that when
my program runs, they take forever.  (Maybe not literally forever---I
abort the program after a few seconds.)

What I'd like to have happen is every time I search using a regular
expression is after a few seconds (the exact number being user
definable), it will stop searching, perhaps by raising an exception.

How can I implement this?

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


optparse functionality missing

2008-06-20 Thread Jeff Keasler


Hi,

optparse doesn't seem to have a pass-through capability for command line 
parameters/options that were not registered with add_option.


I'm not the first person to complain about this.  On Wed Mar 17 08:20:10 CET 
2004, there's a thread titled Perceived optparse shortcomings where someone 
complains of the same problem.


In a scripting environment, I often want to strip some of the command line 
options off the argument list, and then pass the remaining options to another 
module that is deeper in the tool chain.


optparse doesn't seem to allow this, as far as I can tell.  It requires that 
you register all possible options with add_option() or an error is flagged. 
When my second tier module is an autoconf script that could have hundreds of 
its own options, it seems dumb to have to register all those options, just to 
have to reconvert them to command-line options so that I can pass them to the 
autoconf command line.


Could we get a mode added to optparse so that any commandline 
parameters/options that are not registered via add_option() can be in the args 
return value of the parse_args() method?


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


Re: optparse functionality missing

2008-06-20 Thread Robert Kern

Jeff Keasler wrote:


Hi,

optparse doesn't seem to have a pass-through capability for command line 
parameters/options that were not registered with add_option.


I'm not the first person to complain about this.  On Wed Mar 17 08:20:10 
CET 2004, there's a thread titled Perceived optparse shortcomings 
where someone complains of the same problem.


In a scripting environment, I often want to strip some of the command 
line options off the argument list, and then pass the remaining options 
to another module that is deeper in the tool chain.


optparse doesn't seem to allow this, as far as I can tell.  It requires 
that you register all possible options with add_option() or an error is 
flagged. When my second tier module is an autoconf script that could 
have hundreds of its own options, it seems dumb to have to register all 
those options, just to have to reconvert them to command-line options so 
that I can pass them to the autoconf command line.


Could we get a mode added to optparse so that any commandline 
parameters/options that are not registered via add_option() can be in 
the args return value of the parse_args() method?


If you code it up with unit tests and documentation, it has a good chance. But 
in the meantime, you can tell optparse to stop processing options using the 
standard -- marker. For example:


$ cat mycommand.py
import optparse

parser = optparse.OptionParser()
parser.add_option('--known-option', action='store_true')

options, args = parser.parse_args()
print 'options = %r' % (options,)
print 'args = %r' % (args,)

$ python mycommand.py --known-option -- --configure-flag --other-unknown
options = Values at 0xb4a7d8: {'known_option': True}
args = ['--configure-flag', '--other-unknown']

--
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco

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


  1   2   >