how to debug python application crashed occasionally

2010-04-21 Thread jacky wang
Hello

  recently, I met a problem with one python application running with
python2.5 | debian/lenny adm64 system: it crashed occasionally in our
production environment. The problem started to happen just after we
upgraded the python application from python2.4 | debian/etch amd64.

  after configuring the system to enable core dump & debugging with
the core dumps by following the guide line from 
http://wiki.python.org/moin/DebuggingWithGdb,
I became more confused about that.

  The first crash case was happening in calling python-xml module,
which is claimed as a pure python module, and it's not supposed to
crash python interpreter. because the python application is relatively
a big one, I can not show u guys the exact source code related with
the crash, but only the piece of python modules. GDB shows it's
crashed at string join operation:

#0  string_join (self=0x7f7075baf030, orig=)
at ../Objects/stringobject.c:1795
1795../Objects/stringobject.c: No such file or directory.
in ../Objects/stringobject.c

and pystack macro shows the details:

gdb) pystack
/usr/lib/python2.5/StringIO.py (271): getvalue
/usr/lib/python2.5/site-packages/_xmlplus/dom/minidom.py (62):
toprettyxml
/usr/lib/python2.5/site-packages/_xmlplus/dom/minidom.py (47): toxml

  at that time, we also found python-xml module has performance issue
for our application, so we decided to use python-lxml to replace
python-xml. After that replacement, the crash was gone. That's a bit
weird for me, but anyway, it's gone.

  Unfortunately, another two 'kinds' of crashes happening after that,
and the core dumps show they are not related with the replacement.

  One is crashed with "Program terminated with signal 11", and the
pystack macro shows it's crashed at calling the built-in id()
function.

#0  visit_decref (op=0x20200a3e22726574, data=0x0) at ../Modules/
gcmodule.c:270
270 ../Modules/gcmodule.c: No such file or directory.
in ../Modules/gcmodule.c


  Another is crashed with "Program terminated with signal 7", and the
pystack macro shows it's crashed at the exactly same operation (string
join) as the first one (python-xml), but in different library python-
simplejson:

#0  string_join (self=0x7f5149877030, orig=)
at ../Objects/stringobject.c:1795
1795../Objects/stringobject.c: No such file or directory.
in ../Objects/stringobject.c

(gdb) pystack
/var/lib/python-support/python2.5/simplejson/encoder.py (367): encode
/var/lib/python-support/python2.5/simplejson/__init__.py (243): dumps

  I'm not good at using gdb & C programming, then I tried some other
ways to dig further:
   * get the source code of python2.5, but can not figure out the
crash reason :(
   * since Debian distribution provides python-dbg package, and I
tried to use python2.5-dbg interpreter, but not the python2.5, so that
I can get more debug information in the core dump file. Unfortunately,
my python application is using a bunch of C modules, and not all of
them provides -dbg package in Debian/Lenny. So it still doesn't make
any progress yet.

  I will be really appreciated if somebody can help me about how to
debug the python crashes.

  Thanks in advance!

BR
Jacky Wang
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Chris Rebert
On Tue, Apr 20, 2010 at 9:52 PM, Ben Finney  wrote:
> Sebastian  writes:
>> All locales return error messages in English. Only the Japanese uses
>> Japanese which my regular expressions cannot handle at the moment.
>
> What exactly are you expecting to happen, and what exactly happens
> instead?
>
> General advice with character sets in Python apply: always explicitly
> declare the encoding of input, then decode to Unicode interally as early
> as possible, and process all text that way. Only fix into an encoding
> when it's time to output.

I think he has more of a *literal* language problem: He doesn't know
Japanese and thus can't read the Japanese error message in order to
develop a regex for it. I assume there's some reason he can't just do
a blind equality test on the error message string(s).

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python glibc crypt() function

2010-04-21 Thread Peter Otten
luca72 wrote:

> On 20 Apr, 19:38, Peter Otten <__pete...@web.de> wrote:
>> luca72 wrote:
>> > Hello i have to do this :
>> > glibc crypt() function, using salt $1$abcdefgh$
>>
>> > cryptPw = crypt(plainPw, "$1$abcdefgh$")
> Thanks
> The result is correct i obtain the same with ctypes and crypt module,
> so i think that is better to use the crypt module is correct?

Yes, use the crypt module.
-- 
http://mail.python.org/mailman/listinfo/python-list


how does a queue stop the thread?

2010-04-21 Thread kaiix
A simple thread pool example. My question is, since *MyThread.run*
will loop endless, how does the thread know the time to quit? how does
the *queue* notify the thread? is there any shared variables, like a
*lock*?

When I set daemon false, it stays in the loop, not quit any more.
what's the role does the daemon state plays?

code:

import Queue
import threading

def do_some_thing(x):
print int(x)

class MyThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue

def run(self):
while True:
params = self.queue.get()
do_some_thing(params)
self.queue.task_done()

q = Queue.Queue()

for i in range(1, 5):
t = MyThread(q)
t.setDaemon(True)
t.start()

for x in range(10):
q.put(x)

q.join()
-- 
http://mail.python.org/mailman/listinfo/python-list


error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
Hello.

I read a string from an utf-8 file:

fichierLaTeX = codecs.open(sys.argv[1], "r", "utf-8")
s = fichierLaTeX.read()
fichierLaTeX.close()

I can then print the string without error with 'print s'.

Next I parse this string:

def parser(s):
  i = 0
  while i < len(s):
if s[i:i+1] == '\\':
   i += 1
   if s[i:i+1] == '\\':
 print "backslash"
   elif s[i:i+1] == '%':
  print "pourcentage"
   else:
  if estUnCaractere(s[i:i+1]):
motcle = ""
while estUnCaractere(s[i:i+1]):
  motcle += s[i:i+1]
  i += 1
   print "mot-clé '"+motcle+"'"

but when I run this code, I get this error:

Traceback (most recent call last):
  File "./versOO.py", line 115, in 
  parser(s)
File "./versOO.py", line 105, in parser
print "mot-clé '"+motcle+"'"
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
position 6: ordinal not in range(128)

What must I do to solve this?

Thanks!

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


Re: Operations on sparse matrices

2010-04-21 Thread Helmut Jarausch
On 04/19/10 08:03, pp wrote:
> I am currently dealing with sparse matrices and have doubts on whether
> we can use
> 1.) dot (for matrix multiplication) and inv (inverse) operations of
> numpy on sparse matrices of CSR format.
> 

I don't know of any use of the inverse of a sparse matrix.
Note, in nearly all cases the inverse of a sparse matrix is a full matrix.
Instead of inverting a matrix solve a linear system with that matrix.
What do you need the inverse for?

Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how does a queue stop the thread?

2010-04-21 Thread Kushal Kumaran
On Wed, Apr 21, 2010 at 1:38 PM, kaiix  wrote:
> A simple thread pool example. My question is, since *MyThread.run*
> will loop endless, how does the thread know the time to quit? how does
> the *queue* notify the thread? is there any shared variables, like a
> *lock*?
>
> When I set daemon false, it stays in the loop, not quit any more.
> what's the role does the daemon state plays?
>

Take a look at the documentation for the threading module here:
http://docs.python.org/release/2.6/library/threading.html, and the
discussion on daemon vs non-daemon threads here:
http://blog.doughellmann.com/2008/01/pymotw-threading_13.html

Basically, as long as at least one non-daemon thread is still running,
your program will not exit.

> 

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


Re: how does a queue stop the thread?

2010-04-21 Thread Kushal Kumaran
On Wed, Apr 21, 2010 at 1:50 PM, Kushal Kumaran
 wrote:
> On Wed, Apr 21, 2010 at 1:38 PM, kaiix  wrote:
>> A simple thread pool example. My question is, since *MyThread.run*
>> will loop endless, how does the thread know the time to quit? how does
>> the *queue* notify the thread? is there any shared variables, like a
>> *lock*?
>>
>> When I set daemon false, it stays in the loop, not quit any more.
>> what's the role does the daemon state plays?
>>
>
> Take a look at the documentation for the threading module here:
> http://docs.python.org/release/2.6/library/threading.html, and the
> discussion on daemon vs non-daemon threads here:
> http://blog.doughellmann.com/2008/01/pymotw-threading_13.html
>
> Basically, as long as at least one non-daemon thread is still running,
> your program will not exit.
>
>> 
>

And I should have read your mail better.  Take a look at the
documentation of the task_done method:
http://docs.python.org/library/queue.html

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


Re: Write web apps in Python?

2010-04-21 Thread Bruno Desthuilliers

Bryan a écrit :


I think I see what you mean


Err...

-- correct me if I'm wrong: 


You are, sorry !-)


You want to
keep complex application data structures around between requests.


Nope. I want to keep all my settings parsed, my librairies loaded, all 
my connections opened etc. That is, all the time consuming stuff at app 
startup - which, with PHP, mostly happens for each and every request.



PHP frameworks generally allow and encourage application code
to be independent of the underlying plumbing.

This is debatable at best. PHP code (except cli PHP code of course) is
written without any care for persistent global state, concurrency
issues, race conditions etc - because it's written with the idea that
the code serving a request will be runned in total isolation. CGI
heritage here, obviously.


No, that's good web-app structure, regardless of language and server
interface. If we keep persistent global state in a shared database
rather than program variables,


Err... Did you really read what you're answering too ???

Also, I never said this execution model was necessarily bad - just that 
it had pros *and* cons.



And please note I'm not criticizing this
design- just pointing one of it's consequences.


Many large,
sophisticated, high-volume web apps are in PHP.

Did anyone pretend otherwise ?


How about this howler: "The PHP execution model (mostly based on CGI
FWIW) tends to be a bit unpractical for non-trivial applications".


"tends to be a bit unpractical" != "doesn't work".

Many large, sopĥisticated etc applications are written in C. Does that 
make C a practical application programming language ?


Now I'm sorry to say that for quite a few "sophisticated" PHP apps I've 
seen (and eventually had to work on), the "startup" part - parsing the 
include files, configuration, establishing connections etc - took a good 
part of the total processing time.

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


Re: Is it better to extend a class, or do something repetitious in the main part of a program?

2010-04-21 Thread Vinay Sajip
On Apr 19, 3:45 pm, J  wrote:
> First, before I get farther,
>
> Is there a way for theloggingmodule to natively handle lists and
> dict objects whenlogging?
>
> e.g. take this {'key1':'val1','key2':'val2'} and have it logged like this:
>
> INFO: key1: val1
> INFO: key2: val2
>
> If I pass the dict or list directly to the logger, it is logged the
> same as if you simply did this:
>
> mydict={1:1, 2:2}
> mylist=[1,2,3]
>
> print mydict
> print mylist
>
> >>> {1:1,2:2}
> >>> [1,2,3]
>
> It came up that I wanted to haveloggingpresent command line options
> from optparse if the log level was set to debug...  so they'd look
> something like this in the output:
>
> debug: True
> sleep_time: 30
> log_file: /tmp/testlog
>
> So the options I've managed to work out are to either parse the list
> or dict object item by item and feed those items one at a time into
> the logger:
>
> for i in mylist:
>    logging.info(i)
>
> Or to extend the StreamHandler class to handle this by creating a new
> report.msg...
>
> Then the discussion came up: which is better?  To parse a dictionary
> or list in the main code and pass each item to the logger one at a
> time, or to extend the logger to handle it natively?
>
> Any thoughts on which is the more proper way to handle cases like this?

Since you want to have a specific output format for the logged
information, the best way to go would be to write your own Formatter
subclass, and check in its format() method whether you have lists/
dicts and then format them however you want into a string, and return
the appropriately formatted string from that method.

Regards,

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread Peter Otten
f...@slick.airforce-one.org wrote:

> Hello.
> 
> I read a string from an utf-8 file:
> 
> fichierLaTeX = codecs.open(sys.argv[1], "r", "utf-8")
> s = fichierLaTeX.read()
> fichierLaTeX.close()
> 
> I can then print the string without error with 'print s'.
> 
> Next I parse this string:
> 
> def parser(s):
>   i = 0
>   while i < len(s):
> if s[i:i+1] == '\\':
>i += 1
>if s[i:i+1] == '\\':
>  print "backslash"
>elif s[i:i+1] == '%':
> print "pourcentage"
>else:
>   if estUnCaractere(s[i:i+1]):
> motcle = ""
> while estUnCaractere(s[i:i+1]):
> motcle += s[i:i+1]
> i += 1
>print "mot-clé '"+motcle+"'"
> 
> but when I run this code, I get this error:
> 
> Traceback (most recent call last):
>   File "./versOO.py", line 115, in 
>   parser(s)
> File "./versOO.py", line 105, in parser
> print "mot-clé '"+motcle+"'"
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
> position 6: ordinal not in range(128)
> 
> What must I do to solve this?

>>> "mot-clé" + "mot-clé"
'mot-cl\xc3\xa9mot-cl\xc3\xa9'

>>> u"mot-clé" + u"mot-clé"
u'mot-cl\xe9mot-cl\xe9'

>>> "mot-clé" + u"mot-clé"
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: 
ordinal not in range(128)

codecs.open().read() returns unicode, but your literals are all bytestrings.
When you are mixing unicode and str Python tries to convert the bytestring 
to unicode using the ascii codec, and of course fails for non-ascii 
characters.

Change your string literals to unicode by adding the u-prefix and you should 
be OK.

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


rfind bug ?

2010-04-21 Thread Stef Mientki

With the following code, I would expect a result of 5 !!

