Re: Windows PE and Python 2.6 (Side-by-Side error)

2008-12-17 Thread Werner Merkl

Martin v. Löwis schrieb:

Has anyone an idea?


You should not install "for all users" before copying it,
but "just for me".

Regards,
Martin
This works! Thank you very much! (Sorry for the delay. I had been on a 
conference, than I was ill and than, there were more urgent things...)


But now PyScripter may not use Python 2.6 internally. E. g., if I load a 
python-dll (import bz2) I receise an Runtime ERROR! R6034...



Again, thanx a lot
Werner
--
http://mail.python.org/mailman/listinfo/python-list


Re: eval() and global variables

2008-12-17 Thread Peter Otten
Juan Pablo Romero Méndez wrote:

> Suppose this function is given:
> 
> def f(x,y):
>   return x+y+k
> 
> 
> Is it possible to somehow assign a value to k without resorting to
> making k global?

You can replace the function's global dictionary:

>>> def f(x, y):
... return x+y+k
...
>>> function = type(f)
>>> function(f.func_code, dict(k=1))(2, 3)
6
>>> k
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'k' is not defined

This is a hack, of course.

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


Re: mod_python resources

2008-12-17 Thread Graham Dumpleton
On Dec 17, 11:10 am, Дамјан Георгиевски  wrote:
> > I'm trying again because I'm stubborn. Maybe the fourth time will be
> > the charm...
>
> > Are there any good tutorials out there for setting up Apache with
> > mod_python?
>
> mod_python is depreceated, nobody uses it. usemod_wsgihttp://www.modwsgi.org/

The mod_python package is not deprecated, although it could be said to
be sleeping at the moment. You'll also probably still find that more
new people choose mod_python over mod_wsgi. This is because it has the
more obvious name to look for when Googling. It also has publisher and
PSP high level handler which are still attractive to many as they are
more lightweight and easier to get into than the large WSGI
frameworks. Finally, the Django folks still recommend in their
documentation to use mod_python.

Anyway, if wanting to host a WSGI capable application, using mod_wsgi
would be the more obvious choice. If wanting to write your own
framework, or work at low level, basing it on WSGI rather than
mod_python specific APIs would certainly be a better long term
direction to take.

Graham

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


Re: help I'm getting delimited

2008-12-17 Thread John Machin
On Dec 17, 9:39 pm, aka  wrote:
> Due to being in a hurry I didn't paste correctly (sorry).
>
> The intention is to put values of column 1 ("id") in the roles list,
> therefore appending within the loop, to fill a session var.
>
> The complete code is:

It's *not* complete. It's missing "import csv".

>
> roles = []
> inp = 'C:/temp/test.csv'
> try:
>     fp = open(inp, 'rb')
>     reader = csv.reader(fp, dialect='excel', delimiter=';')
>     for r in reader:
>         roles.append(r)
>         ## ultimately should be something like r.id or r[0]
>         ## first row of csv file should be skipped because of column
> names
>
> except:
>     msg = 'Something's wrong with the csv.reader'

But you don't print the message! In any case, using the try/except
like that *hides* any useful diagnostic information; it gives only an
indication that something is wrong, but not what is wrong and where it
is wrong.

If you throw away the try/except, you will get a more meaningful
message -- possibly that "csv" is not defined!! -- and the traceback
will tell you in which line the error occured.

> return dict(file=inp,roles=str(roles))

Why do you think that you need (a) that complicated expression (b) the
str() call? Assuming you are intending to make a function out of all
that, what's wrong with returning a (simple) tuple:
   return inp, roles
?

The above 'return' statement is not inside a function/method. You
would have got this message:
SyntaxError: 'return' outside function
People will very soon lose patience with you if you persist in not
posting the actual code that you ran.

> The roles list isn't populated at all :(

This could mean (if the code that was posted is moderately similar to
that which was run) that the error happened before the first time that
roles.append(r) was executed ;-)

Please divulge the contents of test.csv  -- but not if it's huge!
Considering trying to get your code to work first with a data file of
close-to-minimal size and complexity, like this:
8<---
id,other_info
tom,1
dick,2
harry,3
8<---

By the way, you mentioned the UnicodeReader class in your original
post, but you don't seem to use it ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python, XML and XPath

2008-12-17 Thread Diez B. Roggisch
Hole wrote:

> Hi all,
> 
> I hope this is not an "overasked" question but I find myself quite
> confused about python xml management (I have to use python for a
> project and I come from java world, you know...where frameworks,
> libraries and tools to use are standard de iure or standard de facto).



I suggest you use one of the available binary builds for windows:

http://pypi.python.org/pypi/lxml/2.1

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


Re: Selecting a different superclass

2008-12-17 Thread Marco Mariani

psaff...@googlemail.com wrote:


The problem is that IDPointSet and MicroArrayPointSet will need to
inherit from PointSet or TraceablePointSet based on whether I'm
handling traceable points or not. Can I select a superclass
conditionally like this in Python? Am I trying to do something really
evil here?

Any other bright ideas on my application also welcome.


I think you should investigate something different than subclassing, 
like a "Strategy" domain pattern or something similar.

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


Re: Selecting a different superclass

2008-12-17 Thread Marco Mariani

Marco Mariani wrote:

I think you should investigate something different than subclassing, 
like a "Strategy" domain pattern or something similar.


s/domain/design/

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread r
I think there is a simpler answer to all this(not to take away from
the other ones here though which are all great). When writing
procedural code how would you like it if vars inside functions were
automatically global. Your code with be blowing chunks in no time.
Thats the reason for global declarations and instance.var :)


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


Re: Transferring a file over sockets

2008-12-17 Thread Tim Golden

Ferdinand Sousa wrote:

I am using sockets to transfer a file over LAN. There are 2 scripts, the
server opens a listens for connection and the client is run on another
machine. I always make sure the server is run first. The strange thing is
that if the the server script is double-clicked and executed (run in a
console with title %Python path%python.exe) the output file saved on the
server is truncated. It works just fine if you open the server script in
IDLE and then run it. The client script can be run in either way, it still
works. You could try using any arbitrary file to test this behaviour after
changing the file name in both the scripts.


A couple of things:

* Are you aware that there's no particular guarantee
that what's sent along a socket in one write will
be received in one read? There's a HOWTO here if
you're interested:

http://docs.python.org/dev/howto/sockets.html

* Also, unless this is an exercise for yourself in investigating
how sockets work, (in which case... read the docs :)), you might
consider that you're reinventing the wheel not a little. But
perhaps you knew that?

TJG



==
# file receiver
# work in progress

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '192.168.1.17'
PORT = 31400

s.bind((HOST, PORT))
s.listen(3)
conn, addr = s.accept()
print 'conn at address',addr
conn.send('READY')
f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
fsize=int(conn.recv(8))
print 'File size',fsize
f.write(conn.recv(fsize))
f.close()
conn.close()
s.close()

raw_input('Press any key to exit')


===

# file sender !!!
# Work in progress

import socket, os
from stat import ST_SIZE


HOST = '192.168.1.17'
PORT = 31400  # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST,PORT))
if s.recv(5)!='READY':
raw_input('Unable to connect \n\n Press any key to exit ...')
s.close()
exit()

f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
fsize=os.stat(f.name)[ST_SIZE]

s.send(str(fsize))
s.send(f.read())

s.close()
f.close()

===

Thanks for reading!!

Best regards,
Ferdi





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


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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Richard Brodie

"walterbyrd"  wrote in message 
news:518b9dd9-69c5-4d5b-bd5f-ad567be62...@b38g2000prf.googlegroups.com...

> However in the methods are within a class, the scoping seems to work
> differently.

Not really, self is a formal parameter to the function. It would be
a strange language where a function's own arguments weren't in scope.

>def b(self):
>print self.x

Try changing it to:

def b(somethingotherthanself):
print self.x



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


Re: getting object instead of string from dir()

2008-12-17 Thread Andrew Nelis
On Dec 17, 5:16 pm, Rominsky  wrote:
> I am trying to use dir to generate a list of methods, variables, etc.
> I would like to be able to go through the list and seperate the
> objects by type using the type() command, but the dir command returns
> a list of strings.  When I ask for the type of an element, the answer
> is always string.  How do I point at the variables themselves.  A
> quick example is:
>
> a = 5
> b = 2.0
> c = 'c'
>
> lst = dir()
>
> for el in lst:
>     print type(el)
>
> Right now I am understandably getting all types being output as
> strings, how do i get the type of the actual objects returned from dir
> ()?

The builtin functions "locals" and "globals" will both return a
dictionary whose keys are the variable names and values are the items
corresponding to those keys;

>>> locals()['b']
2.0
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting object instead of string from dir()

2008-12-17 Thread david wright


--- On Wed, 12/17/08, Rominsky  wrote:

From: Rominsky 
Subject: getting object instead of string from dir()
To: python-list@python.org
Date: Wednesday, December 17, 2008, 12:16 PM

I am trying to use dir to generate a list of methods, variables, etc.
I would like to be able to go through the list and seperate the
objects by type using the type() command, but the dir command returns
a list of strings.  When I ask for the type of an element, the answer
is always string.  How do I point at the variables themselves.  A
quick example is:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
    print type(el)

Right now I am understandably getting all types being output as
strings, how do i get the type of the actual objects returned from dir
()?
--
http://mail.python.org/mailman/listinfo/python-list

Forgive me if you are well aware of this, just thought i'd chime in with the 
"Duck typing speech":

