Re: when should I explicitly close a file?

2010-04-24 Thread Alf P. Steinbach

* Steven D'Aprano:

On Fri, 23 Apr 2010 13:19:41 +0200, Alf P. Steinbach wrote:


But for a literal context-free interpretation e.g. the 'sys.getrefcount'
function is not documented as CPython only and thus an implementation
that didn't do reference counting would not be a conforming Python
implementation.


Since Jython and IronPython are conforming Python implementations, and 
Guido has started making policy decisions specifically to support these 
other implementations (e.g. the language feature moratorium, PEP 3003), I 
think we can assume that this is a documentation bug.


The documentation for Jython specifies the same for 'sys.getrefcount'.

However, testing:


*sys-package-mgr*: processing new jar, 'C:\Program Files\jython2.5.1\jython.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\resources.jar'

*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\rt.jar'
*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\jsse.jar'
*sys-package-mgr*: processing new jar, 'C:\Program Files\Java\jre6\lib\jce.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\charsets.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\dnsns.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\localedata.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunjce_provider.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunmscapi.jar'
*sys-package-mgr*: processing new jar, 'C:\Program 
Files\Java\jre6\lib\ext\sunpkcs11.jar'

A created
Traceback (most recent call last):
  File "c:\test\refcount.py", line 17, in 
writeln( str( sys.getrefcount( a ) - 1 ) )
AttributeError: 'systemstate' object has no attribute 'getrefcount'



However, a Python implementation that always returned 0 for 
sys.getrefcount would technically satisfy the word of the documentation, 
if not the spirit.


Yes.

OK, learned something new: I though Jython actually implemented getrefcount.

The Jython docs says it does...


Cheers,

- Alf

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


Re: DLLs loading in interpreter but not with direct run on Windows

2010-04-24 Thread Michel Claveau - MVP
Hi!

AMHA (IMO), it is PyQT4 who change the DLL loader...

@+
-- 
MCI
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socked and bytes operation

2010-04-24 Thread luca72


i attach some part of the server so maybe you can help me to
understand :
Packet description (before encryption)

Messages sent back and forth between newcamd and a cardserver always
consist of
a three byte header and (optional) data bytes. The header always
starts with a
command tag byte. This is always the first byte (byte 1) of a message.
In case of an ECM or EMM this is simply the table id of the ECM (0x80,
0x81)
or EMM (0x82 - 0x8f). Other commands use cmd tags starting from 0xe0
like this:

#define CWS_FIRSTCMDNO 0xe0

typedef enum
{
MSG_CLIENT_2_SERVER_LOGIN = CWS_FIRSTCMDNO,
MSG_CLIENT_2_SERVER_LOGIN_ACK,
MSG_CLIENT_2_SERVER_LOGIN_NAK,
MSG_CARD_DATA_REQ,
MSG_CARD_DATA,
MSG_SERVER_2_CLIENT_NAME,
MSG_SERVER_2_CLIENT_NAME_ACK,
MSG_SERVER_2_CLIENT_NAME_NAK,
MSG_SERVER_2_CLIENT_LOGIN,
MSG_SERVER_2_CLIENT_LOGIN_ACK,
MSG_SERVER_2_CLIENT_LOGIN_NAK,
MSG_ADMIN,
MSG_ADMIN_ACK,
MSG_ADMIN_LOGIN,
MSG_ADMIN_LOGIN_ACK,
MSG_ADMIN_LOGIN_NAK,
MSG_ADMIN_COMMAND,
MSG_ADMIN_COMMAND_ACK,
MSG_ADMIN_COMMAND_NAK,
MSG_KEEPALIVE = CWS_FIRSTCMDNO + 0x1d,
} net_msg_type_t;


Client to Server Login

This describes how to login . Remember each card has its
own dedicated TCP port, this is how you choose, which card you want.

Client <- Server 1/5 - 090f - Thu Jan  8 17:20:17 CET 2004
encryption: none
--
00: 77 9d cc 5d d2 0d 59 2e dc ed b8 17 c1 ab w  ]  Y.   (this
are the bites that i receive ofter the connection)

After opening a TCP connection to the server, the client first
receives 14
random bytes. These bytes are to be XORed to the Triple-DES key from
the config
file. (cardserver: DESKEY = 0102030405060708091011121314). The result
forms the
Triple DES key to be used to send Username and Password to the
cardserver, I
call it the login key.

for make this i do :
import socket,crypt, itertools
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('192.168.1.11',11502))
ricevo = sock.recv(8192)
stringa = '0102030405060708091011121314'
ricevo = map(ord, ricevo)
print ricevo
#print '\n'
#luca= []
stringa = map(ord, stringa)
print stringa

plain_chars = []

for cypher_char, key_char in zip(ricevo, itertools.cycle(stringa)):
plain_char = (cypher_char) ^ (key_char)
plain_chars.append(plain_char)
print plain_chars

i get:
[133, 234, 201, 215, 129, 130, 252, 113, 15, 226, 29, 193, 67, 103]


Client -> Server 1/5 - 090f - Thu Jan  8 17:20:18 CET 2004
encryption: login
--
00: e0 00 29 64 75 6d 6d 79 00 24 31 24 61 62 63 64 )dummy $1$abcd
10: 65 66 67 68 24 6e 70 53 45 54 51 73 72 49 6d 33   efgh$npSETQsrIm3
20: 35 4d 51 66 69 55 49 41 64 6e 2e 00   5MQfiUIAdn.

Next the client has to send a packet with cmd =
MSG_CLIENT_2_SERVER_LOGIN (e0)
including username and password in the data field.
The username is sent as a C-String (NULL terminated), the password
follows directly after the zero termination byte of the username. The
password has to be put through the glibc crypt() function, using salt
$1$abcdefgh$. The password in the data field has to be NULL terminated
and the
packet encrypted with the login key.

cryptPw = crypt(plainPw, "$1$abcdefgh$");

If i understand right i have to do this :

ris = cript.crypt(password,"$1$abcdefgh$")
than
sock.send('e0'+password+ris)
and than read again
is this correct?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NotImplemented used in Decimal

2010-04-24 Thread Steven D'Aprano
On Fri, 23 Apr 2010 23:51:39 -0700, Chris Rebert wrote:

> If the conversion to Decimal in  _convert_other() fails, the operator
> method returns NotImplemented to signal to the interpreter that Decimal
> doesn't know how to do the requested operation with an operand of the
> given type; the interpreter will fall back by calling the reflected
> method of the other operand. 

I knew that works with comparisons __eq__ etc, but somehow I had a mental 
blank about arithmetic operators! Yes, you're right, and the docs say so 
explicitly. Somehow I had convinced myself that NotImplemented only 
worked with comparisons.


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


NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
I wanted to do something like this:

while True:
  try:
def fun(a, b=b, c=c): pass
  except NameError as ne:
name = get_the_var_name(ne)
locals()[name] = ''
  else: break

What's be best way to implement the function 
get_the_var_name(ne) that returns the name
of the variable that could not be found?

Thanks in advance,

Yingjie


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


MySQLdb - weird formatting inconsistency

2010-04-24 Thread Anthra Norell

Hi all,

   Can anyone explain this? Three commands with three different cutoff 
dates (12/30, 12/31 and 1/1) produce a formatting inconsistency. Examine 
the third field. The first and last run represents it correctly. The 
second run strips it. The field happens to be a record ID and getting it 
stripped in an unpredictable manner obviously makes it useless as an ID.


>>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1987-12-30";')

761L
>>> for item in finance: print item

(9737, '', ' 2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg', 
'IIfAC', '', '', 'Schweigegeld')
(9738, '', ' 2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC', 
'', '', '1 Schachtel 9mm')
(9739, '', ' 2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', 
'', '', 'Gladiolen')

. . .

>>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1987-12-31";')

792L
>>> for item in finance: print item
  
(9737, '', '2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg', 'IIfAC', 
'', '', 'Schweigegeld')
(9738, '', '2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC', 
'', '', '1 Schachtel 9mm')
(9739, '', '2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', 
'', '', 'Gladiolen')

. . .

>>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1988-01-01";')

796L
>>> for item in finance: print item
  
(9737, '', ' 2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg', 
'IIfAC', '', '', 'Schweigegeld')
(9738, '', ' 2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC', 
'', '', '1 Schachtel 9mm')
(9739, '', ' 2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', 
'', '', 'Gladiolen')

. . .
  Curiously, the second command with a limit works correctly, including 
'limit 792' which is equal to the number of selected records.


>>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1987-12-31" limit 792;')

792L
>>> for item in finance: print item

(9737, '', ' 2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg', 
'IIfAC', '', '', 'Schweigegeld')
(9738, '', ' 2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC', 
'', '', '1 Schachtel 9mm')
(9739, '', ' 2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', 
'', '', 'Gladiolen')

. . .

  A variation of the problematic command showing only the IDs between 
start and end marks works okay too:


>>> finance.execute ('select concat("|", Item, "|") from j where Date 
between "1987-01-01" and "1987-12-31";')

792L
>>> for item in finance: print item
  
('| 2278|',)

('| 2279|',)
('| 2280|',)
. . .

  
  After playing around with various date combinations the impression 
emerges that only 1/1/87-12/31/87 misbehaves. All other combinations I 
tried did okay.
  All runs were done in an IDLE window, one after the other, with the 
same cursor object.

  Ubuntu: 9.10. Python: 2.6. MySQL: 5.1.45.
  The record ID is declared as char (8) and a unique key. I don't think 
this matters, though, since run from the mysql command line everything 
works fine. It does look like a MySQLdb problem and a pretty weird one 
at that. Suggestions?


Thanks

Frederic

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


Re: NameError: how to get the name?

2010-04-24 Thread James Mills
On Sat, Apr 24, 2010 at 9:19 PM, Yingjie Lan  wrote:
> I wanted to do something like this:
>
> while True:
>  try:
>    def fun(a, b=b, c=c): pass
>  except NameError as ne:
>    name = get_the_var_name(ne)
>    locals()[name] = ''
>  else: break
>
> What's be best way to implement the function
> get_the_var_name(ne) that returns the name
> of the variable that could not be found?

A NameError Exception does not store the name of the
variable. it has two attributes: .args and .message
both of which contain (usually) a string such as:

"name 'x' is not defined"

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


Re: NameError: how to get the name?

2010-04-24 Thread Steven D'Aprano
On Sat, 24 Apr 2010 04:19:43 -0700, Yingjie Lan wrote:

> I wanted to do something like this:
> 
> while True:
>   try:
> def fun(a, b=b, c=c): pass
>   except NameError as ne:
> name = get_the_var_name(ne)
> locals()[name] = ''
>   else: break

This won't work. Writing to locals() does not actually change the local 
variables. Try it inside a function, and you will see it doesn't work:

def test():
x = 1
print(x)
locals()['x'] = 2
print(x)

A better approach would be:

try:
b
except NameError:
b = ''
# and the same for c

def fun(a, b=b, c=c):
pass


Better still, just make sure b and c are defined before you try to use 
them!



> What's be best way to implement the function get_the_var_name(ne) that
> returns the name of the variable that could not be found?


'name' in locals()



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


Re: On Class namespaces, calling methods

2010-04-24 Thread Lie Ryan
On 04/24/10 06:07, Aahz wrote:
> In article <4bc120bd$0$8850$c3e8...@news.astraweb.com>,
> Steven D'Aprano   wrote:
>>
>> I can only think of two circumstances where old-style classes are 
>> *wrong*: if you use multiple inheritance with a diamond diagram ("...now 
>> you have THREE problems" *wink*), if you intend using descriptors such as 
>> properties, or if you need __slots__. That's three circumstances: 
>> multiple inheritance with a diamond diagram, descriptors, __slots__, and 
>> __getattribute__. Four circumstances.
>>
>> Any other time, they're merely discouraged, and gone in Python 3.x.
> 
> Discouraged by some people.  I certainly encourage the use of old-style
> classes unless there's a specific reason to use new-style classes.

For what reason?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: delay conversion from c_char_p to string

2010-04-24 Thread Thomas Heller
Brendan Miller schrieb:
> 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?

Yes, there is.  When you create a subclass of c_char_p (or any other 'simple'
ctypes type like c_wchar_p or even c_int and alike) then the automatic 
conversion
to native Python types like string, unicode, integer is not done.
The function will return an instance of that specific class; you can
retrive the value via the .value property and deallocate the resources
in the destructor for example.


-- 
Thanks,
Thomas

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


Re: Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

2010-04-24 Thread DarkBlue
On Apr 22, 4:55 pm, joamag  wrote:
> Does anybody know a cross platform way to retrieve the default DNS
> server IP address in python ?
>
> Thanks !
> João


import os,urllib2,re


def getIpAddr():
"""
Function for parsing external ip adress by pinging dyndns.com
"""
External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read()
m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
my_IP= m.group(1)
return my_IP




print('Current Ip from DynDns :  %s ') % getIpAddr()



this gets you your ip address

hope it helps.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
--- On Sat, 4/24/10, Steven D'Aprano  
wrote:

> From: Steven D'Aprano 
> Subject: Re: NameError: how to get the name?
> To: python-list@python.org
> Date: Saturday, April 24, 2010, 4:07 PM
> On Sat, 24 Apr 2010 04:19:43 -0700,
> Yingjie Lan wrote:
> 
> > I wanted to do something like this:
> > 
> > while True:
> >   try:
> > def fun(a, b=b, c=c): pass
> >   except NameError as ne:
> > name = get_the_var_name(ne)
> > locals()[name] = ''
> >   else: break
> 
> This won't work. Writing to locals() does not actually
> change the local 
> variables. Try it inside a function, and you will see it
> doesn't work:
> 

I tried this, and it worked:

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> while True:
... try: print a
... except: locals()['a']="HERE YOU ARE"
... else: break
... 
HERE YOU ARE
>>> 



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


Re: NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
--- On Sat, 4/24/10, James Mills  wrote:

> From: James Mills 
> Subject: Re: NameError: how to get the name?
> To: "Yingjie Lan" 
> Cc: "python list" 
> Date: Saturday, April 24, 2010, 4:03 PM
> On Sat, Apr 24, 2010 at 9:19 PM,
> Yingjie Lan 
> wrote:
> > I wanted to do something like this:
> >
> > while True:
> >  try:
> >    def fun(a, b=b, c=c): pass
> >  except NameError as ne:
> >    name = get_the_var_name(ne)
> >    locals()[name] = ''
> >  else: break
> >
> > What's be best way to implement the function
> > get_the_var_name(ne) that returns the name
> > of the variable that could not be found?
> 
> A NameError Exception does not store the name of the
> variable. it has two attributes: .args and .message
> both of which contain (usually) a string such as:
> 
> "name 'x' is not defined"
> 

Thanks for the info, that's a little messy to play with though.

Yingjie


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


Reminder: 6 days left for EuroPython 2010 talk submissions

2010-04-24 Thread Alex Willmer
The EuroPython 2010 call for papers closes this Friday on 30th April.
We've already had many submissions covering Python 3, Python 2.7,
IronPython, Game Programming, Testing, Behavior Driven Development,
NoSQL, Accessiblilty and others.

We still are looking for talks and tutorials on Django, PyPy, Twisted,
HTML5, Unladen Swallow, Testing and whatever you wish to present.

http://www.europython.eu/submission/

EuroPython
--
This year EuroPython will be held from the 17th to 24th July in
Birmingham, UK. It will include over 100 talks, tutorials, sprints and
social events. Confirmed speakers so far include Guido van Rossum,
Raymond Hettinger and Brett Cannon.

http://www.europython.eu

Registration

Registration is open now. For the best registration rates, book early!
Early Bird rate is open until 10th May. Speakers can attend at the
discounted rate Speaker Rate.

http://www.europython.eu/registration/

Help Us Out
---
EuroPython is run by volunteers, like you! We could use a hand, and
any contribution is welcome.
Go to http://wiki.europython.eu/Helping to join us!
Go to http://www.europython.eu/contact/ to contact us directly!

Sponsors

Sponsoring EuroPython is a unique opportunity to affiliate with this
prestigious conference and to reach a large number of Python users
from computing professionals to academics, from entrepreneurs to
motivated and well-educated job seekers.
http://www.europython.eu/sponsors/

Spread the Word
---
We are a community-run not-for-profit conference. Please help to
spread the word by distributing this announcement to colleagues,
project mailing lists, friends, your blog, Web site, and through your
social networking connections. Take a look at our publicity resources:
http://wiki.europython.eu/Publicity

General Information
---
For more information about the conference, please visit the official
site: http://www.europython.eu/

Looking forward to see you!
The EuroPython Team
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NameError: how to get the name?

2010-04-24 Thread Gary Herron

Yingjie Lan wrote:

--- On Sat, 4/24/10, Steven D'Aprano  
wrote:

  

From: Steven D'Aprano 
Subject: Re: NameError: how to get the name?
To: python-list@python.org
Date: Saturday, April 24, 2010, 4:07 PM
On Sat, 24 Apr 2010 04:19:43 -0700,
Yingjie Lan wrote:



I wanted to do something like this:

while True:
  try:
def fun(a, b=b, c=c): pass
  except NameError as ne:
name = get_the_var_name(ne)
locals()[name] = ''
  else: break
  

This won't work. Writing to locals() does not actually
change the local 
variables. Try it inside a function, and you will see it

doesn't work:




I tried this, and it worked:

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

Type "help", "copyright", "credits" or "license" for more information.
  

while True:


... try: print a
... except: locals()['a']="HERE YOU ARE"
... else: break
... 
HERE YOU ARE
  


Yes, but as Steven D'Aprano said, this won't work inside a function.  
Try it.


Also if you find an instance where this works, you can't rely on the 
behavior.  If you  RTM, you'll find this:


locals()¶ 

   Update and return a dictionary representing the current local symbol
   table. Free variables are returned by locals()
    when it is
   called in function blocks, but not in class blocks.

   Note: The contents of this dictionary should not be modified;
   changes may not affect the values of local and free variables used
   by the interpreter.

Gary Herron




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


Re: DLLs loading in interpreter but not with direct run on Windows

2010-04-24 Thread TerryP
On Apr 23, 4:47 pm, JTimoty  wrote:
> Hi,
>
> I've got a weird problem, apparently related to the way python
> searches for DLLs on Windows.
>
> I compiled PyQt4 (no errors) but scripts that use it fail with "DLL
> load failed: Invalid access to memory location."
> If I play with loading the modules inside the interpreter, I see no
> errors.
>
> Even more, if I use IDLE, running scripts fails. However, if I _first_
> type "from PyQt4 import QtGui" in the python shell, the script
> executes without problems.
>

I have the batch file that sets up my python 2 and python 3 sessions,
each append the site-packages\PyQt4 directory to my %Path%, so that
Windows will definitely be able to see the (Py)Qt DLL files located
there.

If you take a look at the PyQt4 package, __init__.py is just a stub.
I'm not familiar with SIP, so I don't know what pyqtconfig.py does.
Everything that you import however, is basically the name of a .pyd
and .dll file there. E.g. from pyQt4 import QtGui = there is a
QtGui.pyd and QtGui.dll module.

The .pyd file is what you're really importing, but I don't recall the
relationship to the dll files clumped around it. You can probably find
it in the python or windows manuals somewhere.


> What is the difference between these two cases? How is loading dlls
> different in ( $> python script.py ) and just ( $> python )?
>
> Thanks,
> Tim.

Example from my system:

> vim test.py

> python test.py
['My CWD', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\Dev
Files\\Languages\\Python\\26\\DLLs', 'C:\\DevFiles\\Languages\\Python\
\26\\lib', 'C:\
\DevFiles\\Languages\\Python\\26\\lib\\plat-win', 'C:\\DevFiles\
\Languages\\Python\\2
6\\lib\\lib-tk', 'C:\\DevFiles\\Languages\\Python\\26', 'C:\\DevFiles\
\Languages\\Pyt
hon\\26\\lib\\site-packages', 'C:\\DevFiles\\Languages\\Python\\26\\lib
\\site-package
s\\gtk-2.0']

> python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\DevFiles\\Languages\
\Python\\26\\DLL
s', 'C:\\DevFiles\\Languages\\Python\\26\\lib', 'C:\\DevFiles\
\Languages\\Python\\26\
\lib\\plat-win', 'C:\\DevFiles\\Languages\\Python\\26\\lib\\lib-tk',
'C:\\DevFiles\\L
anguages\\Python\\26', 'C:\\DevFiles\\Languages\\Python\\26\\lib\\site-
packages', 'C:
\\DevFiles\\Languages\\Python\\26\\lib\\site-packages\\gtk-2.0']
>>>

I also note that there is no python26.zip file in %SystemRoot%
\system32, just DLL files corresponding to each version of Python I
have installed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Abstract attributes

2010-04-24 Thread Raymond Wynne
I was coding a simple abstract class for a database interface for a library I 
am writing. However, it occurred to me that you can't ask for a simple 
"abstract attribute", an attribute that is required for the class. Now, that 
could be implemented as a property, but a getter that merely returns an 
attribute adds unnecessary code. Here's a simple pure-Python implementation:
class AbcAttrMeta:
@classmethod
def __call__(cls, *args, **kwargs):
inst = cls(*args, **kwargs)
notin = list()
for n in self.__abattrs__:
if not hasattr(inst, n):
self.notin.append(n)
if notin:
raise TypeError(
"Can't instantiate abstract class {0} with abstract attributes 
{1}".format(
str(type(cls))[5:-2],
', ' .join(notin))
return inst




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


Re: MySQLdb - weird formatting inconsistency

2010-04-24 Thread Sebastian Bassi
Hi,

Could you post a minimal version of the DB (a DB dump) to test it?
Just remove most information and leave on the ones needed to reproduce
the error. Also remove any personal/confidential information. Then
dump the DB so I can test it here.
Best,
SB.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb - weird formatting inconsistency

2010-04-24 Thread John Nagle

Anthra Norell wrote:

Hi all,

   Can anyone explain this? Three commands with three different cutoff 
dates (12/30, 12/31 and 1/1) produce a formatting inconsistency. Examine 
the third field. The first and last run represents it correctly. The 
second run strips it. The field happens to be a record ID and getting it 
stripped in an unpredictable manner obviously makes it useless as an ID.


 >>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1987-12-30";')

761L
 >>> for item in finance: print item

(9737, '', ' 2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg', 'IIfAC', '', 
'', 'Schweigegeld')
(9738, '', ' 2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC', '', '', 
'1 Schachtel 9mm')
(9739, '', ' 2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', '', 
'', 'Gladiolen')
. . .

 >>> finance.execute ('select * from j where Date between "1987-01-01" 
and "1987-12-31";')

792L
 >>> for item in finance: print item
(9737, '', '2278', datetime.date(1987, 1, 1), 5000.0, 'IIfVNg',  'IIfAC', '', 
'', 'Schweigegeld')
> (9738, '', '2279', datetime.date(1987, 1, 5), 28.5, 'IIfVZc', 'IIfAC',  '', 
'', '1 Schachtel 9mm')

(9739, '', '2280', datetime.date(1987, 1, 5), 73.85, 'IIfVZm', 'IIfAC', '', '', 
'Gladiolen')
. . .


   Those are the first and second runs.  Where, exactly, do they differ?

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


help require debugging C/C++ extension modules written for Python (ubuntu)

2010-04-24 Thread michel parker

Hi,

Can
you please guide me in selecting a tool (ide) that would debug python and  C/C++
extension modules written for Python at a same time?

Regards,Michell
  
_
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb - weird formatting inconsistency

2010-04-24 Thread Anthra Norell

Anthra Norell wrote:

Sebastian Bassi wrote:

Hi,

Could you post a minimal version of the DB (a DB dump) to test it?
Just remove most information and leave on the ones needed to reproduce
the error. Also remove any personal/confidential information. Then
dump the DB so I can test it here.
Best,
SB.

  

Sebastian,
 Thank you very much for your offer to help. I turned the machine back 
on just now and ran the test again. The error is gone! Everything seems 
to work all right. My conclusion is that a buffer or cache must have 
kept serving malformed leftovers. Making a new cursor object might have 
fixed the problem there and then. Still, caching with this much 
retention is a little unexpected and might cause processing errors, as 
the case proves. Or should one perhaps throw a cursor away after one 
execution and make a new one for the next command?
 Well, anyway I'm glad to escape the embarrassment of having you waste 
time stalking phantoms.

Thanks again

Frederic

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


Wanted: Python solution for ordering dependencies

2010-04-24 Thread Jonathan Fine

Hi

I'm hoping to avoid reinventing a wheel (or other rolling device).  I've 
got a number of dependencies and, if possible, I want to order them so 
that each item has its dependencies met before it is processed.


I think I could get what I want by writing and running a suitable 
makefile, but that seems to be such a kludge.


Does anyone know of an easily available Python solution?

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


Re: Wanted: Python solution for ordering dependencies

2010-04-24 Thread Chris Rebert
On Sat, Apr 24, 2010 at 1:53 PM, Jonathan Fine  wrote:
> Hi
>
> I'm hoping to avoid reinventing a wheel (or other rolling device).  I've got
> a number of dependencies and, if possible, I want to order them so that each
> item has its dependencies met before it is processed.
>
> I think I could get what I want by writing and running a suitable makefile,
> but that seems to be such a kludge.
>
> Does anyone know of an easily available Python solution?

http://pypi.python.org/pypi/topsort/0.9
http://www.bitformation.com/art/python_toposort.html

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


Re: NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
--- On Sat, 4/24/10, Gary Herron  wrote:
> From: Gary Herron 
> Subject: Re: NameError: how to get the name?
> To: 
> Cc: python-list@python.org
> Date: Saturday, April 24, 2010, 8:03 PM
> Yingjie Lan wrote:
> > --- On Sat, 4/24/10, Steven D'Aprano 
> wrote:
> > 
> >   
> >> From: Steven D'Aprano 
> >> Subject: Re: NameError: how to get the name?
> >> To: python-list@python.org
> >> Date: Saturday, April 24, 2010, 4:07 PM
> >> On Sat, 24 Apr 2010 04:19:43 -0700,
> >> Yingjie Lan wrote:
> >> 
> >> 
> >>> I wanted to do something like this:
> >>> 
> >>> while True:
> >>>   try:
> >>> def fun(a, b=b, c=c):
> pass
> >>>   except NameError as ne:
> >>> name =
> get_the_var_name(ne)
> >>> locals()[name] = ''
> >>>   else: break
> >>>   
> >> This won't work. Writing to locals() does not
> actually
> >> change the local variables. Try it inside a
> function, and you will see it
> >> doesn't work:
> >> 
> >> 

No it DOESN'T work, and both of you are precisely correct.
Just for playing around, I substituted 
"locals()" by "globals()" and it worked as desired:


def wrapfun():
   while True:
 try: print a
 except: globals()['a']="HERE YOU ARE"
 else: break
 finally: print globals()['a']
wrapfun()


Thanks for the information! BTW, why would 
locals() and globals() differ in this respect?
For example:


def wrapfun():
   while True:
 try: print a
 except: locals()['a']="HERE YOU ARE"
 else: break
 finally: print locals()['a']
wrapfun()


That would print "HERE YOU ARE" infinitely.
Apparently, the dict gets modified, but is
not the same as the one actually used to
resolve name 'a' in the statement 'print a'.

Yingjie


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


Re: NameError: how to get the name?

2010-04-24 Thread Chris Rebert
On Sat, Apr 24, 2010 at 4:17 PM, Yingjie Lan  wrote:
> --- On Sat, 4/24/10, Gary Herron  wrote:
>> From: Gary Herron 
>> Date: Saturday, April 24, 2010, 8:03 PM
>> Yingjie Lan wrote:
>> > --- On Sat, 4/24/10, Steven D'Aprano 
>> wrote:
>> >> From: Steven D'Aprano 
>> >> Subject: Re: NameError: how to get the name?
>> >> To: python-list@python.org
>> >> Date: Saturday, April 24, 2010, 4:07 PM
>> >> On Sat, 24 Apr 2010 04:19:43 -0700,
>> >> Yingjie Lan wrote:
>> >>
>> >>
>> >>> I wanted to do something like this:
>> >>>
>> >>> while True:
>> >>>   try:
>> >>>     def fun(a, b=b, c=c):
>> pass
>> >>>   except NameError as ne:
>> >>>     name =
>> get_the_var_name(ne)
>> >>>     locals()[name] = ''
>> >>>   else: break
>> >>>
>> >> This won't work. Writing to locals() does not
>> actually
>> >> change the local variables. Try it inside a
>> function, and you will see it
>> >> doesn't work:
>
> No it DOESN'T work, and both of you are precisely correct.
> Just for playing around, I substituted
> "locals()" by "globals()" and it worked as desired:

> Thanks for the information! BTW, why would
> locals() and globals() differ in this respect?

The module-level (i.e. global) namespace is implemented by CPython
using an actual dictionary; globals() returns a proxy to that
dictionary and lets you manipulate it.
In contrast, as an optimization, CPython implements local variables in
functions using predetermined offsets into an array of predetermined
length; the dictionary returned by locals() is dynamically constructed
on-demand and does not affect the actual array used for the local
variables (I suppose it could have been made to do so, but there's
probably a complexity or optimization reason for why not).

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


Re: [Python3] Reading a binary file and wrtiting the bytes verbatim in an utf-8 file

2010-04-24 Thread Antoine Pitrou

Hello,

> I have to read the contents of a binary file (a PNG file exactly), and
> dump it into an RTF file.
> 
> The RTF-file has been opened with codecs.open in utf-8 mode.

You should use the built-in open() function. codecs.open() is outdated in 
Python 3.

> As I expected, the utf-8 decoder chokes on some combinations of bits;
> how can I tell python to dump the bytes as they are, without
> interpreting them?

Well, the one thing you have to be careful about is to flush text buffers 
before writing binary data. But, for example:

>>> f = open("TEST", "w", encoding='utf8')
>>> f.write("héhé")
4
>>> f.flush()
>>> f.buffer.write(b"\xff\x00")
2
>>> f.close()

gives you:

$ hexdump -C TEST
  68 c3 a9 68 c3 a9 ff 00   |h..h|

(utf-8 encoded text and then two raw bytes which are invalid utf-8)

Another possibility is to open the file in binary mode and do the 
encoding yourself when writing text. This might actually be a better 
solution, since I'm not sure RTF uses utf-8 by default.

Regards

Antoine.


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


Re: On Class namespaces, calling methods

2010-04-24 Thread Aahz
In article <4bd2e20...@dnews.tpgi.com.au>,
Lie Ryan   wrote:
>On 04/24/10 06:07, Aahz wrote:
>> In article <4bc120bd$0$8850$c3e8...@news.astraweb.com>,
>> Steven D'Aprano   wrote:
>>>
>>> I can only think of two circumstances where old-style classes are 
>>> *wrong*: if you use multiple inheritance with a diamond diagram ("...now 
>>> you have THREE problems" *wink*), if you intend using descriptors such as 
>>> properties, or if you need __slots__. That's three circumstances: 
>>> multiple inheritance with a diamond diagram, descriptors, __slots__, and 
>>> __getattribute__. Four circumstances.
>>>
>>> Any other time, they're merely discouraged, and gone in Python 3.x.
>> 
>> Discouraged by some people.  I certainly encourage the use of old-style
>> classes unless there's a specific reason to use new-style classes.
>
>For what reason?

Because it's simpler and because it allows people to write code that
works with 1.5.2+ if they want.  Admittedly, few people want to go back
that far, but lots of people are still using Python 2.2, and I prefer to
avoid new-style classes with 2.2 because of the changes in 2.3.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wanted: Python solution for ordering dependencies

2010-04-24 Thread Aahz
In article ,
Jonathan Fine   wrote:
>
>I'm hoping to avoid reinventing a wheel (or other rolling device).  I've 
>got a number of dependencies and, if possible, I want to order them so 
>that each item has its dependencies met before it is processed.
>
>I think I could get what I want by writing and running a suitable 
>makefile, but that seems to be such a kludge.

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

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
-- 
http://mail.python.org/mailman/listinfo/python-list


py2exe breaking wxPython?

2010-04-24 Thread Alex Hall
Hello all,
I have a compiled version of my project, but the wx functions do not
work. When run from the python source, instead of the compiled .exe
file, wx works as expected. I am including msvcr90.dll in the dist
folder. I looked for answers on Google, but I could not really follow
the tutorials and forums I found. Is there a simple fix for this? BTW,
I am still using py2exe and python2.6. Here is the traceback I get
when using the .exe version of the project and calling a wx operation:

Traceback (most recent call last):
  File "sw.pyw", line 217, in 
  File "dict.pyc", line 73, in showLookupDialog
  File "wx\_core.pyc", line 7978, in __init__
  File "wx\_core.pyc", line 7552, in _BootstrapApp
  File "dict.pyc", line 26, in OnInit
AttributeError: 'NoneType' object has no attribute 'Bind'
Traceback (most recent call last):
  File "sw.pyw", line 217, in 
  File "dict.pyc", line 73, in showLookupDialog
  File "wx\_core.pyc", line 7978, in __init__
  File "wx\_core.pyc", line 7552, in _BootstrapApp
  File "dict.pyc", line 26, in OnInit
AttributeError: 'NoneType' object has no attribute 'Bind'

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe breaking wxPython?

2010-04-24 Thread John Bokma
Alex Hall  writes:

> Hello all,
> I have a compiled version of my project, but the wx functions do not
> work. When run from the python source, instead of the compiled .exe
> file, wx works as expected. I am including msvcr90.dll in the dist
> folder. I looked for answers on Google, but I could not really follow
> the tutorials and forums I found. Is there a simple fix for this? BTW,
> I am still using py2exe and python2.6. Here is the traceback I get
> when using the .exe version of the project and calling a wx operation:
>
> Traceback (most recent call last):
>   File "sw.pyw", line 217, in 
>   File "dict.pyc", line 73, in showLookupDialog
>   File "wx\_core.pyc", line 7978, in __init__
>   File "wx\_core.pyc", line 7552, in _BootstrapApp
>   File "dict.pyc", line 26, in OnInit
> AttributeError: 'NoneType' object has no attribute 'Bind'
> Traceback (most recent call last):
>   File "sw.pyw", line 217, in 
>   File "dict.pyc", line 73, in showLookupDialog
>   File "wx\_core.pyc", line 7978, in __init__
>   File "wx\_core.pyc", line 7552, in _BootstrapApp
>   File "dict.pyc", line 26, in OnInit
> AttributeError: 'NoneType' object has no attribute 'Bind'

I use wx with Perl, and use a packager and know that for Perl you have
to include additional dlls to make wx work (there is a module that
nicely handles that for you, which I am use). In short just the ms dll
is not enough as far as I know.

-- 
John Bokma   j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
-- 
http://mail.python.org/mailman/listinfo/python-list


question about google project hosting!

2010-04-24 Thread zhengqing
Hi,
is there any one know some python libraries to check if there is a
new download for a google project hosting project?
or are there any API which has such kind of function?
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about google project hosting!

2010-04-24 Thread Chris Rebert
On Sat, Apr 24, 2010 at 9:11 PM, zhengqing  wrote:
>    is there any one know some python libraries to check if there is a
> new download for a google project hosting project?
>    or are there any API which has such kind of function?

You could use the Universal Feed Parser (http://www.feedparser.org/)
on http://code.google.com/feeds/p/PROJECT-NAME-HERE/updates/basic

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


exceptions from daemon threads which access the global namespace at interpreter shutdown (how to squelch output?)

2010-04-24 Thread Ben Cohen
I've got an application which makes fairly heavy use of daemon threads to 
perform 'background' processing and various other long-running tasks that are 
likely to block.

My original understanding of threading.Thread's daemon threads was that I could 
safely fire them off and essentially forget about managing them from the main 
thread's perspective as long as they don't do anything that's not thread safe 
-- eg I can fire them off, let them do their background twiddling and safely 
let the threading machinery manage their lifespan assuming that
1. the 'background twiddling' is threadsafe and
2. the thread can safely 'die' at any point without requiring a 
shutdown procedure

Some coding later and I learn this isn't exactly the case, as there as in an 
additional requirement on the use of daemon threads -- they can't reference 
anything in the global namespace else they may raise exceptions at interpreter 
shutdown.  This is because as part of the shutdown procedure, the interpreter 
sets all global variables to None.  A daemon thread may run while this is 
occuring/after it occured but before the process exits, attempt to access a 
global variable and then throw an exception.  The exception may get printed if 
the interpreter catches/prints it before the process exits.

I garnered this understanding from this problem description -- (although all 
mistakes in description are my own)

http://bugs.python.org/issue1722344

In this bug report they are discussing an interpreter problem which affects 
non-daemon threads -- I'm not attempting to claim that I'm being affected by an 
interpreter bug, I reference this link only because it contains good 
descriptions of the interpreter shutdown process as well as the additional 
requirements the interpreter places on 'daemon' threads (beyond just being 
'thread-safe'):
> When Python begins to shutdown it takes
> each module and sets each variable in the global namespace to None. If a
> thread has not terminated before the interpreter terminates then the
> thread tries to use a global variable which has been set to None.
> 
> This is not about to change since this occurs because of coding
> "errors". You must make sure that either your thread is as safe as a
> __del__ method (which means no global namespace access) or you can't let
> the app exit until you are positive all of your threads have terminated,
> not just asked them to shutdown since this is all asynchronous.
> > > which means no global namespace access
> > Does that mean that you cannot use len and range in a Thread?
> 
> No, it means you have to be careful if you do. Shutting down properly
> will take care of things. Otherwise you need to save a reference
> locally (either on an object or as a local variable) and use that
> reference instead of relying on the one defined in the global
> namespace.
Here's an example of one such traceback from one of my application runs:

> Exception in thread Thread-11 (most likely raised during interpreter 
> shutdown):
> (pydev-2.6) ncohen$ 

In this run, the process exited before even one traceback finished printing but 
this will be timing dependent, -- sometimes I'll see tracebacks from many 
backgrounds threads and sometimes just snippets like this.  They will typically 
be AttributeError's or TypeError's resulting from failed attempts to access 
variables from the global namespace.  Here's a longer example from another run:

> Traceback (most recent call last):
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
>  line 525, in __bootstrap_inner
>   File "/Users/ncohen/software/viis/viis/apps/permassh.py", line 184, in run
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py",
>  line 224, in serve_forever
> : 'NoneType' object has no attribute 
> 'select'
> 
> Traceback (most recent call last):
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
>  line 525, in __bootstrap_inner
>   File "/Users/ncohen/software/viis/viis/apps/permassh.py", line 184, in run
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py",
>  line 224, in serve_forever
> : 'NoneType' object has no attribute 
> 'select'
> Exception in thread Thread-7 (most likely raised during interpreter shutdown):
> Traceback (most recent call last):
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
>  line 525, in __bootstrap_inner
>   File 
> "/Users/ncohen/pydev-2.6/lib/python2.6/site-packages/paramiko/transport.py", 
> line 1571, in run
>   File 
> "/Users/ncohen/pydev-2.6/lib/python2.6/site-packages/paramiko/transport.py", 
> line 1386, in _log
>   File 
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py",
>  line 1105, in log
> : 'NoneType' object has no attribute 
> 'IntType'
> (pydev-2.6)breathe-wifi:viis.webapp ncohen$ 

> (pydev-2.6) ncohen$ 

My que

☆☉☉☆ Barely Legal Babes - Free downloads! V ideos Images, Barely Legal young teens

2010-04-24 Thread Custhelp
☆☉☉☆  Barely Legal Babes - Free downloads! Videos Images, Barely Legal
young teens

http://liquid-cash.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
--- On Sun, 4/25/10, Chris Rebert  wrote:

> From: Chris Rebert 
> Subject: Re: NameError: how to get the name?
> To: "Yingjie Lan" 
> Cc: python-list@python.org
> Date: Sunday, April 25, 2010, 3:27 AM
> On Sat, Apr 24, 2010 at 4:17 PM,
> Yingjie Lan 
> wrote:
> > --- On Sat, 4/24/10, Gary Herron 
> wrote:
> >> From: Gary Herron 
> >> Date: Saturday, April 24, 2010, 8:03 PM
> >> Yingjie Lan wrote:
> >> > --- On Sat, 4/24/10, Steven D'Aprano 
> >> > 
> >> wrote:
> >> >> From: Steven D'Aprano 
> >> >> Subject: Re: NameError: how to get the
> name?
> >> >> To: python-list@python.org
> >> >> Date: Saturday, April 24, 2010, 4:07 PM
> >> >> On Sat, 24 Apr 2010 04:19:43 -0700,
> >> >> Yingjie Lan wrote:
> >> >>
> >> >>
> >> >>> I wanted to do something like this:
> >> >>>
> >> >>> while True:
> >> >>>   try:
> >> >>>     def fun(a, b=b, c=c):
> >> pass
> >> >>>   except NameError as ne:
> >> >>>     name =
> >> get_the_var_name(ne)
> >> >>>     locals()[name] = ''
> >> >>>   else: break
> >> >>>
> >> >> This won't work. Writing to locals() does
> not
> >> actually
> >> >> change the local variables. Try it inside
> a
> >> function, and you will see it
> >> >> doesn't work:
> >
> > No it DOESN'T work, and both of you are precisely
> correct.
> > Just for playing around, I substituted
> > "locals()" by "globals()" and it worked as desired:
> 
> > Thanks for the information! BTW, why would
> > locals() and globals() differ in this respect?
> 
> The module-level (i.e. global) namespace is implemented by
> CPython
> using an actual dictionary; globals() returns a proxy to
> that
> dictionary and lets you manipulate it.
> In contrast, as an optimization, CPython implements local
> variables in
> functions using predetermined offsets into an array of
> predetermined
> length; the dictionary returned by locals() is dynamically
> constructed
> on-demand and does not affect the actual array used for the
> local
> variables (I suppose it could have been made to do so, but
> there's
> probably a complexity or optimization reason for why not).
> 

Thanks, that's good to know. The locals() behaves rather 
strangely, as can be demonstrated by the following two 
tests (the first one is from Steven, thanks Steve):

#file: fun.py:

def test():
x = 1
print (x)
locals()['x'] = 2
print (x, locals()['x'])

def test2():
locals()['x'] = 2
print (locals()['x'])
print x

test()
test2()

-And the output: python fun.py---
1
(1, 1)
2
Traceback (most recent call last):
  File "fun.py", line 21, in 
test2()
  File "fun.py", line 17, in test2
print x
NameError: global name 'x' is not defined

-

I don't know how to make sense out of it.
Any suggestions? 

Yingjie


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


Re: [Python3] Reading a binary file and wrtiting the bytes verbatim in an utf-8 file

2010-04-24 Thread Stefan Behnel

Antoine Pitrou, 25.04.2010 02:16:

Another possibility is to open the file in binary mode and do the
encoding yourself when writing text. This might actually be a better
solution, since I'm not sure RTF uses utf-8 by default.


That's a lot cleaner as it doesn't use two interfaces to write to the same 
file, and doesn't rely on any specific coordination between those two 
interfaces.


Stefan

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


Re: NameError: how to get the name?

2010-04-24 Thread Chris Rebert
On Sat, Apr 24, 2010, Yingjie Lan  wrote:
> On Sun, 4/25/10, Chris Rebert  wrote:
>> On Sat, Apr 24, 2010, Yingjie Lan  wrote:
>> > On Sat, 4/24/10, Gary Herron  wrote:
>> >> Yingjie Lan wrote:
>> >> > On Sat, 4/24/10, Steven D'Aprano  wrote:
>> >> >> On Sat, 24 Apr 2010, Yingjie Lan wrote:
>> >> >>> I wanted to do something like this:
>> >> >>>
>> >> >>> while True:
>> >> >>>   try:
>> >> >>>     def fun(a, b=b, c=c):
>> >> pass
>> >> >>>   except NameError as ne:
>> >> >>>     name =
>> >> get_the_var_name(ne)
>> >> >>>     locals()[name] = ''
>> >> >>>   else: break
>> >> >>>
>> >> >> This won't work. Writing to locals() does
>> not
>> >> actually
>> >> >> change the local variables. Try it inside
>> a
>> >> function, and you will see it
>> >> >> doesn't work:
>> >
>> > No it DOESN'T work, and both of you are precisely
>> correct.
>> > Just for playing around, I substituted
>> > "locals()" by "globals()" and it worked as desired:
>> 
>> > Thanks for the information! BTW, why would
>> > locals() and globals() differ in this respect?
>>
>> The module-level (i.e. global) namespace is implemented by
>> CPython
>> using an actual dictionary; globals() returns a proxy to
>> that
>> dictionary and lets you manipulate it.
>> In contrast, as an optimization, CPython implements local
>> variables in
>> functions using predetermined offsets into an array of
>> predetermined
>> length; the dictionary returned by locals() is dynamically
>> constructed
>> on-demand and does not affect the actual array used for the
>> local
>> variables (I suppose it could have been made to do so, but
>> there's
>> probably a complexity or optimization reason for why not).
>>
>
> Thanks, that's good to know. The locals() behaves rather
> strangely, as can be demonstrated by the following two
> tests (the first one is from Steven, thanks Steve):
>
> #file: fun.py:
>
> def test():
>    x = 1
>    print (x)
>    locals()['x'] = 2
>    print (x, locals()['x'])
>
> def test2():
>    locals()['x'] = 2
>    print (locals()['x'])
>    print x
>
> test()
> test2()
>
> -And the output: python fun.py---
> 1
> (1, 1)
> 2
> Traceback (most recent call last):
>  File "fun.py", line 21, in 
>    test2()
>  File "fun.py", line 17, in test2
>    print x
> NameError: global name 'x' is not defined
>
> -
>
> I don't know how to make sense out of it.
> Any suggestions?

My working theory is that attempting to modify a key in locals()
corresponding to an extant variable has no effect, even on just the
locals() dictionary itself, and is ignored, but adding or modifying
other keys in locals() works like a normal dictionary.

def test3():
x = 1
print x
locals()['x'] = 2
locals()['y'] = 3
print x, locals()['x'], locals()['y']
print y

$ python test3.py
1
1 1 3
Traceback (most recent call last):
  File "Desktop/tmp.py", line 22, in 
test3()
  File "Desktop/tmp.py", line 7, in test3
print y
NameError: global name 'y' is not defined


As for why you get a NameError in test2() [assuming that's part of
what's confusing you], there's no assignment to `x` in test2(), so
Python reasons you must be referring to a global variable `x`. But
there is no such global variable, hence the NameError.

Cheers,
Chris
--
Your mail client's quoting style is a bit annoying.
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NameError: how to get the name?

2010-04-24 Thread Yingjie Lan
--- On Sun, 4/25/10, Chris Rebert  wrote:

> From: Chris Rebert 
> Subject: Re: NameError: how to get the name?
> To: "Yingjie Lan" 
> Cc: "python list" 
> Date: Sunday, April 25, 2010, 10:09 AM
> On Sat, Apr 24, 2010, Yingjie Lan
> 
> wrote:
> > On Sun, 4/25/10, Chris Rebert 
> wrote:
> >> On Sat, Apr 24, 2010, Yingjie Lan 
> wrote:
> >> > On Sat, 4/24/10, Gary Herron 
> wrote:
> >> >> Yingjie Lan wrote:
> >> >> > On Sat, 4/24/10, Steven D'Aprano
> 
> wrote:
> >> >> >> On Sat, 24 Apr 2010, Yingjie Lan
> wrote:
> >> >> >>> I wanted to do something
> like this:
> >> >> >>>
> >> >> >>> while True:
> >> >> >>>   try:
> >> >> >>>     def fun(a, b=b, c=c):
> >> >> pass
> >> >> >>>   except NameError as ne:
> >> >> >>>     name =
> >> >> get_the_var_name(ne)
> >> >> >>>     locals()[name] = ''
> >> >> >>>   else: break
> >> >> >>>
> >> >> >> This won't work. Writing to
> locals() does
> >> not
> >> >> actually
> >> >> >> change the local variables. Try
> it inside
> >> a
> >> >> function, and you will see it
> >> >> >> doesn't work:
> >> >
> >> > No it DOESN'T work, and both of you are
> precisely
> >> correct.
> >> > Just for playing around, I substituted
> >> > "locals()" by "globals()" and it worked as
> desired:
> >> 
> >> > Thanks for the information! BTW, why would
> >> > locals() and globals() differ in this
> respect?
> >>
> >> The module-level (i.e. global) namespace is
> implemented by
> >> CPython
> >> using an actual dictionary; globals() returns a
> proxy to
> >> that
> >> dictionary and lets you manipulate it.
> >> In contrast, as an optimization, CPython
> implements local
> >> variables in
> >> functions using predetermined offsets into an
> array of
> >> predetermined
> >> length; the dictionary returned by locals() is
> dynamically
> >> constructed
> >> on-demand and does not affect the actual array
> used for the
> >> local
> >> variables (I suppose it could have been made to do
> so, but
> >> there's
> >> probably a complexity or optimization reason for
> why not).
> >>
> >
> > Thanks, that's good to know. The locals() behaves
> rather
> > strangely, as can be demonstrated by the following
> two
> > tests (the first one is from Steven, thanks Steve):
> >
> > #file: fun.py:
> >
> > def test():
> >    x = 1
> >    print (x)
> >    locals()['x'] = 2
> >    print (x, locals()['x'])
> >
> > def test2():
> >    locals()['x'] = 2
> >    print (locals()['x'])
> >    print x
> >
> > test()
> > test2()
> >
> > -And the output: python fun.py---
> > 1
> > (1, 1)
> > 2
> > Traceback (most recent call last):
> >  File "fun.py", line 21, in 
> >    test2()
> >  File "fun.py", line 17, in test2
> >    print x
> > NameError: global name 'x' is not defined
> >
> > -
> >
> > I don't know how to make sense out of it.
> > Any suggestions?
> 
> My working theory is that attempting to modify a key in
> locals()
> corresponding to an extant variable has no effect, even on
> just the
> locals() dictionary itself, and is ignored, but adding or
> modifying
> other keys in locals() works like a normal dictionary.
> 
> def test3():
>     x = 1
>     print x
>     locals()['x'] = 2
>     locals()['y'] = 3
>     print x, locals()['x'], locals()['y']
>     print y
> 
> $ python test3.py
> 1
> 1 1 3
> Traceback (most recent call last):
>   File "Desktop/tmp.py", line 22, in 
>     test3()
>   File "Desktop/tmp.py", line 7, in test3
>     print y
> NameError: global name 'y' is not defined
> 
> 
> As for why you get a NameError in test2() [assuming that's
> part of
> what's confusing you], there's no assignment to `x` in
> test2(), so
> Python reasons you must be referring to a global variable
> `x`. But
> there is no such global variable, hence the NameError.
> 
> Cheers,
> Chris
> --

Thanks, very nice observations. What's puzzling me is this:
in the first test, it seems locals() ignores assignment,
thus locals()['x'] == x holds True, but in the second one,
as 'x' does not exists, it allows assignment. I might assume
this: locals() keeps two mappings: one is the true locals,
another is an ordinary dict (let's called the faked one). 
when you look up a value,  it first looks into the true locals, 
then the faked one only if not found there. As for setting a 
value, it always works on the faked one. 

That's my best guess, anyway.

It seems better to be explicit: simply throws an exception
whenever one tries to set a value for locals()['x'] -- just
say that locals() are unlike globals(), it is read only.


Yingjie


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


Re: how to debug python application crashed occasionally

2010-04-24 Thread jacky wang
could anyone help me?


On Apr 21, 2:55 pm, jacky wang  wrote:
> 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 
> fromhttp://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