Re: BackupRead problem

2011-01-15 Thread Stefan Sonnenberg-Carstens

Am 15.01.2011 16:06, schrieb Stefan Sonnenberg-Carstens:

I'm trying to create a Backup/Restore app.
I'm really struggeling for a long time.

I can successfully read directories, but not files.
Every time, I'll try I get "Access denied", Error 5.

It's running with admin privs.

Any ideas ?

#!python
import sys
import os
import os.path
import getopt
import time


DELETE=0x0001
READ_CONTROL=0x0002
WRITE_DAC=0x0004
WRITE_OWNER=0x0008
SYNCHRONIZE=0x0010
STANDARD_RIGHTS_REQUIRED=0x000FL
STANDARD_RIGHTS_READ=READ_CONTROL
STANDARD_RIGHTS_WRITE=READ_CONTROL
STANDARD_RIGHTS_EXECUTE=READ_CONTROL
STANDARD_RIGHTS_ALL=0x001F
SPECIFIC_RIGHTS_ALL=0x

FILE_ATTRIBUTE_REPARSE_POINT=0x400

from ctypes import *

if os.name == 'nt':

import win32security
import win32process
import win32file

try:
import win32api
except ImportError,e:
print >>sys.stderr,'Could not load win32api module. Can not 
continue'

os._exit(1)
try:
import wmi
except ImportError,e:
print >>sys.stderr,'Could not load wmi module. Can not continue'
os._exit(1)
try:
import ctypes
except ImportError,e:
print >>sys.stderr,'Could not load ctypes module. Can not 
continue'

os._exit(1)
else:
print >>sys.stderr,'Sorry, your platform %s is not supported' % 
os.name

os._exit(1)