Pythonic programming style that determines an object's type by inspection of 
its method or attribute signature rather than by explicit relationship to some 
type object ("If it looks like a duck and quacks like a duck, it must be a 
duck.") By emphasizing interfaces rather than specific types, well-designed 
code improves its flexibility by allowing polymorphic substitution. Duck-typing 
avoids tests using type() or isinstance(). Instead, it typically employs the 
EAFP (Easier to Ask Forgiveness than Permission) style of programming.

http://en.wikipedia.org/wiki/Duck_typing



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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread John Machin
On Dec 18, 2:19 am, walterbyrd  wrote:
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant.  Or is there some good reason for this?
>
> Here is what I mean:
>
> def a():
>     x = 99
>     print x
>
> def b():
>     print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
>     def a(self):
>         self.x = 99
>         print self.x
>     def b(self):
>         print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?

Two questions for you:
(1) If you were to change the sloppy inconsistent scoping mechanism in
classes, what would you change it to?
(2) What do you think of the following:
 class ab():
 def a(self):
 self.x = 99
 print self.x
 def b(me):
 print me.x
 ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting object instead of string from dir()

2008-12-17 Thread Christian Heimes
Rominsky schrieb:
> I am trying to use dir to generate a list of methods, variables, etc.
> I would like to be able to go through the list and seperate the
> objects by type using the type() command, but the dir command returns
> a list of strings.  When I ask for the type of an element, the answer
> is always string.  How do I point at the variables themselves.  A
> quick example is:
> 
> a = 5
> b = 2.0
> c = 'c'
> 
> lst = dir()
> 
> for el in lst:
> print type(el)

for name, obj in vars().iteritems():
print name, obj

Christian

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


Re: Python, XML and XPath

2008-12-17 Thread Hole
> > I need to use XML parsing with xpath: a simple xml reader.
>
> You can try lxml, have a look herehttp://codespeak.net/lxml/xpathxslt.html


Thanks a lot to everyone!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 10:00 am, r  wrote:
> When writing
> procedural code how would you like it if vars inside functions were
> automatically global. Your code with be blowing chunks in no time.

That was my point - I consider python's ordinary use of lexical
scoping to be a good thing, and I was wondering why this "good thing"
was not used in classes, as well as outside of classes.

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 9:04 am, rdmur...@bitdance.com wrote:

> Yes.  It's called Object Oriented Programming.

I think you mean it's *Python* Object Oriented Programming. I am not
sure that every other OO language works like that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 10:17 am, "Richard Brodie"  wrote:
> Not really, self is a formal parameter to the function. It would be
> a strange language where a function's own arguments weren't in scope.

Thank you, that makes sense to me.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative imports in Python 3.0

2008-12-17 Thread Benjamin
On Dec 17, 4:01 am, Nicholas  wrote:
> Imagine a module that looks like
>
> ModuleDir
>      __init__.py
>      a.py
>      b.py
>
> In python 2.x I used to have tests at the end of each of my modules,
> so that module b.py might look something like
>
> import a
>  ..
>  ..
>
> if __name__ == '__main__':
>    runtests()
>
> But under Python 3.0 this seems impossible.  For usual use import a.py
> has to become the line:
>
> from . import a
>
> But if I use that form it is no longer possible to run b.py as a
> standalone script without raising an error about using relative
> imports.
>
> I am sure I am not the first to run into this issue, but what is the
> solution?

Use absolute imports:

from ModuleDir import a

>
> Best wishes,
>
> Nicholas

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


Re: help I'm getting delimited

2008-12-17 Thread aka
Hi John, thanks.
You're right, I didn't past the method header because I thought it
didn't matter when the input filename is hardcoded.
The try/except isn't very helpful indeed so I commented it out.
You're right I wrongly referred to the UnicodeReader
class in my first post because that's ultimately where I want to go
so
I outcommented it here for you to see.
The fact is that neither csv.reader nor the UnicodeReader will read
the file, while writing with the UnicodeWriter
works like a charm.
That's why I put str() around roles to see any content.
I simplified the csv-file by cutting off columns without result. The
file looks now like:

id;company;department
12;Cadillac;Research
11;Ford;Accounting
10;Chrysler;Sales


The dictionary on the return is because this code is part of my
TurboGears application.
The entire method is:


import csv
from utilities.urw   import UnicodeWriter, UnicodeReader


@expose(allow_json=True)
def import_roles(self, input=None, *args, **kwargs):
inp = 'C:/temp/test.csv'
roles = []
msg = ''
## try:
fp = open(inp, 'rb')
reader = csv.reader(fp, dialect='excel', delimiter=';')
## reader = UnicodeReader(fp, dialect='excel', delimiter=';')
for r in reader:
roles.append(r[0])
fp.close()
## except:
## msg = "Something's wrong with the csv.reader"
return dict(filepath=inp,
roles=str(roles),
msg=msg)


csv.reader results in: for r in reader: Error: line contains NULL
byte


Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
can't decode byte 0xff in position 0: unexpected code byte


Will post only complete code from now on thanks.

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


Re: help I'm getting delimited

2008-12-17 Thread aka
Hi John, thanks.
You're right, I didn't past the method header because I thought it
didn't matter when the input filename is hardcoded.
The try/except isn't very helpful indeed so I commented it out.
You're right I wrongly referred to the UnicodeReader
class in my first post because that's ultimately where I want to go so
I outcommented it here for you to see.
The fact is that neither csv.reader nor the UnicodeReader will read
the file, while writing with the UnicodeWriter
works like a charm.
That's why I put str() around roles to see any content.
I simplified the csv-file by cutting off columns without result. The
file looks now like:

id;company;department
12;Cadillac;Research
11;Ford;Accounting
10;Chrysler;Sales

The dictionary on the return is because this code is part of my
TurboGears application.
The entire method is:

import csv
from utilities.urw   import UnicodeWriter, UnicodeReader

@expose(allow_json=True)
def import_roles(self, input=None, *args, **kwargs):
inp = 'C:/temp/test.csv'
roles = []
msg = ''
## try:
fp = open(inp, 'rb')
reader = csv.reader(fp, dialect='excel', delimiter=';')
## reader = UnicodeReader(fp, dialect='excel', delimiter=';')
for r in reader:
roles.append(r[0])
fp.close()
## except:
## msg = "Something's wrong with the csv.reader"
return dict(filepath=inp,
roles=str(roles),
msg=msg)

csv.reader results in: for r in myreader: Error: line contains NULL
byte

Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
can't decode byte 0xff in position 0: unexpected code byte

Will post only complete code from now on thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT: Binary tree logarithms properties

2008-12-17 Thread Terry Reedy

Mr.SpOOn wrote:

Hi,
I'm searching for a clear explanation of binary tree properties,
expecially the ones related to logarithms.

For example, I know that in a tree with 2n-1 nodes, we have log(n)
levels, from 0 to log(n).


A *complete* binary tree with n levels has 2**n - 1 nodes.  This is 
easily shown by induction. (Try Wikipedia for induction proof if you are 
not familiar with such.)



So, if k is the level, the nodes on a level have indexes between 2^k
and 2^(k+1)-1.

For k=0 we have 2 and 3.
For k=1 we have 4, 5, 6, 7
and so on.


Nodes only have single number indexes if you arrange them linearly. 
Then the index depends on how you arrange them, whether you start the 
array indexes with 0 or 1, and whether you start the level numbers with 
0 or 1.  Call the breadth-first sequence bf.  Then the 1-based slice for 
1-first level k is bf[2**(k-1):2**k)].  Again, proof by induction.


Terry Jan Reedy

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


Re: The rule of literal string

2008-12-17 Thread Li Han
Thank you, Chris! You have made me learn more about Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: help I'm getting delimited

2008-12-17 Thread aka
Due to being in a hurry I didn't paste correctly (sorry).
The intention is to parse a csv file and (ultimately) put values of
column 1 ("id") in a list (so I need to append in the loop) that will
be used to fill a session var.
The complete code is:

roles = []
inp = 'C:/temp/test.csv'
try:
fp = open(inp, 'rb')
reader = csv.reader(fp, dialect='excel', delimiter=';')
for r in reader:
roles.append(r)
## ultimately should be something like r.id
## first row of csv file should be skipped because of column
names
or r[0]
except:
msg = 'Something's wrong with the csv.reader'
return dict(file=inp,roles=str(roles))


The roles list isn't populated at all :(
--
http://mail.python.org/mailman/listinfo/python-list


Re: help I'm getting delimited

2008-12-17 Thread aka
Due to being in a hurry I didn't paste correctly (sorry).

The intention is to put values of column 1 ("id") in the roles list,
therefore appending within the loop, to fill a session var.

The complete code is:

roles = []
inp = 'C:/temp/test.csv'
try:
fp = open(inp, 'rb')
reader = csv.reader(fp, dialect='excel', delimiter=';')
for r in reader:
roles.append(r)
## ultimately should be something like r.id or r[0]
## first row of csv file should be skipped because of column
names

except:
msg = 'Something's wrong with the csv.reader'
return dict(file=inp,roles=str(roles))


The roles list isn't populated at all :(
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative imports in Python 3.0

2008-12-17 Thread Brian Allen Vanderburg II

nicholas.c...@gmail.com wrote:

Imagine a module that looks like

ModuleDir
 __init__.py
 a.py
 b.py


In python 2.x I used to have tests at the end of each of my modules,
so that module b.py might look something like

import a
 ..
 ..

if __name__ == '__main__':
   runtests()

But under Python 3.0 this seems impossible.  For usual use import a.py
has to become the line:

from . import a

But if I use that form it is no longer possible to run b.py as a
standalone script without raising an error about using relative
imports.

I am sure I am not the first to run into this issue, but what is the
solution?

Best wishes,

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

Sorry for the duplicate, sent to wrong email.

Python 3 (and I think 2.6) now use absolute import when using a 'import 
blah' statement.


if ('.' in __name__) or hasattr(globals, '__path__'):
  from . import a
else:
  import a

If '__name__' has a'.' then it is either a package or a module in a 
package, in which case relative imports can be used.  If it does not 
have a '.' it may still be a package but the '__init__.py' file, in 
which case the module has a '__path__' attribute, so relative imports 
can be used.  Otherwise it is not a package or in a package so absolute 
imports must used.  Also, since it is not in a package it is assumed 
that it is top module (__main__) or possible module imported from the 
top that is not in a package, such as a.py doing an 'import b', b would 
be a module but not a package so still probably need absolute imports, 
my guess anyway.


But I also think that 'from . import a' would be nice if it would work 
from non-packages as well, meaning just 'import a' if it is a non-package.


Brian A. Vanderburg II
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread r
On Dec 17, 12:20 pm, walterbyrd  wrote:
> On Dec 17, 10:00 am, r  wrote:
>
> > When writing
> > procedural code how would you like it if vars inside functions were
> > automatically global. Your code with be blowing chunks in no time.
>
> That was my point - I consider python's ordinary use of lexical
> scoping to be a good thing, and I was wondering why this "good thing"
> was not used in classes, as well as outside of classes.

The whole point for even writing a class is for shared attributes.
Write procedural code if you don't like classes. When ever you see
self.var think of it as Class.instance(var)... makes total sense to
me? Obliviously you have not done much procedural coding or you would
know why this HAS to be. sorry if i sound rude.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 8:41 am, prueba...@latinmail.com wrote:

> If scoping worked as you want, how, pray tell, would you define object
> attributes?- Hide quoted text -

I suppose you could do this:

class className():
varname = "whatever"
def fname(self, varname):
   . . . .

Instead of having variable defined within methods to be global
everywhere within the class.

Anyway, it's not a matter of what I like, I am just trying to
understand the reason behind the scoping rules.

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


Relative imports in Python 3.0

2008-12-17 Thread Nicholas
Imagine a module that looks like

ModuleDir
 __init__.py
 a.py
 b.py


In python 2.x I used to have tests at the end of each of my modules,
so that module b.py might look something like

import a
 ..
 ..

if __name__ == '__main__':
   runtests()

But under Python 3.0 this seems impossible.  For usual use import a.py
has to become the line:

from . import a

But if I use that form it is no longer possible to run b.py as a
standalone script without raising an error about using relative
imports.

I am sure I am not the first to run into this issue, but what is the
solution?

Best wishes,

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


Re: Python, XML and XPath

2008-12-17 Thread Diez B. Roggisch
Diez B. Roggisch wrote:

> Hole wrote:
> 
>> Hi all,
>> 
>> I hope this is not an "overasked" question but I find myself quite
>> confused about python xml management (I have to use python for a
>> project and I come from java world, you know...where frameworks,
>> libraries and tools to use are standard de iure or standard de facto).
> 
> 
> 
> I suggest you use one of the available binary builds for windows:
> 
> http://pypi.python.org/pypi/lxml/2.1

Most probably better:

http://pypi.python.org/pypi/lxml/2.1.3

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


Re: Managing timing in Python calls

2008-12-17 Thread Ross
Interesting stuff - I hadn't come across the 'with' syntax before, so 
I've learned something already.


I was briefly excited to learn about the callLater command which is just 
a convenience class for the wxTimer class.   It seems to be pretty much 
a parallel of the

var t = window.setTimeout( function () { do_when_timed_out}

sort of thing in AJAX.

However, as is well grumbled on the 'net, you can't use wxTimer from a 
non-main thread.   So that dropped off my plate.


But getting my head around my AJAX problem versus my python 
implementation, I realized my use of those javascript structures were 
really just used because javascript doesn't allow any threading at all.


With Python, just having my other processing path in a thread is enough, 
and I can use the brutish time.sleep() function, without worrying about 
blocking the processing of my mainline (UI) thread.  So I'm able to proceed.


I do want to know more about the 'with' command tho' so I'll look into that.

Thx again.

Ross.



cmdrrickhun...@yaho.com wrote:

I believe WxTimerEvent is handled using the event queue, which isn't
going to do what you want.  An event which goes through the queue does
not get processed until you return to the queue.

What you want to do is actually a rather difficult task to do
generically.  Should the task be interrupted immediately?  Or is a
tiny latency acceptable?  Should the function being terminated get to
handle its own termination?  Or should the termination be forced on
it.  What sort of overhead is acceptable for this "set_timeout"
behavior?

I would not be surprised if there isn't a built in solution, because
its so hard, but rather built in tools which can be used to do it.

If your timeouts are on the order of seconds, you might be able to
just check time.time() at the begining, and compare it to the current
time later in the function.  This could be on the main thread or on a
worker thread.

If you need better handling, you may want to look at how condition
variables and such work.

Finally, thread has a function to send a Keyboard Interrupt to the
main thread.  I believe you could do your work on the main thread, and
catch the interrupt.

"Background" tasks are not easy to implement in any language (other
than perhaps AJAX ;-) ).

Remember, Python does not support truly simultaneous threads.  It
actually does timeslices of about 100 operations.  Any solution you
choose should work given this information.

And as for a "nicer" construct, I personally just learned of how to
handle the "with" command.  I could see something like

class Timeout:
def __init__(self, t):
self.t = t
def __enter__(self):
self.start = time.time()
def __exit__(self, x, y, z):
return None
def __nonzero__(self):
return time.time() - self.start <= self.t