>>> a= 'word1 word2 word3'
>>> a.rfind(' ',7)
11

Is this a bug ?

thanks,
Stef

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


Re: rfind bug ?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki  wrote:
> With the following code, I would expect a result of 5 !!
>
 a= 'word1 word2 word3'
 a.rfind(' ',7)
> 11
>
> Is this a bug ?

No. Don't you think someone would have found such an obvious bug by now?

You want regular str.find(), which searches from left-to-right:
 a= 'word1 word2 word3'
>>> a.find(' ')
5

str.rfind() is a variant of str.find() that searches from
right-to-left. The "r" is for "right".

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rfind bug ?

2010-04-21 Thread James Mills
On Wed, Apr 21, 2010 at 6:51 PM, Stef Mientki  wrote:
>
> With the following code, I would expect a result of 5 !!
>
 a= 'word1 word2 word3'
 a.rfind(' ',7)
> 11
>
> Is this a bug ?

Python's documentation states:

 |  rfind(...)
 |  S.rfind(sub [,start [,end]]) -> int
 |
 |  Return the highest index in S where substring sub is found,
 |  such that sub is contained within s[start:end].  Optional
 |  arguments start and end are interpreted as in slice notation.
 |
 |  Return -1 on failure.

"Return the highest index in S"

I haven't looked at python's source code for the
str object, but perhaps this is exactly what it's
algorithm does!

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


Re: rfind bug ?

2010-04-21 Thread Peter Otten
Chris Rebert wrote:

[didn't see the original message]

> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki 
> wrote:
>> With the following code, I would expect a result of 5 !!
>>
> a= 'word1 word2 word3'
> a.rfind(' ',7)
>> 11
>>
>> Is this a bug ?
> 
> No. Don't you think someone would have found such an obvious bug by now?

Indeed.

OP: you may be looking for

>>> a = "a bb ccc"
>>> a[::-1].find(" ")
3

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
> Change your string literals to unicode by adding the u-prefix and you should 
> be OK.

Thanks, it solved the problem... for a while!

I need now to know if s[i] gives the next byte or the next character,
when I scan the string s. I've googled pages about python and unicode,
but didn't find a solution to that. I scan the string read from the
file char by char to construct s, but now get the same error when just
trying 'print s'.

Is there a way to tell python that all strings and characters are to
be treated as UTF-8? I have LC_ALL=en_GB.utf-8 in my shell, but python
does'nt seem to use this variable?

Thanks!

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


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Sebastian
> General advice with character sets in Python apply: always explicitly
> declare the encoding of input, then decode to Unicode interally as early
> as possible, and process all text that way. Only fix into an encoding
> when it's time to output.

Maybe I was too vague when describing my problem. As Chris correctly
guessed, I have a literal language problem.

> > All locales return error messages in English. Only the Japanese uses
> > Japanese which my regular expressions cannot handle at the moment.
>
> What exactly are you expecting to happen, and what exactly happens
> instead?

My regular expressions turn the Amazon error messages into Python
exceptions.

This works fine as long as they are in English: "??? is not a valid
value for BrowseNodeId. Please change this value and retry your
request.", for instance, will raise an InvalidParameterValue
exception. However, the Japanese version returns the error message "???
は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエストを実行してください。" which will not be
successfully handled.

This renders the my module pretty much useless for Japanese users.

I'm was therefore wondering if someone with more knowledge of Japanese
than me can have a look at my expressions. Maybe the Japanese messages
are completely different...

I have a collection of sample messages here (all files *-jp-*.xml):
http://bitbucket.org/basti/python-amazon-product-api/src/tip/tests/2009-11-01/

Any help is appreciated!

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 2:29 AM,   wrote:
>> Change your string literals to unicode by adding the u-prefix and you should
>> be OK.
>
> Thanks, it solved the problem... for a while!
>
> I need now to know if s[i] gives the next byte or the next character,
> when I scan the string s. I've googled pages about python and unicode,
> but didn't find a solution to that. I scan the string read from the
> file char by char to construct s, but now get the same error when just
> trying 'print s'.

Assuming s = fichierLaTeX.read() as from your code snippet, the next
character. When in doubt, check what `type(s)` is; if it's , indices are in bytes; if it's , indices are in
code points.

Please give the full stack traceback for your error.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code redundancy

2010-04-21 Thread Jean-Michel Pichavant

Alan Harris-Reid wrote:

Jean-Michel Pichavant wrote:

Alan Harris-Reid wrote:

Hi,

During my Python (3.1) programming I often find myself having to 
repeat code such as...


class1.attr1 = 1
class1.attr2 = 2
class1.attr3 = 3
class1.attr4 = 4
etc.

Is there any way to achieve the same result without having to repeat 
the class1 prefix?  Before Python my previous main language was 
Visual Foxpro, which had the syntax...


with class1
  .attr1 = 1
  .attr2 = 2
  .attr3 = 3
  .attr4 = 4
  etc.
endwith

Is there any equivalent to this in Python?

Any help would be appreciated.

Alan Harris-Reid

Hello,

Use an effective text editor, repeating stuff should not be a 
problem. In a more general manner, avoid trying to speed your writing 
while you should care speeding the reading.
Most of the tricks you could use will confuse the reader (unless the 
reader is familiar with Visual foxpro).


Anyway,

for attrName, value in [
   ('attr1', 1),
   ('attr2', 2),
   ('attr3', 3),
   ]:
   setattr(class1, attrName, value)

or

class Foo:
   def __init__(self):
  self.attr1=None
  self.attr2=None
  self.attr3=None

   def set(self, *args, **kwargs):
  for k in kwargs:
 if hasattr(self, k):
 setattr(self, k, kwargs[k])
 else:
   raise AttributeError('%s instance has no attribute "%s"' % 
(self.__class__.__name__, k))


f = Foo()
f.set(attr1=25)
print f.__dict__
f.set(attr3=4, attr2=89)
print f.__dict__
f.set(bar= 8)

output:
{'attr2': None, 'attr3': None, 'attr1': 25}
{'attr2': 89, 'attr3': 4, 'attr1': 25}
AttributeError: Foo instance has no attribute "bar"


JM


Hi Jean-Michel,

Interesting solutions, but I think for the effort involved (and 
readability) I'll stick to repeating the class.


Regards,
Alan

that's the way to go :)

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


Re: how does a queue stop the thread?

2010-04-21 Thread kaiix
@kushal, thanks for your replies.

before i wrote the email, i've already read the python docs carefully.
i need the proof from code, i mean python source code. i tried to
prove some of my assumptions that lead the loop quit, and i traced
back to Queue.py, threading.py, dummy_thread.py, now i need some hints
to help me understanding the sample from python source code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Write web apps in Python?

2010-04-21 Thread Adam Tauno Williams
On Wed, 2010-04-21 at 10:28 +0200, Bruno Desthuilliers wrote:
> Bryan a écrit :
> > 
> > I think I see what you mean
> 
> Err...
> 
> > -- correct me if I'm wrong: 
> 
> You are, sorry !-)
> 
> > You want to
> > keep complex application data structures around between requests.
> 
> Nope. I want to keep all my settings parsed, 

Store them in the session.
> my librairies loaded, 

Enable APC

> all my connections opened etc.

If you are talking about RDBMS connections, use persistent connections.

Then you have everything you've asked for.

> Now I'm sorry to say that for quite a few "sophisticated" PHP apps I've 
> seen (and eventually had to work on), the "startup" part - parsing the 
> include files, configuration, establishing connections etc - took a good 
> part of the total processing time.


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


Re: rfind bug ?

2010-04-21 Thread Paul Rudin
Peter Otten <__pete...@web.de> writes:


> OP: you may be looking for
>
 a = "a bb ccc"
 a[::-1].find(" ")
> 3


But you should be aware of the effeciency implications of doing
this. a[::-1] constructs a new list. It's probably faster to do e.g.:
len(a) - a.rfind(..) - 1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: deleting objects present in a list

2010-04-21 Thread Dave Angel
(For some reason you posted your response before the message you were 
replying to.  That's called Top-posting, and is bad form on these 
mailing lists)


Sandy wrote:

Thanks for the replies.

Terry,
What does 'immediately' mean? I did a small test and here are the
results.

import psutil

def testing():
class Object():
pass

l =}
apm =sutil.avail_phymem()/(1024*1024)
print 'Before creating objs: ' + repr(apm)

for i in xrange(50):
l.update({Object():1})

apm =sutil.avail_phymem()/(1024*1024)
print 'After creating objs: ' + repr(apm)
return l

def hello():
myl =esting()

apm =sutil.avail_phymem()/(1024*1024)
print 'Before deleting: ' + repr(apm)

del myl

# Here I want to delete the objects in the list
# deleting myl doesn't seem to change the memory

apm =sutil.avail_phymem()/(1024*1024)
print 'After deleting: ' + repr(apm)


if __name__ ='__main__':
hello()

OUTPUT:
Before creating objs: 2516L
After creating objs: 2418L
Before deleting: 2418L
After deleting: 2430L

In my original case the memory is not getting released even after long
time.

- dksr


On Apr 20, 8:44 pm, Terry Reedy  wrote:
  

On 4/20/2010 3:21 PM, Sandy wrote:



Hi all,
I have large number of objects created and to handle them properly, I
store them in a list. How can I delete all of these objects (delete I
mean here is to remove the object from memory not just from list)?
I cannot use the list to iterate through the objects to delete them.
Because 'del' only reduces the reference count and as it is present in
the list it is not deleted. I cannot delete the list because I loose
control over the objects.
  

Deleting the list is the best you can do. If that deletes the last
reference, then the interpreter will delete the object when it feels
like it. For *current* CPython, this will be immediately. For other
implementations, whenever.



  
First, you're using some 3rd party library for measuring some kind of 
memory usage.  I'd guess you're probably using 
http://code.google.com/p/psutil/  Since I'm not familiar with 
how they derive these numbers, I can only make a good guess as to how 
valid they are.   And if avail_phymem refers to what its name implies, 
it has little to do with Python.  Python doesn't control physical 
memory, only virtual.


So let's skip those numbers and talk about what CPython actually does.  
As others have pointed out, other implementations will be different.


When you delete a large number of objects (which you can do with 
myl=None), CPython may keep some of the memory under its own control for 
reuse.  A future object of the same size will fit nicely in the hole, 
and that may be faster than calling free() and malloc() again.


When CPython calls free(), the C runtime library almost certainly keeps 
the memory for reuse by subsequent calls to malloc(). Operating system 
calls for allocating and freeing memory are (usually) done in larger, 
fixed-size blocks.  In Windows for example, the granularity is 4k or 64k 
for the more efficient methods of memory allocation.  Swapfile 
allocation, for example, is always in 4k multiples.  See function call 
VirtualAlloc().  Anyway, if there's even a single allocated byte in a 
block, it can't release the block.  And searching for such blocks is slow.


When the operating system is told to free something, it usually does not 
"free" physical memory immediately.  In the case of Windows, it marks 
the block as available, and eventually a daemon task will zero it.  But 
it could very well be "charged to" the current process until some other 
process needs the physical memory.  What it does do is free it from 
virtual memory.  But notice that Virtual memory is represented by a 
swapfile on disk, and Windows doesn't support a file with gaps in it 
(sparse allocation).  So unless this particular allocation is at the end 
of the file, the size isn't likely to go down.



If you really want to "observe" that the memory has been "released,"  
I'd suggest calling a similar testing() function a second time, with 
objects of the same size, and see whether the numbers get any worse.  
I'd say they won't, at least not by much, assuming there's any validity 
to this avail_phymem() function.


I also have to point out that using "del myl" is not necessary for 
freeing up the memory.  All that's necessary is for the myl name to stop 
referring to the list, and the list will go away.  So myl=42 will work 
just as well.


HTH
DaveA


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


shove does not store data as expected

2010-04-21 Thread Alex
Dear all,
I'm trying to use the shove module (http://pypi.python.org/pypi/shove)
for a simple script. The script read a CSV file ad store the data.
When I check the content of the "store" object (instance of Shove)
*before* I close it, the data are all there but when I close and re-
open it some data are lost. How it is possible? There is something
wrong in my code or I didn't understand how shove works?

Thanks in advance.

Here is a sample of my code:

DBNAME = "file://store.db"

csv_file ="test.csv"
cities = csv.reader(open(csv_file), delimiter=";")
store = Shove(DBNAME, compress=True)
for city,region,code in cities:
entry_list = store.setdefault(region, [])
data = 'just a sample'
entry = {city:data}
entry_list.append(entry)

print "Before closing"
for k,v in store.iteritems():
print "Key->", k
print "Value->", v
print ""
store.close()

store = Shove(DBNAME, compress=True)
print "After closing and re-opening"
for k,v in store.iteritems():
print "Key->", k
print "Value->", v
print ""

Here is the output