if len(sys.argv) >= 1:
try:
opts,args = getopt.getopt(sys.argv[1:],'h',('help',))
except getopt.GetoptError,e:
print str(e)
if not ctypes.windll.shell32.IsUserAnAdmin():
win32api.ShellExecute(None,'runas',sys.executable,' 
'.join(sys.argv),r'C:\WINDOWS',0)

else:
print >>sys.stderr,'Running with administrative privileges'
token = 
win32security.OpenProcessToken(win32process.GetCurrentProcess(),win32security.TOKEN_ADJUST_PRIVILEGES|win32security.TOKEN_QUERY)

if token:
for priv in 
(win32security.SE_BACKUP_NAME,win32security.SE_RESTORE_NAME):

luid = win32security.LookupPrivilegeValue(None,priv)
newState = [(luid,win32security.SE_PRIVILEGE_ENABLED)]
try:
win32security.AdjustTokenPrivileges(token,0,newState)
except:
print >>sys.stderr,'Could not get (some) required 
priviledge(s): ',win32api.FormatMessage(win32api.GetLastError())

os._exit(1)
win32api.CloseHandle(token)
else:
print >>sys.stderr,'Could not get token for running process'
os._exit(1)
print >>sys.stderr,'Acquired backup/restore context 
(SeRestorePrivilege and SeBackupPrivilege enabled)'
inf = 
win32file.CreateFile(r'C:\Windows\System32\drivers\etc\hosts',READ_CONTROL,0,None,win32file.OPEN_EXISTING,win32file.FILE_FLAG_BACKUP_SEMANTICS,None)

buf = win32file.AllocateReadBuffer(4096)
ctx = 0
(bytes_read,buf,ctx) = 
win32file.BackupRead(inf,4096,buf,False,True,ctx)

MS's documenation sucks.
Just found some code on the web regarding root-kits,
but after changing

win32file.CreateFile(r'C:\Windows\System32\drivers\etc\hosts',READ_CONTROL,0,None,win32file.OPEN_EXISTING,win32file.FILE_FLAG_BACKUP_SEMANTICS,None)

to

win32file.CreateFile(r'C:\Windows\System32\drivers\etc\hosts',win32file.GENERIC_READ,0,None,win32file.OPEN_EXISTING,win32file.FILE_FLAG_BACKUP_SEMANTICS,None)


it works.

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


BackupRead problem

2011-01-15 Thread Stefan Sonnenberg-Carstens

I'm trying to create a Backup/Restore app.
I'm really struggeling for a long time.

I can successfully read directories, but not files.
Every time, I'll try I get "Access denied", Error 5.

It's running with admin privs.

Any ideas ?

#!python
import sys
import os
import os.path
import getopt
import time


DELETE=0x0001
READ_CONTROL=0x0002
WRITE_DAC=0x0004
WRITE_OWNER=0x0008
SYNCHRONIZE=0x0010
STANDARD_RIGHTS_REQUIRED=0x000FL
STANDARD_RIGHTS_READ=READ_CONTROL
STANDARD_RIGHTS_WRITE=READ_CONTROL
STANDARD_RIGHTS_EXECUTE=READ_CONTROL
STANDARD_RIGHTS_ALL=0x001F
SPECIFIC_RIGHTS_ALL=0x

FILE_ATTRIBUTE_REPARSE_POINT=0x400

from ctypes import *

if os.name == 'nt':

import win32security
import win32process
import win32file

try:
import win32api
except ImportError,e:
print >>sys.stderr,'Could not load win32api module. Can not 
continue'

os._exit(1)
try:
import wmi
except ImportError,e:
print >>sys.stderr,'Could not load wmi module. Can not continue'
os._exit(1)
try:
import ctypes
except ImportError,e:
print >>sys.stderr,'Could not load ctypes module. Can not continue'
os._exit(1)
else:
print >>sys.stderr,'Sorry, your platform %s is not supported' % os.name
os._exit(1)

if len(sys.argv) >= 1:
try:
opts,args = getopt.getopt(sys.argv[1:],'h',('help',))
except getopt.GetoptError,e:
print str(e)
if not ctypes.windll.shell32.IsUserAnAdmin():
win32api.ShellExecute(None,'runas',sys.executable,' 
'.join(sys.argv),r'C:\WINDOWS',0)

else:
print >>sys.stderr,'Running with administrative privileges'
token = 
win32security.OpenProcessToken(win32process.GetCurrentProcess(),win32security.TOKEN_ADJUST_PRIVILEGES|win32security.TOKEN_QUERY)

if token:
for priv in 
(win32security.SE_BACKUP_NAME,win32security.SE_RESTORE_NAME):

luid = win32security.LookupPrivilegeValue(None,priv)
newState = [(luid,win32security.SE_PRIVILEGE_ENABLED)]
try:
win32security.AdjustTokenPrivileges(token,0,newState)
except:
print >>sys.stderr,'Could not get (some) required 
priviledge(s): ',win32api.FormatMessage(win32api.GetLastError())

os._exit(1)
win32api.CloseHandle(token)
else:
print >>sys.stderr,'Could not get token for running process'
os._exit(1)
print >>sys.stderr,'Acquired backup/restore context 
(SeRestorePrivilege and SeBackupPrivilege enabled)'
inf = 
win32file.CreateFile(r'C:\Windows\System32\drivers\etc\hosts',READ_CONTROL,0,None,win32file.OPEN_EXISTING,win32file.FILE_FLAG_BACKUP_SEMANTICS,None)

buf = win32file.AllocateReadBuffer(4096)
ctx = 0
(bytes_read,buf,ctx) = 
win32file.BackupRead(inf,4096,buf,False,True,ctx)

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


Re: What INI config file module allows lists of duplicate same-named options?

2011-01-09 Thread Stefan Sonnenberg-Carstens

Am 09.01.2011 21:43, schrieb Thomas L. Shinnick:
Having (possibly) surveyed all the available pypi config file modules, 
I still haven't seen one that allows an obvious and familiar extension 
of the strict Windows INI format.


Each INI-style config module seems to enforce the strict rule: each 
option in a section must have a different name - no duplicates.  Thus 
it is impossible to have a simple list, e.g.


[pathset  uk]
pathpair: /bath/* to/london/*
pathpair: /bath/upload/** to/london/*
pathpair: /firth/*to/forth/*
pathpair: /firth/upload/**to/forth/*

Rather you must give each line a separate name, e.g.

[pathset  uk]
pathpair001: /bath/*  to/london/*
pathpair002: /bath/upload/**  to/london/*
pathpair003: /firth/* to/forth/*
pathpair004: /firth/upload/** to/forth/*
  |   |  |   |  |   |
pathpair068: /glasgow/*   to/edinburgh/*
pathpair069: /glasgow/upload/**   to/edinburgh/*
  |   |  |   |  |   |

This is not ideal for a number of reasons.  Do you know of a library 
module that has the (optional?) ability to handle duplicate-named 
options, returning them as a list?


If instead someone can point me to a reasonable Apache-style config 
module, that might also serve.  I've looked for such and the few found 
seemed to be either bare bones or clumsily stripped out of something 
much larger.



--
I'm a pessimist about probabilities; I'm an optimist about possibilities.
Lewis Mumford  (1895-1990)


I've let ini style files alone some time ago.
Whenever possible I use JSON based files.

Your example could then look like this:

{
"pathpairs":{
"uk":[
["/bath/*","/london/*"],
["/bath/upload/**","/london/*"],
["/firth/*,"/forth/*"],
["/firth/upload/**","/forth/*"]
  ]
}
}

Since Python 2.7, json is in the standard library.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Attaching C++ libraries to Python app.

2011-01-05 Thread Stefan Sonnenberg-Carstens

Am 05.01.2011 23:44, schrieb Rohit Coder:
I am just asking. In future I may need to import any C++ library, not 
a Crypto, but some other. Is it possible?

Yes.
There are at least five possible ways:

- Handcode the interface and glue code (http://docs.python.org/extending)
- use SWIG to autogenerate (mostly) the interface (http://www.swig.org)
- using BOOST's python interface 
(http://www.boost.org/doc/libs/1_45_0/libs/python/doc/index.html)
- using ctypes module for runtime interaction (like and use it a _lot_, 
very cool for rapid prototyping :-) 
http://docs.python.org/library/ctypes.html)

- cython (different approach, implements a python subset, http://cython.org)

I used SWIG and ctypes in the past, as it seems (for me) the easiest way.


> Date: Wed, 5 Jan 2011 22:44:15 +0100
> Subject: Re: Attaching C++ libraries to Python app.
> From: stefan.sonnenb...@pythonmeister.com
> To: passionate_program...@hotmail.com
> CC: python-list@python.org
>
> You don't need to reinvent the wheel:
>
> http://www.dlitz.net/software/pycrypto/
>
> Am Mi, 5.01.2011, 22:21 schrieb Rohit Coder:
> >
> > Is it possible to use C++ libraries within a Python application? I am
> > planning to write an encryption program and want to use GnuPG C++
> > 
libraries.elementFontfont-familyfont-sizefont-stylefont-variantfont-weightletter-spacingline-heighttext-decorationtext-aligntext-indenttext-transformwhite-spaceword-spacingcolorBackgroundbg-attachmentbg-colorbg-imagebg-positionbg-repeatBoxwidthheightborder-topborder-rightborder-bottomborder-leftmarginpaddingmax-heightmin-heightmax-widthmin-widthoutline-coloroutline-styleoutline-widthPositioningpositiontopbottomrightleftfloatdisplayclearz-indexListlist-style-imagelist-style-typelist-style-positionTablevertical-alignborder-collapseborder-spacingcaption-sideempty-cellstable-layoutEffectstext-shadow-webkit-box-shadowborder-radiusOtheroverflowcursorvisibility

> > ...Rohit. --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> MfG,
>
> Stefan Sonnenberg-Carstens
>
> IT Architect


element

Font
font-family 
font-size   
font-style  
font-variant
font-weight 
letter-spacing  
line-height 
text-decoration 
text-align  
text-indent 
text-transform  
white-space 
word-spacing
color   
Background
bg-attachment   
bg-color
bg-image
bg-position 
bg-repeat   
Box
width   
height  
border-top  
border-right
border-bottom   
border-left 
margin  
padding 
max-height  
min-height  
max-width   
min-width   
outline-color   
outline-style   
outline-width   
Positioning
position
top 
bottom  
right   
left
float   
display 
clear   
z-index 
List
list-style-image
list-style-type 
list-style-position 
Table
vertical-align  
border-collapse 
border-spacing  
caption-side
empty-cells 
table-layout
Effects
text-shadow 
-webkit-box-shadow  
border-radius   
Other
overflow
cursor  
visibility  

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


Re: Attaching C++ libraries to Python app.

2011-01-05 Thread Stefan Sonnenberg-Carstens
You don't need to reinvent the wheel:

http://www.dlitz.net/software/pycrypto/

Am Mi, 5.01.2011, 22:21 schrieb Rohit Coder:
>
> Is it possible to use C++ libraries within a Python application? I am
> planning to write an encryption program and want to use GnuPG C++
> libraries.elementFontfont-familyfont-sizefont-stylefont-variantfont-weightletter-spacingline-heighttext-decorationtext-aligntext-indenttext-transformwhite-spaceword-spacingcolorBackgroundbg-attachmentbg-colorbg-imagebg-positionbg-repeatBoxwidthheightborder-topborder-rightborder-bottomborder-leftmarginpaddingmax-heightmin-heightmax-widthmin-widthoutline-coloroutline-styleoutline-widthPositioningpositiontopbottomrightleftfloatdisplayclearz-indexListlist-style-imagelist-style-typelist-style-positionTablevertical-alignborder-collapseborder-spacingcaption-sideempty-cellstable-layoutEffectstext-shadow-webkit-box-shadowborder-radiusOtheroverflowcursorvisibility
> ...Rohit. 
>   --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
MfG,

Stefan Sonnenberg-Carstens

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


Re: list 2 dict?

2011-01-02 Thread Stefan Sonnenberg-Carstens

Am 02.01.2011 19:19, schrieb Emile van Sebille:

On 1/2/2011 8:31 AM Stefan Sonnenberg-Carstens said...


A last one:

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict((x[1],x[0]) for x in ((l.pop(),l.pop()) for x in xrange(len(l)/2)))


This also works:


l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']

pop=l.pop

dict([(pop(),pop()) for i in l])


No, it does not:

>>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>>>
>>> pop=l.pop
>>>
>>> dict([(pop(),pop()) for i in l])
{'a': 7, 'b': 8, 4: 3, 6: 5}
>>>
--
http://mail.python.org/mailman/listinfo/python-list


Re: list 2 dict?

2011-01-02 Thread Stefan Sonnenberg-Carstens

Am 02.01.2011 16:36, schrieb Octavian Rasnita:

From: "Ian Kelly"



On 1/2/2011 6:18 AM, Octavian Rasnita wrote:

Hi,

If I want to create a dictionary from a list, is there a better way than the 
long line below?

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']

d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in 
range(len(l)) if x %2 == 1]))

d = dict(zip(l[0::2], l[1::2]))

Or, using the "grouper" function recipe from the itertools documentation:

d = dict(grouper(2, l))

Cheers,
Ian


The grouper-way looks nice, but I tried it and it didn't work:

from itertools import *
...
d = dict(grouper(2, l))

NameError: name 'grouper' is not defined

I use Python 2.7. Should it work with this version?
Octavian


A last one:

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict((x[1],x[0]) for x in ((l.pop(),l.pop()) for x in xrange(len(l)/2)))
--
http://mail.python.org/mailman/listinfo/python-list


Re: list 2 dict?

2011-01-02 Thread Stefan Sonnenberg-Carstens

Am 02.01.2011 14:18, schrieb Octavian Rasnita:

Hi,

If I want to create a dictionary from a list, is there a better way than the 
long line below?

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']

d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in 
range(len(l)) if x %2 == 1]))

print(d)

{8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}

Thanks.

Octavian


Or so:

dict(zip((y for x,y in enumerate(l) if x%2==0),(y for x,y in 
enumerate(l) if not x%2==0)))

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


Re: list 2 dict?

2011-01-02 Thread Stefan Sonnenberg-Carstens

Am 02.01.2011 14:18, schrieb Octavian Rasnita:

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']

l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict(zip(l[0::2],l[1::2]))
{8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}
--
http://mail.python.org/mailman/listinfo/python-list


Re: etl tool!!!!!

2010-12-28 Thread Stefan Sonnenberg-Carstens

Am 28.12.2010 13:57, schrieb krishna kumar:

Is there any efficient etl tool developed in python?

Yes, Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Keeping track of the N largest values

2010-12-27 Thread Stefan Sonnenberg-Carstens

Am 26.12.2010 19:51, schrieb Stefan Sonnenberg-Carstens:

l = []
K = 10

while 1:
a = input()
if len(l) == K:
l.remove(min(l))
l=[x for x in l if x < a] + [a] + [x for x in l if x > a]
print l 

A minor fault made it into my prog:

l = [0]
K = 10

while 1:
a = input()
l=[x for x in l if x < a] + [a] + [x for x in l if x > a]
if len(l) == K:
l.remove(min(l))
print l
--
http://mail.python.org/mailman/listinfo/python-list


Re: Keeping track of the N largest values

2010-12-26 Thread Stefan Sonnenberg-Carstens

Am 25.12.2010 16:42, schrieb Roy Smith:

I'm processing a stream of N numbers and want to keep track of the K
largest.  There's too many numbers in the stream (i.e. N is too large)
to keep in memory at once.  K is small (100 would be typical).

> From a theoretical point of view, I should be able to do this in N log K
time.  What I'm doing now is essentially:

top = [-1]# Assume all x are>= 0
for x in input():
 if x<= top[0]:
 continue
 top.append(x)
 if len(top)>  K:
 top.sort()
 top.pop(0)

I can see pathological cases (say, all input values the same) where
running time would be N K log K, but on average (N>>  K and random
distribution of values), this should be pretty close to N.

Is there a better way to do this, either from a theoretical running time
point of view, or just a nicer way to code this in Python?

Here is my version:

l = []
K = 10

while 1:
a = input()
if len(l) == K:
l.remove(min(l))
l=[x for x in l if x < a] + [a] + [x for x in l if x > a]
print l
--
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to parse a HUGE(1gb) xml file

2010-12-25 Thread Stefan Sonnenberg-Carstens

Am 25.12.2010 20:41, schrieb Roy Smith:

In article,
  Adam Tauno Williams  wrote:


XML works extremely well for large datasets.

Barf.  I'll agree that there are some nice points to XML.  It is
portable.  It is (to a certain extent) human readable, and in a pinch
you can use standard text tools to do ad-hoc queries (i.e. grep for a
particular entry).  And, yes, there are plenty of toolsets for dealing
with XML files.

On the other hand, the verbosity is unbelievable.  I'm currently working
with a data feed we get from a supplier in XML.  Every day we get
incremental updates of about 10-50 MB each.  The total data set at this
point is 61 GB.  It's got stuff like this in it:

 FALSE

That's 54 bytes to store a single bit of information.  I'm all for
human-readable formats, but bloating the data by a factor of 432 is
rather excessive.  Of course, that's an extreme example.  A more
efficient example would be:

 1173722

which is 26 bytes to store an integer.  That's only a bloat factor of
6-1/2.

Of course, one advantage of XML is that with so much redundant text, it
compresses well.  We typically see gzip compression ratios of 20:1.
But, that just means you can archive them efficiently; you can't do
anything useful until you unzip them.

Sending complete SQLite databases is absolute perfect.
For example Fedora uses (used?) this for their yum catalog updates.
Download to the right place, point your tool to it, ready.

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


Re: using strings from extension module question + possible documentation error

2010-12-24 Thread Stefan Sonnenberg-Carstens

Am 24.12.2010 13:31, schrieb Oleg Leschov:

Hi,

I am writing an extension module in which I want to do some heavy
number crunching on large amount of data.

The input data is prepared using Python code and passed to this
extension module via strings containing binary data, and the result is
also passed back as a list of pretty large strings containing binary
data.

So first I thought I'll just pass the empty strings made in Python
along with input data strings, so my extension code would just fill
those empty strings with the results.

But then I read the PyString docs, it says I must not modify any
strings even though it seems to be possible...

strings are immutable.
If you pass a string and the underlying C module changes it's contents, 
this idiom is broken.

The source for endless pain ...

Ok then I decided I'll create a list and fill it with strings from my
C extension code..

However to avoid data copying I wish to fill the newly created (using
PyString_FromString(NULL,x) ) strings' buffers (obtained with
PyString_AsString) with data after creating them in my extension
module.

You could as an alternative just use byte arrays. These are changeable.

The question is - is this allowed? Because the doc says this

The data must not be modified in any way, unless the string was just created 
using PyString_FromStringAndSize(NULL, size).

Once the string has been used by python code, you should not change it.
strings are immutable in python, so that every operation on a string 
returns a new one.

See above.

but what exactly does "just created" mean? will it not be considered
Just created means just that. It is created and not been passed back to 
the interpreter.

So long you may change it.

"just created" if I call any more Python stuff after
PyString_FromString, like another PyString_FromString along with
PyString_AsString? Which I certainly intend to do since I first create
all strings I want, and then do my calculations which fill those with
actual data.


Another question is this - why does PyString_AsString doc states that

Return a NUL-terminated representation of the contents of string.

when strings may contain binary data and thus NOT NUL-terminated in
general? is this a documentation error or I can't access binary
strings using PyString_AsString ?

Read carefully: NUL-terminated representation of the contents of the string.
It may contain other data, but the C-API will take care that this 
"contract" will be valid.

P.S. the doc quotes are from
http://docs.python.org/release/2.6.6/c-api/string.html

There do they live, indeed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to parse a HUGE(1gb) xml file

2010-12-23 Thread Stefan Sonnenberg-Carstens

Am 23.12.2010 21:27, schrieb Nobody:

On Wed, 22 Dec 2010 23:54:34 +0100, Stefan Sonnenberg-Carstens wrote:


Normally (what is normal, anyway?) such files are auto-generated,
and are something that has a apparent similarity with a database query
result, encapsuled in xml.
Most of the time the structure is same for every "row" thats in there.
So, a very unpythonic but fast, way would be to let awk resemble the
records and write them in csv format to stdout.

awk works well if the input is formatted such that each line is a record;

You shouldn't tell it to awk.

it's not so good otherwise. XML isn't a line-oriented format; in
particular, there are many places where both newlines and spaces are just
whitespace. A number of XML generators will "word wrap" the resulting XML
to make it more human readable, so line-oriented tools aren't a good idea.

I never had the opportunity seeing awk fail on this task :-)

For large datasets I always have huge question marks if one says "xml".
But I don't want to start a flame war.
--
http://mail.python.org/mailman/listinfo/python-list


Re: round in 2.6 and 2.7

2010-12-23 Thread Stefan Sonnenberg-Carstens

Am 23.12.2010 19:57, schrieb Hrvoje Niksic:

I stumbled upon this.  Python 2.6:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.

9.95

9.9493

"%.16g" % 9.95

'9.949'

round(9.95, 1)

10.0

So it seems that Python is going out of its way to intuitively round
9.95, while the repr retains the unnecessary digits.  However, with 2.7
it's exactly the opposite:

Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.

9.95

9.95

"%.16g" % 9.95

'9.949'

round(9.95, 1)

9.9

Is the change to round() expected?

Indeed:
http://svn.python.org/view?view=rev&revision=76373
--
http://mail.python.org/mailman/listinfo/python-list


Re: compiling Python 2.7.1 with readline module fails on Debian (Virtualbox)

2010-12-23 Thread Stefan Sonnenberg-Carstens

Am 23.12.2010 15:37, schrieb Benedict Verheyen:

On 23/12/2010 14:09, Benedict Verheyen wrote:


I started from scratch.
I tried to build readline 6 and 5, and installing the packages system wide
as opposed to $HOME/local, but everytime Python fails to find it.

On stable, apt-get build-dep python 2.6 doesn't work, but
apt-get build-dep python 2.5 does so i did that.

At configure time, the libreadline library is now found but i have no clue
why it didn't find the readline version i built.
At the end of the Python build, it still ends with this message:

Failed to build these modules:
readline

The other modules that where automatically installed via the build-dep
command are built successfully.
I have pasted the config.log here:
 http://paste.pocoo.org/show/308859/

Thanks,
Benedict


I did a new test, again completely from scratch.
I built ncurses and readline, installed them system wide (normal make install
without a --prefix).
Then i editing Modules/Setup.dist to include readline and adding the correct 
paths
to the include and libs files. configure now finds the readline library.
However, ncurses and readline still fail to be built.

I had a look at the output of make.
This is the error for ncurses:

building '_curses' extension
gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall 
-Wstrict-prototypes -I/home/benedict/local/include -I.
-IInclude -I./Include -I/usr/local/include 
-I/home/benedict/src/Python-2.7.1/Include -I/home/benedict/src/Python-2.7.1 -c
/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.c -o 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.o

gcc -pthread -shared 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.o
 -L/home/benedict/local/lib
-L/usr/local/lib -L. -lncurses -lpython2.7 -o 
build/lib.linux-x86_64-2.7/_curses.so
/usr/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libncurses.a(lib_addch.o): 
relocation R_X86_64_32 against `a local symbol'
can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libncurses.a: could not 
read symbols: Bad value
collect2: ld returned 1 exit status

building '_curses_panel' extension
gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall 
-Wstrict-prototypes -I/home/benedict/local/include -I.
-IInclude -I./Include -I/usr/local/include 
-I/home/benedict/src/Python-2.7.1/Include -I/home/benedict/src/Python-2.7.1 -c
/home/benedict/src/Python-2.7.1/Modules/_curses_panel.c -o 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_curses_panel.o

gcc -pthread -shared 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_curses_panel.o
 -L/home/benedict/local/lib
-L/usr/local/lib -L. -lpanel -lncurses -lpython2.7 -o 
build/lib.linux-x86_64-2.7/_curses_panel.so
/usr/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libpanel.a(p_above.o): 
relocation R_X86_64_32 against `a local symbol' can
not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libpanel.a: could not read 
symbols: Bad value
collect2: ld returned 1 exit status

The error for readline:

gcc -pthread  -Xlinker -export-dynamic -o python \
Modules/python.o \
-L. -lpython2.7 -lpthread -ldl  -lutil -lreadline 
-L/usr/local/lib   -lm
/usr/local/lib/libreadline.so: undefined reference to `PC'
/usr/local/lib/libreadline.so: undefined reference to `tgetflag'
/usr/local/lib/libreadline.so: undefined reference to `tgetent'
/usr/local/lib/libreadline.so: undefined reference to `UP'
/usr/local/lib/libreadline.so: undefined reference to `tputs'
/usr/local/lib/libreadline.so: undefined reference to `tgoto'
/usr/local/lib/libreadline.so: undefined reference to `tgetnum'
/usr/local/lib/libreadline.so: undefined reference to `BC'
/usr/local/lib/libreadline.so: undefined reference to `tgetstr'
collect2: ld returned 1 exit status
make: *** [python] Fout 1

Thanks,
Benedict


OK, I compiled it successfully under Debian 5.07 i386.
ncurses and libreadline are compiled from hand, both 
--prefix=$HOME/usr/local.
For python the only extra needed was export 
LD_LIBRARY_PATH=$HOME/usr/local/lib:$LD_LIBRARY_PATH



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


Re: compiling Python 2.7.1 with readline module fails on Debian (Virtualbox)

2010-12-23 Thread Stefan Sonnenberg-Carstens

Am 23.12.2010 15:37, schrieb Benedict Verheyen:

On 23/12/2010 14:09, Benedict Verheyen wrote:


I started from scratch.
I tried to build readline 6 and 5, and installing the packages system wide
as opposed to $HOME/local, but everytime Python fails to find it.

On stable, apt-get build-dep python 2.6 doesn't work, but
apt-get build-dep python 2.5 does so i did that.

At configure time, the libreadline library is now found but i have no clue
why it didn't find the readline version i built.
At the end of the Python build, it still ends with this message:

Failed to build these modules:
readline

The other modules that where automatically installed via the build-dep
command are built successfully.
I have pasted the config.log here:
 http://paste.pocoo.org/show/308859/

Thanks,
Benedict


I did a new test, again completely from scratch.
I built ncurses and readline, installed them system wide (normal make install
without a --prefix).
Then i editing Modules/Setup.dist to include readline and adding the correct 
paths
to the include and libs files. configure now finds the readline library.
However, ncurses and readline still fail to be built.

I had a look at the output of make.
This is the error for ncurses:

building '_curses' extension
gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall 
-Wstrict-prototypes -I/home/benedict/local/include -I.
-IInclude -I./Include -I/usr/local/include 
-I/home/benedict/src/Python-2.7.1/Include -I/home/benedict/src/Python-2.7.1 -c
/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.c -o 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.o

gcc -pthread -shared 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_cursesmodule.o
 -L/home/benedict/local/lib
-L/usr/local/lib -L. -lncurses -lpython2.7 -o 
build/lib.linux-x86_64-2.7/_curses.so
/usr/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libncurses.a(lib_addch.o): 
relocation R_X86_64_32 against `a local symbol'
can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libncurses.a: could not 
read symbols: Bad value
collect2: ld returned 1 exit status

building '_curses_panel' extension
gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall 
-Wstrict-prototypes -I/home/benedict/local/include -I.
-IInclude -I./Include -I/usr/local/include 
-I/home/benedict/src/Python-2.7.1/Include -I/home/benedict/src/Python-2.7.1 -c
/home/benedict/src/Python-2.7.1/Modules/_curses_panel.c -o 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_curses_panel.o

gcc -pthread -shared 
build/temp.linux-x86_64-2.7/home/benedict/src/Python-2.7.1/Modules/_curses_panel.o
 -L/home/benedict/local/lib
-L/usr/local/lib -L. -lpanel -lncurses -lpython2.7 -o 
build/lib.linux-x86_64-2.7/_curses_panel.so
/usr/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libpanel.a(p_above.o): 
relocation R_X86_64_32 against `a local symbol' can
not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.3.2/../../../../lib/libpanel.a: could not read 
symbols: Bad value
collect2: ld returned 1 exit status

The error for readline:

gcc -pthread  -Xlinker -export-dynamic -o python \
Modules/python.o \
-L. -lpython2.7 -lpthread -ldl  -lutil -lreadline 
-L/usr/local/lib   -lm
/usr/local/lib/libreadline.so: undefined reference to `PC'
/usr/local/lib/libreadline.so: undefined reference to `tgetflag'
/usr/local/lib/libreadline.so: undefined reference to `tgetent'
/usr/local/lib/libreadline.so: undefined reference to `UP'
/usr/local/lib/libreadline.so: undefined reference to `tputs'
/usr/local/lib/libreadline.so: undefined reference to `tgoto'
/usr/local/lib/libreadline.so: undefined reference to `tgetnum'
/usr/local/lib/libreadline.so: undefined reference to `BC'
/usr/local/lib/libreadline.so: undefined reference to `tgetstr'
collect2: ld returned 1 exit status
make: *** [python] Fout 1

Thanks,
Benedict

It seems some libs are compiled without PIC (position indepentent code), 
but python

should be.
That will not work.
I'm currently setting up a VM, to try to follow the steps.

Why do you want to build a readline etc when it is in the system ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: compiling Python 2.7.1 with readline module fails on Debian (Virtualbox)

2010-12-23 Thread Stefan Sonnenberg-Carstens

Am 23.12.2010 10:04, schrieb Benedict Verheyen:

On 22/12/2010 18:47, Stefan Sonnenberg-Carstens wrote:

Am 22.12.2010 09:33, schrieb Benedict Verheyen:



Did you try

apt-get install build-essential
apt-get build-dep python2.7

before trying to compile ?

Anyway, the config.log file is always of special interest.
Btw, which Debian release are you running ?
If the system is set up correctly it should not be necessary to change env vars 
to get it built.



I use Debian stable so "apt-get build-dep python2.7" doesn't work.
I removed all installed Python 2.7.1 files and so on and tried to build/install 
from scratch.
Now i can't get Python to find the readline library.
What is the normal procedure to make sure the readline library is found?

I downloaded readline 6.1, build it and installed it to $HOME/local
$HOME/local/lib contains libreadline.so.

I did the following to try to enable detection of the libreadline:

- export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH
- vi Modules/Setup and uncomment the line that specifies the readline module.
   Added -L$HOME/local/lib
- Tried ./configure with env CPPFLAGS="-I$HOME/local/include" 
LDFLAGS="-L$HOME/local/lib"
- Edit setup.py, to add_dirs to $HOME/local/include and $HOME/local/lib
- Tried to export CPPFLAGS and LDFLAGS

Nothing seems to work.
What am i doing wrong?

Cheers,
Benedict


apt-get build-dep python 2.6
should do, the dependencies haven't changed (IMHO).
Then wipe away $HOME/usr/local (if you can, btw),
reset all env vars to default (perhaps reboot).
Then untar python from scratch, cd into that dir, and type
./configure --prefix=$HOME/usr/local
make
make install

that should be all.
Otherwise your box is somewhat messed up.

Anyway, your config.log is of special interest (did I say that already ?)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to parse a HUGE(1gb) xml file

2010-12-22 Thread Stefan Sonnenberg-Carstens

Am 20.12.2010 20:34, schrieb spaceman-spiff:

Hi c.l.p folks

This is a rather long post, but i wanted to include all the details&  everything i 
have tried so far myself, so please bear with me&  read the entire boringly long 
post.

I am trying to parse a ginormous ( ~ 1gb) xml file.


0. I am a python&  xml n00b, s&  have been relying on the excellent beginner book 
DIP(Dive_Into_Python3 by MP(Mark Pilgrim) Mark , if u are readng this, you are AWESOME& 
 so is your witty&  humorous writing style)


1. Almost all exmaples pf parsing xml in python, i have seen, start off with 
these 4 lines of code.

import xml.etree.ElementTree as etree
tree = etree.parse('*path_to_ginormous_xml*')
root = tree.getroot()  #my huge xml has 1 root at the top level
print root

2. In the 2nd line of code above, as Mark explains in DIP, the parse function 
builds&  returns a tree object, in-memory(RAM), which represents the entire 
document.
I tried this code, which works fine for a small ( ~ 1MB), but when i run this 
simple 4 line py code in a terminal for my HUGE target file (1GB), nothing 
happens.
In a separate terminal, i run the top command,&  i can see a python process, 
with memory (the VIRT column) increasing from 100MB , all the way upto 2100MB.

I am guessing, as this happens (over the course of 20-30 mins), the tree 
representing is being slowly built in memory, but even after 30-40 mins, 
nothing happens.
I dont get an error, seg fault or out_of_memory exception.

My hardware setup : I have a win7 pro box with 8gb of RAM&  intel core2 quad 
cpuq9400.
On this i am running sun virtualbox(3.2.12), with ubuntu 10.10 as guest os, with 
23gb disk space&  2gb(2048mb) ram, assigned to the guest ubuntu os.

3. I also tried using lxml, but an lxml tree is much more expensive, as it 
retains more info about a node's context, including references to it's parent.
[http://www.ibm.com/developerworks/xml/library/x-hiperfparse/]

When i ran the same 4line code above, but with lxml's elementree ( using the 
import below in line1of the code above)
import lxml.etree as lxml_etree

i can see the memory consumption of the python process(which is running the code) 
shoot upto ~ 2700mb&  then, python(or the os ?) kills the process as it nears 
the total system memory(2gb)

I ran the code from 1 terminal window (screenshot :http://imgur.com/ozLkB.png)
&  ran top from another terminal (http://imgur.com/HAoHA.png)

4. I then investigated some streaming libraries, but am confused - there is 
SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse 
interface[http://effbot.org/zone/element-iterparse.htm]

Which one is the best for my situation ?

Any&  all code_snippets/wisdom/thoughts/ideas/suggestions/feedback/comments/ of 
the c.l.p community would be greatly appreciated.
Plz feel free to email me directly too.

thanks a ton

cheers
ashish

email :
ashish.makani
domain:gmail.com

p.s.
Other useful links on xml parsing in python
0. http://diveintopython3.org/xml.html
1. 
http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator
2. http://codespeak.net/lxml/tutorial.html
3. 
https://groups.google.com/forum/?hl=en&lnk=gst&q=parsing+a+huge+xml#!topic/comp.lang.python/CMgToEnjZBk
4. http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
5.http://effbot.org/zone/element-index.htm
http://effbot.org/zone/element-iterparse.htm
6. SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML



Normally (what is normal, anyway?) such files are auto-generated,
and are something that has a apparent similarity with a database query 
result, encapsuled in xml.

Most of the time the structure is same for every "row" thats in there.
So, a very unpythonic but fast, way would be to let awk resemble the 
records and write them in csv format to stdout.
then pipe that to your python cruncher of choice and let it do the hard 
work.

The awk part can be done in python, anyway, so could skip that.

And take a look at xmlsh.org, they offer tools for the command line, 
like xml2csv. (Need java, btw).


Cheers

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


Toy http server

2010-12-22 Thread Stefan Sonnenberg-Carstens

Sorry, this one is cross post.
I posted my question below some time ago to then jython ml, because this
hit me first with jython.
Anyway, time passed, problem not solved.

So, I'd like to know if some of you know where my error lies:


Hi all,

I've played around with some code-kata of mine from the past.
It's a toy http server:

import socket
import select
import sys
import time
srv = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
srv.setblocking(0)
srv.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True) # Not sure if 
this is needed. I only remember I've started once for some reason. 
Commenting out does no change

if srv:
srv.bind(('',8080))
srv.setblocking(0)
srv.listen(5)
conns = [srv]
buffers = {}
while True:
reader,writer,failed = select.select(conns,conns,[],1)
if failed:
sys.exit(1)

if srv in reader:
client,(ip,port) = srv.accept()
client.setblocking(False)
conns += [client]
for r in reader:
if r is not srv:
data = r.recv(1024)
if not data:
conns.remove(r)
else:
if r in buffers:
buffers[r] += data
else:
buffers[r] = data
for w in writer:
if w is not srv:
if w in buffers and buffers[w][-2:] == '\r\n':
msg = 'HTTP/1.0 200 OK\r\nServer: 
Jython/2.5\r\nContent-type: text/plain\r\n\r\nThe answer is: 42\r\n'

w.send(msg)
w.close() # THIS ONLY WORKS WHEN THIS LINE IS PRESENT
conns.remove(w) # AND THEN THIS IS MUST
buffers[w] = ''

But today I was asking myself why I should always close the socket, and 
if wouldn't be more efficient if

I let it open.
To make a long story short: it does not work.
If I comment out the lines with the comment all upper case,
jython consumes one core in my laptop completely and ab.exe from apache 
bails out after a while.


Changing send() to sendall() did not do the trick.

I've searched the web and did not find anything meaningful.

Perhaps someone here can switch the light on.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Catching user switching and getting current active user from root on linux

2010-12-22 Thread Stefan Sonnenberg-Carstens

Am 22.12.2010 20:28, schrieb mpnordland:

ok, I'll give one more chance.
First, to pacify those who hate google groups: What is a good usenet
client?
second, How should I set up this proxy so that when a connection is
made, it request's authentication, and then log's the request, if
authentication is not gotten, how do I have it block (or firewall) the
request? Furthermore, I would like for the proxy to be squid. So all
of the nitty gritty should have to do with squid.
Just install Squid, enable user authentication and grant access only to 
authenticated people.
Now, configure logrotated and tell to rotate logs every day/week/month 
(your mileage will vary).
After rotating run a program such as webalizer to get stats (even on a 
per user basis).
If you have smart guys under your users, set up a) a transparent proxy 
intercepting http/https requests

or b) set up a iptables firewall with redirection to the squid port.

There are many, many, many how-to documents on the net describing 
exactly what you want to do.


A first starting point could be 
http://www.comfsm.fm/computing/squid/FAQ.html


And your problems are solved a long time ago:

http://www.faqs.org/docs/Linux-mini/TransparentProxy.html

And, the most important thing:

Check your local laws for this intention.
Some, like our german law, require these things to be under clear rules.

Cheers

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


Re: Code review request

2010-12-22 Thread Stefan Sonnenberg-Carstens

Am 22.12.2010 19:34, schrieb Jason Staudenmayer:

Hi All,
I'm a python newbie so please be kind. I've been reading book after book and have written 
a script or two but this is my first real "program". Just looking for any 
suggestions and pointers. I've done some work with bash scripts and php (not OOP) a while 
a go. I'm not a programmer but would like to possibly expand in to it (right now an IT 
guy). Am I on the right track so far?

The program should be documented enough to explain.

"""
Created on Tue Dec 21 13:39:41 2010
@author: jason

Usage: cmd_drug_testing.py [options]...
Will select a random employee from the local database (located in the current 
directory)
and display the name by default.

This program (Drug Testing) was written to help select employees for drug 
testing.
Program usage:

 -h, --help   Displays this help info
 -l, --list-emplysLists all employees in the database
 -s, --select Select employee for testing
 -r, --remove-emply   Delete employee from database, usage: -d 
employee_name or id
 -e, --edit-emply Edit employee data in database, usage: -e 
employee_name or id
field_to_change new_value
 -i, --insert-emply   Insert new employee in database:
 must fit this format -- "id:or '','lastname, 
firstname', 'email',0"
 -f   Display database name and full path

This program was written by, Jason S. For the use of AA.

"""

# import our needed modules
import sqlite3 as sqlite
import sys, getopt

#set global vars
global cursor
global conn


def usage():
 """ this just prints the usage in the doc string"""
 print __doc__

def dbconnect(sql):
 """handel the connction to the sqlite db  """
 dbConnection = sqlite.connect("drug-test.db")
 #set the cursor
 cursor = dbConnection.cursor()
 try:
 #execute the sql passed from the other funtions and return the results
 result = cursor.execute(sql)
 dbConnection.commit()
 except sqlite.Error, e:
 result = e.args[0]

 return result


def listEmployees(sql):
 #list all records in the database
 listemp = dbconnect(sql)
 for lst in listemp:
 print "%s, %s, %s, %s" % (lst[0],lst[1],lst[2],lst[3])

def selectOneEmployee(sql):
 #select a random record from the database
 listemp = dbconnect(sql)
 for empl in listemp:
 print empl[0]

def removeOneEmployee(sqlchk,sqldel):
 #delete one employee by ID number
 chk = dbconnect(sqlchk)

 if chk.fetchone() != None:
 #check to make sure the ID is valid in the database
 emply = dbconnect(sqlchk)
 emply = emply.fetchall()
 print "trying to remove employee %s" % (emply)
 try:
 dbconnect(sqldel)

 except sqlite.Error, e:
 result = e.args[0]
 print result

 else:
 print "Employees ID is invalid, please check the ID number"

def insertEmployee(sql):
 #insert a new employee into the database
 print sql
 try:
 dbconnect(sql)

 except sqlite.Error, e:
 result = e.args[0]
 print result

def main(argv):
 """The purpose of this program is to select an empployee from this 
database for random drug
 testing. This program can also perform maintainance on same database."""
 if argv == []:
 sql = "SELECT name FROM employees ORDER BY RANDOM() LIMIT 1;"
 print "The following employee has been selected\n"
 selectOneEmployee(sql)

 try:
 #get the options passed by the user
 opts, args = getopt.getopt(argv, 
"hlsr:e:i:d",["Help","list-emplys","select","remove-emply=","edit-emply=","insert-emply="])

 except getopt.GetoptError:
 usage()
 sys.exit(2)

 #check throught the options and respond accordingly
 for opt, arg in opts:
 if opt in ("-h", "--help"):
 usage()
 sys.exit()
 elif opt == '-d':
 global _debug
 _debug = 1
 elif opt in ("-l", "--list-emplys"):
 sql = "select * from employees"
 listEmployees(sql)
 elif opt in ("-s","--select"):
 sql = "SELECT name FROM employees ORDER BY RANDOM() LIMIT 1;"
 print "The following employee has been selected\n"
 selectOneEmployee(sql)
 elif opt in ("-r","--remove-emply"):
 if arg == "":

 sys.exit("You must provice the ID for the employee to remove")
 sqlchk = "select * from employees where id = \"%s\"" % (arg)
 sqldel = "delete from employees where id = \"%s\""  % (arg)
 removeOneEmployee(sqlchk,sqldel)
 elif opt in ("-i", "--insert-emply"):
 sql = "insert into employees values(%s)" % (arg)
 insertEmployee(sql)

if __name__ == "__main__":
 main(sys.argv[1:]

Re: compiling Python 2.7.1 with readline module fails on Debian (Virtualbox)

2010-12-22 Thread Stefan Sonnenberg-Carstens

Am 22.12.2010 09:33, schrieb Benedict Verheyen:

Hi,


i'm trying to compile Python 2.7.1 on Debian (Virtual Box).
Compiling end successfully but readline and curses fail to build.

I'm working with virtualenv and I install all my packages in $HOME/local.
I've downloaded readline, compiled and installed it in $HOME/local, same with 
ncurses.
Both were the latest releases.
But somehow, Python isn't able to build them.

I get this message:

Failed to build these modules:
_curses_curses_panelreadline

So it does seems to find the modules.
I'm not used to dealing with configure, and make so i can't debug much.
I first tried this configure line:
./configure --enable-shared --prefix=$HOME/local

Next i tried this one:
env CPPFLAGS="-I$HOME/local/include" LDFLAGS="-L$HOME/local/lib" ./configure 
--enable-shared --prefix=$HOME/local

But make yields the same error.

Any clues on what I do wrong?

The source of ncurses and readline are placed in $HOME/src.
Both packages are installed in $HOME/local

Thanks for any advice,

Regards,
Benedict


Did you try

apt-get install build-essential
apt-get build-dep python2.7

before trying to compile ?

Anyway, the config.log file is always of special interest.
Btw, which Debian release are you running ?
If the system is set up correctly it should not be necessary to change 
env vars to get it built.



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


Re: Scanning directories for new files?

2010-12-21 Thread Stefan Sonnenberg-Carstens

Am 21.12.2010 20:17, schrieb Matty Sarro:

Hey everyone.
I'm in the midst of writing a parser to clean up incoming files,
remove extra data that isn't needed, normalize some values, etc. The
base files will be uploaded via FTP.
How does one go about scanning a directory for new files? For now
we're looking to run it as a cron job but eventually would like to
move away from that into making it a service running in the
background.

When You say cron, I assume you're running linux.
One approach would be to os.walk() the directory in question, and filling a
dict with the absolute name of the file as key and the output from 
stat() as content.

Then re-scan regularly and check for changes in mtime,ctime etc.

A less ressource consuming approach would be to use Linux' inotify 
infrastructure,

which can be used from python https://github.com/seb-m/pyinotify

And, your service is only an import away :-)

https://github.com/seb-m/pyinotify/blob/master/python2/examples/daemon.py
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying an existing excel spreadsheet

2010-12-20 Thread Stefan Sonnenberg-Carstens

Am 20.12.2010 22:56, schrieb Ed Keith:

I have a user supplied 'template' Excel spreadsheet. I need to create a new 
excel spreadsheet based on the supplied template, with data filled in.

I found the tools here http://www.python-excel.org/,  and 
http://sourceforge.net/projects/pyexcelerator/. I have been trying to use the 
former, since the latter seems to be devoid of documentation (not even any 
docstrings).


My first thought was to copy the template, open the copy, modify it and save 
the modifications. But it looks like if I open an existing spreadsheet it must 
be read only.
Could you post some code ? Did you try a simple file copy or do you 
iterate over all the cells ?

  So I tried to  open the template, copy it to a new spreadsheet and write the 
new spreadsheet, but I can't seem to copy the images, and it looks like copying 
the formatting is going to be difficult.

Can anyone give me any tips or advice?

Thanks in advance,

-EdK

Ed Keith

e_...@yahoo.com



Blog: edkeith.blogspot.com




As long as your program only needs to run under windows,
COM automation is IMHO the best solution.
Python tells Excel what to do.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How can I intentionally crash my lappy?

2010-12-20 Thread Stefan Sonnenberg-Carstens
http://pcsupport.about.com/od/tipstricks/ht/makebsodxp.htm
Am Mo, 20.12.2010, 09:45 schrieb mechtheist:
> I am no programmer, but know the rudiments [the rudi'est of rudiments]
> of working with Python.  I have a simple need,  to have a simple
> script/app I can run that will crash my PC.  On my desktops, I can
> always hit the reset, but that is not an option with my laptop.  Can
> anyone tell me of an easy way to guarantee a program will instantly
> kill/BSOD my windows7/64bit laptop?
>
> Any help is much appreciated.
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: True lists in python?

2010-12-19 Thread Stefan Sonnenberg-Carstens

Am 19.12.2010 07:18, schrieb Dmitry Groshev:

Is there any way to use a true lists (with O(c) insertion/deletion and
O(n) search) in python? For example, to make things like reversing
part of the list with a constant time.

reversing part of the list could also be interpreted as reading it
in the opposite direction from a given starting point to a known end.

Perhaps you want to look into the array module, but you
have to  sacrifice the typelessness of a true python list:
it can only contain numerical data (also char, byte).

But don't know the timing pattern of array objects, only that they are *way*
faster then lists.


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


Re: how to measure TCP congestion windows using python ??

2010-12-19 Thread Stefan Sonnenberg-Carstens

Am 19.12.2010 11:39, schrieb plz:

hi! i'm newbie about python and i want to measure the value of cwnd in TCP 
socket.

I have searched from an internet and got that we could handle it from SOL_TCP, 
TCP_INFO...since unix had #include  and we could  refer to 
tcp_info
for getting the information of TCP by using this method

but i don't know how to declare the tcp_info in Python..
does anyone know how to handle this? please suggest me

Thanks in advance



Here is non-portable way to get the data under Linux:

import socket
from ctypes import *
cdll.LoadLibrary('libc.so.6')
libc = CDLL('libc.so.6')

class tcp_info(Structure):
_fields_  = [
('tcpi_state',c_uint8),
('tcpi_ca_state',c_uint8),
('tcpi_retransmits',c_uint8),
('tcpi_probes',c_uint8),
('tcpi_backoff',c_uint8),
('tcpi_options',c_uint8),
('tcpi_snd_wscale',c_uint8,4),
('tcpi_rcv_wscale',c_uint8,4),
('tcpi_rto',c_uint32),
('tcpi_ato',c_uint32),
('tcpi_snd_mss',c_uint32),
('tcpi_rcv_mss',c_uint32),
('tcpi_unacked',c_uint32),
('tcpi_sacked',c_uint32),
('tcpi_lost',c_uint32),
('tcpi_retrans',c_uint32),
('tcpi_fackets',c_uint32),
('tcpi_last_data_sent',c_uint32),
('tcpi_last_ack_sent',c_uint32),
('tcpi_last_data_recv',c_uint32),
('tcpi_last_ack_recv',c_uint32),
('tcpi_pmtu',c_uint32),
('tcpi_rcv_ssthresh',c_uint32),
('tcpi_rtt',c_uint32),
('tcpi_rttvar',c_uint32),
('tcpi_snd_ssthresh',c_uint32),
('tcpi_snd_cwnd',c_uint32),
('tcpi_advmss',c_uint32),
('tcpi_reordering',c_uint32),
('tcpi_rcv_rtt',c_uint32),
('tcpi_rcv_space',c_uint32),
('tcpi_total_retrans',c_uint32)
]

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_TCP)
info = tcp_info()
pinfo = pointer(info)
len_info = c_uint32(sizeof(tcp_info))
plen_info = pointer(len_info)
res = 
libc.getsockopt(sock.fileno(),socket.SOL_TCP,socket.TCP_INFO,pinfo,plen_info)
if res == 0:
for n in (x[0] for x in tcp_info._fields_):
print n,getattr(info,n)
else:
print 'getsockopt() failed. (%i)' % res

Tested under Ubuntu 10.10 and Python 2.6.6.

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


Re: how to measure TCP congestion windows using python ??

2010-12-19 Thread Stefan Sonnenberg-Carstens

Am 19.12.2010 11:39, schrieb plz:

hi! i'm newbie about python and i want to measure the value of cwnd in TCP 
socket.

I have searched from an internet and got that we could handle it from SOL_TCP, 
TCP_INFO...since unix had #include  and we could  refer to 
tcp_info
for getting the information of TCP by using this method

but i don't know how to declare the tcp_info in Python..
does anyone know how to handle this? please suggest me

Thanks in advance



Which OS ?
I remember these parts not being really paltform indepentend.
--
http://mail.python.org/mailman/listinfo/python-list


Re: If/then style question

2010-12-16 Thread Stefan Sonnenberg-Carstens

Am 16.12.2010 22:49, schrieb John Gordon:

(This is mostly a style question, and perhaps one that has already been
discussed elsewhere.  If so, a pointer to that discussion will be
appreciated!)

When I started learning Python, I wrote a lot of methods that looked like
this:


   def myMethod(self, arg1, arg2):

 if some_good_condition:

   if some_other_good_condition:

 if yet_another_good_condition:

   do_some_useful_stuff()
   exitCode = good1

 else:
   exitCode = bad3

   else:
 exitCode = bad2

 else:
   exitCode = bad1

 return exitCode


But lately I've been preferring this style:


   def myMethod(self, arg1, arg2):

 if some_bad_condition:
   return bad1

 elif some_other_bad_condition:
   return bad2

 elif yet_another_bad_condition:
   return bad3


else:

do_some_useful_stuff()
return good1

I like this style more, mostly because it eliminates a lot of indentation.

However I recall one of my college CS courses stating that "one entry,
one exit" was a good way to write code, and this style has lots of exits.

Are there any concrete advantages of one style over the other?

Thanks.






The advantage in latter case is fewer operations, because you can skip 
the assignments,

and it is more readable.

The "one entry, one exit" is an advice. Not a law.
Your code is OK.

As long as it works ;-)


P.S.:
Sorry, I could not resist:
return bad1 if some_bad_condition else bad2 if some_other_bad_condition 
else bad3 if yet_another_bad_condition else good1 if 
do_some_useful_stuff() else good1

Or consider this:
return [x for x,y in 
((bad1,some_bad_condition),(bad2,some_other_bad_condition),(bad3,yet_another_bad_condition),(good1,do_some_useful_stuff() 
or True)) if x][0]


Neither self explanatory nor readable :-(
--
http://mail.python.org/mailman/listinfo/python-list


Re: O'Reilly Python Certification

2010-12-15 Thread Stefan Sonnenberg-Carstens

Am 15.12.2010 22:11, schrieb Steve Holden:

On 12/15/2010 3:40 PM, Tim Chase wrote:

On a more serious note, it would be interesting to know if it's possible
to test out of the certification for those of us that have been using
Python for a long time.

That's an interesting idea - let a bunch of experienced Python users
tell me what a lousy job I have done of explaining the language. :)

Seriously, I would be interested, and it's a terrific idea. I can't do
anything before January, but if anyone is interested in taking part in
such a review of the materials I'd be grateful if they would contact me
privately by email on a "no promises" basis.

regards
  Steve

I think he meant: take the test without study first.
I'd be interested in both, though.

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


Re: Dealing with pywin32 on Linux

2010-12-15 Thread Stefan Sonnenberg-Carstens

Am 15.12.2010 13:58, schrieb Benedict Verheyen:

Hi,


I'm moving my development environment (python, Django, virtualenv) from Windows
to Linux (Debian more specific). However, on one app (uses Django), i have a 
log on
module that uses AD to authenticate users.
It's based on the active_directory wrapper of Tim Golden.
That needs the win32 module but installation of pywin32 fails on importing:

import _winreg
ImportError: No module named _winreg

Kind of logical in hindsight :)

However, this makes programming and testing under Linux a bit of a pain if 
you're
dealing with Windows specific stuff.
What is the correct way to proceed (i want to keep my development environment 
in Linux)?

I could adjust my software modules to use the Linux counterparts of the AD 
functions
that i have to authenticate.
Or is there another way to deal with this?

Thanks for any insight,

Cheers,
Benedict


Just change to LDAP as authentication method.
Even Active Directory offers LDAP (w/o SSL), and there
are modules to interact with LDAP using python.
And, it is platform indipendent.

See here: http://www.python-ldap.org/

I've done this quite often to auth users in an AD with apache/ldap-auth.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hashlib in one line

2010-11-29 Thread Stefan Sonnenberg-Carstens

Am 29.11.2010 14:50, schrieb Thomas Guettler:

Hi,

I think it would be nice if you could use the hashlib in one line:

hashlib.sha256().update('abc').hexdigest()

Unfortunately update() returns None.

Is there a way to convert a string to the hexdigest of sha256 in one line?

   Thomas



Like so ?

>>> hashlib.sha256('abc').hexdigest()
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
--
http://mail.python.org/mailman/listinfo/python-list


Re: do something every n seconds

2010-11-25 Thread Stefan Sonnenberg-Carstens
Windows or UNIX ?

Am Do, 25.11.2010, 13:38 schrieb Santiago Caracol:
> Hello,
>
> how can I do something (e.g. check if new files are in the working
> directory) every n seconds in Python?
>
> Santiago
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
MfG,

Stefan Sonnenberg-Carstens

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


Re: Glob in python which supports the ** wildcard

2010-11-22 Thread Stefan Sonnenberg-Carstens

Am 22.11.2010 22:43, schrieb Martin Lundberg:

Hi,

I want to be able to let the user enter paths like this:

apps/name/**/*.js

and then find all the matching files in apps/name and all its
subdirectories. However I found out that Python's glob function
doesn't support the recursive ** wildcard. Is there any 3rd party glob
function which do support **?

Regards,

Martin Lundberg


os.walk() or os.path.walk() can be used.
You need to traverse the file system.
AFAIK there is no support for this.
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does Pygoogle allows for advanced search options?

2010-11-21 Thread Stefan Sonnenberg-Carstens

Am 21.11.2010 20:35, schrieb Petar Milin:

Hello ALL!
I am playing with the modified version of the Pygoogle (by Stefan). 
Interesting thing is that get_result_count() gives numbers/results 
different from those in Google. Has anyone checked this? Does anyone 
know why?


Best,
Petar


AFAIK there are several reports which point to the same observation;
even time & location (requesting IP) seem to generate different results.


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


Re: Does Pygoogle allows for advanced search options?

2010-11-21 Thread Stefan Sonnenberg-Carstens

Am 21.11.2010 11:01, schrieb Petar Milin:


On 20/11/10 22:34, Chris Rebert wrote:

On Thu, Nov 18, 2010 at 3:26 AM, neocortex  wrote:

The library doesn't seem to have built-in support for filtering by
language (and Google lacks a search query-string-based operator for
that), but it looks like you could implement that feature by adding an
"lr" parameter with an appropriate value to the query `args`
dictionary. See the "lr?" entry under "Web Search Specific Arguments"
onhttp://code.google.com/apis/websearch/docs/reference.html, and
lines 68&  102 of pygoogle.py.

 From those lines, it can be concluded that lr=lang_?? is not
supported, unfortunately.

Right; that's why I said "you could implement that feature". Pretty
easily it would seem.
Thanks for believing in me ( ;-) ), but I am a newbie in Python world, 
although with some experience in other prog. languages.
So, if I read pygoogle.py well, I sould add lr parameter in init and 
then after lines 68 and 102?

Correct.
I just did not read source code enough to realize the parameter is needed
twice. You just could hack that out, too :-)
Normally (what is normal, anyway ?) it's good practice to create an
contructor (__init__) in python which has the most common options
as keyword arguments and default values assigned, like so:

class Foo:
def __init__(self,lr='lang_en',url='http://some.of.this'):
self.lr = lr
self.url = url
...


Thanks again! You guys here are very kind and helpful!
Best,
Petar



#!/usr/bin/python
"""
Google AJAX Search Module
http://code.google.com/apis/ajaxsearch/documentation/reference.html
"""
try:
import simplejson as json
except:
import json
import urllib

__author__ = "Kiran Bandla"
__version__ = "0.1"
URL = 'http://ajax.googleapis.com/ajax/services/search/web?'

#Web Search Specific Arguments
#http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_web
#SAFE,FILTER
"""
SAFE
This optional argument supplies the search safety level which may be one of:
* safe=active - enables the highest level of safe search filtering
* safe=moderate - enables moderate safe search filtering (default)
* safe=off - disables safe search filtering
"""
SAFE_ACTIVE = "active"
SAFE_MODERATE   = "moderate"
SAFE_OFF= "off"

"""
FILTER
This optional argument controls turning on or off the duplicate content filter:

* filter=0 - Turns off the duplicate content filter
* filter=1 - Turns on the duplicate content filter (default)

"""
FILTER_OFF  = 0
FILTER_ON   = 1

#Standard URL Arguments
#http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_args
"""
RSZ
This optional argument supplies the number of results that the application 
would like to recieve. 
A value of small indicates a small result set size or 4 results. 
A value of large indicates a large result set or 8 results. If this argument is 
not supplied, a value of small is assumed. 
"""
RSZ_SMALL = "small"
RSZ_LARGE = "large"