def doSomethingLong(timeout = True): # true guarentees bailout never
occurs
   while timeout:
   doAnIteration()

with Timeout(3) as t:
doSomethingLong(t)



and have your Timeout class have a flag which it sets when
doSomethingLong needs to bail out, using whatever method is "best" for
your particular application.  This is, of course pseudocode - I've not
run it through python msyself.  Hopefully any errors are obvious
enough that you can work around them.

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


Re: Python is slow

2008-12-17 Thread RadicalEd
On Dec 10, 1:42 pm, cm_gui  wrote:
> http://blog.kowalczyk.info/blog/2008/07/05/why-google-should-sponsor-...
>
> I fully agree with Krzysztof Kowalczyk .
> Can't they build a faster VM for Python since they love the language
> so much?
>
> Python is SLOW.    And I am not comparing it with compiled languages
> like C.
> Python is even slower than PHP!
>
> Just go to any Python website and you will know.
> An example is:http://www2.ljworld.com/
> And this site is created by the creators of Django!
>
> And it is not just this Python site that is slow. There are many many
> Python sites which are very slow. And please don’t say that it could
> be the web hosting or the server which is slow — because when so many
> Python sites are slower than PHP sites, it couldn’t be the web
> hosting.   Also, Zope/Plone is even slower.
>
> Python is slow. Very slow.

I did a DataBase consult with MySQLdb and PHP with 30 rows and who
you think was the better and faster, YES, Python for almost 10
seconds, and I have to configure the php.ini for PHP could show me the
DATA.
He is just a futile troll frustrated with Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.0 crashes displaying Unicode at interactive prompt

2008-12-17 Thread jhermann
Assuming those survived the switch to 3.0, you can use site.py und
sys.displayhook to customize to the old behaviour (i.e. change it to a
version using ascii instead of repr). Since this only affects
interactive use, it's also no problem for portability of code, unlike
"solutions" like forcing the defaultencoding etc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: help I'm getting delimited

2008-12-17 Thread aka
John, this is the actual code I ran in TurboGears which is a Python
framework.
I should have left away the import statements. Trust me, the problem
isn't in there because the UnicodeWriter is functioning perfectly.
I did allready sanitate the csv file to these four lines in Notepad so
there isn't anything more than this:

id;company;department
12;Cadillac;Research
11;Ford;Accounting
10;Chrysler;Sales

The only possible problematic lines are marked # here:

> >     def import_roles(self, input=None, *args, **kwargs):
> >         inp = 'C:/temp/test.csv'
> >         roles = []
> >         msg = ''
> >         ## try:
> >         fp = open(inp, 'rb') #
> >         reader = csv.reader(fp, dialect='excel', delimiter=';') #
> >         ## reader = UnicodeReader(fp, dialect='excel', delimiter=';') #
> >         for r in reader:
> >             roles.append(r[0]) #
> >         fp.close()
> >         ## except:
> >             ## msg = "Something's wrong with the csv.reader"
> >         return dict(filepath=inp,
> >                     roles=str(roles),
> >                     msg=msg)

Yeah rdmur, I'll have a look at the Python commandline.
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting object instead of string from dir()

2008-12-17 Thread Rominsky
On Dec 17, 10:59 am, Christian Heimes  wrote:
> Rominsky schrieb:
>
> > I am trying to use dir to generate a list of methods, variables, etc.
> > I would like to be able to go through the list and seperate the
> > objects by type using the type() command, but the dir command returns
> > a list of strings.  When I ask for the type of an element, the answer
> > is always string.  How do I point at the variables themselves.  A
> > quick example is:
>
> > a = 5
> > b = 2.0
> > c = 'c'
>
> > lst = dir()
>
> > for el in lst:
> >     print type(el)
>
> for name, obj in vars().iteritems():
>     print name, obj
>
> Christian

I do have some understanding of the pythonic methodology of
programming, though by far I still don't consider myself an expert.
The problem at hand is that I am coming from a matlab world and trying
to drag my coworkers with me.  I have gotten a lot of them excited
about using python for this work, but the biggest gripe everytime is
they want their matlab ide.  I am trying to experiment with making
similar pieces of the ide, in particular I am working on the workspace
window which lists all the current variables in the namespace, along
with their type, size, value, etc  I am trying to create a python
equivalent.  I can get dir to list all the variables names in a list
of strings, but I am trying to get more info them.  hence the desire
to do a type command on them.  I like the locals and globals commands,
but I am still trying to get more info.  I have started using the eval
command with the strings, which is working, but I am curious if there
is a better or more elegant way of getting the info.  The eval example
would be something like:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
   print el + '\t' + str(eval('type(%s)'%el))

It works, now I am curious if there is a better way.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Transferring a file over sockets

2008-12-17 Thread Hendrik van Rooyen

Ferdinand Sousa  wrote:

>==
.# file receiver
># work in progress
>
>import socket
>
>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>HOST = '192.168.1.17'
>PORT = 31400
>
>s.bind((HOST, PORT))
>s.listen(3)
>conn, addr = s.accept()
>print 'conn at address',addr
>conn.send('READY')
>f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
>fsize=int(conn.recv(8))
>print 'File size',fsize
>f.write(conn.recv(fsize))

This recv is not guaranteed to actually receive the number of
characters you are asking for - it will stop when it has received that
many, yes, but it may return with less, or even none if there is a 
time out set.

The TCP is a more or less featureless stream of characters.

Consider including a start marker so you know where the lesson starts.
If you do this, consider the possibility of having a "false sync".
Sending/receiving  the length is good - also google for "netstring"
Google for "escaping".

Consider writing a loop to receive until the required length has been
received - look at the docs for the recv function - it can tell you how 
much has been received.

>f.close()
>conn.close()
>s.close()
>
>raw_input('Press any key to exit')
>
>
>===
>
># file sender !!!
># Work in progress
>
>import socket, os
>from stat import ST_SIZE
>
>
>HOST = '192.168.1.17'
>PORT = 31400  # Arbitrary non-privileged port
>
>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
>s.connect((HOST,PORT))
>if s.recv(5)!='READY':
>raw_input('Unable to connect \n\n Press any key to exit ...')
>s.close()
>exit()
>
>f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
>fsize=os.stat(f.name)[ST_SIZE]
>
>s.send(str(fsize))
>s.send(f.read())

Are you sure that the send will send all the chars
that you ask it to send?

If yes - why do you think this?

What are the values that the send can return?

>
>s.close()
>f.close()
>
>===


hth - Hendrik


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


Re: Transferring a file over sockets

2008-12-17 Thread MRAB

Hendrik van Rooyen wrote:

Ferdinand Sousa  wrote:


==

.# file receiver

# work in progress

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '192.168.1.17'
PORT = 31400

s.bind((HOST, PORT))
s.listen(3)
conn, addr = s.accept()
print 'conn at address',addr
conn.send('READY')
f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
fsize=int(conn.recv(8))
print 'File size',fsize
f.write(conn.recv(fsize))


This recv is not guaranteed to actually receive the number of
characters you are asking for - it will stop when it has received that
many, yes, but it may return with less, or even none if there is a 
time out set.


The TCP is a more or less featureless stream of characters.

Consider including a start marker so you know where the lesson starts.
If you do this, consider the possibility of having a "false sync".
Sending/receiving  the length is good - also google for "netstring"
Google for "escaping".

Consider writing a loop to receive until the required length has been
received - look at the docs for the recv function - it can tell you how 
much has been received.



f.close()
conn.close()
s.close()

raw_input('Press any key to exit')


===

# file sender !!!
# Work in progress

import socket, os
from stat import ST_SIZE
   
HOST = '192.168.1.17'

PORT = 31400  # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST,PORT))
if s.recv(5)!='READY':
   raw_input('Unable to connect \n\n Press any key to exit ...')
   s.close()
   exit()

f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
fsize=os.stat(f.name)[ST_SIZE]

s.send(str(fsize))
s.send(f.read())


Are you sure that the send will send all the chars
that you ask it to send?

If yes - why do you think this?

What are the values that the send can return?


s.close()
f.close()

===


You're also sending the length as a bytestring str(fsize), which will 
have an unknown length, but you're receiving the length as exactly 8 
bytes. Either mark the end of the length in some way (with '\n'?) and 
look for that when receiving, or pad what you send to exactly 8 bytes 
(str(fsize).zfill(8)).

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


[Help] The pywinauto Can't select the MDI's menu using the MenuItems() which return [].

2008-12-17 Thread 为爱而生
I can't use the MenuItems() in my MDI application.
Any example is very nice Thanks a lot!

-- 
"OpenBookProject"-开放图书计划邮件列表
详情: http://groups.google.com/group/OpenBookProject
维基: http://wiki.woodpecker.org.cn/
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting object instead of string from dir()

2008-12-17 Thread Jean-Paul Calderone

On Wed, 17 Dec 2008 11:52:17 -0800 (PST), Rominsky  
wrote:

On Dec 17, 10:59 am, Christian Heimes  wrote:

Rominsky schrieb:

> I am trying to use dir to generate a list of methods, variables, etc.
> I would like to be able to go through the list and seperate the
> objects by type using the type() command, but the dir command returns
> a list of strings.  When I ask for the type of an element, the answer
> is always string.  How do I point at the variables themselves.  A
> quick example is:

> a = 5
> b = 2.0
> c = 'c'

> lst = dir()

> for el in lst:
>     print type(el)

for name, obj in vars().iteritems():
print name, obj

Christian


I do have some understanding of the pythonic methodology of
programming, though by far I still don't consider myself an expert.
The problem at hand is that I am coming from a matlab world and trying
to drag my coworkers with me.  I have gotten a lot of them excited
about using python for this work, but the biggest gripe everytime is
they want their matlab ide.  I am trying to experiment with making
similar pieces of the ide, in particular I am working on the workspace
window which lists all the current variables in the namespace, along
with their type, size, value, etc  I am trying to create a python
equivalent.  I can get dir to list all the variables names in a list
of strings, but I am trying to get more info them.  hence the desire
to do a type command on them.  I like the locals and globals commands,
but I am still trying to get more info.  I have started using the eval
command with the strings, which is working, but I am curious if there
is a better or more elegant way of getting the info.  The eval example
would be something like:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
  print el + '\t' + str(eval('type(%s)'%el))

It works, now I am curious if there is a better way.



What about this:


for name, obj in vars().iteritems():
print name, obj

Christian


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


[ANN] EuroPython 2009 – Call for Partic ipation!

2008-12-17 Thread Martin P. Hellwig
On behalf of the EuroPython 2009 organisation it is my privilege and 
honour to announce the 'Call for Participation' for EuroPython 2009!
EuroPython is the conference for the communities around Python, 
including the Django, Zope and Plone communities.
This year's conference will be held in Birmingham, UK from Monday 29th 
June to Saturday 4th July 2009.


Talks & Themes
Do you have something you wish to present at EuroPython? Go to 
http://www.europython.eu/talks/cfp/  for this year's themes and 
submissions criteria, the deadline is on 5th April 2009.


Other Talks, Activities and Events
Have you got something which does not fit the above? Visit 
http://www.europython.eu/talks/ .


Help Us Out
We could use a hand any contribution is welcome, please take a look at 
http://www.europython.eu/contact/ .


Sponsors
An unique opportunity to affiliate with the prestigious EuroPython 
conference!

http://www.europython.eu/sponsors/

Spread the Word
Improve our publicity by distributing this announcement in your corner 
of the community, please coordinate this with the organizers: 
http://www.europython.eu/contact/


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


Looking forward to see you!

The EuroPython Team

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


Re: mysql hash generator in python

2008-12-17 Thread News123
Hi Matias.


Could you show us the 'class implementation'?


Whether class or not the result should be the same.
If it isn't, then there should be a small error.
If we have both code versions to look at it's easier to help you.


bye


N