Before closing
Key-> Marche
Value-> [{'Ancona': 'just a sample'}, {'Ascoli Piceno': 'just a
sample'}]
Key-> Piemonte
Value-> [{'Alessandria': 'just a sample'}, {'Asti': 'just a sample'}]
Key-> Puglia
Value-> [{'Bari': 'just a sample'}, {'Barletta-Andria-Trani': 'just a
sample'}]
Key-> Valle d'Aosta
Value-> [{'Aosta': 'just a sample'}]

After closing and re-opening
Key-> Marche
Value-> [{'Ancona': 'just a sample'}]
Key-> Piemonte
Value-> [{'Alessandria': 'just a sample'}]
Key-> Puglia
Value-> [{'Bari': 'just a sample'}, {'Barletta-Andria-Trani': 'just a
sample'}]
Key-> Valle d'Aosta
Value-> [{'Aosta': 'just a sample'}]


Here is the content of the CSV file:

Alessandria;Piemonte;AL
Ancona;Marche;AN
Aosta;Valle d'Aosta;AO
Ascoli Piceno;Marche;AP
Asti;Piemonte;AT
Bari;Puglia;BA
Barletta-Andria-Trani;Puglia;BT

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


Re: rfind bug ?

2010-04-21 Thread Stef Mientki
On 21-04-2010 10:56, Chris Rebert wrote:
> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki  wrote:
>   
>> With the following code, I would expect a result of 5 !!
>>
>> 
> a= 'word1 word2 word3'
> a.rfind(' ',7)
>   
>> 11
>>
>> Is this a bug ?
>> 
> No. Don't you think someone would have found such an obvious bug by now?
>   
if it's not a bug,
then the start index has no meaning ...
... and some would call that a bug.

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


Re: rfind bug ?

2010-04-21 Thread Peter Otten
Paul Rudin wrote:

> Peter Otten <__pete...@web.de> writes:
> 
> 
>> OP: you may be looking for
>>
> a = "a bb ccc"
> a[::-1].find(" ")
>> 3
> 
> 
> But you should be aware of the effeciency implications of doing
> this. a[::-1] constructs a new list. 

A new string, yes.

> It's probably faster to do e.g.:
> len(a) - a.rfind(..) - 1

Yes, especially if the string is "long":

$ python -m timeit -s'a = "word1 word2 word3"' 'a[::-1].rfind(" ")'
100 loops, best of 3: 0.834 usec per loop
$ python -m timeit -s'a = "word1 word2 word3"*100' 'a[::-1].rfind(" ")'
10 loops, best of 3: 5.04 usec per loop

$ python -m timeit -s'a = "word1 word2 word3"' 'len(a)-a.rfind(" ")-1'
100 loops, best of 3: 0.587 usec per loop
$ python -m timeit -s'a = "word1 word2 word3"*100' 'len(a)-a.rfind(" ")-1'
100 loops, best of 3: 0.592 usec per loop

But be aware of the following difference:

>>> a[::-1].rfind("not there")
-1

>>> len(a) - a.rfind("not there") -1
8

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


Re: how does a queue stop the thread?

2010-04-21 Thread James Mills
On Wed, Apr 21, 2010 at 7:36 PM, kaiix  wrote:
> before i wrote the email, i've already read the python docs carefully.
> i need the proof from code, i mean python source code. i tried to
> prove some of my assumptions that lead the loop quit, and i traced
> back to Queue.py, threading.py, dummy_thread.py, now i need some hints
> to help me understanding the sample from python source code.

You would need to look into the source code of
python's builtin 'thread' module which is the low-level
threading implementation used by python's 'threading'
module (which is a wrapper atop this).

'threading' aside from everything else it does, really in the
end calls thread.start_new_thread(...)

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


Re: Write web apps in Python?

2010-04-21 Thread Paul Rubin
Bruno Desthuilliers  writes:
> Nope. I want to keep all my settings parsed, my librairies loaded, all
> my connections opened etc. That is, all the time consuming stuff at
> app startup - which, with PHP, mostly happens for each and every
> request.

I thought we have WSGI for this?  Nothing stops a Python app from
working like PHP.  PHP has an advantage when you want to run mutually
hostile apps in the same process (relevant if you're running ultra-cheap
shared hosting and you want to put 1000 customers' virtual hosts in the
same mod_php instance), but I don't think you're looking to do that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code redundancy

2010-04-21 Thread Andreas Löscher
You can do something like this:

>>> class A(): pass
>>> inst=A()
>>> exec("""
... a=1
... b=2
... c=3
... d=4
... """) in inst.__dict__
>>>  inst.a
1
>>>

This executes the Statement in the exec function and uses inst.__dict__
as namespace. But be aware, that this is not recommended. If you mess
with __dict__, you won't be able to replace it with some logic
(parameter) if you need to do something more than setting a variable.

Best



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


Re: Write web apps in Python?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 2:33 AM, Adam Tauno Williams
 wrote:
> On Wed, 2010-04-21 at 10:28 +0200, Bruno Desthuilliers wrote:
>> Bryan a écrit :
>> >
>> > I think I see what you mean
>>
>> Err...
>>
>> > -- correct me if I'm wrong:
>>
>> You are, sorry !-)
>>
>> > You want to
>> > keep complex application data structures around between requests.
>>
>> Nope. I want to keep all my settings parsed,
>
> Store them in the session.

I don't think that makes sense. You still have to re-parse the
settings upon starting each new session to store it in the session in
the first place.
Even then, you're suggesting needlessly keeping separate copies of the
settings data for each session, going from O(1) to O(N) in space;
that's rather wasteful.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rfind bug ?

2010-04-21 Thread Jean-Michel Pichavant

Stef Mientki wrote:

On 21-04-2010 10:56, Chris Rebert wrote:
  

On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki  wrote:
  


With the following code, I would expect a result of 5 !!


  

a= 'word1 word2 word3'
a.rfind(' ',7)
  


11

Is this a bug ?

  

No. Don't you think someone would have found such an obvious bug by now?
  


if it's not a bug,
then the start index has no meaning ...
... and some would call that a bug.

cheers,
Stef
  

a.rfind(' ', 12)
Out[12]: -1

looks like the start index has a meaning ...

JM

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


Re: rfind bug ?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 2:59 AM, Stef Mientki  wrote:
> On 21-04-2010 10:56, Chris Rebert wrote:
>> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki  wrote:
>>> With the following code, I would expect a result of 5 !!
>>>
>> a= 'word1 word2 word3'
>> a.rfind(' ',7)
>>
>>> 11
>>>
>>> Is this a bug ?
>>>
>> No. Don't you think someone would have found such an obvious bug by now?
>>
> if it's not a bug,
> then the start index has no meaning ...
> ... and some would call that a bug.

Ah, I neglected to take your use of .rfind()'s second parameter into account!

As can be interpolated from the part of the docs James quotes:
s.rfind(' ', 7) === s[7:].rfind(' ') + 7 # overlooking the 'not present' case

That is, the second parameter to .rfind(), namely `start`, is relative
to the *left* end of the string, not the right end. I can see how this
might be unintuitive, but it does make the API more uniform.

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


Re: rfind bug ?

2010-04-21 Thread Alf P. Steinbach

* Chris Rebert:

On Wed, Apr 21, 2010 at 2:59 AM, Stef Mientki  wrote:

On 21-04-2010 10:56, Chris Rebert wrote:

On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki  wrote:

With the following code, I would expect a result of 5 !!


a= 'word1 word2 word3'
a.rfind(' ',7)


11

Is this a bug ?


No. Don't you think someone would have found such an obvious bug by now?


if it's not a bug,
then the start index has no meaning ...
... and some would call that a bug.


Ah, I neglected to take your use of .rfind()'s second parameter into account!

As can be interpolated from the part of the docs James quotes:
s.rfind(' ', 7) === s[7:].rfind(' ') + 7 # overlooking the 'not present' case

That is, the second parameter to .rfind(), namely `start`, is relative
to the *left* end of the string, not the right end. I can see how this
might be unintuitive, but it does make the API more uniform.


It seems that the OP also thought it was relative to the left end of the string.

The difference is what it signifies: start of search, or end of search.

With rfind the "start" parameter signifies the end of the search, and 
conversely, the third parameter "end" signifies where the search starts. :-)



Cheers,

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


Re: shove does not store data as expected

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 2:51 AM, Alex  wrote:
> I'm trying to use the shove module (http://pypi.python.org/pypi/shove)
> for a simple script. The script read a CSV file ad store the data.
> When I check the content of the "store" object (instance of Shove)
> *before* I close it, the data are all there but when I close and re-
> open it some data are lost. How it is possible? There is something
> wrong in my code or I didn't understand how shove works?
>
> Thanks in advance.
>
> Here is a sample of my code:
>
> DBNAME = "file://store.db"
>
>    csv_file ="test.csv"
>    cities = csv.reader(open(csv_file), delimiter=";")
>    store = Shove(DBNAME, compress=True)
>    for city,region,code in cities:
>        entry_list = store.setdefault(region, [])
>        data = 'just a sample'
>        entry = {city:data}
>        entry_list.append(entry)

If `shove` is like the std lib `shelve` module, the following might
fix the problem:

#untested
for city,region,code in cities:
entry_list = store.get(region, [])
data = 'just a sample'
entry = {city:data}
entry_list.append(entry)
store[region] = entry_list

Explanation:
The explicit assignment back to the `store` pseudo-dictionary lets it
properly update its internal state to reflect the change to the value
(in this case, the list) associated with the region key. In your
original version, you merely modified the list in-place, and (due to
the way Python works) `store` has no way of knowing that you've done
that and thus doesn't do the necessary bookkeeping, hence the behavior
you're observing.

See also: http://docs.python.org/library/shelve.html#example

And further note that shove seems to be beta and (apart from
docstrings in the source code) undocumented.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Declarative programming for the Model part of an application

2010-04-21 Thread pca
Dear all,

Last week, I raised the question "Could declarative programming be
useful for the Model part of an application ?", and I suggested an
open-source project, Yoopf, to provide such a paradigm in Python.
Stefan told me that the proposal lacked clarity.  I have thus updated
the description, and he now finds it much clearer.  You can find it at
http://www.yoopf.org

So, I would appreciate your feedback again on such a project : can
declarative programming be useful for the Model part of an
application ?  Do you think that Yoopf and its "programming by
formula" paradigm can bring value to python developments ?  How would
it compare to database triggers and stored procedures, for example ?
What could be some typical applications ?

Many thanks in advance !
Pierre C.
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter question

2010-04-21 Thread Rotwang
Apologies in advance if this is a totally stupid question, I've tried 
looking at the Tkinter documentation on the web but since I'm something 
of an ignoramus when it comes to programming generally I didn't 
understand what I was reading. Anyway...


I've written a module that allows me to manipulate sound data, and I've 
been trying to add a method to my sound class that shows me what a 
waveform looks like while I'm working on it in IDLE. After reading a bit 
about Tkinter, and via some trial and error, I came up with something a 
bit like this:


def draw(self, w, h):
out = Tkinter.Canvas(width = w, height = h)
# a load of out.create_line(...)'s go here
out.pack()
out.mainloop()

It works, but the problem is that I can't do anything else with IDLE 
until I close the image window. Is there a way around this, so that I 
can leave the image open while I continue to do other stuff?

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread fab
Thanks for your insights.

I have taken the easy way out, I read on a page that python 3 worked
by default in UTF-8, so I downloaded and installed it.

Apart from a few surprises (print is not a funtion, and rules about
mixing spaces and tabs in indentation are much more strict, and I
guess more is to come :^) everything now works transparently.

Thanks again.

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


Re: rfind bug ?

2010-04-21 Thread Stef Mientki
On 21-04-2010 12:33, Alf P. Steinbach wrote:
> * Chris Rebert:
>> On Wed, Apr 21, 2010 at 2:59 AM, Stef Mientki
>>  wrote:
>>> On 21-04-2010 10:56, Chris Rebert wrote:
 On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki
  wrote:
> With the following code, I would expect a result of 5 !!
>
 a= 'word1 word2 word3'
 a.rfind(' ',7)

> 11
>
> Is this a bug ?
>
 No. Don't you think someone would have found such an obvious bug by
 now?

>>> if it's not a bug,
>>> then the start index has no meaning ...
>>> ... and some would call that a bug.
>>
>> Ah, I neglected to take your use of .rfind()'s second parameter into
>> account!
>>
>> As can be interpolated from the part of the docs James quotes:
>> s.rfind(' ', 7) === s[7:].rfind(' ') + 7 # overlooking the 'not
>> present' case
>>
>> That is, the second parameter to .rfind(), namely `start`, is relative
>> to the *left* end of the string, not the right end. I can see how this
>> might be unintuitive, but it does make the API more uniform.
>
> It seems that the OP also thought it was relative to the left end of
> the string.
>
> The difference is what it signifies: start of search, or end of search.
>
> With rfind the "start" parameter signifies the end of the search, and
> conversely, the third parameter "end" signifies where the search
> starts. :-)
>
thanks Alf,
that's indeed what I was missing.

cheers,
Stef

>
> Cheers,
>
> - Alf

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


Re: rfind bug ?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 3:33 AM, Alf P. Steinbach  wrote:
> * Chris Rebert:
>> On Wed, Apr 21, 2010 at 2:59 AM, Stef Mientki 
>> wrote:
>>> On 21-04-2010 10:56, Chris Rebert wrote:
 On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki 
 wrote:
>
> With the following code, I would expect a result of 5 !!
>
 a= 'word1 word2 word3'
 a.rfind(' ',7)

> 11
>
> Is this a bug ?
>
 No. Don't you think someone would have found such an obvious bug by now?

>>> if it's not a bug,
>>> then the start index has no meaning ...
>>> ... and some would call that a bug.
>>
>> Ah, I neglected to take your use of .rfind()'s second parameter into
>> account!
>>
>> As can be interpolated from the part of the docs James quotes:
>> s.rfind(' ', 7) === s[7:].rfind(' ') + 7 # overlooking the 'not present'
>> case
>>
>> That is, the second parameter to .rfind(), namely `start`, is relative
>> to the *left* end of the string, not the right end. I can see how this
>> might be unintuitive, but it does make the API more uniform.
>
> It seems that the OP also thought it was relative to the left end of the
> string.

It really doesn't help that the example in question is kinda lousy
(particularly, it's symmetrical with respect to whitespace and has
only 2 spaces).
Case in point, my misinterpretation of the OP's misinterpretation
(i.e. having the indexing of `start` be relative to the end of the
string) still plausibly explains his confusion.

Cheers,
Chris
--
Excellent catch.
*Considers getting Alien Life-Form merch*

> The difference is what it signifies: start of search, or end of search.
>
> With rfind the "start" parameter signifies the end of the search, and
> conversely, the third parameter "end" signifies where the search starts. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter question

2010-04-21 Thread James Mills
On Wed, Apr 21, 2010 at 8:45 PM, Rotwang  wrote:
> def draw(self, w, h):
>        out = Tkinter.Canvas(width = w, height = h)
>        # a load of out.create_line(...)'s go here
>        out.pack()
>        out.mainloop()
>
> It works, but the problem is that I can't do anything else with IDLE until I
> close the image window. Is there a way around this, so that I can leave the
> image open while I continue to do other stuff?

From reading the documentation myself (pydoc)...

It would seem your only option is to make a thread
out of this (not my preferred way - I was hoping it was
possible to poll the Tk event system...).

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


Re: Re: Code redundancy

2010-04-21 Thread Alan Harris-Reid

Andreas Löscher wrote:

You can do something like this:

  

class A(): pass
inst=)
exec("""


... a=
... b=2
... c=3
... d=4
... """) in inst.__dict__
  

 inst.a


1
  


This executes the Statement in the exec function and uses inst.__dict__
as namespace. But be aware, that this is not recommended. If you mess
with __dict__, you won't be able to replace it with some logic
(parameter) if you need to do something more than setting a variable.

Best

Hi Andreas, thanks for the reply,

Looks like I'll be sticking with repeating the class-name for a while.

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


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Ben Finney
Sebastian  writes:

> My regular expressions turn the Amazon error messages into Python
> exceptions.
>
> This works fine as long as they are in English: "??? is not a valid
> value for BrowseNodeId. Please change this value and retry your
> request.", for instance, will raise an InvalidParameterValue
> exception. However, the Japanese version returns the error message
> "??? は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエス
> トを実行してください。" which will not be successfully handled.
>
> This renders the my module pretty much useless for Japanese users.

Your problem, then, appears to be that you're attacking the issue at the
wrong layer. Parsing messages in natural language and hoping to
reconstruct a structure is going to be an exercise in frustration.

Doesn't the API have defined response codes and parameters that you can
use, instead of parsing error strings in various natural languages?

-- 
 \ “Giving every man a vote has no more made men wise and free |
  `\  than Christianity has made them good.” —Henry L. Mencken |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread Peter Otten
f...@slick.airforce-one.org wrote:

> I have taken the easy way out, I read on a page that python 3 worked
> by default in UTF-8, so I downloaded and installed it.

Just a quick reminder: UTF-8 is not the same as unicode. Python3 works in 
unicode and by default uses UTF-8 to read from or write into files.

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


Re: shove does not store data as expected

2010-04-21 Thread Alex
On 21 Apr, 12:36, Chris Rebert  wrote:
[cut]
>
> Explanation:
> The explicit assignment back to the `store` pseudo-dictionary lets it
> properly update its internal state to reflect the change to the value
> (in this case, the list) associated with the region key. In your
> original version, you merely modified the list in-place, and (due to
> the way Python works) `store` has no way of knowing that you've done
> that and thus doesn't do the necessary bookkeeping, hence the behavior
> you're observing.
>
> See also:http://docs.python.org/library/shelve.html#example
>
> And further note that shove seems to be beta and (apart from
> docstrings in the source code) undocumented.

Thanks a lot for the clear explanation. It works!
I will read the docs more carefully next time :-)

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


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Sebastian
> > My regular expressions turn the Amazon error messages into Python
> > exceptions.
>
> > This works fine as long as they are in English: "??? is not a valid
> > value for BrowseNodeId. Please change this value and retry your
> > request.", for instance, will raise an InvalidParameterValue
> > exception. However, the Japanese version returns the error message
> > "??? は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエス
> > トを実行してください。" which will not be successfully handled.
>
> > This renders the my module pretty much useless for Japanese users.
>
> Your problem, then, appears to be that you're attacking the issue at the
> wrong layer. Parsing messages in natural language and hoping to
> reconstruct a structure is going to be an exercise in frustration.
>
> Doesn't the API have defined response codes and parameters that you can
> use, instead of parsing error strings in various natural languages?

No, unfortunately not. If it did, I would have used it.

The Amazon API returns an XML response which contains error messages
if a request fails. These messages consist of an error code and an
error description in natural language. Luckily, the description seems
to stick to the same format and is (in all but one case) in plain
English. Much to my dismay I discovered that the Japanese locale
translates the error message!

For example, this is the bit of XML returned for the German locale:

  

  AWS.InvalidParameterValue
  ??? is not a valid value for BrowseNodeId. Please
change this value and retry your request.

  

The corresponding part from the Japanese locale looks like this:

  

  AWS.InvalidParameterValue
  ???
は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエストを実行してください。

  

Of course, one could argue that the type of error (in this case
"AWS.InvalidParameterValue") would be enough. However, in order to
return a maeningful error message, I would like to parse the
description itself - and for this some knowledge of Japanese would be
helpful.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 4:46 AM, Sebastian  wrote:
>> > My regular expressions turn the Amazon error messages into Python
>> > exceptions.
>>
>> > This works fine as long as they are in English: "??? is not a valid
>> > value for BrowseNodeId. Please change this value and retry your
>> > request.", for instance, will raise an InvalidParameterValue
>> > exception. However, the Japanese version returns the error message
>> > "??? は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエス
>> > トを実行してください。" which will not be successfully handled.
>>
>> > This renders the my module pretty much useless for Japanese users.
>>
>> Your problem, then, appears to be that you're attacking the issue at the
>> wrong layer. Parsing messages in natural language and hoping to
>> reconstruct a structure is going to be an exercise in frustration.
>>
>> Doesn't the API have defined response codes and parameters that you can
>> use, instead of parsing error strings in various natural languages?
>
> No, unfortunately not. If it did, I would have used it.
>
> The Amazon API returns an XML response which contains error messages
> if a request fails. These messages consist of an error code and an
> error description in natural language. Luckily, the description seems
> to stick to the same format and is (in all but one case) in plain
> English. Much to my dismay I discovered that the Japanese locale
> translates the error message!
>
> For example, this is the bit of XML returned for the German locale:
>
>      
>        
>          AWS.InvalidParameterValue
>          ??? is not a valid value for BrowseNodeId. Please
> change this value and retry your request.
>        
>      
>
> The corresponding part from the Japanese locale looks like this:
>
>      
>        
>          AWS.InvalidParameterValue
>          ???
> は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエストを実行してください。 Message>
>        
>      
>
> Of course, one could argue that the type of error (in this case
> "AWS.InvalidParameterValue") would be enough. However, in order to
> return a maeningful error message, I would like to parse the
> description itself - and for this some knowledge of Japanese would be
> helpful.

Just throwing this out there, but perhaps you could grep for the
relevant terms in the error message and intuit it from there?
For example:

# terms = whatever the actual param names are
terms = "BrowseNodeId FooNodeId FooQueryType".split()
for term in terms:
if term in err_msg:
raise AmazonError, err_code + " for " +repr(term)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


socked and bytes operation

2010-04-21 Thread luca72
Hello i have this question :
i connect to the server in this way:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('192.168.1.11',11502))
rcv = sock.recv(8124)
here i get 14 random bytes , in a string with strange chars like :
¬¨^.á‹•Ò
a„ãj
I think because sock.recv return a string.
Now i have to xor this 14 bytes with a key stored in file as a sting.
Can you help me to understand how to do it.

Thanks

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


Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
Hi,

 Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

  For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
 modify_state()
 val = f()
 restore_state()
 return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socked and bytes operation

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 5:37 AM, luca72  wrote:
> Hello i have this question :
> i connect to the server in this way:
> sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> sock.connect(('192.168.1.11',11502))
> rcv = sock.recv(8124)
> here i get 14 random bytes , in a string with strange chars like :
> ¬¨^.á‹•Ò
> a„ãj
> I think because sock.recv return a string.
> Now i have to xor this 14 bytes with a key stored in file as a sting.
> Can you help me to understand how to do it.

# Disclaimer: Completely untested
from itertools import cycle
f = open("path/to/key_file", 'r') # open file
key = f.read() # read from file
f.close() # close file
# convert strings to lists of integers
rcv = map(ord, rcv)
key = map(ord, key)
plain_chars = []
# do the XOR-ing
for cypher_char, key_char in zip(rcv, cycle(key)):
plain_char = chr(ord(cypher_char) ^ ord(key_char))
plain_chars.append(plain_char)
# join decrypted characters into a string
# and output it
print ''.join(plain_chars) # Python idiom

You'll probably need to read up on what zip(), map(), ord(), and chr() do:
http://docs.python.org/library/functions.html

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread python
Hi Peter,

> Just a quick reminder: UTF-8 is not the same as unicode. Python3 works in 
> unicode and by default uses UTF-8 to read from or write into files.

I'm not the OP, but wanted to make sure I was fully understanding your
point.

Are you saying all open() calls in Python that read text files,
automatically convert UTF-8 content to Unicode in the same manner as the
following might when using Python 2.6?

codecs.open( fileName, mode='r', encoding='UTF8', ... )

Thanks for your feedback,

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


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 5:51 AM, ++imanshu  wrote:
>     Is it possible to something along these lines in python :-
>
> map = {
> 'key1': f(),
> 'key2': modify_state(); val = f(); restore_state(); val,
> 'key3': f(),
> }
>
>      For 'key2' I want to store the value returned by f() but after
> modifying the state. Do we have something like a "bare block".

Based on what I can find about "bare blocks", Nope. And we like it that way :-)

> I am
> trying to avoid this :-
>
> def f2():
>     modify_state()
>     val = f()
>     restore_state()
>     return val
>
> map = {
> 'key1': f(),
> 'key2': f2()
> 'key3': f(),
> }

FWIW, I don't see what's wrong with this. You could probably refactor
f2() to use the `with` statement and a context manager, but that's
getting tangential.
However, the question arises: Why do you have global state in the first place?

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Write web apps in Python?

2010-04-21 Thread Bryan
Bruno Desthuilliers wrote:
> Nope. I want to keep all my settings parsed, my librairies loaded, all
> my connections opened etc. That is, all the time consuming stuff at app
> startup - which, with PHP, mostly happens for each and every request.

O.K. I wasn't clear on your objection. As I said the first time, I
think you've gotten some bad info on PHP. Or maybe you're just behind
the times.

> Many large, sopĥisticated etc applications are written in C. Does that
> make C a practical application programming language ?

It's at least a strong clue.

> Now I'm sorry to say that for quite a few "sophisticated" PHP apps I've
> seen (and eventually had to work on), the "startup" part - parsing the
> include files, configuration, establishing connections etc - took a good
> part of the total processing time.

You didn't say when that was, but PHP caching has come a long way.
Google is your friend.

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread Peter Otten
pyt...@bdurham.com wrote:

> Are you saying all open() calls in Python that read text files,
> automatically convert UTF-8 content to Unicode in the same manner as the
> following might when using Python 2.6?
> 
> codecs.open( fileName, mode='r', encoding='UTF8', ... )


That's what I meant to say, but it's not actually true.

Quoting http://docs.python.org/py3k/library/functions.html#open

"""
open(file, mode='r', buffering=None, encoding=None, errors=None, 
newline=None, closefd=True)

[...]

encoding is the name of the encoding used to decode or encode the file. This 
should only be used in text mode. The default encoding is platform dependent 
(whatever locale.getpreferredencoding() returns), but any encoding supported 
by Python can be used. See the codecs module for the list of supported 
encodings.
"""

So it just happend to be UTF-8 on my machine.

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


Re: python glibc crypt() function

2010-04-21 Thread geremy condra
On Wed, Apr 21, 2010 at 2:29 AM, luca72  wrote:
> On 20 Apr, 19:38, Peter Otten <__pete...@web.de> wrote:
>> luca72 wrote:
>> > Hello i have to do this :
>> > glibc crypt() function, using salt $1$abcdefgh$
>>
>> > cryptPw = crypt(plainPw, "$1$abcdefgh$")
> Thanks
> The result is correct i obtain the same with ctypes and crypt module,
> so i think that is better to use the crypt module is correct?
>
> Luca

Be aware that the beginning of the salt string determines the
type of hash you're using, and it looks to me like that's the
old-style MD5-based hash. It's far from 'broken', but it has a
few issues. Better would be to move to the SHA512-based
$6$ if your platform supports it.

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


Re: Write web apps in Python?

2010-04-21 Thread Bruno Desthuilliers

Bryan a écrit :

Bruno Desthuilliers wrote:

Nope. I want to keep all my settings parsed, my librairies loaded, all
my connections opened etc. That is, all the time consuming stuff at app
startup - which, with PHP, mostly happens for each and every request.


O.K. I wasn't clear on your objection. As I said the first time, I
think you've gotten some bad info on PHP. 


Well, I have to admit I've only used it professionnaly for the past 6 
years or so, so my knowledge may be a bit lacking...





Many large, sopĥisticated etc applications are written in C. Does that
make C a practical application programming language ?


It's at least a strong clue.


Oh, yes ? Then why don't you use C for web programming ?-)

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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread python
Hi Peter,

>> Are you saying all open() calls in Python that read text files,
>> automatically convert UTF-8 content to Unicode in the same manner as the
>> following might when using Python 2.6?
>> 
>> codecs.open( fileName, mode='r', encoding='UTF8', ... )

> That's what I meant to say, but it's not actually true.

Thanks for the clarification.

It sounds like Python 3 has unified the standard library open() function
and the codecs.open() into a single function?

In other words, would it be accurate to say that in Python 3, there is
no longer a need to use codecs.open()?

Any idea if the above applies to Python 2.7?

Thank you Peter!

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


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread Dave Angel

++imanshu wrote:

Hi,

 Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

  For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
 modify_state()
 val = f()
 restore_state()
 return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu

  

How about something like:

mymap = {
'key1': f(),
'key2': [modify_state(), f(), restore_state()][1],
'key3': f(),
}

This builds a list of three values, and uses only the [1] item from the 
list.


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


Re: Write web apps in Python?

2010-04-21 Thread Tim Chase

On 04/21/2010 08:46 AM, Bruno Desthuilliers wrote:

Bryan a écrit :

Bruno Desthuilliers wrote:

Many large, sopĥisticated etc applications are written in C. Does that
make C a practical application programming language ?


It's at least a strong clue.


Oh, yes ? Then why don't you use C for web programming ?-)


Many large, sophisticated etc applications are written in COBOL 
too.  I'm sure it makes a practical web programming language 
too... ;-)


-tkc



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


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
On Apr 21, 7:31 pm, Dave Angel  wrote:
> ++imanshu wrote:
> > Hi,
>
> >      Is it possible to something along these lines in python :-
>
> > map = {
> > 'key1': f(),
> > 'key2': modify_state(); val = f(); restore_state(); val,
> > 'key3': f(),
> > }
>
> >       For 'key2' I want to store the value returned by f() but after
> > modifying the state. Do we have something like a "bare block". I am
> > trying to avoid this :-
>
> > def f2():
> >      modify_state()
> >      val = f()
> >      restore_state()
> >      return val
>
> > map = {
> > 'key1': f(),
> > 'key2': f2()
> > 'key3': f(),
> > }
>
> > Thank You,
> > Himanshu
>
> How about something like:
>
> mymap = {
> 'key1': f(),
> 'key2': [modify_state(), f(), restore_state()][1],
> 'key3': f(),
>
> }
>
> This builds a list of three values, and uses only the [1] item from the
> list.
>
> DaveA

This is cool. I'll use it.

Thank You,
Himanshu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread ++imanshu
On Apr 21, 6:10 pm, Chris Rebert  wrote:
> On Wed, Apr 21, 2010 at 5:51 AM, ++imanshu  wrote:
> >     Is it possible to something along these lines in python :-
>
> > map = {
> > 'key1': f(),
> > 'key2': modify_state(); val = f(); restore_state(); val,
> > 'key3': f(),
> > }
>
> >      For 'key2' I want to store the value returned by f() but after
> > modifying the state. Do we have something like a "bare block".
>
> Based on what I can find about "bare blocks", Nope. And we like it that way 
> :-)
>
> > I am
> > trying to avoid this :-
>
> > def f2():
> >     modify_state()
> >     val = f()
> >     restore_state()
> >     return val
>
> > map = {
> > 'key1': f(),
> > 'key2': f2()
> > 'key3': f(),
> > }
>
> FWIW, I don't see what's wrong with this. You could probably refactor
> f2() to use the `with` statement and a context manager, but that's
> getting tangential.
> However, the question arises: Why do you have global state in the first place?
>
> Cheers,
> Chris
> --http://blog.rebertia.com

f() = flavor independent os api for getting path to a folder, uses
effective user id (eg Folder.FSFindFolder(Folders.kUserDomain,
Folders.kInternetPlugInFolderType, Folders.kDontCreateFolder))
modify_state() = change current effective user id
restore_state() = restore to old user id

Thanks for the reply.

Thank You,
Himanshu

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


Re: Tkinter question

2010-04-21 Thread eb303
On Apr 21, 12:45 pm, Rotwang  wrote:
> Apologies in advance if this is a totally stupid question, I've tried
> looking at the Tkinter documentation on the web but since I'm something
> of an ignoramus when it comes to programming generally I didn't
> understand what I was reading. Anyway...
>
> I've written a module that allows me to manipulate sound data, and I've
> been trying to add a method to my sound class that shows me what a
> waveform looks like while I'm working on it in IDLE. After reading a bit
> about Tkinter, and via some trial and error, I came up with something a
> bit like this:
>
> def draw(self, w, h):
>         out = Tkinter.Canvas(width = w, height = h)
>         # a load of out.create_line(...)'s go here
>         out.pack()
>         out.mainloop()
>
> It works, but the problem is that I can't do anything else with IDLE
> until I close the image window. Is there a way around this, so that I
> can leave the image open while I continue to do other stuff?

Just run your program directly, either from a terminal or a DOS
console or by double-clicking on it in a file manager if the proper
file associations have been defined (they are by default on Windows).

There might be some IDLE trick to make it work, but I don't use IDLE
myself.

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


Re: Inline Calculation of Dictionary Values

2010-04-21 Thread Peter Otten
Dave Angel wrote:

> ++imanshu wrote:
>> Hi,
>>
>>  Is it possible to something along these lines in python :-
>>
>> map = {
>> 'key1': f(),
>> 'key2': modify_state(); val = f(); restore_state(); val,
>> 'key3': f(),
>> }
>>
>>   For 'key2' I want to store the value returned by f() but after
>> modifying the state. Do we have something like a "bare block". I am
>> trying to avoid this :-
>>
>> def f2():
>>  modify_state()
>>  val = f()
>>  restore_state()
>>  return val
>>
>> map = {
>> 'key1': f(),
>> 'key2': f2()
>> 'key3': f(),
>> }
>>
>> Thank You,
>> Himanshu
>>
>>   
> How about something like:
> 
> mymap = {
> 'key1': f(),
> 'key2': [modify_state(), f(), restore_state()][1],
> 'key3': f(),
> }
> 
> This builds a list of three values, and uses only the [1] item from the
> list.

Please think of the children ;)

The function based approach is the way to go:

def f2():
cookie = modify_state()
try:
return f()
finally:
restore_state(cookie)

This will restore the state even if f() raises an exception.

If you want something more fancy you can parameterize f2() by the three 
functions or look into context managers.

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


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Terry Reedy

On 4/21/2010 7:46 AM, Sebastian wrote:


The Amazon API returns an XML response which contains error messages
if a request fails. These messages consist of an error code and an
error description in natural language. Luckily, the description seems
to stick to the same format and is (in all but one case) in plain
English. Much to my dismay I discovered that the Japanese locale
translates the error message!


Could you, when you get an error message, resubmit the request in the 
standard locale so you get the messages in English? Or is the 'locale' 
set by the url -- amazon.com versus amazon.co.jp?


After you parse, are you trying to formulate a substitute message in 
Japanese?


Terry Jan Reedy

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


Re: Japanese (speaking) developer needed for a bit of regex magic

2010-04-21 Thread Terry Reedy

On 4/21/2010 5:31 AM, Sebastian wrote:


This works fine as long as they are in English:
"??? is not a valid  value for BrowseNodeId.

>  Please change this value and retry your request.",
> for instance, will raise an InvalidParameterValue

exception. However, the Japanese version returns the error message "???
は、BrowseNodeIdの値として無効です。値を変更してから、再度リクエストを実行してください。"


My daughter, in 2nd year college Japanese, says that the above is 
basically a translation of the English boilerplate. The only variable 
info is 'BrowserNodeId', which you can read just fine already.
So we do not understand what your problem is and what you want to 
accomplish.



I have a collection of sample messages here (all files *-jp-*.xml):
http://bitbucket.org/basti/python-amazon-product-api/src/tip/tests/2009-11-01/


Is this a commercial product? Are you willing to pay for serious help, 
if needed?


Terry Jan Reedy


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


Re: error when printing a UTF-8 string (python 2.6.2)

2010-04-21 Thread Terry Reedy

On 4/21/2010 9:29 AM, Peter Otten wrote:

pyt...@bdurham.com wrote:


Are you saying all open() calls in Python that read text files,
automatically convert UTF-8 content to Unicode in the same manner as the
following might when using Python 2.6?

codecs.open( fileName, mode='r', encoding='UTF8', ... )



That's what I meant to say, but it's not actually true.


I wish it were, though.


Quoting http://docs.python.org/py3k/library/functions.html#open

"""
open(file, mode='r', buffering=None, encoding=None, errors=None,
newline=None, closefd=True)

[...]

encoding is the name of the encoding used to decode or encode the file. This
should only be used in text mode. The default encoding is platform dependent
(whatever locale.getpreferredencoding() returns), but any encoding supported
by Python can be used. See the codecs module for the list of supported
encodings.
"""

So it just happend to be UTF-8 on my machine.


Unfortunately, it is not on US Windows.

Terry Jan Reedy

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


xml.dom.minidom character encoding

2010-04-21 Thread C. Benson Manica
I have the following simple script running on 2.5.2 on a machine where
the default character encoding is "ascii":

#!/usr/bin/env python
#coding: utf-8

import xml.dom.minidom
import codecs

str=u""
doc=xml.dom.minidom.parseString( str )
xml=doc.toxml( encoding="utf-8" )
file=codecs.open( "foo.xml", "w", "utf-8" )
file.write( xml )
file.close()

I've specified utf-8 every place I can find that the documentation
allows me to, and yet this doesn't even come close to working without
UnicodeEncodeErrors.  What on Earth do I have to do to please the
character encoding gods?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.dom.minidom character encoding

2010-04-21 Thread Peter Otten
C. Benson Manica wrote:

> I have the following simple script running on 2.5.2 on a machine where
> the default character encoding is "ascii":
> 
> #!/usr/bin/env python
> #coding: utf-8
> 
> import xml.dom.minidom
> import codecs
> 
> str=u" \"ó\"/>"
> doc=xml.dom.minidom.parseString( str )
> xml=doc.toxml( encoding="utf-8" )
> file=codecs.open( "foo.xml", "w", "utf-8" )
> file.write( xml )
> file.close()
> 
> I've specified utf-8 every place I can find that the documentation
> allows me to, and yet this doesn't even come close to working without
> UnicodeEncodeErrors.  What on Earth do I have to do to please the
> character encoding gods?

Verify every step as you proceed?

>>> import xml.dom.minidom
>>> s = u""
>>> doc = xml.dom.minidom.parseString(s)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.5/xml/dom/minidom.py", line 1925, in parseString
return expatbuilder.parseString(string)
  File "/usr/lib/python2.5/xml/dom/expatbuilder.py", line 940, in 
parseString
return builder.parseString(string)
  File "/usr/lib/python2.5/xml/dom/expatbuilder.py", line 223, in 
parseString
parser.Parse(string, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 
62: ordinal not in range(128)

It seems that parseString() doesn't like unicode -- let's try a byte string 
then:

>>> doc = xml.dom.minidom.parseString(s.encode("utf-8"))
>>> xml = doc.toxml(encoding="utf-8")

No complaints -- let's have a look at the result:

>>> xml
''

That's a byte string, no need for codecs.open() then:

>>> f = open("foo.xml", "w")
>>> f.write(xml)
>>> f.close()

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


Re: xml.dom.minidom character encoding

2010-04-21 Thread C. Benson Manica
On Apr 21, 1:58 pm, Peter Otten <__pete...@web.de> wrote:
> C. Benson Manica wrote:
>> (snip)
>
> It seems that parseString() doesn't like unicode

Yes, I noticed that, and I already tried...

> -- let's try a byte string
> then:
>
> >>> doc = xml.dom.minidom.parseString(s.encode("utf-8"))
> >>> xml = doc.toxml(encoding="utf-8")

...except that it didn't work:

  File "./demo.py", line 8, in 
doc=xml.dom.minidom.parseString( str.encode("utf-8") )
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
62: ordinal not in range(128)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socked and bytes operation

2010-04-21 Thread John Nagle

luca72 wrote:

Hello i have this question :
i connect to the server in this way:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('192.168.1.11',11502))
rcv = sock.recv(8124)
here i get 14 random bytes , in a string with strange chars like :
¬¨^.á‹•Ò
a„ãj
I think because sock.recv return a string.
Now i have to xor this 14 bytes with a key stored in file as a sting.
Can you help me to understand how to do it.


Port 11502?  What are you talking to?  An eSoft Distributed
Intelligent Architecture node? Unlikely. Let me guess. A Counter-Strike
server.

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


Re: xml.dom.minidom character encoding

2010-04-21 Thread Peter Otten
C. Benson Manica wrote:

> On Apr 21, 1:58 pm, Peter Otten <__pete...@web.de> wrote:
>> C. Benson Manica wrote:
>>> (snip)
>>
>> It seems that parseString() doesn't like unicode
> 
> Yes, I noticed that, and I already tried...
> 
>> -- let's try a byte string
>> then:
>>
>> >>> doc = xml.dom.minidom.parseString(s.encode("utf-8"))
>> >>> xml = doc.toxml(encoding="utf-8")
> 
> ...except that it didn't work:
> 
>   File "./demo.py", line 8, in 
> doc=xml.dom.minidom.parseString( str.encode("utf-8") )
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 62: ordinal not in range(128)

Are you sure that your script has

str = u"..."

like in your post and not just

str = "..."

?

Peter


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


Re: Write web apps in Python?

2010-04-21 Thread Bryan
Bruno Desthuilliers wrote:
> Bryan a écrit :
>
> > Bruno Desthuilliers wrote:
> >> Nope. I want to keep all my settings parsed, my librairies loaded, all
> >> my connections opened etc. That is, all the time consuming stuff at app
> >> startup - which, with PHP, mostly happens for each and every request.
>
> > O.K. I wasn't clear on your objection. As I said the first time, I
> > think you've gotten some bad info on PHP.
>
> Well, I have to admit I've only used it professionnaly for the past 6
> years or so, so my knowledge may be a bit lacking...

Competent PHP developers know how to cache libraries and various
connections and configurations. If that's been a stumbling point if
your own work, well, again, Google is your friend.

> >> Many large, sopĥisticated etc applications are written in C. Does that
> >> make C a practical application programming language ?
>
> > It's at least a strong clue.
>
> Oh, yes ? Then why don't you use C for web programming ?-)

Did you forget what you argued? Had you said that PHP is impractical
for writing operating systems, I would have silently agreed. You wrote
the howler: "The PHP execution model (mostly based on CGI FWIW) tends
to be a bit unpractical for non-trivial applications".

PHP *rocks* for serious web apps. PHP is specifically designed for web
apps. PHP became a major-league programming language *soley* on its
strength and success in building web apps. Popular as PHP is, it
"tends to be a bit unpractical" for anything but web apps.

'Round here we love Python. I prefer Python to Perl or PHP even in
those languages' particular areas of specialization. Advocating for
Python does not require spreading myths about PHP.

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


Re: xml.dom.minidom character encoding

2010-04-21 Thread C. Benson Manica
On Apr 21, 2:25 pm, Peter Otten <__pete...@web.de> wrote:

> Are you sure that your script has
>
> str = u"..."
>
> like in your post and not just
>
> str = "..."

No :-)

str=u""
doc=xml.dom.minidom.parseString( str.encode("utf-8") )
xml=doc.toxml( encoding="utf-8")
file=codecs.open( "foo.xml", "w", "utf-8" )
file.write( xml )
file.close()

fails:

  File "./demo.py", line 12, in 
file.write( xml )
  File "/usr/lib/python2.5/codecs.py", line 638, in write
return self.writer.write(data)
  File "/usr/lib/python2.5/codecs.py", line 303, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
62: ordinal not in range(128)

but dropping the encoding argument to doc.toxml() seems to finally
work.  I'd be curious to know why the code you posted (that worked for
you) didn't for me, but at this point I'm just happy with something
functional.  Thank you very kindly!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.dom.minidom character encoding

2010-04-21 Thread Peter Otten
C. Benson Manica wrote:

> On Apr 21, 2:25 pm, Peter Otten <__pete...@web.de> wrote:
> 
>> Are you sure that your script has
>>
>> str = u"..."
>>
>> like in your post and not just
>>
>> str = "..."
> 
> No :-)
> 
> str=u" \"ó\"/>"
> doc=xml.dom.minidom.parseString( str.encode("utf-8") )
> xml=doc.toxml( encoding="utf-8")
> file=codecs.open( "foo.xml", "w", "utf-8" )
> file.write( xml )
> file.close()
> 
> fails:
> 
>   File "./demo.py", line 12, in 
> file.write( xml )
>   File "/usr/lib/python2.5/codecs.py", line 638, in write
> return self.writer.write(data)
>   File "/usr/lib/python2.5/codecs.py", line 303, in write
> data, consumed = self.encode(object, self.errors)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 62: ordinal not in range(128)

But that's a different error (codecs.open().write()) on a different line. 
What you said was failing (xml.dom.minidom.parseString()) worked. 

> but dropping the encoding argument to doc.toxml() seems to finally
> work.  I'd be curious to know why the code you posted (that worked for
> you) didn't for me, but at this point I'm just happy with something
> functional.  Thank you very kindly!

The following worked for me an should work for you, too:

$ cat tmp.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.dom.minidom

str = u""
doc = xml.dom.minidom.parseString(str.encode("utf-8"))

xml = doc.toxml(encoding="utf-8")

file = open("foo.xml", "w")
file.write( xml )
file.close()
$ python2.5 tmp.py
$ cat foo.xml
$

Btw., str is a bad variable name because it shadows the builtin str type.

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


Re: Write web apps in Python?

2010-04-21 Thread Emile van Sebille

On 4/21/2010 11:37 AM Bryan said...

'Round here we love Python. I prefer Python to Perl or PHP even in
those languages' particular areas of specialization. Advocating for
Python does not require spreading myths about PHP.


You're missing the point -- set-up and tear-down overhead is involved 
for both python and php cgi based web serving, and Bruno I'm sure would 
concur that python in this suffers similarly.  You point at exactly the 
issue when you noted earlier:


Bryan> I think I see what you mean -- correct me if
Bryan> I'm wrong: You want to keep complex application
Bryan> data structures around between requests. That
Bryan> sounds appealing in terms of efficiency, but
Bryan> it's bad for scalability and robustness.

It's more than appealing -- for example, it's fact with Zope, where Zeo 
adds the scalability.


Emile






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


Re: Usable street address parser in Python?

2010-04-21 Thread Albert van der Horst
In article <4bcddc5a$0$1630$742ec...@news.sonic.net>,
John Nagle   wrote:
>Iain King wrote:
>> Not sure on the volume of addresses you're working with, but as an
>> alternative you could try grabbing the zip code, looking up all
>> addresses in that zip code, and then finding whatever one of those
>> address strings most closely resembles your address string (smallest
>> Levenshtein distance?).
>
>The parser doesn't have to be perfect, but it should
>reliably reports when it fails.  Then I can run the hard cases through
>one of the commercial online address standardizers.  I'd like to
>be able to knock off the easy cases cheaply.

In a similar situation I did the exact reverse. ( analysing
assembler code sequences for the stack effect.)
I made a list of all exceptions, and checked against that first.
If it is not an exception, the rule should apply.
If it doesn't, call Houston.
(Of course one starts with making an input canonical, all upper case
maybe reordering etc.)

>
>What I want to do is to first extract the street number and
>undecorated street name only, match that to a large database of US businesses
>stored in MySQL, and then find the best match from the database
>hits.  So I need reliable extraction of undecorated street name and number.  
>The
>other fields are less important.

This kind of problem remains very tricky ...

At least in the Netherlands we have a book containing information
about how the spelling of a street should be officially using a limited
number of characters.

>
>   John Nagle

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


ctypes errcheck question

2010-04-21 Thread Brendan Miller
According to the ctypes docs: http://docs.python.org/library/ctypes.html

An errcheck function should return the args tuple when used with out
parameters (section 15.15.2.4. Function prototypes). However, in other
cases it says to return the result, or whatever result you want
returned from the function.

I have a single errcheck function that checks result codes and throws
an exception if the result indicates an error. I'd like to reuse it
with a number of different function, some of which are specified with
out parameters, and some of which are specified in the normal method:

func = my_lib.func
func.restype = c_long
func.argtypes = [c_int, etc]

So, my question is can do I have my errcheck function return the args
tuple in both cases? The documentation seems kind of ambiguous about
how it will behave if a function loaded without output parameters
returns the arguments tuple from errcheck.

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


Deleting more than one element from a list

2010-04-21 Thread candide
Is the del instruction able to remove _at the same_ time more than one 
element from a list ?



For instance, this seems to be correct :


>>> z=[45,12,96,33,66,'c',20,99]
>>> del z[2], z[6],z[0]
>>> z
[12, 33, 66, 'c', 20]
>>>


However, the following doesn't work :

>> z=[45,12,96,33,66,'c',20,99]
>>> del z[2], z[3],z[6]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: list assignment index out of range
>>>


Does it mean the instruction

del z[2], z[3],z[6]

to be equivalent to the successive calls


del z[2]
del z[3]
del z[6]

?










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


Re: Deleting more than one element from a list

2010-04-21 Thread Simon Brunning
On 21 April 2010 20:56, candide  wrote:
> Is the del instruction able to remove _at the same_ time more than one
> element from a list ?

Yup:

>>> z=[45,12,96,33,66,'c',20,99]
>>> del z[:]
>>> z
[]

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


Re: Deleting more than one element from a list

2010-04-21 Thread Gary Herron

candide wrote:
Is the del instruction able to remove _at the same_ time more than one 
element from a list ?



For instance, this seems to be correct :


>>> z=[45,12,96,33,66,'c',20,99]
>>> del z[2], z[6],z[0]
>>> z
[12, 33, 66, 'c', 20]
>>>


However, the following doesn't work :

>> z=[45,12,96,33,66,'c',20,99]
>>> del z[2], z[3],z[6]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: list assignment index out of range
>>>


Does it mean the instruction

del z[2], z[3],z[6]

to be equivalent to the successive calls


del z[2]
del z[3]
del z[6]


Yes, those are equivalent.  The reason it fails is that, by the time it 
gets around to the third delete, there is no longer in index [6] in the 
list.  The element you were thinking of is now at index [4].


This, however, will work as you expected:

   del z[6], z[3],z[2]




--
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Re: Deleting more than one element from a list

2010-04-21 Thread Mensanator
On Apr 21, 2:56 pm, candide  wrote:
> Is the del instruction able to remove _at the same_ time more than one
> element from a list ?
>
> For instance, this seems to be correct :
>
>  >>> z=[45,12,96,33,66,'c',20,99]
>  >>> del z[2], z[6],z[0]
>  >>> z
> [12, 33, 66, 'c', 20]
>  >>>
>
> However, the following doesn't work :
>
>  >> z=[45,12,96,33,66,'c',20,99]
>  >>> del z[2], z[3],z[6]
> Traceback (most recent call last):
>    File "", line 1, in 
> IndexError: list assignment index out of range
>  >>>
>
> Does it mean the instruction
>
> del z[2], z[3],z[6]
>
> to be equivalent to the successive calls
>
> del z[2]
> del z[3]
> del z[6]

That's part of the problem. Let's look at a better example.

>>> z = [0,1,2,3,4,5,6]
>>> del z[0],z[3],z[6]
Traceback (most recent call last):
  File "", line 1, in 
del z[0],z[3],z[6]
IndexError: list assignment index out of range
>>> z
[1, 2, 3, 5, 6]

Yes, the error was caused by the list shrinking between calls,
so the 6 did not get deleted. But notice that 3 is still there
and 4 is missing.

If you must delete this way, do it bottom up so that the index
remains valid for the subsequent calls:

>>> z = [0,1,2,3,4,5,6]
>>> del z[6],z[3],z[0]
>>> z
[1, 2, 4, 5]


>
> ?

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


Re: Deleting more than one element from a list

2010-04-21 Thread Emile van Sebille

On 4/21/2010 12:56 PM candide said...

Is the del instruction able to remove _at the same_ time more than one
element from a list ?


For instance, this seems to be correct :


 >>> z=[45,12,96,33,66,'c',20,99]


Not as I see it -- watch your index values - they change after each 
delete is completed.  It'll work if you order them backwards though.


>>> a = range(10)
>>> del a[0],a[2],a[4],a[6]
>>> a
[1, 2, 4, 5, 7, 8]
>>> a = range(10)
>>> del a[6],a[4],a[2],a[0]
>>> a
[1, 3, 5, 7, 8, 9]
>>>

Emile

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


Windows debugging symbols for python 2.5.4 and pywin32 214

2010-04-21 Thread Alexandre Fayolle
Hi everyone,

I have a production server running a Windows Service written in Python, which 
uses python 2.5.4 (yes I know it is old, but I am somewhat stuck with this for 
now) and pywin32 214. 

Given a set of manipulations, I get a stack overflow in the service, and a bad  
crash. The same operation when running without 
win32serviceutil.ServiceFramework does not trigger the bug. I'm looking for 
some debugging tools (debug build of the interpreter and the pywin32 
libraries) that some good soul could have kept from a previous debugging 
session to try to get a C stack trace and understand what causes this. 

Any hint towards what could cause that stack overflow would be welcome too. 
The application is multithreaded (and uses pyro and twisted). I can provide 
more information for the curious. 

Many thanks. 

-- 
Alexandre Fayolle
Logilab, Paris, France. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for registration package

2010-04-21 Thread kj




I'm looking for a Python-based, small, self-contained package to
hand out API keys, in the same spirit as Google API keys.

The basic specs are simple: 1) enforce the "one key per customer"
rule; 2) be robot-proof; 3) be reasonably difficult to circumvent
even for humans.

(This is for a web service we would like to implement; the goal is
to be able to control the load on our servers.  Therefore, if the
package includes an automated log-analysis component, all the
better, but this is not necessary.)

Any suggestions would be appreciated.

Thanks!

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


ctypes: delay conversion from c_char_p to string

2010-04-21 Thread Brendan Miller
I have a function exposed through ctypes that returns a c_char_p.
Since I need to deallocate that c_char_p, it's inconvenient that
ctypes copies the c_char_p into a string instead of giving me the raw
pointer. I believe this will cause a memory leak, unless ctypes is
smart enough to free the string itself after the copy... which I
doubt.

Is there some way to tell ctypes to return an actual c_char_p, or is
my best bet to return a c_void_p and cast to c_char_p when I'm reading
to convert to a string?

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


Python-URL! - weekly Python news and links (Mar 9)

2010-04-21 Thread Cameron Laird
QOTW:  "I used to think anonymous functions (AKA blocks, etc...) would
be a
nice feature for Python.

Then I looked at a stack trace from a different programming language
with
lots of anonymous functions. (I believe it was perl.)

I became enlightened." - Jonathan Gardner, apparently echoing Guido's
criterion of debuggability in language design
   http://groups.google.com/group/comp.lang.python/msg/3ebe7a0b78086acf


Editor Cameron Laird apologizes for the following three entries, which
appeared in the last installment only in an unusably garbled form:
   There is no module in the standard library to handle filesystem
paths
   in an OO way - but why?
   http://groups.google.com/group/comp.lang.python/t/f580fb3763208425/

   A "History Channel" special: how the way a TAB key was interpreted
   changed over time
   http://groups.google.com/group/comp.lang.python/t/82d9181fcd31ffe4/

   After a false start, finally we get our first "Is it Call-By-Value
or
   Call-By-Reference?" thread of the year!
   http://groups.google.com/group/comp.lang.python/t/fd36962c4970ac48/
---
Back in the present,
   Three new preliminary Python versions are now available for
testing:
   Python 2.7 alpha 4
   http://groups.google.com/group/comp.lang.python/t/779e761d934dbc1a/
   Python 3.1.2 release candidate
   http://permalink.gmane.org/gmane.comp.python.general/656887
   Python 2.6.5 release candidate 1
   http://permalink.gmane.org/gmane.comp.python.devel/111319

   Forget those Java recipes when implementing the Singleton pattern:
   http://groups.google.com/group/comp.lang.python/t/9228a3763eb552b3/

   How to obtain a module docstring without actually importing it:
   http://groups.google.com/group/comp.lang.python/t/ca97d63ace6ea81d/

   Do something only if a certain module is already in use by the
current
   program:
   http://groups.google.com/group/comp.lang.python/t/ee22c223fa73a429/

   Functions, bound methods, unbound ones: what are their differences?
   http://groups.google.com/group/comp.lang.python/t/72ab93ba395822ed/

   Automatically adding global names to a module: how to do that, and
   alternatives to use when it's not a good idea:
   http://groups.google.com/group/comp.lang.python/t/40837c4567d64745/

   Raymond Hettinger on the rationale behind the collections.Counter
class:
   http://groups.google.com/group/comp.lang.python/t/64d0fe87f7ea9e6/

   How Tk 8.5 + ttk (the version that ships with Python 2.6) compares
to
   other GUI toolkits:
   http://groups.google.com/group/comp.lang.python/t/d8d24eacf022ed75/

   The actual behavior of slicing like L[n::-1] is not properly
documented:
   http://groups.google.com/group/comp.lang.python/t/add9aa920b55eacc/

   Documenting a function with #comments instead of a proper docstring
is
   silly, isn't it? How does that affect source code quality?
(Including
   historical disgression going back to the PDP-8, the Altair and even
   nanocomputers...)
   http://groups.google.com/group/comp.lang.python/t/dea5c94f3d058e26/

   Coming from Perl, one has to unlearn (bad?) habits and embrace
Python's
   "rigid flexibility":
   http://groups.google.com/group/comp.lang.python/t/4bfdc60d3f58c960/
   http://groups.google.com/group/comp.lang.python/t/24bfa00b428f868f/

   And for those perl-like oneliner fans, here is dos2unix:
   http://groups.google.com/group/comp.lang.python/t/c4b63debe91d51c7/

   Perl has CPAN. Python has PyPI + easy_install, but they lack many
   important features. How could we improve that?
   http://groups.google.com/group/comp.lang.python/t/c2c452cc4aaa6e98/

   The pysandbox project provides a sandbox where untrusted code
cannot
   modify its environment; now looking for someone to find holes in
it:
   http://groups.google.com/group/comp.lang.python/t/87bf10f8acede7c3/



Everything Python-related you want is probably one or two clicks away
in
these pages:

   Python.org's Python Language Website is the traditional
   center of Pythonia
   http://www.python.org
   Notice especially the master FAQ
   http://www.python.org/doc/FAQ.html

   PythonWare complements the digest you're reading with the
   marvelous daily python url
http://www.pythonware.com/daily

   Just beginning with Python?  This page is a great place to start:
   http://wiki.python.org/moin/BeginnersGuide/Programmers

   The Python Papers aims to publish "the efforts of Python
enthusiasts":
   http://pythonpapers.org/
   The Python Magazine is a technical monthly devoted to Python:
   http://pythonmagazine.com

   Readers have recommended the "Planet" site:
   http://planet.python.org

   comp.lang.python.announce announces new Python software.  Be
   sure to scan this newsgroup weekly.
   http://groups.google.com/group/comp.lang.py

Python-URL! - weekly Python news and links (Mar 17)

2010-04-21 Thread Cameron Laird
QOTW:  "... [T]hat kills yet another usage of C ..." - Maciej
Fijalkowski
   http://morepypy.blogspot.com/2009/11/some-benchmarking.html


   Making operations in the Fraction class automatically return a
subclass
   instance when called with subclass arguments:
   http://groups.google.com/group/comp.lang.python/t/2512697a901d4752/

   Providing alternate constructors for an existing class:
   http://groups.google.com/group/comp.lang.python/t/99e87afb58f3ef3d/

   Checking existence of files matching a certain pattern:
   http://groups.google.com/group/comp.lang.python/t/a95183b7ae931b7a/

   How to gracefully exit the current process, when there are multiple
   threads, on Windows:
   http://groups.google.com/group/comp.lang.python/t/5a6a7bcfd30fb7ce/

   How inspect.stack()/sys._getframe() interact with local variables
and
   references:
   http://groups.google.com/group/comp.lang.python/t/fc8bf697fa9a01a4/

   Alternatives to the re module when looking for a large set of
constant
   strings:
   http://groups.google.com/group/comp.lang.python/t/63e6e177be7dea21/

   Why does Python syntax allow "break" from no more than a single
loop
   at one time?
   http://groups.google.com/group/comp.lang.python/t/96f3407480ed39ab/

   Trying to start a new Python meeting group is not easy:
   http://groups.google.com/group/comp.lang.python/t/a24bf882c72c555/

   Bad interactions between cmd.exe code page (Windows), Popen, and
the
   UTF-8 encoding:
   http://groups.google.com/group/comp.lang.python/t/2da1f1de573e5663/

   An example showing how to use the locale module to format a number
with
   thousand separators:
   http://groups.google.com/group/comp.lang.python/t/42e63e3a53f03593/

   How non-ASCII characters get entered from the Python interactive
console:
   http://groups.google.com/group/comp.lang.python/t/77271907fb195aca/

   Raymond Hettinger on the rationale behind the collections.Counter
class:
   http://groups.google.com/group/comp.lang.python/t/64d0fe87f7ea9e6/

   You could not go to PyCon Atlanta? No problem - many PyCon talks
from
   the last two years are available here:
   httppycon.blip.tv/
   http://python.mirocommunity.org/

   An interesting problem: find out the the algorithm used to compute
a
   given CRC
   http://groups.google.com/group/comp.lang.python/t/b22db1e3e63db596/



Everything Python-related you want is probably one or two clicks away
in
these pages:

   Python.org's Python Language Website is the traditional
   center of Pythonia
   http://www.python.org
   Notice especially the master FAQ
   http://www.python.org/doc/FAQ.html

   PythonWare complements the digest you're reading with the
   marvelous daily python url
http://www.pythonware.com/daily

   Just beginning with Python?  This page is a great place to start:
   http://wiki.python.org/moin/BeginnersGuide/Programmers

   The Python Papers aims to publish "the efforts of Python
enthusiasts":
   http://pythonpapers.org/
   The Python Magazine is a technical monthly devoted to Python:
   http://pythonmagazine.com

   Readers have recommended the "Planet" site:
   http://planet.python.org

   comp.lang.python.announce announces new Python software.  Be
   sure to scan this newsgroup weekly.
   http://groups.google.com/group/comp.lang.python.announce/topics

   Python411 indexes "podcasts ... to help people learn Python ..."
   Updates appear more-than-weekly:
   http://www.awaretek.com/python/index.html

   The Python Package Index catalogues packages.
   http://www.python.org/pypi/

   Much of Python's real work takes place on Special-Interest Group
   mailing lists
   http://www.python.org/sigs/

   Python Success Stories--from air-traffic control to on-line
   match-making--can inspire you or decision-makers to whom you're
   subject with a vision of what the language makes practical.
   http://www.pythonology.com/success

   The Python Software Foundation (PSF) has replaced the Python
   Consortium as an independent nexus of activity.  It has official
   responsibility for Python's development and maintenance.
   http://www.python.org/psf/
   Among the ways you can support PSF is with a donation.
   http://www.python.org/psf/donations/

   The Summary of Python Tracker Issues is an automatically generated
   report summarizing new bugs, closed ones, and patch submissions.
   
http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date

   nullege is an interesting search Web application, with the
intelligence
   to distinguish between Python code and comments.  It provides what
   appear to be relevant results, and demands neither Java nor CSS be
   enabled:
   http://www.nullege.com

   Although unmaintained since 2002, the Cetus collection of Python
   hyperlinks retains a few gems.
   

Python-URL! - weekly Python news and links (Feb 9)

2010-04-21 Thread Cameron Laird
QOTW:  "You see? That's what I like about the Python community: people
even
apologise for apologising :)" - Tim Golden
http://groups.google.com/group/comp.lang.python/msg/858d1c31d0c2adff


   The third alpha version of Python 2.7 is ready for testing:
   http://groups.google.com/group/comp.lang.python/t/6f49dacfe8759508/

   How to enumerate all possible strings matching a given regular
expression:
   http://groups.google.com/group/comp.lang.python/t/1b78346c6661ac4f/

   Which language features do you like most?
   http://groups.google.com/group/comp.lang.python/t/599b3c9772421ece/

   Implementing a two-dimensional array in a simple way seems to
actually
   be more efficient than other, more sophisticated alternatives:
   http://groups.google.com/group/comp.lang.python/t/55e595d6dc4ca3f4/

   The new GIL (to be implemented in Python 3.2) will provide less
overhead,
   especially in multicore CPUs:
   http://groups.google.com/group/comp.lang.python/t/586ef2d3685fa7ea/

   In Python 3, 'exec' inside a function does not have the same effect
   as before:
   http://groups.google.com/group/comp.lang.python/t/7a046e4ede9c310a/

   Using Queue objects to feed and synchronize several worker threads:
   http://groups.google.com/group/comp.lang.python/t/32256dd608c9c02/

   New generation IDEs should provide much better and integrated
   refactoring tools:
   http://groups.google.com/group/comp.lang.python/t/e019614ea149e7bd/

   There is no module in the standard library to handle filesystem
paths
   in an OO way - but why?
   http://groups.google.com/group/comp.lang.pythonf580fb3763208425ece/

   A "History Channel" special: how the way a TAB key was interpreted
   changed over time
   http://groups.google.com/group/comp.lang.python82d9181fcd31ffea3f4/

   After a false start, finally we get our first "Is it Call-By-Value
or
   Call-By-Reference?" thread of the year!
   http://groups.google.com/group/comp.lang.pythonfd36962c4970ac487ea/



Everything Python-related you want is probably one or two clicks away
in
these pages:

   Python.org's Python Language Website is the traditional
   center of Pythonia
   http://www.python.org
   Notice especially the master FAQ
   http://www.python.org/doc/FAQ.html

   PythonWare complements the digest you're reading with the
   marvelous daily python url
http://www.pythonware.com/daily

   Just beginning with Python?  This page is a great place to start:
   http://wiki.python.org/moin/BeginnersGuide/Programmers

   The Python Papers aims to publish "the efforts of Python
enthusiasts":
   http://pythonpapers.org/
   The Python Magazine is a technical monthly devoted to Python:
   http://pythonmagazine.com

   Readers have recommended the "Planet" site:
   http://planet.python.org

   comp.lang.python.announce announces new Python software.  Be
   sure to scan this newsgroup weekly.
   http://groups.google.com/group/comp.lang.python.announce/topics

   Python411 indexes "podcasts ... to help people learn Python ..."
   Updates appear more-than-weekly:
   http://www.awaretek.com/python/index.html

   The Python Package Index catalogues packages.
   http://www.python.org/pypi/

   Much of Python's real work takes place on Special-Interest Group
   mailing lists
   http://www.python.org/sigs/

   Python Success Stories--from air-traffic control to on-line
   match-making--can inspire you or decision-makers to whom you're
   subject with a vision of what the language makes practical.
   http://www.pythonology.com/success

   The Python Software Foundation (PSF) has replaced the Python
   Consortium as an independent nexus of activity.  It has official
   responsibility for Python's development and maintenance.
   http://www.python.org/psf/
   Among the ways you can support PSF is with a donation.
   http://www.python.org/psf/donations/

   The Summary of Python Tracker Issues is an automatically generated
   report summarizing new bugs, closed ones, and patch submissions.
   
http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date

   nullege is an interesting search Web application, with the
intelligence
   to distinguish between Python code and comments.  It provides what
   appear to be relevant results, and demands neither Java nor CSS be
   enabled:
   http://www.nullege.com

   Although unmaintained since 2002, the Cetus collection of Python
   hyperlinks retains a few gems.
   http://www.cetus-links.org/oo_python.html

   Python FAQTS
   http://python.faqts.com/

   The Cookbook is a collaborative effort to capture useful and
   interesting recipes.
   http://code.activestate.com/recipes/langs/python/

   Many Python conferences around the world are in preparation.
   Watch this space for links to them.

   Among several Python-oriented

Re: ctypes: delay conversion from c_char_p to string

2010-04-21 Thread Brendan Miller
Here's the method I was using. Note that tmp_char_ptr is of type
c_void_p. This should avoid the memory leak, assuming I am
interpreting the semantics of the cast correctly. Is there a cleaner
way to do this with ctypes?

def get_prop_string(self, prop_name):
# Have to work with c_void_p to prevent ctypes from copying to a string
# without giving me an opportunity to destroy the original string.
tmp_char_ptr = _get_prop_string(self._props, prop_name)
prop_val = cast(tmp_char_ptr, c_char_p).value
_string_destroy(tmp_char_ptr)
return prop_val

On Wed, Apr 21, 2010 at 3:15 PM, Brendan Miller  wrote:
> I have a function exposed through ctypes that returns a c_char_p.
> Since I need to deallocate that c_char_p, it's inconvenient that
> ctypes copies the c_char_p into a string instead of giving me the raw
> pointer. I believe this will cause a memory leak, unless ctypes is
> smart enough to free the string itself after the copy... which I
> doubt.
>
> Is there some way to tell ctypes to return an actual c_char_p, or is
> my best bet to return a c_void_p and cast to c_char_p when I'm reading
> to convert to a string?
>
> Thanks
> Brendan
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code redundancy

2010-04-21 Thread Ryan Kelly
On Tue, 2010-04-20 at 14:43 +0100, Alan Harris-Reid wrote:
> Hi,
> 
> During my Python (3.1) programming I often find myself having to repeat 
> code such as...
> 
> class1.attr1 = 1
> class1.attr2 = 2
> class1.attr3 = 3
> class1.attr4 = 4
> etc.
> 
> Is there any way to achieve the same result without having to repeat the 
> class1 prefix?  Before Python my previous main language was Visual 
> Foxpro, which had the syntax...
> 
> with class1
>.attr1 = 1
>.attr2 = 2
>.attr3 = 3
>.attr4 = 4
>etc.
> endwith
> 
> Is there any equivalent to this in Python?


Please don't take this as in invitation to disregard the excellent
advice already received in this thread - I just want to point out that
python can usually be bent to your will.  Observe:

  
  from withhacks import namespace

  with namespace(class1):
  attr1 = 1
  attr2 = 2


This will do pretty much what you get from the "with" statement in
javascript (I assume it's similar to Visual Foxpro).


But don't use this in any real code.  Seriously, don't even think about
it.  You don't want to know the kind of abuses that go on under the
covers to make this kind of syntax hacking work...



  Cheers,

 Ryan



-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
r...@rfk.id.au|  http://www.rfk.id.au/ramblings/gpg/ for details


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


Re: Deleting more than one element from a list

2010-04-21 Thread candide

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


Python-URL! - weekly Python news and links (Apr 21)

2010-04-21 Thread Cameron Laird
QOTW:  "There's no RightAnswer(tm), just our best guess as to what is
the most
useful behavior for the most number of people." - Raymond Hettinger
   http://groups.google.com/group/comp.lang.python/msg/e7f78ef27811781b


   First beta version of Python 2.7 is available:
   http://groups.google.com.ar/group/comp.lang.python/t/d036954e11a264dd/

   Why still a 2.7 release, when 3.1 is already out?
   http://groups.google.com.ar/group/comp.lang.python/t/f8a5028affe772ed/

   Deciding when to pass around a parameter, and when to keep it as an
   instance attribute:
   http://groups.google.com.ar/group/comp.lang.python/t/6f51302327b58aac/

   A class is more than just a container for functions and attributes:
   http://groups.google.com.ar/group/comp.lang.python/t/5525bd981dd9ece6/

   Lie Ryan tries to explain the various escaping levels of a regular
   expression string:
   http://groups.google.com.ar/group/comp.lang.python/t/3a27b819307c0cb6/

   Peter Otten shows how to make string.Template resolve dotted.names
from
   nested dictionaries:
   http://groups.google.com.ar/group/comp.lang.python/t/d482f74d16fcbbb2/

   Simple string operations, regular expressions, and a full blown
parser:
   the right tools for different jobs
   http://groups.google.com.ar/group/comp.lang.python/t/888b3fe934e2c5e2/

   The list comprehension loop variable, nested scopes, and common
sense
   applied to programming languages:
   http://groups.google.com.ar/group/comp.lang.python/t/2b64b9a9069a324f/

   Performance of list vs. set equality operations -- later:
encapsulation,
   the Open/Closed principle, and why overriding __special__ methods
may
   not affect certain built-in type operations:
   http://groups.google.com.ar/group/comp.lang.python/t/818d143c7e9550bc/

   __del__ methods are not the same as C++ destructors; they might not
even
   be called:
   http://groups.google.com.ar/group/comp.lang.python/t/6c875525954df888/

   Abstract Base Classes affect the way issubclass() works -- it's not
   based only on the chain of MRO anymore:
   http://groups.google.com.ar/group/comp.lang.python/t/bd97c8e475e4da67/

   wxPython and a bad example of 'property' usage:
   http://groups.google.com.ar/group/comp.lang.python/t/d88a387b5dca/

   Keeping client expectations realistic and avoiding scope creep:
   http://www.gossamer-threads.com/lists/python/python/819932


Everything Python-related you want is probably one or two clicks away
in
these pages:

   Python.org's Python Language Website is the traditional
   center of Pythonia
   http://www.python.org
   Notice especially the master FAQ
   http://www.python.org/doc/FAQ.html

   PythonWare complements the digest you're reading with the
   marvelous daily python url
http://www.pythonware.com/daily

   Just beginning with Python?  This page is a great place to start:
   http://wiki.python.org/moin/BeginnersGuide/Programmers

   The Python Papers aims to publish "the efforts of Python
enthusiasts":
   http://pythonpapers.org/
   The Python Magazine is a technical monthly devoted to Python:
   http://pythonmagazine.com

   Readers have recommended the "Planet" site:
   http://planet.python.org

   comp.lang.python.announce announces new Python software.  Be
   sure to scan this newsgroup weekly.
   http://groups.google.com/group/comp.lang.python.announce/topics

   Python411 indexes "podcasts ... to help people learn Python ..."
   Updates appear more-than-weekly:
   http://www.awaretek.com/python/index.html

   The Python Package Index catalogues packages.
   http://www.python.org/pypi/

   Much of Python's real work takes place on Special-Interest Group
   mailing lists
   http://www.python.org/sigs/

   Python Success Stories--from air-traffic control to on-line
   match-making--can inspire you or decision-makers to whom you're
   subject with a vision of what the language makes practical.
   http://www.pythonology.com/success

   The Python Software Foundation (PSF) has replaced the Python
   Consortium as an independent nexus of activity.  It has official
   responsibility for Python's development and maintenance.
   http://www.python.org/psf/
   Among the ways you can support PSF is with a donation.
   http://www.python.org/psf/donations/

   The Summary of Python Tracker Issues is an automatically generated
   report summarizing new bugs, closed ones, and patch submissions.
   
http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date

   nullege is an interesting search Web application, with the
intelligence
   to distinguish between Python code and comments.  It provides what
   appear to be relevant results, and demands neither Java nor CSS be
   enabled:
   http://www.nullege.com

   Although unmaintained since 2002, the Cetus collection of P

Re: Code redundancy

2010-04-21 Thread Alan Harris-Reid

Ryan Kelly wrote:

On Tue, 2010-04-20 at 14:43 +0100, Alan Harris-Reid wrote:
  

Hi,

During my Python (3.1) programming I often find myself having to repeat 
code such as...


class1.attr1 = 1
class1.attr2 = 2
class1.attr3 = 3
class1.attr4 = 4
etc.

Is there any way to achieve the same result without having to repeat the 
class1 prefix?  Before Python my previous main language was Visual 
Foxpro, which had the syntax...


with class1
   .attr1 = 1
   .attr2 = 2
   .attr3 = 3
   .attr4 = 4
   etc.
endwith

Is there any equivalent to this in Python?




Please don't take this as in invitation to disregard the excellent
advice already received in this thread - I just want to point out that
python can usually be bent to your will.  Observe:

  
  from withhacks import namespace


  with namespace(class1):
  attr1 = 1
  attr2 = 2


This will do pretty much what you get from the "with" statement in
javascript (I assume it's similar to Visual Foxpro).


But don't use this in any real code.  Seriously, don't even think about
it.  You don't want to know the kind of abuses that go on under the
covers to make this kind of syntax hacking work...



  Cheers,

 Ryan

Hi Ryan, thanks for that.

No - I will not be adopting that solution.  Is there anything Python 
can't do if you bend the rules far enough?  ;-)


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


withhacks module (Re: Code redundancy)

2010-04-21 Thread python
Ryan,

Your withhacks module looks very interesting.
http://pypi.python.org/pypi/withhacks

What are your specific concerns about its use? Are there portability
concerns?

Malcolm


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


Re: withhacks module (Re: Code redundancy)

2010-04-21 Thread Ryan Kelly
On Wed, 2010-04-21 at 19:43 -0400, pyt...@bdurham.com wrote:
> Ryan,
> 
> Your withhacks module looks very interesting.
> http://pypi.python.org/pypi/withhacks
> 
> What are your specific concerns about its use? Are there portability
> concerns?


It combines two things you just don't see in respectable python programs
- bytecode hacking and trace-function hacking.  And I'm sure its
performance is less than stellar, although much of that could be fixed
with some careful bytecode caching.

I'd be surprised if it runs under alternative python implementations,
and not surprised if it doesn't even work on Python 3 - although I
haven't tried it to confirm either way.

Having said that, it should work as advertised on Python 2.X  with
minimal fuss. So if you don't care about portability or about that dirty
feeling you get from messing with the Python internals, then have at
it :-)


  Cheers,

 Ryan



-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
r...@rfk.id.au|  http://www.rfk.id.au/ramblings/gpg/ for details


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


Re: when should I explicitly close a file?

2010-04-21 Thread Lawrence D'Oliveiro
In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:

> Since in python nothing is guaranteed about implicit file close ...

It is guaranteed that objects with a reference count of zero will be 
disposed. In my experiments, this happens immediately.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Download Proprietary Microsoft Products Now

2010-04-21 Thread Lawrence D'Oliveiro
In message , "Martin 
v. Löwis" wrote:

> Brian Blais wrote:
>
>> On Apr 12, 2010, at 16:36 , Martin v. Loewis is wrote:
>> 
>>> If you are planning to build Python extension modules in the next five
>>> years, I recommend that you obtain a copy of VS Express
>> 
>> Am I missing something here?  I have heard this before, but I have built
>> extension modules many times under windows (using Cython) and never once
>> used a MS product.
> 
> It's fine if your package supports being compiled with Mingw32. A lot of
> source code can't be compiled this way, either because gcc doesn't
> support some of the MS extensions (in particular wrt. COM) ...

But then such code will not be portable to anything but Windows.

> ... or because Mingw32 doesn't provide the header files (in particular
> wrt. C++), or because linking with a library is necessary that uses the
> MSVC mangling, not the g++ one (again, for C++).

Again, that would be code that’s not portable off Windows.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when should I explicitly close a file?

2010-04-21 Thread Chris Rebert
On Wed, Apr 21, 2010 at 5:53 PM, Lawrence D'Oliveiro wrote:
> In message <4bc9aad...@dnews.tpgi.com.au>, Lie Ryan wrote:
>
>> Since in python nothing is guaranteed about implicit file close ...
>
> It is guaranteed that objects with a reference count of zero will be
> disposed.

> In my experiments, this happens immediately.

Experiment with an implementation other than CPython and prepare to be
surprised.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: withhacks module (Re: Code redundancy)

2010-04-21 Thread python
Ryan,

> So if you don't care about portability or about that dirty feeling you get 
> from messing with the Python internals, then have at it :-)

Warnings aside, its very clever code. Thanks for sharing!

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


Re: Download Proprietary Microsoft Products Now

2010-04-21 Thread Lawrence D'Oliveiro
In message <4bc8547...@dnews.tpgi.com.au>, Lie Ryan wrote:

> ... so you should get the full installer if you want to insure yourself
> from Microsoft pulling the plug out.

I wonder how many Windows users will be able to figure that out...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Download Proprietary Microsoft Products Now

2010-04-21 Thread David Cournapeau
On Thu, Apr 22, 2010 at 9:59 AM, Lawrence D'Oliveiro
 wrote:

>> ... or because Mingw32 doesn't provide the header files (in particular
>> wrt. C++), or because linking with a library is necessary that uses the
>> MSVC mangling, not the g++ one (again, for C++).
>
> Again, that would be code that’s not portable off Windows.

Not really, part of the issue is that mingw uses ancient gcc (3.x
series), and g++ 3.x has poor C++ support compared to MSVC (or gcc
4.x).

There is also the issue that gcc debugging symbols are totally
incompatible with MSVC, so you cannot debug things from anything but
gdb. Gcc for win64 is also not that stable yet - when porting numpy
and scipy on windows 64, I got numerous issues with it.

None of this has anything to do with portability.

cheers,

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


  1   2   >