class pygoogle:

def __init__(self,query,pages=10):
self.pages = pages  #Number of pages. default 10
self.query = query
self.filter = FILTER_ON #Controls turning on or off the duplicate 
content filter. On = 1.
self.rsz = RSZ_LARGE#Results per page. small = 4 /large = 8
self.safe = SAFE_OFF#SafeBrowsing -  active/moderate/off
self.lr = 'lang_en' # Default searches to english

def __search__(self,print_results = False):
results = []
for page in range(0,self.pages):
rsz = 8
if self.rsz == RSZ_SMALL:
rsz = 4
args = {'q' : self.query,
'v' : '1.0',
'start' : page*rsz,
'rsz': self.rsz,
'safe' : self.safe, 
'filter' : self.filter,
'lr': self.lr
}
q = urllib.urlencode(args)
search_results = urllib.urlopen(URL+q)
data = json.loads(search_results.read())
if print_results:
if data['responseStatus'] == 200:
for result in  data['responseData']['results']:
if result:
print 
'[%s]'%(urllib.unquote(result['titleNoFormatting']))
print 
result['content'].strip("...").replace("",'').replace("",'').replace("'","'").strip()
print urllib.unquote(result['unescapedUrl'])+'\n'   
 
results.append(data)
return results

def search(self):
"""Returns a dict of Title/URLs"""
results = {}
for data in self.__search__():
for result in  data['responseData']['results']:
if result:
title = urllib.unquote(result['titleNoFormatting'])
results[title] = urllib.unquote(result['unescapedUrl

Re: Is it possible to use Google's advanced search options?

2010-11-20 Thread Stefan Sonnenberg-Carstens

Am 20.11.2010 10:37, schrieb Petar Milin:

Hello!
Can anyone help me with getting number of hits from Google, but with 
restricton on domain (e.g., .edu) and language (e.g., lang_de)? I have 
tried with the Pygoogle (from: http://code.google.com/p/pygoogle/ 
): 


from pygoogle import pygoogle
word = u'something'
request_word = word.encode('utf-8')
request = ('%s+site:.edu' % request_word)
g = pygoogle(request)
g.get_result_count()


Now, I realized that domain restriction works, but language does not, 
since it cannot be specified in request like domain in example above.
Please, help! Is this possible with the Pygoogle? If not, how can I 
make that happen?


Thanks!
PM



See below

word = u'something'
request_word = word.encode('utf-8')
request = ('%s site:.edu options lang:de' % request_word)
g = pygoogle(request)
g.get_result_count()
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try to use unicode

2010-11-19 Thread Stefan Sonnenberg-Carstens

Am 20.11.2010 06:53, schrieb Mikael B:

Hi.

I'm learning python. python 2.6.6 on ubuntu 10.10 I'm swedish so I try 
to use

unicode to get swedish characters. I've checked wikipedia.
utf-8 is said to be an unicode encoding..

this is the test program:

# -*- coding: utf-8 -*-

import readline

s=raw_input(u'Månadslön:')

and this is the output:

Traceback (most recent call last):
  File "test_uni.py", line 5, in 
s=raw_input(u'Månadslön:')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in 
position 1: ordinal not in range(128)


I'm doing something wrong...

Mikael


Your console is not unicode ready.
Please take a look at the locale command and vars like
LC_ALL, LC_LANG etc.
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing the EAX register with Python

2010-11-19 Thread Stefan Sonnenberg-Carstens
Hi,
just read my mail :-)
You can just build an debugger in python yourself.
The script I posted should give you an idea.

Am Fr, 19.11.2010, 08:17 schrieb Tim Roberts:
> dutche  wrote:
>>
>>Hi folks, I have a unusual question here.
>>
>>How can I change the value of EAX register under python under Linux??
>>As paimei does under Windows.
>>
>>My project is to have a python program that loads a C program and sets
>>a breakpoint at some address, and then with this breakpoint I change
>>the EAX register and then continue the program execution.
>>
>>With Windows and paimei I did that, but under Linux I don't know yet.
>
> You will need to find a Linux application equivalent to PaiMei.  Your
> question is not "how can I change EAX", your question is "where can I find
> a Linux debugger that can be controlled from Python?"
>
> I don't know the answer to that.  gdb is quite powerful, and you can
> certainly control it by connecting to its stdin and stdout connections.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
MfG,

Stefan Sonnenberg-Carstens

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


Re: dpkt

2010-11-18 Thread Stefan Sonnenberg-Carstens

Am 19.11.2010 06:39, schrieb Verde Denim:

Hi
I'm looking to find some help working with dpkt. As you probably know, 
there really isn't any documentation, and not being astute at Python 
as of yet leaves me with a lot of gaps.
Is there anyone here that can point me in a direction toward writing 
some test code for parsing gre packets?


Thanks for the input; I appreciate the help.

Jack

There is.
It is inside the code (not the obvious way).

http://www.monkey.org/~dugsong/dpkt/pydoc/public/dpkt-module.html

(May be outdated)
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing the EAX register with Python

2010-11-18 Thread Stefan Sonnenberg-Carstens

Am 18.11.2010 21:20, schrieb dutche:

Hi folks, I have a unusual question here.

How can I change the value of EAX register under python under Linux??
As paimei does under Windows.

My project is to have a python program that loads a C program and sets
a breakpoint at some address, and then with this breakpoint I change
the EAX register and then continue the program execution.

With Windows and paimei I did that, but under Linux I don't know yet.

Any ideas?

Thank you

You asked for it:

from ctypes import *
import time
import os
cdll.LoadLibrary('libc.so.6')
libc = CDLL('libc.so.6')

PTRACE_TRACEME = 0
PTRACE_GETREGS = 12
PTRACE_SETREGS = 13
PTRACE_SYSCALL = 24

SYS_WRITE =  4
SYS_IOCTL = 54

class user_regs_struct(Structure):
_fields_ = [
('ebx',c_ulong),
('ecx',c_ulong),
('edx',c_ulong),
('esi',c_ulong),
('edi',c_ulong),
('ebp',c_ulong),
('eax',c_ulong),
('xds',c_ulong),
('xes',c_ulong),
('xfs',c_ulong),
('xgs',c_ulong),
('orig_eax',c_ulong),
('eip',c_ulong),
('xcs',c_ulong),
('eflags',c_ulong),
('esp',c_ulong),
('xss',c_ulong),
]

child = os.fork()
if child == 0:
libc.ptrace(PTRACE_TRACEME,0,None,None)
os.execl('/bin/ls','ls')
else:
while True:
pid,status = os.wait()
if status != 0:
reg = pointer(user_regs_struct())
libc.ptrace(PTRACE_GETREGS,pid,None,reg)
if reg.contents.orig_eax == SYS_IOCTL:
print 'IOCTL ebx,ecx = 0x%0x,0x%0x' % 
(reg.contents.ebx,reg.contents.ecx)

# replace IOCTL with SYS_WRITE
reg.contents.orig_eax = SYS_WRITE
libc.ptrace(PTRACE_SETREGS,pid,None,reg)
libc.ptrace(PTRACE_SYSCALL,pid,None,None)
else:
os._exit(0)


from ctypes import *
import time
import os
cdll.LoadLibrary('libc.so.6')
libc = CDLL('libc.so.6')

PTRACE_TRACEME = 0
PTRACE_GETREGS = 12
PTRACE_SETREGS = 13
PTRACE_SYSCALL = 24

SYS_WRITE =  4
SYS_IOCTL = 54

class user_regs_struct(Structure):
_fields_ = [
('ebx',c_ulong),
('ecx',c_ulong),
('edx',c_ulong),
('esi',c_ulong),
('edi',c_ulong),
('ebp',c_ulong),
('eax',c_ulong),
('xds',c_ulong),
('xes',c_ulong),
('xfs',c_ulong),
('xgs',c_ulong),
('orig_eax',c_ulong),
('eip',c_ulong),
('xcs',c_ulong),
('eflags',c_ulong),
('esp',c_ulong),
('xss',c_ulong),
]

child = os.fork()
if child == 0:
libc.ptrace(PTRACE_TRACEME,0,None,None)
os.execl('/bin/ls','ls')
else:
while True:
pid,status = os.wait()
if status != 0:
reg = pointer(user_regs_struct())
libc.ptrace(PTRACE_GETREGS,pid,None,reg)
if reg.contents.orig_eax == SYS_IOCTL:
print 'IOCTL ebx,ecx = 0x%0x,0x%0x' % 
(reg.contents.ebx,reg.contents.ecx)
# replace IOCTL with SYS_WRITE
reg.contents.orig_eax = SYS_WRITE 
libc.ptrace(PTRACE_SETREGS,pid,None,reg)
libc.ptrace(PTRACE_SYSCALL,pid,None,None)
else:
os._exit(0)<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I have a question about JavaFit

2010-11-18 Thread Stefan Sonnenberg-Carstens

Am 18.11.2010 20:17, schrieb Ben James:

On 18/11/2010 10:05, Chris Rebert wrote:

"We" are a technical discussion maillinglist. About a computer
programming language. Named Python.
An unrelated computer programming language which happens to be named
"Java" also exists, but is not this mailinglist's topic of discussion.
Some computer programmers personally consume a lot of coffee, but
aside from that, as a group, this maillinglist's members have nothing
to do with "Javalution Coffee Company" or their "JavaFit" coffee
products.
To put it simply, you're barking up entirely the wrong tree.
How exactly were you led to believe that python-list@python.org had
anything to do with JavaFit in the first place?

Regards,
Chris, random maillinglist subscriber

On Tue, Nov 16, 2010 at 4:45 PM, Bill Fishbaugher
  wrote:

Hello,

I was searching online to find more info about JavaFit and I came 
across your information.


Can you tell me, are you still involved with JavaFit? If you are, 
how are things going for you?


Please let me know.

Sincerely,
Bill


Is a JavaFit what you have when you drink waay too much coffee?

Java FIT : Java Framework for integrated testing
http://fit.c2.com/
Last update to site Oct 2007

There seems (ed) to be a python port. Status unknown.
I guess what bill is looking for is test driven development.
This thingy seems really strange.

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


Re: what's the precision of fractions.Fraction?

2010-11-18 Thread Stefan Sonnenberg-Carstens

Am 18.11.2010 17:27, schrieb Daniel Fetchinson:

I do a recursive evaluation of an expression involving fractions and
unsurprisingly the numerator and denominator grows pretty quickly.
After 10-20 iterations the number of digits in the numerator and
denominator (as integers) reaches 80-100. And I'm wondering until what
point I can trust the result since I'm using fractions.Fraction for
the whole procedure. Are Fraction's infinite precision? Or will I get
some sort of an exception if python is not able to represent the
numerator and/or denominator as integers?

http://www.python.org/dev/peps/pep-3141/ was not very helpful in this
regard nor was http://docs.python.org/library/fractions.html

Any ideas?

Cheers,
Daniel


I'm not sure what you really want to know.
Fractions are, by their math definition, of unlimited precision.
For example, PI/2 is accurate (with endless precision as PI is a 
trancendent number),

but 3.1415926/2 (the decimal representation) is not.
So is fraction.Fraction(1,3) of unlimited precision, thus giving this:

from fractions import Fraction
>>> one_tenth=Fraction(1,10)
>>> a =  one_tenth+one_tenth
>>> a
Fraction(1, 5)
>>> a =  one_tenth+one_tenth+one_tenth
>>> a
Fraction(3, 10)

now compare to standard floats:
>>> one_tenth=1.0/10
>>> a =  one_tenth+one_tenth
>>> a
0.2
>>> a =  one_tenth+one_tenth+one_tenth
>>> a
0.30004
(this error is caused by the internal representation of floats as binary 
numbers, see No. 14 in Python tut).



I think you really want to know, how large numerator/denominator can be.
As python has bignum support (type=long), the only limit is the one your 
virtual memory system

offers, which is almost always approx. the amount of ram.

If you need it really *precise*, cast your Fractions into type Decimal:

>>> from decimal import Decimal
>>> one_tenth=Fraction(1,10)
>>> three_tenth = one_tenth*3
>>> d = Decimal(three_tenth.numerator)/Decimal(three_tenth.denominator)
>>> d
Decimal('0.3')

The operator '/' (__div__) is properly over-written.

You can even tweak the precision getcontext().prec by setting it to a 
higher value.


Cheers

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


Re: Is a 32-bit build faster than a 64-bit build

2010-11-12 Thread Stefan Sonnenberg-Carstens

Am 12.11.2010 22:24, schrieb Raymond Hettinger:

My hypotheses is that for apps not needing the 64-bit address space,
the 32-bit version has better memory utilization and hence better
cache performance.  If so, then switching python versions may enable a
single server to handle a greater traffic load.  Has anyone here tried
that?
In most cases (all?) I have seen did the performance not differ when 
compared

between running python scripts in 32 or 64 bit env.
But that is not true for compiled code.
Just take your script and do some benchmarking.

In germany we say: probieren geht über studieren.
Don't study - try it.

An ex-post explanation will show at hand, I promise :-)
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: importing site fails - why?