Matías Hernández wrote:
> (sorry for my english, but i'm speak spanish)
> Hi list.. this is my first post... and obviously if for help..
> 
> I try to implement the password function of mysql in a python script.
> 
> I read that the password function of mysql was implemented with a double
>  sha1()
> 
> I python i try this:
> example1:
> if __name__=="__main__":
>   s = hashlib.sha1()
>   s.update('test')
>   s2 = hashlib.sha1()
>   s2.update(s.digest()
>   print s2.hexdigest()
> 
> This script show me the same hash that password function of mysql, but
> when i implement this in a class the hash is different??? why???
> 
> And if is posible??? can you show a example code of old_password
> function of mysql in python???
> 
> THNX
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help improving number guessing game

2008-12-17 Thread Arnaud Delobelle
"D'Arcy J.M. Cain"  writes:

> On Tue, 16 Dec 2008 13:59:24 -0800
> Scott David Daniels  wrote:
>>> > def yesno(s):
>> >s = s.strip().lower()
>> >if not s in ("y", "n"):

There was a thread about "is not" recently.  Python also allows "not in".

if s not in ("y", "n"):

> You could also do this to be a little more user friendly:
>   if not (s and s[0] in ("y", "n")):

Or:
if s[:1] not in ("y", n"):

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


Why no lexical scoping for a method within a class?

2008-12-17 Thread rdmurray
Quoth walterbyrd :
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant.  Or is there some good reason for this?

Yes.  It's called Object Oriented Programming.

> Here is what I mean:
> 
> def a():
> x = 99
> print x
> 
> def b():
> print x
> 
> a()
> b() # raises an exception because x is not defined.
> 
> However in the methods are within a class, the scoping seems to work
> differently.
> 
> class ab():
> def a(self):
> self.x = 99
> print self.x
> def b(self):
> print self.x
> 
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?

Because x is an attribute.

If you don't understand what that means, read any introductory
article on OOP.

To give you a clue, if you had said:

class ab():
def a(self):
x = 99
print x
def b(self):
print x

You'd have gotten the exception you expected (assuming x wasn't
defined globally).

--RDM

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


Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
For a language as well structured as Python, this seems somewhat
sloppy, and inconsistant.  Or is there some good reason for this?

Here is what I mean:

def a():
x = 99
print x

def b():
print x

a()
b() # raises an exception because x is not defined.

However in the methods are within a class, the scoping seems to work
differently.

class ab():
def a(self):
self.x = 99
print self.x
def b(self):
print self.x

i = ab()
i.a()
i.b() # this works, why no lexical scoping?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python, XML and XPath

2008-12-17 Thread Enrico
"Hole"  ha scritto nel messaggio
news:daf5cd00-36dc-4ab4-976e-a6d859b52...@w24g2000prd.googlegroups.com...
> Hi all,
>
> I hope this is not an "overasked" question but I find myself quite
> confused about python xml management (I have to use python for a
> project and I come from java world, you know...where frameworks,
> libraries and tools to use are standard de iure or standard de facto).
>
> I need to use XML parsing with xpath: a simple xml reader.

You can try lxml, have a look here
http://codespeak.net/lxml/xpathxslt.html

>
> I've found that the standard distribution of python doesn't include
> xpath support modules so I've decided to add libxml2.
>
> I've installed cygwin c compiler but I still have errors like the
> following:
>
> C:\Python25\Lib\libxml2-python-2.6.9>python setup.py build install

I never used  libxml2 but version 2.6.9 seems quite old, according to this
page:
http://users.skynet.be/sbi/libxml-python/

Bye, Enrico



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


Re: Selecting a different superclass

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 6:41 AM, psaff...@googlemail.com
 wrote:
> This might be a pure OO question, but I'm doing it in Python so I'll
> ask here.
>

>
> The problem is that IDPointSet and MicroArrayPointSet will need to
> inherit from PointSet or TraceablePointSet based on whether I'm
> handling traceable points or not. Can I select a superclass
> conditionally like this in Python? Am I trying to do something really
> evil here?

You're doing something really dynamic; don't think I'd call it
inherently evil though.
All you have to do is use a variable in the inheritance syntax, it's
really quite simple:

superclass = TraceablePointSet if tracing else PointSet

class IDPointSet(superclass):
#body code here

#use same trick for MicroArrayPointSet

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex problem ..

2008-12-17 Thread Steve Holden
Analog Kid wrote:
> Hi guys:
> Thanks for your responses. Points taken. Basically, I am looking for a
> combination of the following ...
> [^\w] and %(?!20) ... How do I do this in a single RE?
> 
> Thanks for all you help.
> Regards,
> AK
> 
> On Mon, Dec 15, 2008 at 10:54 PM, Steve Holden  > wrote:
> 
> Analog Kid wrote:
> > Hi All:
> > I am new to regular expressions in general, and not just re in python.
> > So, apologies if you find my question stupid :) I need some help with
> > forming a regex. Here is my scenario ...
> > I have strings coming in from a list, each of which I want to check
> > against a regular expression and see whether or not it "qualifies". By
> > that I mean I have a certain set of characters that are
> permissible and
> > if the string has characters which are not permissible, I need to flag
> > that string ... here is a snip ...
> >
> > flagged = list()
> > strs = ['HELLO', 'Hi%20There', '123...@#@']
> > p =  re.compile(r"""[^a-zA-Z0-9]""", re.UNICODE)
> > for s in strs:
> > if len(p.findall(s)) > 0:
> > flagged.append(s)
> >
> > print flagged
> >
> > my question is ... if I wanted to allow '%20' but not '%', how
> would my
> > current regex (r"""[^a-zA-Z0-9]""") be modified?
> >
> The essence of the approach is to observe that each element is a
> sequence of zero or more "character", where character is "either
> letter/digit or escape." So you would use a pattern like
> 
> "([a-zA-Z0-9]|%[0-9a-f][0-9a-f])+"
> 
> 
Did you *try* the above pattern?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Free place to host python files?

2008-12-17 Thread Matt Nordhoff
James Mills wrote:
> On Wed, Dec 17, 2008 at 10:25 AM, Chris Rebert  wrote:
>> I'll plug Bitbucket (http://bitbucket.org/). It gives you 150MB of
>> Mercurial hosting for free, along with a bug tracker and wiki. And I
>> hear it's implemented using Django.
> 
> FreeHG (http://freehg.org) is pretty good too :)
> 
> cheers
> James

I'll throw in  too. No wiki, though.

And , I guess.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Transferring a file over sockets

2008-12-17 Thread Jean-Paul Calderone

On Wed, 17 Dec 2008 17:41:46 +0530, Ferdinand Sousa  
wrote:

I am using sockets to transfer a file over LAN. There are 2 scripts, the
server opens a listens for connection and the client is run on another
machine. I always make sure the server is run first. The strange thing is
that if the the server script is double-clicked and executed (run in a
console with title %Python path%python.exe) the output file saved on the
server is truncated. It works just fine if you open the server script in
IDLE and then run it. The client script can be run in either way, it still
works. You could try using any arbitrary file to test this behaviour after
changing the file name in both the scripts.

==
# file receiver
# work in progress

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '192.168.1.17'
PORT = 31400

s.bind((HOST, PORT))
s.listen(3)
conn, addr = s.accept()
print 'conn at address',addr
conn.send('READY')
f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
fsize=int(conn.recv(8))
print 'File size',fsize
f.write(conn.recv(fsize))
f.close()
conn.close()
s.close()

raw_input('Press any key to exit')


===

# file sender !!!
# Work in progress

import socket, os
from stat import ST_SIZE


HOST = '192.168.1.17'
PORT = 31400  # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST,PORT))
if s.recv(5)!='READY':
   raw_input('Unable to connect \n\n Press any key to exit ...')
   s.close()
   exit()

f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
fsize=os.stat(f.name)[ST_SIZE]

s.send(str(fsize))
s.send(f.read())

s.close()
f.close()

===

Thanks for reading!!


You're not handling your sockets properly.  send() does not necessary
send the entire string you pass to it.  recv(n) does not necessarily
return an n-length string.  Read the socket documentation more carefully
and try fixing those issues.  Or try using Twisted which presents a
somewhat easier API to use.

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


Re: help I'm getting delimited

2008-12-17 Thread rdmurray
Quoth John Machin :
> On Dec 18, 1:28 am, aka  wrote:
> >  @expose(allow_json=True)
> 
> Means what? Does what?
> Does the problem still happen without that?

Means what he's posting is not a standalone script :)

He says it's part of his turbogears ap.  @expose says that this
method is callable by name from a URL, and allow_json means
it can be called with a parameter requesting a json formatted
response instead of html.

> Funny, the indentation changed there --- for the very last time, is
> that the actual code of a standalone script that reproduces the
> problem?

Alex, I would strongly suggest that you move your code out into a
standalone script and debug it there (you'll get more help from this
group if you do, for one thing!).  After you get it working
standalone you can incorporate it back into your Turbogears ap.

--RDM

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread pruebauno
On Dec 17, 10:19 am, walterbyrd  wrote:
> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant.  Or is there some good reason for this?
>
> Here is what I mean:
>
> def a():
>     x = 99
>     print x
>
> def b():
>     print x
>
> a()
> b() # raises an exception because x is not defined.
>
> However in the methods are within a class, the scoping seems to work
> differently.
>
> class ab():
>     def a(self):
>         self.x = 99
>         print self.x
>     def b(self):
>         print self.x
>
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?

If scoping worked as you want, how, pray tell, would you define object
attributes?
--
http://mail.python.org/mailman/listinfo/python-list


Re: help I'm getting delimited

2008-12-17 Thread John Machin
On Dec 18, 1:28 am, aka  wrote:
> Hi John, thanks.
> You're right, I didn't past the method header because I thought it
> didn't matter when the input filename is hardcoded.
> The try/except isn't very helpful indeed so I commented it out.
> You're right I wrongly referred to the UnicodeReader
> class in my first post because that's ultimately where I want to go
> so
> I outcommented it here for you to see.
> The fact is that neither csv.reader nor the UnicodeReader will read
> the file, while writing with the UnicodeWriter
> works like a charm.
> That's why I put str() around roles to see any content.
> I simplified the csv-file by cutting off columns without result. The
> file looks now like:
>
> id;company;department
> 12;Cadillac;Research
> 11;Ford;Accounting
> 10;Chrysler;Sales
>
> The dictionary on the return is because this code is part of my
> TurboGears application.
> The entire method is:
>
> import csv
> from utilities.urw       import UnicodeWriter, UnicodeReader

Pardon my ignorance, but what is "utilities.urw"??

>
>     @expose(allow_json=True)

Means what? Does what?
Does the problem still happen without that?

Funny, the indentation changed there --- for the very last time, is
that the actual code of a standalone script that reproduces the
problem?


>     def import_roles(self, input=None, *args, **kwargs):
>         inp = 'C:/temp/test.csv'
>         roles = []
>         msg = ''
>         ## try:
>         fp = open(inp, 'rb')
>         reader = csv.reader(fp, dialect='excel', delimiter=';')
>         ## reader = UnicodeReader(fp, dialect='excel', delimiter=';')
>         for r in reader:
>             roles.append(r[0])
>         fp.close()
>         ## except:
>             ## msg = "Something's wrong with the csv.reader"
>         return dict(filepath=inp,
>                     roles=str(roles),
>                     msg=msg)
>
> csv.reader results in: for r in reader: Error: line contains NULL
> byte

Looks like the file is stuffed. Have you tried inspecting it with a
tool that would actually show a '\x00' or a '\xff' unambiguously? If
you don't have a fancy one, use the Python interactive prompt:

   open('your_file.csv', 'rb').read()
>
> Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
> can't decode byte 0xff in position 0: unexpected code byte
>
> Will post only complete code from now on thanks.

Just make sure it's runnable and it's what you actually ran thanks.
--
http://mail.python.org/mailman/listinfo/python-list


something else instead of PIL?

2008-12-17 Thread Reimar Bauer
Hi

what has happened to PIL? No updates since two years.

Or does one know an alternative lib for resizing images?

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Diez B. Roggisch
walterbyrd wrote:

> For a language as well structured as Python, this seems somewhat
> sloppy, and inconsistant.  Or is there some good reason for this?
> 
> Here is what I mean:
> 
> def a():
> x = 99
> print x
> 
> def b():
> print x
> 
> a()
> b() # raises an exception because x is not defined.
> 
> However in the methods are within a class, the scoping seems to work
> differently.
> 
> class ab():
> def a(self):
> self.x = 99
> print self.x
> def b(self):
> print self.x
> 
> i = ab()
> i.a()
> i.b() # this works, why no lexical scoping?

Because what you do is to create instance variables. Why do you expect them
not working if you explicitly access them?

The real analog of your example would be this:

class ab():
   def a(self):
   x = 100
   print x

   def b(self):
   print x

which provokes the same error.

however, there *are* different scoping rules, classes don't create a lexical
scope for their own variables:


class foo(object):
x = 100

def a(self):
print x


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


Python, XML and XPath

2008-12-17 Thread Hole
Hi all,

I hope this is not an "overasked" question but I find myself quite
confused about python xml management (I have to use python for a
project and I come from java world, you know...where frameworks,
libraries and tools to use are standard de iure or standard de facto).

I need to use XML parsing with xpath: a simple xml reader.

I've found that the standard distribution of python doesn't include
xpath support modules so I've decided to add libxml2.

I've installed cygwin c compiler but I still have errors like the
following:

C:\Python25\Lib\libxml2-python-2.6.9>python setup.py build install
C:\Python25\lib\distutils\dist.py:247: UserWarning: 'licence'
distribution option is deprecated; use 'license'
  warnings.warn(msg)
running build
running build_py
running build_ext
building 'libxml2mod' extension
writing build\temp.win32-2.5\Release\libxml2mod.def
C:\MinGW\bin\gcc.exe -mno-cygwin -shared -s build
\temp.win32-2.5\Release\libxml2-py.o build\temp.win32-2.5\Release
\libxml.o build\temp.win32-2.5\Release\types.o build
\temp.win32-2.5\Release\libxml2mod
.def -L/usr\lib -LC:\Python25\libs -LC:\Python25\PCBuild -llibxslt -
llibexslt -llibxml2 -lpython25 -lmsvcr71 -o build
\lib.win32-2.5\libxmlmods\libxml2mod.pyd
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x1629):
undefined reference to `_imp__xmlFree'
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x2802):
undefined reference to `_imp__xmlFree'
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x44b2):
undefined reference to `_imp__xmlFree'
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x50e4):
undefined reference to `_imp__xmlFree'
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x5519):
undefined reference to `_imp__xmlFree'
build\temp.win32-2.5\Release\libxml2-py.o:libxml2-py.c:(.text+0x7339):
more undefined references to `_imp__xmlFree' follow