2010-11-12 Thread Stefan Sonnenberg-Carstens

Am 12.11.2010 20:24, schrieb Helmut Jarausch:

On Fri, 12 Nov 2010 19:42:46 +0100, Stefan Sonnenberg-Carstens wrote:


Am 12.11.2010 19:32, schrieb Helmut Jarausch:

Hi,

as often before, I've cloned a working system (GenToo) onto another
machine. There, from a livecd and chroot to the cloned root partition

python -v
import site

fails with the following error
Python 2.6.6 (r266:84292, Oct 13 2010, 09:06:24) [GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib64/python2.6/lib-dynload/readline.so", 2); import
readline # dynamically loaded from /usr/lib64/python2.6/lib-
dynload/readline.so

import site

# /usr/lib64/python2.6/site.pyc matches /usr/lib64/python2.6/site.py
import site # precompiled from /usr/lib64/python2.6/site.pyc Traceback
(most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/site.py", line 526, in
  main()
File "/usr/lib64/python2.6/site.py", line 509, in main
  known_paths = addsitepackages(known_paths)
File "/usr/lib64/python2.6/site.py", line 289, in addsitepackages
  addsitedir(sitedir, known_paths)
File "/usr/lib64/python2.6/site.py", line 185, in addsitedir
  addpackage(sitedir, name, known_paths)
File "/usr/lib64/python2.6/site.py", line 159, in addpackage
  if not dircase in known_paths and os.path.exists(dir):
File "/usr/lib64/python2.6/genericpath.py", line 18, in exists
  st = os.stat(path)
TypeError: stat() argument 1 must be encoded string without NULL bytes,
not str

​

I'm puzzled and I'd appreciate any hints to the reason for this
failure.

Many thanks,
Helmut.

You gave the answer yourself.
Chrooting does not automatically mean your SO cache is where it should
be for the target system.
Perhaps your livecd is newer/older than the target system or simply the
python in live environment
is not (most likely) compiled as the one in the target system.


It has been working dozens of time!
Are you saying, an  ldconfig  would rectify the situation?

Unfortunately, I can only test it on monday.

Thanks,
Helmut.





It works as long as the so files are close enough in the way they were 
built,

the symbols they expose and the versions they have.
calling ldconfig _can_ help, but this is not for sure.
You can of course try to call

ldd /usr/bin/python (or where it lives)
and
ldd /usr/lib64/python2.6/lib-dynload/readline.so

to see if there is something abviously wrong.

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


Re: Supporting Python 2.x and 3.x on a PC

2010-11-12 Thread Stefan Sonnenberg-Carstens

Am 12.11.2010 20:05, schrieb Doug Stell:

I support multiple projects, some of which will remain on Python 2.x
and some of which want to use Python 3.1.2. While I have installed
both on my Windows PC, only the last installed version can be used. I
do not have admin rights on the machine, so altering registry settings
is not an option. Any guidance on how I can select between the
versions?

Thanks

"

only the last installed version can be used"

What does that mean ?
I have several versions installed, and switch between them only by taking
the desired interpreter.
C:\Python26\python.exe gives a 2.6 python
C:\Python27\python.exe gives a 2.7 python

If I only start python on windows shell, the latter is run.
If I just do this:
SET PATH=C:\Python26;%PATH%
entering just "python" on the cmd prompt gives me a 2.6 python.

Hope this helps.

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


Re: importing site fails - why?

2010-11-12 Thread Stefan Sonnenberg-Carstens

Am 12.11.2010 19:32, schrieb Helmut Jarausch:

Hi,

as often before, I've cloned a working system (GenToo) onto another
machine. There, from a livecd and chroot to the cloned root partition

python -v
import site

fails with the following error
Python 2.6.6 (r266:84292, Oct 13 2010, 09:06:24)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib64/python2.6/lib-dynload/readline.so", 2);
import readline # dynamically loaded from /usr/lib64/python2.6/lib-
dynload/readline.so

import site

# /usr/lib64/python2.6/site.pyc matches /usr/lib64/python2.6/site.py
import site # precompiled from /usr/lib64/python2.6/site.pyc
Traceback (most recent call last):
   File "", line 1, in
   File "/usr/lib64/python2.6/site.py", line 526, in
 main()
   File "/usr/lib64/python2.6/site.py", line 509, in main
 known_paths = addsitepackages(known_paths)
   File "/usr/lib64/python2.6/site.py", line 289, in addsitepackages
 addsitedir(sitedir, known_paths)
   File "/usr/lib64/python2.6/site.py", line 185, in addsitedir
 addpackage(sitedir, name, known_paths)
   File "/usr/lib64/python2.6/site.py", line 159, in addpackage
 if not dircase in known_paths and os.path.exists(dir):
   File "/usr/lib64/python2.6/genericpath.py", line 18, in exists
 st = os.stat(path)
TypeError: stat() argument 1 must be encoded string without NULL bytes,
not str

​

I'm puzzled and I'd appreciate any hints to the reason for this failure.

Many thanks,
Helmut.

You gave the answer yourself.
Chrooting does not automatically mean your SO cache is where it should 
be for the target system.
Perhaps your livecd is newer/older than the target system or simply the 
python in live environment

is not (most likely) compiled as the one in the target system.

That is _one_ reason I stoped using gentoo and switched to debian/ubuntu.
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: returning results from function

2010-11-11 Thread Stefan Sonnenberg-Carstens

Am 11.11.2010 22:16, schrieb Neil Berg:

Hi Python community,

In a main script, I pass the year (yr), month (mo), day (dy) and hour(hr) into the utc_to_local 
function (pasted below) which converts that date and time into local standard time.  I am passing 
several dates and times into this function and would like to work with the "returned" 
loc_y/m/d/h values, but no output is visibly seen when I run the main script.  I want the 
"return" command to just return the values to me in the main script so I can work with 
them!  Any help is appreciated.

Thanks in advance,
Neil

_
Mac OS X 10.6.4
Python 2.6
_

from datetime import datetime
from pytz import timezone
import pytz

def utc_to_local(yr,mo,dy,hr):
 fmt = '%Y-%m-%d %H %Z'
 utc = pytz.utc
 local = pytz.timezone("US/Pacific")
 utc_dt = datetime(yr,mo,dy,hr, tzinfo=utc)
 loc_dt = utc_dt.astimezone(local) # local date YY-MM-DD HR TZ
 #print utc_dt.strftime(fmt), " = ", loc_dt.strftime(fmt)

 loc_y = loc_dt.year # local year
 loc_m = loc_dt.month # local month
 loc_d = loc_dt.day # local day
 loc_h = loc_dt.hour # local hour

 return loc_y, loc_m, loc_d, loc_h

Could you please be so nice and share the relevant parts of your main 
script with us ?
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I skip over multiple words in a file?

2010-11-11 Thread Stefan Sonnenberg-Carstens

Am 11.11.2010 21:33, schrieb Paul Watson:

On 2010-11-11 08:07, chad wrote:

Let's say that I have an article. What I want to do is read in this
file and have the program skip over ever instance of the words "the",
"and",  "or", and "but". What would be the general strategy for
attacking a problem like this?


I realize that you may need or want to do this in Python.  This would 
be trivial in an awk script.

There are several ways to do this.

skip = ('and','or','but')
all=[]
[[all.append(w) for w in l.split() if w not in skip] for l in 
open('some.txt').readlines()]

print all

If some.txt contains your original question, it returns this:
["Let's", 'say', 'that', 'I', 'have', 'an', 'article.', 'What', 'I', 
'want', 'to
', 'do', 'is', 'read', 'in', 'this', 'file', 'have', 'the', 'program', 
'skip', '
over', 'ever', 'instance', 'of', 'the', 'words', '"the",', '"and",', 
'"or",', '"
but".', 'What', 'would', 'be', 'the', 'general', 'strategy', 'for', 
'attacking',

 'a', 'problem', 'like', 'this?']

But this _one_ way to get there.
Faster solutions could be based on a regex:
import re
skip = ('and','or','but')
all = re.compile('(\w+)')
print [w for w in all.findall(open('some.txt').read()) if w not in skip]

this gives this result (you loose some punctuation etc):
['Let', 's', 'say', 'that', 'I', 'have', 'an', 'article', 'What', 'I', 
'want', '
to', 'do', 'is', 'read', 'in', 'this', 'file', 'have', 'the', 'program', 
'skip',
 'over', 'ever', 'instance', 'of', 'the', 'words', 'the', 'What', 
'would', 'be',
 'the', 'general', 'strategy', 'for', 'attacking', 'a', 'problem', 
'like', 'this

']

But there are some many ways to do it ...

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


Re: Building a Python app with Mozilla

2007-07-05 Thread Stefan Sonnenberg-Carstens
On Do, 5.07.2007, 03:45, greg wrote:
> [EMAIL PROTECTED] wrote:
>
>> wxWidgets will give you native looking apps on both Linux and Windows
>
> Well, maybe. There's more to getting a native feel than
> just using the right widgets. I once saw a Qt-based app on
> MacOSX that had tiny little buttons that were too small
> for the text they contained -- Aqua buttons just don't
> scale down like that. :-(
>
> --
> Greg
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
Qt based buttons don't either.
If some fool uses absolute positioning and sizing,
then it is not the toolkit to blame.
I'm using wxPython for instance and use the sizers and placeholders
there, so this is possible with Qt also.
So, perhaps (which I don't know) Aqua buttons simply don't permit
absolute stuff ?
Or the Aqua programmers read the user interface guidelines carefully ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Making decimal default datatype for floats during runtime

2007-06-21 Thread Stefan Sonnenberg-Carstens
Hi,
I was wondering if there are any tricks around to
do some sort of changing types, float in particular.
I do many operations like summing etc on lots
of floats and always have to do some extra checks where
results are heading 0.0, like round(n,10) for example.
My idea was to tell python in some way not to take the type
float but decimal in an implicit fassion.
It that possible ?

And, is it still true for python 2.4/2.5 that one needs to do
a = Decimal(str(aFloat))
instead of
a = Decimal(aFloat)
as it is in python 2.3 ?

And, at least, has some tried to link (lib)python against tc_malloc
and benchmarked it ?

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


Re: How can I know the name of "caller"

2007-06-19 Thread Stefan Sonnenberg-Carstens
billiejoex schrieb:
> Hi there,
> unfortunately, I'm compelled to apply a sort of monkey patching at the
> code of an existing libreary that I can't modify directly.
> Here's my question
> Having such code:
>
> class test:
>
> def caller(self):
> self.b()
>
> def called(self):
> pass
>
> ...(if it is possible) how can I get, from method "called", the name
> of function/method that called it (in this case "caller")?
>
> Thanks in advance
>
>   
inspect.stack is your friend ;-)

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


Re: labeled break/continue

2007-06-19 Thread Stefan Sonnenberg-Carstens
Matt Chisholm schrieb:
> On Mon, 18 Jun 2007 05:28, faulkner <[EMAIL PROTECTED]> wrote:
>   
>> On Jun 18, 12:35 am, Matt Chisholm <[EMAIL PROTECTED]> wrote:
>> 
>>> Hi.  I was wondering if there had ever been an official decision on
>>> the idea of adding labeled break and continue functionality to Python.
>>>   
> 
>   
>> python-dev just mentioned that the BDFL vetoed it a while ago.
>> they're writing a PEP just to document why it was vetoed.
>>
>> 
>
> Really?  I can't find anything in the python-dev archives, or in the
> list of PEPs.  Does anyone know the PEP number (if one has been
> assigned)?  Or maybe someone could send me a link to the discussion or
> forward the discussion to me off list?  That would be much
> appreciated.
>
> -matt
>   
What should a labled break/continue be good for ?
Sounds like goto to me, which is also almost useless in almost every case.
And to me, a solution are local functions, or properly defined if elif
else stmts.
A local function with no params and/or returns is as good as labeled
thingys.
What python really need is a switch stmt ;-) (Even perl6 will have one -
if one will ever
have perl6...)


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


Re: subprocess leaves child living

2007-06-05 Thread Stefan Sonnenberg-Carstens
Thomas Dybdahl Ahle schrieb:
> Hi, When I do a small program like
>
> from subprocess import Popen
> popen = Popen(["ping", "google.com"])
> from time import sleep
> sleep(100)
>
> start it and kill it, the ping process lives on.
> Is there a way to ensure that the ping process is always killed when the 
> python process is?
> I can't use atexit, as ping then isn't killed when python is killed "in 
> the hard way"
>   
Calling popen.close() perhaps ?
You basically open a pipe, which spawns a shell and the command is then
started in there.
So, if your program quits, the spawned shell is still alive, only the 
pipe is dead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert a bitmap file to an array ?

2007-06-05 Thread Stefan Sonnenberg-Carstens
stef schrieb:
> hello
>
> I can find all kind of procedures to convert an array to a bitmap
> (wxPython, PIL),
> but I can't find the reverse,
> either
>- convert a bitmap to an array
> or
>   - read a bitmap file to an array
>
> thanks,
> Stef Mientki
>   
Take a look at the "struct" module.
There you fill find pack/unpack, which can do what you need.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: updates in sqlite3 do not work

2007-05-29 Thread Stefan Sonnenberg-Carstens
Did you try a commit() ?

SQLite3 works in autocommit mode by default,
so it would help to see your code.


On Mi, 30.05.2007, 00:30, Chris Fonnesbeck wrote:
> I have a script set up to perform UPDATE commands on an sqlite database
> using the sqlite3 module. Everything appears to run fine (there are no
> error
> messages), except that none of the UPDATE commands appear to have actually
> updated the table. If I run each command on its own in a sqlite session,
> the
> UPDATE command works fine, so it is not a SQL syntax issue. UPDATE simply
> seems not to work. Any idea what the problem might be?
>
> Thanks,
> Chris
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Can python create a dictionary from a list comprehension?

2007-05-27 Thread Stefan Sonnenberg-Carstens
erikcw schrieb:
> Hi,
>
> I'm trying to turn o list of objects into a dictionary using a list
> comprehension.
>
> Something like
>
> entries = {}
>  [entries[int(d.date.strftime('%m'))] = d.id] for d in links]
>
> I keep getting errors when I try to do it.  Is it possible?  Do
> dictionary objects have a method equivalent to [].append?  Maybe a
> lambda?
>
> Thanks for your help!
> Erik
>
>   
normally a dict(whatEver) will do ;-)

Example:

a = [1,2,3,4,5,6,7,8,9,10]

aDict = dict([(x,x+1) for x in a if x%2==0])

print aDict

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


Re: Is PEP-8 a Code or More of a Guideline?

2007-05-26 Thread Stefan Sonnenberg-Carstens
Paul McGuire schrieb:
> I'm starting a new thread for this topic, so as not to hijack the one
> started by Steve Howell's excellent post titled "ten small Python
> programs".
>
> In that thread, there was a suggestion that these examples should
> conform to PEP-8's style recommendations, including use of
> lower_case_with_underscores style for function names.  I raised some
> questions about this suggestion, since I liked the names the way they
> were, but as a result, part of the discussion has drifted into a
> separate track about PEP-8, and naming styles.
>
> I was under the impression that lower_case_with_underscores was a
> dated recommendation, and that recent practice is more inclusive of
> mixedCase style identifiers.  On the contrary, Steven Bethard
> straightened me out, saying that PEP-8 used to accept either style,
> but has been amended to accept only lower_case_with_underscores.
>
> My latest thought on the topic relates back to the Martin v. Löwis
> thread-that-would-not-die requesting feedback about PEP 3131, on
> adding support for non-ASCII identifiers in Py3K.  I posted an out-of-
> curiosity comment asking about how underscore separators mix with
> Unicode identifiers, including the mechanics of actually typing an
> underscore on a non-US keyboard.  At this point, I realized that I was
> taking things too far off-topic, so I decided to start a new thread.
>
> Steve, sorry for taking your thread down a rathole.  I hope we can
> move any further PEP-8-related discussion (if the point merits any) to
> this thread.
>
> -- Paul
>
>   
I prefer mixedCaseStyle, and I think that should be "standard", as this 
style is commonly
used in all "major" languages , for example Java,C++,C#.
It shortens the identifiers but leaves the meaning intact.

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


Re: Writelines() a bit confusing

2007-05-19 Thread Stefan Sonnenberg-Carstens
aiwarrior schrieb:
> If file.WriteLines( seq ) accepts a list and it says it writes lines,
> why does it write the whole list in a single line. Be cause of that
> the reverse of file.writelines(seq) is not file.readlines().
> Are the assumptions i made correct? If yes why is this so?
>
> I find a function called writelines not actualy writing the list in
> lines wierd.
>
> [code]
> something = ['re','ri','ro']
> f.writelines( something )
> something_else = f.readlines()
> [/code]
> In this case the list something_else will be diffrent from something
>
>   
Please see this:
 >>> help(f.writelines)
Help on built-in function writelines:

writelines(...)
writelines(sequence_of_strings) -> None.  Write the strings to the file.
   
Note that newlines are not added.  The sequence can be any iterable 
object
producing strings. This is equivalent to calling write() for each 
string.

so you'd want this:

f.writelines([x+os.linesep for x in strings])

or something similar.

Why ? Ask the originator of this function.
One explanation:
If you do this:

f1 = file('file1')
f2 = file('file2','w')
f2.writelines(f1.readlines())
f1.close() ; f2.close()

all is how it should be.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is wsgi ready for prime time?

2007-05-17 Thread Stefan Sonnenberg-Carstens
Michele Simionato schrieb:
> On May 17, 8:09 pm, Ron Garret <[EMAIL PROTECTED]> wrote:
>   
>> The wsgiref module in Python 2.5 seems to be empty:
>>
>> [EMAIL PROTECTED]:~/Sites/modpy]$ python
>> Python 2.5 (r25:51908, Mar  1 2007, 10:09:05)
>> [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.>>> 
>> import wsgiref
>> 
> dir(wsgiref)
>   
>> ['__builtins__', '__doc__', '__file__', '__name__', '__path__']
>>
>>
>>
>> So... is wsgi considered ready for production use, or is it still on the
>> bleeding edge?  And if the former, which implementation should one use?
>>
>> rg
>> 
>
>
> Try help(wsgiref).
>
> I would say that WSGI (the spec) is ready for production use whereas
> wsgiref
> (the implementation in the standard library) is intended for easy
> development
> and testing purposes, not for industrial strenght deployement. On the
> other hand Zope 3 uses Twisted via WSGI as a business class server,
> and I hear that mod_wsgi is slightly more performant than mod_python,
>   
It is not only _slightly_ faster. It is a beast.
> so those are the first options I would consider. But you could post on
> the WSGI list for more.
>
>Michele Simionato
>
>   
IMHO WSGI is _only_ a new way of talking to webservers, like apache.
It is as low-level as (f)cgi, so don't expect too much support at this 
stage -
indeed a module like the cgi one in the std lib would be nice.
As google uses it (mod_wsgi), I would suspect you can use it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Tkinter Progress Bar

2007-05-11 Thread Stefan Sonnenberg-Carstens
On Fr, 11.05.2007, 08:42, Gurpreet Singh wrote:
> Hi
>
> I am a newbie in Python
>
> I am creating a simple Tkinter based application.
> I have written Tkinter GUI source code and the
> programme logic in the same .py file.
>
> I am searching for a way to implement a simple
> Progress bar for my application.
>
> Are there any simple ways of doin it.
>
>
>
>
>
> Yahoo!
> oneSearch: Finally, mobile search
> that gives answers, not web links.
> http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
Please see this:

http://tkinter.unpythonic.net/wiki/ProgressBar

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


Re: Read binary data from MySQL database

2007-05-10 Thread Stefan Sonnenberg-Carstens
On Do, 10.05.2007, 16:19, Christoph Krammer wrote:
> Hello,
>
> I try to write a python application with wx that shows images from a
> MySQL database. I use the following code to connect and get data when
> some event was triggered:
>
> dbconn = MySQLdb.connect(host="localhost", user="...", passwd="...",
> db="images")
> dbcurs = dbconn.cursor()
> dbcurs.execute("""SELECT imgdata FROM images LIMIT 1""")
> imgstring = dbcurs.fetchone()[0]
> frame.showImage(imgstring)
>
> Within my frame, the following method is defined:
>
> def showImage(self, imgstring):
>   imgdata = StringIO.StringIO()
>   imgdata.write(imgstring)
Use
imgdata.write(imgstring.tostring())
or
imgstring.tofile(imgdata)
>   print imgdata.getvalue()
>   wx.ImageFromStream(imgdata, wx.BITMAP_TYPE_GIF)
>   panel = wx.Panel(self, -1)
>   self.panel = panel
>
> But this does not work. The converter says that the data is not valid
> GIF. When I print the content of imgstring after the database select
> statement, it contains something like this:
>
> array('c', 'GIF89aL\x01=\x01\x85\x00\x00\x00\x00\x00\xff\xff\xff
> \x00\xff\xff\xff[...]\x00\x00;')
>
> When I try to print imgstring[1], the result is "I". So I don't quite
> get what this print result is about and why my input should not be
> valid. The data in the database is correct, I can restore the image
> with tools like the MySQL Query Browser.
>
> Thanks in advance,
>  Christoph
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

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


Re: Using the CSV module

2007-05-09 Thread Stefan Sonnenberg-Carstens
Most of the time I found the CSV module not as useful as it might be -
due to the restrictions you describe.

Why not write a simple parser class ?

On Mi, 9.05.2007, 10:40, Nathan Harmston wrote:
> Hi,
>
> I ve been playing with the CSV module for parsing a few files. A row
> in a file looks like this:
>
> some_id\t|\tsome_data\t|t\some_more_data\t|\tlast_data\t\n
>
> so the lineterminator is \t\n and the delimiter is \t|\t, however when
> I subclass Dialect and try to set delimiter is "\t|\t" it says
> delimiter can only be a character.
>
> I know its an easy fix to just do .strip("\t") on the output I get,
> but I was wondering
> a) if theres a better way of doing this when the file is actually
> being parsed by the csv module
> b) Why are delimiters only allowed to be one character in length.
>
> Many Thanks in advance
> Nathan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

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