Does anyone know how to solve this issue or has suggestions for
another (easy-to-install) xml-xpath library?

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


Re: something else instead of PIL?

2008-12-17 Thread skip

Reimar> Hi what has happened to PIL? No updates since two years.

It's well-written, stable code.  As far as I know it does what people want
(at least it's done everything I've needed when I've used it).  Why should
it matter that there hasn't been an official release in two years?

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen inheriting

2008-12-17 Thread Jeremy Sanders
Aaron Brady wrote:

> I thought so too.  The web seems to say that on Linux they are, and on
> Windows, you need to call DuplicateHandle for it.

I hit this problem - it looks like pipes aren't very versatile on Windows.
There's also the complicating factor that the handles in windows aren't the
same as the file numbers that Python uses, so you have to convert between
them.

It would be nice if Python created pipes that are properly inheritable by
default by child processes, as they're mostly used for IPC.

It was so painful that I converted my code to use sockets instead, which
seem much more portable between Windows and Unix (though you don't get to
use socketpair and AF_UNIX in Windows).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Transferring a file over sockets

2008-12-17 Thread Ferdinand Sousa
I am using sockets to transfer a file over LAN. There are 2 scripts, the
server opens a listens for connection and the client is run on another
machine. I always make sure the server is run first. The strange thing is
that if the the server script is double-clicked and executed (run in a
console with title %Python path%python.exe) the output file saved on the
server is truncated. It works just fine if you open the server script in
IDLE and then run it. The client script can be run in either way, it still
works. You could try using any arbitrary file to test this behaviour after
changing the file name in both the scripts.

==
# file receiver
# work in progress

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '192.168.1.17'
PORT = 31400

s.bind((HOST, PORT))
s.listen(3)
conn, addr = s.accept()
print 'conn at address',addr
conn.send('READY')
f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb')
fsize=int(conn.recv(8))
print 'File size',fsize
f.write(conn.recv(fsize))
f.close()
conn.close()
s.close()

raw_input('Press any key to exit')


===

# file sender !!!
# Work in progress

import socket, os
from stat import ST_SIZE


HOST = '192.168.1.17'
PORT = 31400  # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST,PORT))
if s.recv(5)!='READY':
raw_input('Unable to connect \n\n Press any key to exit ...')
s.close()
exit()

f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb')
fsize=os.stat(f.name)[ST_SIZE]

s.send(str(fsize))
s.send(f.read())

s.close()
f.close()

===

Thanks for reading!!

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


Re: something else instead of PIL?

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 12:48 PM, Reimar Bauer  wrote:
> Hi
>
> what has happened to PIL? No updates since two years.

The Python Imaging Library is still current; I guess they just haven't
found any new bugs or seen fit to add new functionality in a while,
though I presume they'll start working on a Python 3.0 port
eventually.

If you don't like PIL, there's always the (much less popular) Python
bindings to ImageMagick:
http://www.imagemagick.org/script/api.php#python

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex problem ..

2008-12-17 Thread Analog Kid
Hi guys:
Thanks for your responses. Points taken. Basically, I am looking for a
combination of the following ...
[^\w] and %(?!20) ... How do I do this in a single RE?

Thanks for all you help.
Regards,
AK

On Mon, Dec 15, 2008 at 10:54 PM, Steve Holden  wrote:

> Analog Kid wrote:
> > Hi All:
> > I am new to regular expressions in general, and not just re in python.
> > So, apologies if you find my question stupid :) I need some help with
> > forming a regex. Here is my scenario ...
> > I have strings coming in from a list, each of which I want to check
> > against a regular expression and see whether or not it "qualifies". By
> > that I mean I have a certain set of characters that are permissible and
> > if the string has characters which are not permissible, I need to flag
> > that string ... here is a snip ...
> >
> > flagged = list()
> > strs = ['HELLO', 'Hi%20There', '123...@#@']
> > p =  re.compile(r"""[^a-zA-Z0-9]""", re.UNICODE)
> > for s in strs:
> > if len(p.findall(s)) > 0:
> > flagged.append(s)
> >
> > print flagged
> >
> > my question is ... if I wanted to allow '%20' but not '%', how would my
> > current regex (r"""[^a-zA-Z0-9]""") be modified?
> >
> The essence of the approach is to observe that each element is a
> sequence of zero or more "character", where character is "either
> letter/digit or escape." So you would use a pattern like
>
> "([a-zA-Z0-9]|%[0-9a-f][0-9a-f])+"
>
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: something else instead of PIL?

2008-12-17 Thread Daniel Fetchinson
>> what has happened to PIL? No updates since two years.
>
> The Python Imaging Library is still current; I guess they just haven't
> found any new bugs or seen fit to add new functionality in a while,
> though I presume they'll start working on a Python 3.0 port
> eventually.

That's actually an interesting question. Does anybody know if PIL is
being ported to 3.0? Are there such plans? Maybe even code?

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Selecting a different superclass

2008-12-17 Thread psaff...@googlemail.com
This might be a pure OO question, but I'm doing it in Python so I'll
ask here.

I'm writing a number crunching bioinformatics application. Read lots
of numbers from files; merge, median and munge; draw plots. I've found
that the most critical part of this work is validation and
traceability - "where does this final value come from? How has it been
combined with other values? Is that right?"

My current implementation stores all my values just as floats with a
class called PointSet for storing one set of such values, with various
mathematical and statistical methods. There are several subclasses of
PointSet (IDPointSet, MicroArrayPointSet) for obtaining values from
different file types and with different processing pipelines.

I'm planning to instead store each value in a TraceablePoint class
which has members that describe the processing stages this value has
undergone and a TraceablePointSet class to store groups of these -
this will contain all the same methods as PointSet, but will operate
on TraceablePoints instead of floats. Of course, this will be much
slower than just floats, so I'd like to be able to switch it on and
off.

The problem is that IDPointSet and MicroArrayPointSet will need to
inherit from PointSet or TraceablePointSet based on whether I'm
handling traceable points or not. Can I select a superclass
conditionally like this in Python? Am I trying to do something really
evil here?

Any other bright ideas on my application also welcome.

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


getting object instead of string from dir()

2008-12-17 Thread Rominsky
I am trying to use dir to generate a list of methods, variables, etc.
I would like to be able to go through the list and seperate the
objects by type using the type() command, but the dir command returns
a list of strings.  When I ask for the type of an element, the answer
is always string.  How do I point at the variables themselves.  A
quick example is:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
print type(el)

Right now I am understandably getting all types being output as
strings, how do i get the type of the actual objects returned from dir
()?
--
http://mail.python.org/mailman/listinfo/python-list


Re: weird dict problem, how can this even happen?

2008-12-17 Thread Joel Hedlund

Steven D'Aprano wrote:

On Tue, 16 Dec 2008 14:32:39 +0100, Joel Hedlund wrote:

Duncan Booth wrote:

Alternatively give up on defining hash and __eq__ for FragmentInfo and
rely on object identity instead.

Object identity wouldn't work so well for caching. Objects would always
be drawn as they appeared for the first time. No updates would be shown
until the objects were flushed from the cache.


Perhaps I don't understand your program structure, but I don't see how 
that follows.


First off, please note that I consider my problem to be solved, many 
thanks to c.l.p and especially Duncan Booth. But of course continued 
discussion on this topic can be both enlightening and entertaining as 
long as people are interested. So here goes:


I'm making a scientific program that visualizes data. It can be thought 
of as a freely zoomable map viewer with a configurable stack of data 
feature renderers, themselves also configurable to enhance different 
aspects of the individual features. As you zoom in, it becomes possible 
to render more types of features in a visually recognizable manner, so 
consequentially more feature renderers become enabled. The determining 
properties for the final image are then the coordinates and zoom level 
of the view, and the current renderer stack configuration. Rendering may 
be time consuming, so I cache the resulting bitmap fragments using a 
"key object" called FragmentInfo that holds this information.


Some renderers may take a very long time to do their work, so in order 
to keep the gui nice and responsive, I interrupt the rendering chain at 
that point, put the remainder of the rendering chain in a job pool, and 
postpone finishing up the rendering until there are cpu cycles to spare. 
At that time, I go through the job pool and ask the cache which fragment 
was most recently accessed. This fragment is the most likely to actually 
be in the current view, and thus the most interesting for the user to 
have finallized.


Now, when the user navigates the view to any given point, the gui asks 
the cache if the bitmap fragments necessary to tile up the current view 
have already been rendered, and if so, retrieves them straight from the 
cache and paints them to the screen. And that's why object identity 
wouldn't work. If the user changes something in the config for the 
current renderer stack (=mutates the objects), the renderers still 
retain the same object identities and therefore the old versions would 
be retrieved from the cache, and no updates would be shown until the 
fragments are flushed from the cache, and the fragment subsequently 
redrawn.


I guess you could delve deep into the data members and pull out all 
their object identities and hash wrt that if you'd really want to, but I 
don't really see the point.


The stupid cache implementation that I originally employed used a dict 
to store the FragmentInfo:BitmapFragment items plus a use tracker (list) 
on the side. This obviously doesn't work, because as soon as the 
renderer stack mutates, list and dict go out of sync and I can no longer 
selectively flush old items from the dict, because it's not readily 
apparent how to reconstruct the old keys. Purely using lists here is 
vastly superior because I can just .pop() the least recently used items 
from the tail and be done with them. Also for lists as small as this, 
the cost in performance is at most negligible.



I've been experimenting with a list cache now and I can't say I'm
noticing any change in performance for a cache of 100 items. 


100 items isn't very big though. If you have 50,000 items you may notice 
significant slow down :)


If having many items in the cache is possible, you should consider using 
a binary search instead of a linear search through the cache. See the 
bisect module.


Thanks for the tip, but I don't forsee this cache ever needing to be 
that big. ~100 is quite enough for keeping the gui reasonably responsive 
in this case.


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


Re: help I'm getting delimited

2008-12-17 Thread aka
Due to being in a hurry I didn't paste correctly so I lost the try
clause (sorry).
The intention is to parse a csv file and (ultimately) put values of
column 1 ("id") in a list (so I need to append in the loop) that will
be used to fill a session var.
The complete code is:

roles = []
inp = 'C:/temp/test.csv'
try:
fp = open(inp, 'rb')
reader = csv.reader(fp, dialect='excel', delimiter=';')
for r in reader:
roles.append(r)   ## ultimately should be something like r.id
or r[0]
except:
msg = 'Something's wrong with the csv.reader'
return dict(file=inp,roles=str(roles))

The roles list isn't populated at all :(
--
http://mail.python.org/mailman/listinfo/python-list


OT: Binary tree logarithms properties

2008-12-17 Thread Mr . SpOOn
Hi,
I'm searching for a clear explanation of binary tree properties,
expecially the ones related to logarithms.

For example, I know that in a tree with 2n-1 nodes, we have log(n)
levels, from 0 to log(n).
So, if k is the level, the nodes on a level have indexes between 2^k
and 2^(k+1)-1.

For k=0 we have 2 and 3.
For k=1 we have 4, 5, 6, 7
and so on.

I know this after I studied some exercises on my book. Anyway there is
no explanation or demonstration of these properties.

I know this is not the better place to ask (or maybe it is?), but
maybe someone can point me to something useful.

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


Re: WinMerge--B/W Shading of Printed Copy to Show Differences?

2008-12-17 Thread W. eWatson

W. eWatson wrote:

Jason Scheirer wrote:

On Dec 16, 3:56 pm, "W. eWatson"  wrote:
Is there a way to highlight differences between the two files when 
printing
in b/w? Help suggests there may be some texturing, but all I see is 
color

choices.
--
W. eWatson

  (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
   Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

 Web Page: 


WinMerge is written in C++ and not even remotely related to Python.
Well, yes, but it has applicability to Python (and maybe other 
languages) in that I can use it to find differences between two sets of 
code.


If not here, where? comp.lang.??? ?

Hmm, I guess no one here uses it. I did find a winmerge mailing list, so 
will work it out there.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: getting object instead of string from dir()

2008-12-17 Thread Kurt Smith
On Wed, Dec 17, 2008 at 1:52 PM, Rominsky  wrote:

> On Dec 17, 10:59 am, Christian Heimes  wrote:
> > Rominsky schrieb:
> >
> > > I am trying to use dir to generate a list of methods, variables, etc.
> > > I would like to be able to go through the list and seperate the
> > > objects by type using the type() command, but the dir command returns
> > > a list of strings.  When I ask for the type of an element, the answer
> > > is always string.  How do I point at the variables themselves.  A
> > > quick example is:
> >
> > > a = 5
> > > b = 2.0
> > > c = 'c'
> >
> > > lst = dir()
> >
> > > for el in lst:
> > > print type(el)
> >
> > for name, obj in vars().iteritems():
> > print name, obj
> >
> > Christian
>
> I do have some understanding of the pythonic methodology of
> programming, though by far I still don't consider myself an expert.
> The problem at hand is that I am coming from a matlab world and trying
> to drag my coworkers with me.  I have gotten a lot of them excited
> about using python for this work, but the biggest gripe everytime is
> they want their matlab ide.  I am trying to experiment with making
> similar pieces of the ide, in particular I am working on the workspace
> window which lists all the current variables in the namespace, along
> with their type, size, value, etc  I am trying to create a python
> equivalent.  I can get dir to list all the variables names in a list
> of strings, but I am trying to get more info them.  hence the desire


Are you familiar with the ipython console?

http://ipython.scipy.org/moin/

It is quite powerful; in particular, the %who and %whos 'magic functions'
will do much of what you'd like:

[501]$ ipython
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?   -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help-> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: a = 'foo'

In [2]: b = 'bar'

In [3]: c = 5.234

In [4]: import os

In [5]: d = os

In [6]: whos
Variable   Type  Data/Info
--
a  str   foo
b  str   bar
c  float 5.234
d  module
os module

In [7]: import numpy as np

In [8]: aa = np.zeros(100)

In [9]: whos
Variable   Type   Data/Info
---
a  strfoo
aa ndarray100: 100 elems, type `float64`, 800 bytes
b  strbar
c  float  5.234
d  module 
np module ages/numpy/__init__.pyc'>
os module 

And I trust you've heard of numpy, scipy and matplotlib?

http://www.scipy.org/

http://matplotlib.sourceforge.net/

 http://numpy.scipy.org/

Cheers,

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Bruno Desthuilliers

walterbyrd a écrit :

On Dec 17, 8:41 am, prueba...@latinmail.com wrote:


If scoping worked as you want, how, pray tell, would you define object
attributes?- Hide quoted text -


I suppose you could do this:

class className():
varname = "whatever"


This defines a class attribute - that is, an attribute of the className 
class object, accessible either thru the className object or it's 
instances if not shadowed by an instance attribute by the same name/



def fname(self, varname):
   . . . .

Instead of having variable defined within methods to be global
everywhere within the class.


There's nothing like a "variable defined within (a) method", because you 
never define methods in Python - only functions. So there's no 
difference in scoping rules for functions defined within a class 
statement block or outside a class statement block.




Anyway, it's not a matter of what I like, I am just trying to
understand the reason behind the scoping rules.


Then you should start with understanding the scoping rules.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Bruno Desthuilliers

walterbyrd a écrit :

On Dec 17, 9:04 am, rdmur...@bitdance.com wrote:


Yes.  It's called Object Oriented Programming.


I think you mean it's *Python* Object Oriented Programming. I am not
sure that every other OO language works like that.



Every OO languages having such a thing as a global variable makes a 
distinction between an instance attributes and a global variable. Your 
problem is that you are confusing variables and attributes. In Python, 
'anything.anyname' (note the dot) is the attribute 'anyname' of object 
'anything'.

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


Location HTTP Header

2008-12-17 Thread ptn
Hi all.

I tried this stupid script on my server:

#! /usr/bin/env python

print 'Location: http://www.google.com\n'

and it didn't work, I get a blank page.  I first tried the Location
header in another script, and when execution got to that point, it
would
just sort of ignore it, because the script would keep running to the
end
of the code (or stop at some unhandled exception).

I ran chmod 755 script.cgi and the cgi script is at the correct
directory, but still nothing.  I think it might be an error from the
server (the old PythonWeb.org webserver), maybe it doesn't support
Location?  Is that even possible?  Or maybe there's some setup I
forgot
to do?

Any ideas?

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


Re: subprocess.Popen inheriting

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 12:21:38 -0200, Jeremy Sanders  
 escribió:



Aaron Brady wrote:


I thought so too.  The web seems to say that on Linux they are, and on
Windows, you need to call DuplicateHandle for it.


Or set bInheritHandle=True when creating the pipe initially. os.pipe()  
doesn't do that.


I hit this problem - it looks like pipes aren't very versatile on  
Windows.
There's also the complicating factor that the handles in windows aren't  
the

same as the file numbers that Python uses, so you have to convert between
them.

It would be nice if Python created pipes that are properly inheritable by
default by child processes, as they're mostly used for IPC.


I'd say it is a bug in os.pipe implementation; they should be inheritable  
by default, as in posix (after all, the code is in "posixmodule.c").


--
Gabriel Genellina

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


Re: The rule of literal string

2008-12-17 Thread Li Han
But what repr() do remain a black hole!
Han

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


C API and memory allocation

2008-12-17 Thread Floris Bruynooghe
Hi

I'm slightly confused about some memory allocations in the C API.
Take the first example in the documentation:

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;

if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
return Py_BuildValue("i", sts);
}

What I'm confused about is the memory usage of "command".  As far as I
understand the compiler provides space for the size of the pointer, as
sizeof(command) would indicate.  So I'm assuming PyArg_ParseTuple()
must allocate new memory for the returned string.  However there is
nothing in the API that provides for freeing that allocated memory
again.  So does this application leak memory then?  Or am I
misunderstanding something fundamental?


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


Re: help I'm getting delimited

2008-12-17 Thread John Machin
On Dec 18, 3:15 am, aka  wrote:
> John, this is the actual code I ran in TurboGears which is a Python
> framework.

It's not complete -- the change in indentation would have caused a
SyntaxError.

If (as you appear to assert) the problem is in the csv module, then
create a small stand-alone no-TurboGears Python script and a test file
which together demonstrate the problem reproducibly so that the
problem can investigated by anyone with a standard TurboGears-free
Python installation.

If you can't reproduce the problem in that manner, then you may need
to seek assistance in a TurboGears-specific forum.

> I should have left away the import statements. Trust me, the problem
> isn't in there because the UnicodeWriter is functioning perfectly.

Do you mean that this file was created by whatever.UnicodeWriter? If
so, did you just now discover this information?

How do you know that "the UnicodeWriter is functioning perfectly"?
What does "functioning perfectly mean to you"? In particular, what
encoding is it using?

> I did allready sanitate the csv file to these four lines in Notepad so
> there isn't anything more than this:
>
> id;company;department
> 12;Cadillac;Research
> 11;Ford;Accounting
> 10;Chrysler;Sales

Which do you mean:
(a) you typed those lines into Notepad yourself
(b) you took a copy of a file created by whatever.UnicodeWriter,
opened it with Notepad, trimmed off some rows and columns, and saved
it again
?

You said earlier
"""
csv.reader results in: for r in reader: Error: line contains NULL
byte

Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
can't decode byte 0xff in position 0: unexpected code byte
"""

Those results are consistent with your file being encoded in utf16_le,
with the utf16_le BOM ('\xff\xfe') at the start of the file.

Have you, as I asked, looked at the file with some better-than-Notepad
diagnostic apparatus?

Here's a likely hypothesis: the file was written in utf16. In that
case:
either (i) you really want utf16 (why?), so:

(1) the csv module will not cope with it, and is not expected to cope
with it

(2) the whatever.UnicodeReader should (in order of preference):
   (a) be allowed to find out for itself that 'utf16' is the go
   (b) be told explicitly that 'utf16' is the go
   (c) be served with a bug report

OR (ii) you really want utf8, so:

(1) the csv module should be happy
(2) the whatever.UnicodeWriter should be told to use 'utf8'
(3) the whatever.UnicodeReader should (in order of preference):
[as above but s/16/8/]

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


Re: The rule of literal string

2008-12-17 Thread Scott David Daniels

Li Han wrote:

But what repr() do remain a black hole!
Han


Try:  print repr(repr("'"))
that might enlighten you.

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


Re: The rule of literal string

2008-12-17 Thread Li Han
On 12月18日, 上午7时12分, Scott David Daniels  wrote:
Scott wrote:
> Try:  print repr(repr("'"))
> that might enlighten you.

I found that print( repr( repr( arbitarystring ) ) ) == repr
( arbitarystring )
--
http://mail.python.org/mailman/listinfo/python-list


Re: The rule of literal string

2008-12-17 Thread Chris Rebert
2008/12/17 Li Han :
> On 12月18日, 上午7时12分, Scott David Daniels  wrote:
> Scott wrote:
>> Try:  print repr(repr("'"))
>> that might enlighten you.
>
> I found that print( repr( repr( arbitarystring ) ) ) == repr
> ( arbitarystring )

As I stated previously, the key rule is:

eval(repr(something)) == something

That is, repr() gives a string of Python code that, when evaluated,
results in what you gave to repr().

So repr('') ==> "''"
And repr("''") ==> "\"''\""
Which when print()-ed is: "''"
And eval("''") is the same as entering two apostrophes ('') at the
REPL, both of which give an empty string object.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: The rule of literal string

2008-12-17 Thread James Mills
On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert  wrote:
> As I stated previously, the key rule is:
>
> eval(repr(something)) == something

This rule is only true for basic data types;

For example:

>>> eval(repr(1)) == 1
True
>>> eval(repr([1, 2, 3])) == [1, 2, 3]
True
>>> eval(repr({"a": 1, "b": 2, "c": 3})) == {"a": 1, "b": 2, "c": 3}
True
>>> eval(repr("foo")) == "foo"
True

I guess the key thing here is that the repr
implementation (__repr__) for str, int, float
list and dict return sensible represenations
that Python _can_ evaluate with eval(...)

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


Re: C API and memory allocation

2008-12-17 Thread Floris Bruynooghe
Hello again

On Dec 17, 11:06 pm, Floris Bruynooghe 
wrote:
> So I'm assuming PyArg_ParseTuple()
> must allocate new memory for the returned string.  However there is
> nothing in the API that provides for freeing that allocated memory
> again.