Re: problem with quoted strings while inserting into varchar field of database.

2007-05-07 Thread Stefan Sonnenberg-Carstens
On Mo, 7.05.2007, 16:50, Carsten Haese wrote:
> On Mon, 2007-05-07 at 07:26 -0700, Daniele Varrazzo wrote:
>> > >> >> > cur.execute("INSERT INTO datatable (data) VALUES (%s);",
>> > >> >> > (pickled_data,))
>>
>> > %s is not a placeholder IMHO.
>>
>> > What happens when using %s is, that the string given will be inserted
>> where
>> > %s is; that is something python does as with every print or such.
>>
>> It is indeed. The behavior you describe would be true if i had used
>> the "%" operator. Read better what i have written: There is no "%"
>> operator.
>
> This confusion is precisely why I think the (py)format paramstyles
> should be deprecated in a future version of the DB-API spec. Question
> marks make it immediately obvious that something other than string
> formatting is happening, and if I'm not mistaken, question marks are SQL
> standard.
>
> --
> Carsten Haese
> http://informixdb.sourceforge.net
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
At least, qmark style is well known to people working with
prepared stmts etc.
They look "natural" - and avoid (even my!) mistakes.
On python-forum.de there was a discussion regarding inserting
data into a sqlite db recently. If I remember correctly the guy
was using the "%s" % data approach (yes, % operator) and failed.
The pysqlite driver did the right thing using the qmark style.
Even in the "Python phrasebook" there are examples of this ugly style.