I've dug a little deeper into this and found that PyArg_ParseTuple
(and friends) end up using PyString_AS_STRING() (Python/getargs.c:793)
which according to the documentation returns a pointer to the internal
buffer of the string and not a copy and that because of this you
should not attempt to free this buffer.

But how can python now know how long to keep that buffer object in
memory for?  When the reference count of the string object goes to
zero the object can be deallocated I though, and then your pointer
will point to something different all of a sudden.  Does this mean you
always have too keep a reference to the original objects when you've
extracted information from them with PyArg_Parse*() functions?  (At
least while you want to hang on to that information.)

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


Re: The rule of literal string

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 3:34 PM, James Mills
 wrote:
> On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert  wrote:
>> As I stated previously, the key rule is:
>>
>> eval(repr(something)) == something
>
> This rule is only true for basic data types;
>
> For example:
>
 eval(repr(1)) == 1
> True
 eval(repr([1, 2, 3])) == [1, 2, 3]
> True
 eval(repr({"a": 1, "b": 2, "c": 3})) == {"a": 1, "b": 2, "c": 3}
> True
 eval(repr("foo")) == "foo"
> True
>
> I guess the key thing here is that the repr
> implementation (__repr__) for str, int, float
> list and dict return sensible represenations
> that Python _can_ evaluate with eval(...)
>
> --JamesMills
>

True, I oversimplified to make things easier to understand. Strictly
speaking, only the basic types make the guarantee I stated. Arbitrary
types can have arbitrary, non-eval()-able repr()s. But the docs do
state eval()-ability as a goal:

repr(object)
Return a string containing a printable representation of an
object. [...] For many types, this function makes an attempt to return
a string that would yield an object with the same value when passed to
eval(), otherwise the representation is a string enclosed in angle
brackets that contains the name of the type of the object together
with additional information often including the name and address of
the object. A class can control what this function returns for its
instances by defining a __repr__() method.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Location HTTP Header

2008-12-17 Thread Gabriel Genellina

En Wed, 17 Dec 2008 20:52:42 -0200, ptn  escribió:


I tried this stupid script on my server:

#! /usr/bin/env python

print 'Location: http://www.google.com\n'

and it didn't work, I get a blank page.  I first tried the Location
header in another script, and when execution got to that point, it
would
just sort of ignore it, because the script would keep running to the
end
of the code (or stop at some unhandled exception).


I assume this is a cgi script. For the Location field to be relevant, the  
Status should be a 3xx (like 307 Temporary Redirect, or 302 Found)
In your case, your server probably has already sent a 200 OK response, so  
Location is ignored.

Try adding a Status line -before Location above- like:
print 'Status: 302 Found"


Any ideas?


I'd use a different protocol other than CGI...

--
Gabriel Genellina

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


Re: The rule of literal string

2008-12-17 Thread Li Han
Chris worte:
[snip]
> And repr("''") ==> "\"''\""
> Which when print()-ed is: "''"
> And eval("''") is the same as entering two apostrophes ('') at the
> REPL, both of which give an empty string object.

On my machine:
>>> repr("''")
'"\'\'"'
Han
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Rhodri James
On Wed, 17 Dec 2008 15:19:32 -, walterbyrd   
wrote:



However in the methods are within a class, the scoping seems to work
differently.


Not really.  Hopefully this commentary will show you why.


class ab():
def a(self):
self.x = 99
print self.x
def b(self):
print self.x

i = ab()
This creates |i|, an instance of class |ab|.  As yet it is pure and  
virgin, having nothing but the methods that it gets from |ab|.  Soon this  
will change...



i.a()


This creates an attribute |x| in |i|, and assigns the number 99 to it.


i.b() # this works, why no lexical scoping?


This works because you ran |i.a()| first, so |i.x| exists and can be  
printed out.  Lexical scoping is going on here, you're just mistaking  
what's being scoped; it's the |self| in |b|, which is in scope because  
it's a parameter.  This particular |self| (the |i| you made earlier)  
happens to have an attribute |x|, so it all works.  If however you'd  
written:


j = ab()
j.b()

then Python would whinge mightily at you, claiming that it knoweth naught  
of this |x| attribute of which you speak, and can it go home now for this  
is a silly place.  The |self| in |b| is still in lexical scope, though.


--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: The rule of literal string

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 3:52 PM, Li Han  wrote:
> Chris worte:
> [snip]
>> And repr("''") ==> "\"''\""
>> Which when print()-ed is: "''"
>> And eval("''") is the same as entering two apostrophes ('') at the
>> REPL, both of which give an empty string object.
>
> On my machine:
 repr("''")
> '"\'\'"'
> Han

Well, I was kinda winging it and didn't actually use the interpreter
for some of those. The point is that the output I gave _could_
reasonably be that of the interpreter (if the implementation details
of repr() were slightly different; it would still meet the spec
anyway) and illustrates the eval()-repr() relationship.

Just keep in mind the equivalence rule and what repr() does should
become obvious.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 4:03 PM, Rhodri James
 wrote:
> On Wed, 17 Dec 2008 15:19:32 -, walterbyrd  wrote:
>
>> However in the methods are within a class, the scoping seems to work
>> differently.
>
> Not really.  Hopefully this commentary will show you why.
>
>> class ab():
>>def a(self):
>>self.x = 99
>>print self.x
>>def b(self):
>>print self.x
>>
>> i = ab()
>
> This creates |i|, an instance of class |ab|.  As yet it is pure and virgin,
> having nothing but the methods that it gets from |ab|.  Soon this will
> change...
>
>> i.a()
>
> This creates an attribute |x| in |i|, and assigns the number 99 to it.
>
>> i.b() # this works, why no lexical scoping?
>
> This works because you ran |i.a()| first, so |i.x| exists and can be printed
> out.  Lexical scoping is going on here, you're just mistaking what's being
> scoped; it's the |self| in |b|, which is in scope because it's a parameter.
>  This particular |self| (the |i| you made earlier) happens to have an
> attribute |x|, so it all works.  If however you'd written:
>
> j = ab()
> j.b()
>
> then Python would whinge mightily at you, claiming that it knoweth naught of
> this |x| attribute of which you speak, and can it go home now for this is a
> silly place.  The |self| in |b| is still in lexical scope, though.
>

Relatedly, Python has no notion of 'declaring' instance variables in a
class (instead, you just create them in __init__ or other methods),
and class variables (in Java terminology: 'static' variables) do not
constitute a scope for variable lookup.
Python is still lexically scoped, it's just that only functions and
the "globals"/toplevel/module-level constitute scopes, not class
bodies.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


[ANN] EuroPython 2009 – Call for Partic ipation!

2008-12-17 Thread Martin P. Hellwig
On behalf of the EuroPython 2009 organisation it is my privilege and 
honour to announce the 'Call for Participation' for EuroPython 2009!
EuroPython is the conference for the communities around Python, 
including the Django, Zope and Plone communities.
This years conference will be held in Birmingham, UK from Monday 29th 
June to Saturday 4th July 2009.


Talk & Themes
Do you have something you wish to present at EuroPython? Go to 
http://www.europython.eu/talks/cfp/  for this years themes and 
submissions criteria, the deadline is on 5th April 2009.


Other Talks, Activities and Events
Have you got something which does not fit the above? Visit 
http://www.europython.eu/talks/ .


Help Us Out
We could use a hand any contribution is welcome, please take a look at 
http://www.europython.eu/contact/ .


Sponsors
An unique opportunity to affiliate with the prestigious EuroPython 
conference!

http://www.europython.eu/sponsors/

Spread the Word
Improve our publicity by distributing this announcement in your corner 
of the community, please coordinate this with the organizers: 
http://www.europython.eu/contact/


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


Looking forward to see you!

The EuroPython Team

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


Re: getting object instead of string from dir()

2008-12-17 Thread Rominsky
On Dec 17, 1:21 pm, Jean-Paul Calderone  wrote:
> On Wed, 17 Dec 2008 11:52:17 -0800 (PST), Rominsky  
> wrote:
> >On Dec 17, 10:59 am, Christian Heimes  wrote:
> >> Rominsky schrieb:
>
> >> > I am trying to use dir to generate a list of methods, variables, etc.
> >> > I would like to be able to go through the list and seperate the
> >> > objects by type using the type() command, but the dir command returns
> >> > a list of strings.  When I ask for the type of an element, the answer
> >> > is always string.  How do I point at the variables themselves.  A
> >> > quick example is:
>
> >> > a = 5
> >> > b = 2.0
> >> > c = 'c'
>
> >> > lst = dir()
>
> >> > for el in lst:
> >> >     print type(el)
>
> >> for name, obj in vars().iteritems():
> >>     print name, obj
>
> >> Christian
>
> >I do have some understanding of the pythonic methodology of
> >programming, though by far I still don't consider myself an expert.
> >The problem at hand is that I am coming from a matlab world and trying
> >to drag my coworkers with me.  I have gotten a lot of them excited
> >about using python for this work, but the biggest gripe everytime is
> >they want their matlab ide.  I am trying to experiment with making
> >similar pieces of the ide, in particular I am working on the workspace
> >window which lists all the current variables in the namespace, along
> >with their type, size, value, etc  I am trying to create a python
> >equivalent.  I can get dir to list all the variables names in a list
> >of strings, but I am trying to get more info them.  hence the desire
> >to do a type command on them.  I like the locals and globals commands,
> >but I am still trying to get more info.  I have started using the eval
> >command with the strings, which is working, but I am curious if there
> >is a better or more elegant way of getting the info.  The eval example
> >would be something like:
>
> >a = 5
> >b = 2.0
> >c = 'c'
>
> >lst = dir()
>
> >for el in lst:
> >   print el + '\t' + str(eval('type(%s)'%el))
>
> >It works, now I am curious if there is a better way.
>
> What about this:
>
> >> for name, obj in vars().iteritems():
> >>     print name, obj
>
> >> Christian
>
> Jean-Paul

vars seems to give an identical response as locals and globals, at
least in my test name space.  All three are new commands for me.  I
like the idea of adopting either vars or locals instead of dir as it
sets up the value for me to use.  I will play with them both a little
more and read up on them to learn there uses and limitations.  The key
step for me is still to be able to automatically query the key names
from vars or locals for what type the variable is.  In my previous
post I discussed using eval and the string in the key name such as

eval('type(%s)'%vars().keys()[0])

I definitely have enough to move forward.  Thanks everyone.  If anyone
has any other ideas I would still be interested in learning more.
Thanks again.
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API and memory allocation

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe  
 escribió:



On Dec 17, 11:06 pm, Floris Bruynooghe 
wrote:

So I'm assuming PyArg_ParseTuple()
must allocate new memory for the returned string.  However there is
nothing in the API that provides for freeing that allocated memory
again.


I've dug a little deeper into this and found that PyArg_ParseTuple
(and friends) end up using PyString_AS_STRING() (Python/getargs.c:793)
which according to the documentation returns a pointer to the internal
buffer of the string and not a copy and that because of this you
should not attempt to free this buffer.


Yes; but you don't have to dig into the implementation; from  
http://docs.python.org/c-api/arg.html :


s (string or Unicode object) [const char *]
Convert a Python string or Unicode object to a C pointer to a character  
string. You must not provide storage for the string itself; a pointer to  
an existing string is stored into the character pointer variable whose  
address you pass.



But how can python now know how long to keep that buffer object in
memory for?


It doesn't - *you* have to ensure that the original string object isn't  
destroyed (by example, incrementing its reference count as long as you  
keep the pointer), or copy the string contents into your own buffer.


--
Gabriel Genellina

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


Re: subprocess.Popen inheriting

2008-12-17 Thread Aaron Brady
On Dec 17, 5:05 pm, "Gabriel Genellina" 
wrote:
> En Wed, 17 Dec 2008 12:21:38 -0200, Jeremy Sanders  
>  escribió:
>
> > Aaron Brady wrote:
>
> >> I thought so too.  The web seems to say that on Linux they are, and on
> >> Windows, you need to call DuplicateHandle for it.
>
> Or set bInheritHandle=True when creating the pipe initially. os.pipe()  
> doesn't do that.
>
> > I hit this problem - it looks like pipes aren't very versatile on  
> > Windows.
> > There's also the complicating factor that the handles in windows aren't  
> > the
> > same as the file numbers that Python uses, so you have to convert between
> > them.
>
> > It would be nice if Python created pipes that are properly inheritable by
> > default by child processes, as they're mostly used for IPC.
>
> I'd say it is a bug in os.pipe implementation; they should be inheritable  
> by default, as in posix (after all, the code is in "posixmodule.c").