A deprecation warning for now would be fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with quoted strings while inserting into varchar field of database.

2007-05-07 Thread Stefan Sonnenberg-Carstens
On Mo, 7.05.2007, 16:26, Daniele Varrazzo wrote:
>> >> >> > cur.execute("INSERT INTO datatable (data) VALUES (%s);",
>> >> >> > (pickled_data,))
>
>> %s is not a placeholder IMHO.
>
>> What happens when using %s is, that the string given will be inserted
>> where
>> %s is; that is something python does as with every print or such.
>
> It is indeed. The behavior you describe would be true if i had used
> the "%" operator. Read better what i have written: There is no "%"
> operator.
>
> cur.execute() receives 2 parameters: a SQL string with placeholders
> and a tuple with values: it's not me mangling values into the SQL
> string. This is the driver responsibility and it has the chance
> because it receives SQL and values as two distinct parameters. The
> driver can ask the SQL string to contain placeholders either in qmark
> "?" or in format "%s" style, but there is no functional difference.
> Notice that the placeholder is always "%s" and not "%d" or "%f" for
> integers or float: there is always an escaping phase converting each
> python object into a properly encoded string and then the placeholders
> are replaced with the value. This happens into the execute()
> machinery.
>
>> By using the qmark style, it is up the the implementation of the
>> cursor.execute method to decide what to do. python itself, and it's
>> string
>> implementation, don't know anything to do with the qmark.
>> So, IMHO it *makes* a difference:
>> with %s the execute function sees a string and nothing more as the
>> parameters are consumed away by the % substitution.
>> with ?, the execute implementation must do it's best, it gets a string
>> and
>> a list/tuple with values.
>
> Again, this would be true for "cur.execute(sql % data)": what i wrote
> is "cur.execute(sql, data)".
>
> -- Daniele
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
Ashes on my head.

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


Re: problem with quoted strings while inserting into varchar field of database.

2007-05-07 Thread Stefan Sonnenberg-Carstens
On Mo, 7.05.2007, 11:32, Daniele Varrazzo wrote:
> On 7 Mag, 10:46, "Stefan Sonnenberg-Carstens"
> <[EMAIL PROTECTED]> wrote:
>> On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
>>
>> > On 7 Mag, 08:55, "krishnakant Mane" <[EMAIL PROTECTED]> wrote:
>> >> On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
>> >> <[EMAIL PROTECTED]> >> Every serious database driver has a
>> >> complete and solid SQL escaping
>> >> > mechanism. This mechanism tipically involves putting placeholders
>> in
>> >> > your SQL strings and passing python data in a separate tuple or
>> >> > dictionary. Kinda
>>
>> >> > cur.execute("INSERT INTO datatable (data) VALUES (%s);",
>> >> > (pickled_data,))
>>
>> >> I will try doing that once I get back to the lab.
>> >> mean while I forgot to mention in my previous email that I use
>> MySQLdb
>> >> for python-mysql connection.
>>
>> Why not use qmark parameter passing (PEP 249) ?
>>
>> cur.execute("INSERT INTO datatable (data) VALUES (?);" ,
>> (pickled_data,))
>>
>> Then the DB driver will take care for you.
>
>>>> import MySQLdb
>>>> print MySQLdb.paramstyle
> format
>
> MySQLdb (as many other drivers) use format parameter passing. Not much
> difference w.r.t. qmark, at least when passing positional parameters:
> the placeholder is "%s" instead of "?". A difference is that "format"
> also allows named parameters (actually it should have been "pyformat",
> but IIRC MySQLdb can also use named placeholders, even if they
> advertise "format").
>
> Anyway it is only a matter of placeholder style: they both allow the
> driver to take care of data escaping, the concept the OT didn't know
> about.
>
> -- Daniele
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
%s is not a placeholder IMHO.
What happens when using %s is, that the string given will be inserted where
%s is; that is something python does as with every print or such.
By using the qmark style, it is up the the implementation of the
cursor.execute method to decide what to do. python itself, and it's string
implementation, don't know anything to do with the qmark.
So, IMHO it *makes* a difference:
with %s the execute function sees a string and nothing more as the
parameters are consumed away by the % substitution.
with ?, the execute implementation must do it's best, it gets a string and
a list/tuple with values.

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


Re: problem with quoted strings while inserting into varchar field of database.

2007-05-07 Thread Stefan Sonnenberg-Carstens
On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
> On 7 Mag, 08:55, "krishnakant Mane" <[EMAIL PROTECTED]> wrote:
>> On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
>> <[EMAIL PROTECTED]> >> Every serious database driver has a
>> complete and solid SQL escaping
>> > mechanism. This mechanism tipically involves putting placeholders in
>> > your SQL strings and passing python data in a separate tuple or
>> > dictionary. Kinda
>>
>> > cur.execute("INSERT INTO datatable (data) VALUES (%s);",
>> > (pickled_data,))
>>
>> I will try doing that once I get back to the lab.
>> mean while I forgot to mention in my previous email that I use MySQLdb
>> for python-mysql connection.
>
> OK: MySQLdb implements the escaping mechanism i described. You can
> find the documentation if you look for it harder.
>
>> I did not find any such reference to storing pickled objects in the API.
>
> Storing pickled object is not different from storing anything else
> into BLOB. You would have faced the same problem if you had to write
> "O'Reilly" in a VARCHAR field.
>
> -- Daniele
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
Why not use qmark parameter passing (PEP 249) ?

cur.execute("INSERT INTO datatable (data) VALUES (?);" , (pickled_data,))

Then the DB driver will take care for you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem to Download ftp file

2007-05-06 Thread Stefan Sonnenberg-Carstens
Perhaps a problem with name resolution ?

On Mo, 7.05.2007, 07:27, Shakil Ahmed wrote:
> hi
> Actually i need to know that how can i download a ftp file from ncbi by
> using python module ftputil.
> please help me.
>
> Thanks
> regards,
> Shakil
>
> import ftputil
>
> host = ftputil.FTPHost('ftp.ncbi.nih.gov/repository/OMIM/morbidmap',
> 'anonymous', 'password')
>
>
>
>
>
> The Error message is:
>
> raise FTPOSError(ftp_error)
>
> FTPOSError: (11001, 'getaddrinfo failed')
>
> Debugging info: ftputil 2.2.2, Python 2.4.3 (win32)
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Re: import conflict

2007-05-06 Thread Stefan Sonnenberg-Carstens
[EMAIL PROTECTED] schrieb:
> Hello,
>
> I have a problem where I need to set up two separate Python projects
> that each live under the same package.  Once they are distributed,
> they will live under the same filesystem path, but during development,
> they are separated.
>
> For example:
>proj1/lib/pkg/foo/mod1.py
>proj2/lib/pkg/bar/mod2.py
>
> Furthermore, proj1 is dependent on proj2, so I want to be able to say
> things like this, from within proj1:
>
> import pkg.foo.mod1
> import pkg.bar.mod2
>
> Of course this doesn't work, even with a PYTHONPATH configured to see
> both projects, because it will find 'pkg' in proj1/lib and so pkg.bar
> will be hidden from view.
>
> Any suggestions?
>
> Thanks!
>
>   
Hi,
my only suggestion would be to overthink your project organization.
You can surely solve that problem with symlinks, but if they depend
on another, perhaps the structure is not optimal.
If you use python 2.5 you can try absolute imports (which I personally 
find not so well):

from __future__ import absolute_import

See here: http://python.mirrors-r-us.net/dev/peps/pep-0328/

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


Re: import conflict

2007-05-06 Thread Stefan Sonnenberg-Carstens
[EMAIL PROTECTED] schrieb:
> Hello,
>
> I have a problem where I need to set up two separate Python projects
> that each live under the same package.  Once they are distributed,
> they will live under the same filesystem path, but during development,
> they are separated.
>
> For example:
>proj1/lib/pkg/foo/mod1.py
>proj2/lib/pkg/bar/mod2.py
>
> Furthermore, proj1 is dependent on proj2, so I want to be able to say
> things like this, from within proj1:
>
> import pkg.foo.mod1
> import pkg.bar.mod2
>
> Of course this doesn't work, even with a PYTHONPATH configured to see
> both projects, because it will find 'pkg' in proj1/lib and so pkg.bar
> will be hidden from view.
>
> Any suggestions?
>
> Thanks!
>
>   
Hi,
my only suggestion would be to overthink your project organization.
You can surely solve that problem with symlinks, but if they depend
on another, perhaps the structure is not optimal.
If you use python 2.5 you can try absolute imports (which I personally 
find not so well):

from __future__ import absolute_import

See here: http://python.mirrors-r-us.net/dev/peps/pep-0328/

Cheers,
Stefan

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


Re: invoke user's standard mail client

2007-05-06 Thread Stefan Sonnenberg-Carstens
Stefan Sonnenberg-Carstens schrieb:
> Gabriel Genellina schrieb:
>   
>> En Fri, 04 May 2007 05:07:44 -0300, [EMAIL PROTECTED]  
>> <[EMAIL PROTECTED]> escribió:
>>
>>   
>> 
>>> the simplest way to launch the user's standard mail client from a
>>> Python program is by creating a mailto: URL and launching the
>>> webbrowser:
>>> But this method is limited: you cannot specify a file to be attached
>>> to the mail. And I guess that there would be problems if the body text
>>> is too complex.
>>> Does somebody know about a better method?
>>> It should be possible at least on Windows, since Acrobat Reader is
>>> able to do it.
>>> 
>>>   
>> On Windows you can use MAPI.
>>
>>   
>> 
> import win32api
> win32api.ShellExecute(0,'open','mailto:',None,None,0)
>   
For completeness

import win32api
win32api.ShellExecute(0,'open','mailto: [EMAIL PROTECTED]',None,None,0)

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


Re: Python Binding

2007-05-06 Thread Stefan Sonnenberg-Carstens
Stefan Behnel schrieb:
> Georg Grabler wrote:
>   
>> There's a C library which i'd like to have python bindings for. I havn't
>> known anything before about how to write python bindings for a C library.
>>
>> I succeeded now by using distutils to write the first bindings for functions
>> and similar.
>>
>> Now, it seems as something is blocking my brain. For the library, i
>> need "custom" types, so types defined in this library (structures),
>> including pointers and similar.
>>
>> I've been thinking about what i will need to represent this lists in python.
>> I thought about creating an external python object, providing "information"
>> i get from the list in C structures which can be converted.
>>
>> Basically, it are list of packages, which have several attributes (next,
>> prev, etc). But i don't know how to supply a proper list from the binding /
>> object written in C.
>>
>> Any suggestions or hints about this?
>> 
>
> Use Pyrex. It's a Python-like language that was designed to write extension
> modules in C and it's great for writing wrappers.
>
> http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
>
> An example wrapper is described here:
>
> http://ldots.org/pyrex-guide/
>
> Hope it helps,
> Stefan
>   
Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward.
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Binding

2007-05-06 Thread Stefan Sonnenberg-Carstens
Stefan Behnel schrieb:
> Georg Grabler wrote:
>   
>> There's a C library which i'd like to have python bindings for. I havn't
>> known anything before about how to write python bindings for a C library.
>>
>> I succeeded now by using distutils to write the first bindings for functions
>> and similar.
>>
>> Now, it seems as something is blocking my brain. For the library, i
>> need "custom" types, so types defined in this library (structures),
>> including pointers and similar.
>>
>> I've been thinking about what i will need to represent this lists in python.
>> I thought about creating an external python object, providing "information"
>> i get from the list in C structures which can be converted.
>>
>> Basically, it are list of packages, which have several attributes (next,
>> prev, etc). But i don't know how to supply a proper list from the binding /
>> object written in C.
>>
>> Any suggestions or hints about this?
>> 
>
> Use Pyrex. It's a Python-like language that was designed to write extension
> modules in C and it's great for writing wrappers.
>
> http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
>
> An example wrapper is described here:
>
> http://ldots.org/pyrex-guide/
>
> Hope it helps,
> Stefan
>   

Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward.
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: invoke user's standard mail client

2007-05-06 Thread Stefan Sonnenberg-Carstens
Gabriel Genellina schrieb:
> En Fri, 04 May 2007 05:07:44 -0300, [EMAIL PROTECTED]  
> <[EMAIL PROTECTED]> escribió:
>
>   
>> the simplest way to launch the user's standard mail client from a
>> Python program is by creating a mailto: URL and launching the
>> webbrowser:
>> But this method is limited: you cannot specify a file to be attached
>> to the mail. And I guess that there would be problems if the body text
>> is too complex.
>> Does somebody know about a better method?
>> It should be possible at least on Windows, since Acrobat Reader is
>> able to do it.
>> 
>
> On Windows you can use MAPI.
>
>   
import win32api
win32api.ShellExecute(0,'open','mailto:',None,None,0)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newstyle classes and __getattribute__

2005-10-28 Thread Stefan Sonnenberg-Carstens
Stefan Sonnenberg-Carstens schrieb:
> James Stroud schrieb:
> 
>> On Friday 28 October 2005 14:26, Stefan Sonnenberg-Carstens wrote:
>>
>>> Hi there,
>>
>>
>> [..clip..]
>>
>>> Now, I do this:
>>>
>>> class T(object):
>>> def __init__(self,name='',port=80):
>>> self.name=name
>>> self.port=port
>>> def __getattribute__(self,key):
>>> if key=='somekey':
>>> return None
>>
>>
>> [..snip..]
>>
>>> But, then surprise:
>>> >>> t = T(name="test123",port=443)
>>> >>> dir(t)
>>>
>>> []
>>>
>>> What the hell is going wrong here ?
>>
>>
>>
>> __getattribute__ is returning None in all cases and dir() is 
>> converting None to [].
>>
>> Anyway, you should have done this:
>>
>> py> class T(object):
>>  def __init__(self,name='',port=80):
>>  self.name=name
>>  def __getattribute__(self,key):
>>  if key=='somekey':
>>  return None
>>  else:
>>    return object.__getattribute__(self, key)
>> 
>> py> t = T(name="test123",port=443)
>> py> dir(t)
>> ['__class__', '__delattr__', '__dict__', '__doc__', 
>> '__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
>> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
>> '__weakref__', 'name']
>>
>> James
>>
> Sorry, but I am right that you explicitly call a "super" 
> __getattribute__ on object and pass it a reference to self and the
> desired key ?
> Only asking for clarification ...
> 
> But why does that work under 2.4.1, and even under ActiveState's 2.4.1 ?
> Was that changed between those 2 releases ?
> Intuitive behaviour of __getattribute__ would be:
> If a key is not handeld in that function, return what you already got.
> 
> Cheers,
> Stefan
> 
Sorry,forget that.
It never worked in those releases,
I made some errors which made me belief that, sorry.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newstyle classes and __getattribute__

2005-10-28 Thread Stefan Sonnenberg-Carstens
James Stroud schrieb:
> On Friday 28 October 2005 14:26, Stefan Sonnenberg-Carstens wrote:
> 
>>Hi there,
> 
> [..clip..]
> 
>>Now, I do this:
>>
>>class T(object):
>>  def __init__(self,name='',port=80):
>>  self.name=name
>>  self.port=port
>>  def __getattribute__(self,key):
>>  if key=='somekey':
>>  return None
> 
> [..snip..]
> 
>>But, then surprise:
>> >>> t = T(name="test123",port=443)
>> >>> dir(t)
>>
>>[]
>>
>>What the hell is going wrong here ?
> 
> 
> __getattribute__ is returning None in all cases and dir() is converting None 
> to [].
> 
> Anyway, you should have done this:
> 
> py> class T(object):
>  def __init__(self,name='',port=80):
>  self.name=name
>  def __getattribute__(self,key):
>  if key=='somekey':
>  return None
>  else:
>    return object.__getattribute__(self, key)
> 
> py> t = T(name="test123",port=443)
> py> dir(t)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
> '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'name']
> 
> James
> 
Sorry, but I am right that you explicitly call a "super" 
__getattribute__ on object and pass it a reference to self and the
desired key ?
Only asking for clarification ...

But why does that work under 2.4.1, and even under ActiveState's 2.4.1 ?
Was that changed between those 2 releases ?
Intuitive behaviour of __getattribute__ would be:
If a key is not handeld in that function, return what you already got.

Cheers,
Stefan

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


Re: newstyle classes and __getattribute__

2005-10-28 Thread Stefan Sonnenberg-Carstens
Stefan Sonnenberg-Carstens schrieb:
> Hi there,
> I'm facing some strange things - but maybe only me is strange - anyway...
> 
> i wrote the following code:
> 
> 
> +++
> class T(object):
> def __init__(self,name='',port=80):
> self.name=name
> self.port=port
> +++
> 
> looks fine - to me.
> 
> Now I can do:
> 
> 
>  >>> t = T(name="test",port=8080)
>  >>> print t
> <__main__.T object at 0x00BE36D0>
> 
>  >>> print t.name
> test
>  >>> dir(t)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
> '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 
> 'name', 'port']
> 
> Everything ok.
> 
> Now, I do this:
> 
> class T(object):
> def __init__(self,name='',port=80):
> self.name=name
> self.port=port
> def __getattribute__(self,key):
> if key=='somekey':
> return None
> 
> where
> 
>  >>> dir(T)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
> '__hash__', '__init__', '__module__', '__new__', '__reduce__', 
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
> 
> is ok, also.
> 
> But, then surprise:
>  >>> t = T(name="test123",port=443)
>  >>> dir(t)
> []
> 
> What the hell is going wrong here ?
> I'm running Python 2.4.2 from python.org on Windows XP SP2, all patches 
> applied.
> 
> Thx in advance.
> 

Ok, to make it worse:
I tried the same under Cygwin with Python 2.4.1 -
it worked as expected.
-- 
http://mail.python.org/mailman/listinfo/python-list


newstyle classes and __getattribute__

2005-10-28 Thread Stefan Sonnenberg-Carstens
Hi there,
I'm facing some strange things - but maybe only me is strange - anyway...

i wrote the following code:


+++
class T(object):
def __init__(self,name='',port=80):
self.name=name
self.port=port
+++

looks fine - to me.

Now I can do:


 >>> t = T(name="test",port=8080)
 >>> print t
<__main__.T object at 0x00BE36D0>

 >>> print t.name
test
 >>> dir(t)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 
'name', 'port']

Everything ok.

Now, I do this:

class T(object):
def __init__(self,name='',port=80):
self.name=name
self.port=port
def __getattribute__(self,key):
if key=='somekey':
return None

where

 >>> dir(T)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

is ok, also.

But, then surprise:
 >>> t = T(name="test123",port=443)
 >>> dir(t)
[]

What the hell is going wrong here ?
I'm running Python 2.4.2 from python.org on Windows XP SP2, all patches 
applied.

Thx in advance.

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