The code looks like this:

ok = CreatePipe(&read, &write, NULL, 0);
Py_END_ALLOW_THREADS
if (!ok)
return win32_error("CreatePipe", NULL);
read_fd = _open_osfhandle((Py_intptr_t)read, 0);
write_fd = _open_osfhandle((Py_intptr_t)write, 1);

'If lpPipeAttributes is NULL, the handle cannot be inherited.'  You
could populate a 'SECURITY_ATTRIBUTES' structure, or call
DuplicateHandle on both of them.

A patch would look like this:

SECURITY_ATTRIBUTES sattribs;
sattribs.nLength = sizeof(sattribs);
sattribs.lpSecurityDescriptor = NULL;
sattribs.bInheritHandle = TRUE;
ok = CreatePipe(&read, &write, &sattribs, 0);

This still doesn't answer whether the file descriptor return by
'_open_osfhandle' can be inherited too.
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API and memory allocation

2008-12-17 Thread Aaron Brady
On Dec 17, 6:42 pm, "Gabriel Genellina" 
wrote:
> En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe  
>  escribió:
> Yes; but you don't have to dig into the implementation; from  
> http://docs.python.org/c-api/arg.html:
>
> s (string or Unicode object) [const char *]
> Convert a Python string or Unicode object to a C pointer to a character  
> string. You must not provide storage for the string itself; a pointer to  
> an existing string is stored into the character pointer variable whose  
> address you pass.
>
> > But how can python now know how long to keep that buffer object in
> > memory for?
>
> It doesn't - *you* have to ensure that the original string object isn't  
> destroyed (by example, incrementing its reference count as long as you  
> keep the pointer), or copy the string contents into your own buffer.

I missed something.  How did you get a reference to the original
string object, with which to increment its reference count?  How do
you know its length to copy it into your own buffer?

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 22:19:43 +0100, Bruno Desthuilliers wrote:

> Your
> problem is that you are confusing variables and attributes. In Python,
> 'anything.anyname' (note the dot) is the attribute 'anyname' of object
> 'anything'.

An easy mistake to make, given that scopes are just namespaces, and 
attribute access is just accessing names in namespaces too.


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


Re: subprocess.Popen inheriting

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 22:46:32 -0200, Aaron Brady   
escribió:

On Dec 17, 5:05 pm, "Gabriel Genellina" 
wrote:

En Wed, 17 Dec 2008 12:21:38 -0200, Jeremy Sanders  
 escribió:

> It would be nice if Python created pipes that are properly  
inheritable by

> default by child processes, as they're mostly used for IPC.

I'd say it is a bug in os.pipe implementation; they should be  
inheritable  

by default, as in posix (after all, the code is in "posixmodule.c").


The code looks like this:

ok = CreatePipe(&read, &write, NULL, 0);
Py_END_ALLOW_THREADS
if (!ok)
return win32_error("CreatePipe", NULL);
read_fd = _open_osfhandle((Py_intptr_t)read, 0);
write_fd = _open_osfhandle((Py_intptr_t)write, 1);

'If lpPipeAttributes is NULL, the handle cannot be inherited.'  You
could populate a 'SECURITY_ATTRIBUTES' structure, or call
DuplicateHandle on both of them.

A patch would look like this:

SECURITY_ATTRIBUTES sattribs;
sattribs.nLength = sizeof(sattribs);
sattribs.lpSecurityDescriptor = NULL;
sattribs.bInheritHandle = TRUE;
ok = CreatePipe(&read, &write, &sattribs, 0);


Yes, that's exactly how os.popen does it (in posixmodule.c)


This still doesn't answer whether the file descriptor return by
'_open_osfhandle' can be inherited too.


It doesn't matter. The OS only cares about file handles, not C RTL  
structures.


--
Gabriel Genellina

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


Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 10:20:21 -0800, walterbyrd wrote:

> On Dec 17, 10:00 am, r  wrote:
>> When writing
>> procedural code how would you like it if vars inside functions were
>> automatically global. Your code with be blowing chunks in no time.
> 
> That was my point - I consider python's ordinary use of lexical scoping
> to be a good thing, and I was wondering why this "good thing" was not
> used in classes, as well as outside of classes.

But it is. You're mistaking lexical scoping for object attribute access. 
The rules for lexical scoping inside a class are (almost) the same as 
they are for inside a function:

def parrot(breed):
def message(colour):
return "The %s %s has beautiful plumage." % (breed, colour)
return message("Blue")

class Parrot:
def parrot(self, breed):
def message(colour):
return "The %s %s has beautiful plumage." % (breed, colour)
return message("Blue")

And in use: 

>>> parrot("Norwegian")
'The Norwegian Blue has beautiful plumage.'
>>> p = Parrot()
>>> p.parrot("Swedish")
'The Swedish Blue has beautiful plumage.'


Notice that to have lexical scoping work, you actually have to nest the 
functions. Otherwise they are in different scopes.


This might lead you believe you can do this:

class Parrot2:
colour = "Blue"
def parrot(self, breed):
return "The %s %s has beautiful plumage." % (breed, colour)


>>> p = Parrot2()
>>> p.parrot("Norwegian")
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in parrot
NameError: global name 'colour' is not defined

What's going on? Why doesn't the parrot method see the name "colour" in 
the class scope?

The reason is that the class scope is deliberately left out of the nested 
scope chain. This was a design decision from when nested scopes were 
introduced: 

"An alternative would have been to allow name binding in class
scope to behave exactly like name binding in function scope.  This
rule would allow class attributes to be referenced either via
attribute reference or simple name.  This option was ruled out
because it would have been inconsistent with all other forms of
class and instance attribute access, which always use attribute
references.  Code that used simple names would have been obscure."

http://www.python.org/dev/peps/pep-0227/

So inside the method, you need to refer to Parrot.colour (or thanks to 
the rules of attribute inheritance, self.colour).

Classes could be closures, but that could radically change the way 
methods and classes work. Depending on design decisions, it might require 
huge changes to the Python compiler. How would it change the existing 
lexical scoping in factory functions?

def factory(colour):
class Parrot:
def parrot(self, breed):
return "The %s %s has beautiful plumage." % (breed, colour)
return Parrot

>>> redparrot = factory("Red")()
>>> redparrot.parrot("Swedish")
'The Swedish Red has beautiful plumage.'


Consider a hypothetical Python with classes included in the lexical 
scoping:

class Parrot3:
colour = "Blue"
def parrot(self):
colour = "Red"


What should method parrot do? I can think of at least three possibilities:

* create a local name colour inside the method scope;
* change the class attribute Parrot3.colour to "Red";
* create an instance attribute self.colour.

Whatever solution you come up with, there is potential inconsistency with 
other parts of the language.



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


Re: C API and memory allocation

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 22:51:03 -0200, Aaron Brady   
escribió:

On Dec 17, 6:42 pm, "Gabriel Genellina" 
wrote:

En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe  
 escribió:



> But how can python now know how long to keep that buffer object in
> memory for?

It doesn't - *you* have to ensure that the original string object isn't  
 

destroyed (by example, incrementing its reference count as long as you  
keep the pointer), or copy the string contents into your own buffer.


I missed something.  How did you get a reference to the original
string object, with which to increment its reference count?


From the original arguments to the function -- the first argument you pass  
to  PyArg_ParseTuple &co.



How do
you know its length to copy it into your own buffer?


Use the "s#" format instead, which returns both a pointer to the string  
contents and its length. Even if you're not going to copy the buffer, it's  
required in case the string could contain any NUL byte.


--
Gabriel Genellina

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


Re: The rule of literal string

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 15:25:41 -0800, Chris Rebert wrote:

> 2008/12/17 Li Han :
>> On 12月18日, 上午7时12分, Scott David Daniels  
wrote:
>> Scott wrote:
>>> Try:  print repr(repr("'"))
>>> that might enlighten you.
>>
>> I found that print( repr( repr( arbitarystring ) ) ) == repr (
>> arbitarystring )
> 
> As I stated previously, the key rule is:
> 
> eval(repr(something)) == something
> 
> That is, repr() gives a string of Python code that, when evaluated,
> results in what you gave to repr().

That is not true in general.


>>> from urllib2 import HTTPError
>>> h = HTTPError("404 quoth the Raven", None, None, None, None)
>>> eval(repr(h))
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
TypeError: __init__() takes exactly 6 arguments (1 given)


Even for built-ins, it's not always true:

>>> eval(repr(float('inf')))
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'inf' is not defined


In other words, there is no guarantee that repr(obj) will round-trip 
correctly, or at all. It's a Nice To Have, and it will often work, but 
it's not something you should rely on for arbitrary objects.

However, I believe it is true for strings.


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


Re: The rule of literal string

2008-12-17 Thread Steven D'Aprano
On Thu, 18 Dec 2008 09:34:12 +1000, James Mills wrote:

> On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert  wrote:
>> As I stated previously, the key rule is:
>>
>> eval(repr(something)) == something
> 
> This rule is only true for basic data types;

Oops, missed this, and the follow ups. 

Note to self: read thread *before* replying.


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


Re: getting object instead of string from dir()

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 11:52:17 -0800, Rominsky wrote:

> I do have some understanding of the pythonic methodology of programming,
> though by far I still don't consider myself an expert. The problem at
> hand is that I am coming from a matlab world and trying to drag my
> coworkers with me.  I have gotten a lot of them excited about using
> python for this work, but the biggest gripe everytime is they want their
> matlab ide.  I am trying to experiment with making similar pieces of the
> ide, in particular I am working on the workspace window which lists all
> the current variables in the namespace, along with their type, size,
> value, etc  I am trying to create a python equivalent.


Have you considered looking at existing IDEs instead of re-inventing the 
wheel? Python even comes with one, IDLE.



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


Re: getting object instead of string from dir()

2008-12-17 Thread rdmurray
Quoth Rominsky :
> vars seems to give an identical response as locals and globals, at
> least in my test name space.  All three are new commands for me.  I

Without arguments vars() returns the same thing as locals().

> like the idea of adopting either vars or locals instead of dir as it
> sets up the value for me to use.  I will play with them both a little
> more and read up on them to learn there uses and limitations.  The key
> step for me is still to be able to automatically query the key names
> from vars or locals for what type the variable is.  In my previous
> post I discussed using eval and the string in the key name such as
> 
> eval('type(%s)'%vars().keys()[0])

>>> eval('type(%s)'%vars().keys()[0])

>>> vars().keys()[0]
'__builtins__'
>>> vars().values()[0]

>>> type(vars().values()[0])

>>> for name, obj in vars().items():
... print "%s (%s)" % (name, type(obj))
... 
__builtins__ ()
__name__ ()
__doc__ ()
__package__ ()

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


Re: getting object instead of string from dir()

2008-12-17 Thread Steve Holden
Steven D'Aprano wrote:
> On Wed, 17 Dec 2008 11:52:17 -0800, Rominsky wrote:
> 
>> I do have some understanding of the pythonic methodology of programming,
>> though by far I still don't consider myself an expert. The problem at
>> hand is that I am coming from a matlab world and trying to drag my
>> coworkers with me.  I have gotten a lot of them excited about using
>> python for this work, but the biggest gripe everytime is they want their
>> matlab ide.  I am trying to experiment with making similar pieces of the
>> ide, in particular I am working on the workspace window which lists all
>> the current variables in the namespace, along with their type, size,
>> value, etc  I am trying to create a python equivalent.
> 
> 
> Have you considered looking at existing IDEs instead of re-inventing the 
> wheel? Python even comes with one, IDLE.
> 
> 
> 
I realise there are some very competent Python programmers whose primary
environment is IDLE, but I'm afraid I always end up frustrated with it.

I think Rominsky will learn a lot by noodling around in the way he
proposes - he does, after all, confess he is experimenting, and that
sounds to me like a great way to find out a lot in a fairly short time.

Matlab is a very specific environment, and students have told me that
there are pieces of its IDE that they really miss in Python, so this
work may result in something that attracts more users to Python.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


  1   2   >