Re: file seek is slow

2010-03-11 Thread CHEN Guang
 
Metalone wrote:
> I just tried the seek test with Cython.
> Cython fseek() : 1.059 seconds.  30% slower than 'C'
> Python f.seek  : 1.458 secondds. 80% slower than 'C'.
> 
> It is amazing to me that Cython generates a 'C' file that is 1478
> lines.
> 
 
PythoidC ( http://pythoidc.googlecode.com ) generates the shortest 'C' file.
PythoidC is the C language like the Python, by the Python and for the Python.
CHEN Guang-- 
http://mail.python.org/mailman/listinfo/python-list


Re: StringChain -- a data structure for managing large sequences of chunks of bytes

2010-03-11 Thread Paul Rubin
"Zooko O'Whielacronx"  writes:
> Every couple of years I run into a problem where some Python code that
> worked well at small scales starts burning up my CPU at larger scales,
> and the underlying issue turns out to be the idiom of accumulating
> data by string concatenation. 

I usually use StringIO or cStringIO for that (python 2.x syntax):

   buf = cStringIO()
   buf.write("first thing")
   buf.write("second thing")
   result = buf.getvalue()

Sometimes I like to use a generator instead:

  def stuff():
 yield "first thing"
 yield "second thing"

  result = ''.join(stuff())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python, Reportlabs, Pil and Windows 7 (64bit)

2010-03-11 Thread Martin v. Loewis
> I’m sorry everyone. I didn’t realise I had installed the 64-bit
> version of Python. Well, at least someone else might find have the
> same problem. But I think that there is going to be a bit of a rough
> patch as everyone moves over to 64-bit.

Expect that move to take a few more years. 64-bit CPUs were introduced
more than ten years ago (e.g. Alpha, in 1992), and only slowly reach
"the masses". People typically still don't have more than 4GiB of memory
in their desktop PCs or laptops, so users who do install 64-bit
operating systems on such hardware are still early adaptors.

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


StringChain -- a data structure for managing large sequences of chunks of bytes

2010-03-11 Thread Zooko O'Whielacronx
Folks:

Every couple of years I run into a problem where some Python code that
worked well at small scales starts burning up my CPU at larger scales,
and the underlying issue turns out to be the idiom of accumulating
data by string concatenation. It just happened again
(http://foolscap.lothar.com/trac/ticket/149 ), and as usual it is hard
to make the data accumulator efficient without introducing a bunch of
bugs into the surrounding code. So this time around I decided to try
encapsulating my preferred more efficient idiom into a reusable class.

So I present to you StringChain, which is an efficient way to
accumulate and process data in many chunks:

http://tahoe-lafs.org/trac/stringchain

Here are some benchmarks generated by running python -OOu -c 'from
stringchain.bench import bench; bench.quick_bench()' as instructed by
the README.txt file.

The N: in the left-hand column is how many bytes were in the test
dataset. The ave rate: number in the right-hand column is how many
bytes per second were processed. "naive" means the string-based idiom
sketched above and "strch" means using the StringChain class.

_buildup init_naive
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:890, ave
rate: 58350579
N:  131072, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:265, ave
rate: 34800398
N:  262144, time: best:0.01,   2th-best:0.01, ave:0.01,
2th-worst:0.01, worst:0.01 (of  5), reps/s: 79, ave
rate: 20745346
N:  524288, time: best:0.05,   2th-best:0.05, ave:0.05,
2th-worst:0.05, worst:0.05 (of  5), reps/s: 20, ave
rate: 10823850
_buildup init_strch
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:  25543, ave
rate: 1674043282
N:  131072, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:  14179, ave
rate: 1858538925
N:  262144, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:   8016, ave
rate: 2101513050
N:  524288, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:   4108, ave
rate: 2154215572
_consume init_naive_loaded
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:931, ave
rate: 61037862
N:  131072, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:270, ave
rate: 35454393
N:  262144, time: best:0.01,   2th-best:0.01, ave:0.01,
2th-worst:0.01, worst:0.01 (of  5), reps/s: 74, ave
rate: 19471963
N:  524288, time: best:0.05,   2th-best:0.05, ave:0.05,
2th-worst:0.05, worst:0.06 (of  5), reps/s: 19, ave
rate: 10146747
_consume init_strch_loaded
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:   4309, ave
rate: 282447500
N:  131072, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:   2313, ave
rate: 303263357
N:  262144, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:   1186, ave
rate: 311159052
N:  524288, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:606, ave
rate: 317814669
_randomy init_naive
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:479, ave
rate: 31450561
N:  131072, time: best:0.01,   2th-best:0.01, ave:0.01,
2th-worst:0.01, worst:0.01 (of  5), reps/s:140, ave
rate: 18461191
N:  262144, time: best:0.02,   2th-best:0.02, ave:0.02,
2th-worst:0.03, worst:0.03 (of  5), reps/s: 42, ave
rate: 11127714
N:  524288, time: best:0.06,   2th-best:0.07, ave:0.08,
2th-worst:0.08, worst:0.09 (of  5), reps/s: 13, ave
rate:  6906341
_randomy init_strch
N:   65536, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:973, ave
rate: 63827127
N:  131072, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:495, ave
rate: 64970669
N:  262144, time: best:0.00,   2th-best:0.00, ave:0.00,
2th-worst:0.00, worst:0.00 (of  5), reps/s:239, ave
rate: 62913360
N:  524288, time: best:0.01,   2th-best:0.01, ave:0.01,
2th-worst:0.01, worst:0.01 (of  5), reps/s:121, ave
rate: 63811569

The naive approach is slower than the StringChain class, and the
bigger the dataset the slower it goes. 

execute bash builtins in python

2010-03-11 Thread alex goretoy
hi,
i'm trying to write a section of my program that needs to run bash builtin
alias and declare, i've googled and tried every type of example i could find
no to avail. this is what I've tried below and it doesn't work, is there a
way for me to execute a bah builin from python? what i need is to take alias
output and pipe it to another command with python and return the results to
a string or list.

>>> p1=Popen(["alias"],stdout=PIPE)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

if i add shell=True i get empty string and no thins error message

>>> p1=Popen(["alias"],stdout=PIPE,shell=True)
>>> p1

>>> p1.stdout.read()
''

thank you,
-Alex Goretoy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bypass UAC control through python script (to be run from batchfile)

2010-03-11 Thread Michel Claveau - MVP
Hi !

Install a resident soff (script) by task-planified, in Administrator
rights. Then, call this script from current work, for bypass UAC.

@-salutations
-- 
Michel Claveau
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inspect.stack() and frame

2010-03-11 Thread Gabriel Genellina
En Thu, 11 Mar 2010 21:03:07 -0300, Félix-Antoine Fortin  
 escribió:

On Mar 11, 6:22 pm, "Alf P. Steinbach"  wrote:

* F lix-Antoine Fortin:

> Given this code :
> # Experience with frame
> import sys
> import inspect

> def foo():
> stack = inspect.stack()
> print "foo frame : " + str(hex(id(sys._getframe(

> def foo2():
> inspect.stack()
> print "foo2 frame : " + str(hex(id(sys._getframe(

> def bar():
> print "bar frame : " + str(hex(id(sys._getframe(

> foo()
> foo()

> foo2()
> foo2()

> bar()
> bar()

> Output example :
> foo frame : 0x84d2c0
> foo frame : 0x844bf0
> foo2 frame : 0x898c90
> foo2 frame : 0x898c90
> bar frame : 0x898f70
> bar frame : 0x898f70

> Why are the ids (address) of the frame for each foo call not the same?

You're dealing with Python objects. You're not dealing with the  
computer's
machine stack. Whether you get the same id for two objects whose  
lifetimes don't
overlap depends on the implementation's memory and id allocation  
strategy.




Okay, I thought I got that when I read the id documentation, but now I
get it.
So the only to compare two ids, is by making sure their lifetimes
overlap. In
this case, instead of keeping the ids, I have to keep a reference on
the frame
to make sure it is still alive when I will compare it with a second
one.


The best way to compare object identities is using the 'is' operator: `a  
is b` returns true if and only if after evaluating both operands they are  
the very same object.

id() may be misleading if you are not careful:

py> id([]) == id([])
True
py> [] is []
False


> Or why the call to "stack = inspect.stack()" change the address of the
> frame?

Does it?


Yeah it does... I always get N different id when I run foo() N times
in a row.


Think again after reading the response below.


Actually, what you said about lifetime applies here too. Here is
another quick
snippet :

import sys
import inspect

def foo():
stack = inspect.stack()
return sys._getframe()

def foo2():
stack = inspect.stack()
del stack
return sys._getframe()

def bar():
inspect.stack()
return sys._getframe()

frame_foo = foo()
frame_foo2 = foo2()
frame_bar = bar()

print sys.getrefcount(frame_foo)
print sys.getrefcount(frame_foo2)
print sys.getrefcount(frame_bar)

Output :
3
2
2

So it seems that there is one more reference to the foo frame because
only because of "stack = inspect.stack()", so its lifetime isn't done
contrary to foo2 and bar frame, and the frame id of a foo frame is
different for each call.

Now, what is keeping a reference on foo frame?


The foo frame keeps a reference to the 'stack' local variable (in its  
f_locals attribute), and 'stack' keeps a reference to the current frame  
too.
This doesn't happen neither in foo2() nor bar(), where the local array is  
empty. inspect.stack() isn't special: any other reference to the current  
frame would have the same effect.


Let's examine the simple example above:

py> id([]) == id([])
True

Python creates an empty list, takes its id, and discards the list. The  
list object is then ready to be re-used again (the interpreter keeps a  
list of free objects for many common types), so the right-hand side gets  
the very same list.


The same thing happens with the frame object in your first examples foo2()  
and bar(): the frame object is discarded after leaving the function, and  
is ready to be used again in the next call. But foo() creates a circular  
reference - the frame object is still alive after leaving the first call,  
so the second call must use a new frame. (The garbage collector will,  
eventually, break the cycle and free those objects, but not very soon).


--
Gabriel Genellina

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


ANNOUNCE: RSON v 0.02 available

2010-03-11 Thread Patrick Maupin
RSON (Readable Serial Object Notation) is a superset of JSON that is
suitable for files that humans have to edit and diff.

The current release is decoder-only, but the decoder will read files
encoded by JSON encoders such as json or simplejson.

The current release consists of a single Python module and a short
manual.  The manual admittedly needs some work, but has a few syntax
examples.

http://code.google.com/p/rson/
-- 
http://mail.python.org/mailman/listinfo/python-list


Any downsides to UPX-ing my 32-bit Python 2.6.4 EXE/PYD/DLL files?

2010-03-11 Thread python
Are there any downsides to UPX-ing my 32-bit Python 2.6.4
development environment EXE/PYD/DLL files?

The reason I'm asking is that I frequently use a custom PY2EXE
script that UPX's copies of these files on every build.

Yes, I could get fancy and try to cache UPXed files, but I think
a simpler, safer, and higher performance solution would be for me
to just UPX my Python 2.6.4 directory once and be done with it.

What are your thoughts?

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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Steven D'Aprano
On Thu, 11 Mar 2010 07:56:59 -0500, Neal Becker wrote:

> The example I showed was just a toy problem.  The real problem is I
> expect to call a function many times, and I want to avoid the overhead
> of the 'if blah' everytime.

Unless the __call__ methods are very small, the overhead of one extra if 
and one extra attribute lookup will be insignificant.


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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Steven D'Aprano
On Thu, 11 Mar 2010 08:20:14 -0800, Steve Howell wrote:

>> (2) special methods like __call__ are only called on the class, not the
>> instance, so you can't give each instance its own method.
>>
>>
> Are you sure about that?  This program prints 1, 2, 1, 2.

The rules for classic classes are different. Since classic classes have 
gone away in 3.0, and are becoming rarer in 2.x, I didn't bother to 
mention the complication.



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


Re: Reverse engineering CRC?

2010-03-11 Thread Lawrence D'Oliveiro
In message <7vlamef7g...@mid.individual.net>, Gregory Ewing wrote:

> I'm going by the fact that the application reports a
> "CRC mismatch" when it's wrong. I can't be sure that what
> it calls a "CRC" is really a true CRC, but it's more than
> a simple sum, because changing one bit in the file results
> in a completely different value.

They could be using a strong cryptographic hash and truncating it to 16 bits 
or something.

In which case you’ve got your work cut out for you...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reverse engineering CRC?

2010-03-11 Thread Lawrence D'Oliveiro
In message , Dave Angel 
wrote:

> However, if there's anything in there about how to derive the polynomial
> algorithm from (a few) samples I missed it entirely.

Given that CRC is all just a sequence of xor operations, what happens if you 
xor various pairs of CRCs together, wouldn’t that cancel out at least parts 
of the operations?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My four-yorkshireprogrammers contribution

2010-03-11 Thread Albert van der Horst
In article <7vdo8sfre...@mid.individual.net>,
Gregory Ewing   wrote:
>MRAB wrote:
>
>> By the standards of just a few years later, that's not so much a
>> microcomputer as a nanocomputer!
>
>Although not quite as nano as another design published
>in EA a couple of years earlier, the EDUC-8:
>
>   http://www.sworld.com.au/steven/educ-8/
>
>It had a *maximum* of 256 bytes -- due to the architecture,
>there was no way of addressing any more. Also it was
>divided into 16-byte pages, with indirect addressing
>required to access anything in a different page from
>the instruction. Programming for it must have been
>rather challenging.
>
>As far as I know, the EDUC-8 is unique in being the
>only computer design ever published in a hobby magazine
>that *wasn't* based on a microprocessor -- it was all
>built out of 9000 and 7400 series TTL logic chips!

There was the 74 computer in Elektuur (now Elektor).
That was a quite respectable computer, built (you guessed)
from 74-series chips. How many were built, I don't know.

>
>--
>Greg

Groetjes Albert

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

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


Re: Docstrings considered too complicated

2010-03-11 Thread Albert van der Horst
In article ,
Grant Edwards   wrote:
>On 2010-03-03, Grant Edwards  wrote:
>> On 2010-03-03, Gregory Ewing  wrote:
>>> Grant Edwards wrote:
>>>
 Just a mediocre copy of the CP/M filesystem, which was in turn
 copied from DEC's RSTS or RSX.
>>>
>>> It was actually an improvement over CP/M's file system. CP/M
>>> didn't have hierarchical directories
>>
>> Neither did the original MS-DOS filesystem.
>>
>>> or timestamps and recorded file sizes in 128-byte blocks
>>> rather than bytes.
>>
>> I thought that was true of the original MS-DOS filesystem as
>> well, but I wouldn't bet money on it.
>
>I definitely remember that old MS-DOS programs would treat
>Ctrl-Z as an EOF marker when it was read from a text file and
>would terminate a text file with a Ctrl-Z when writing one.
>
>I don't know if that was because the underlying filesystem was
>still did everything in blocks or if it was because those
>MS-DOS programs were direct ports of CP/M programs.  I would
>have sworn that the orignal MS-DOS file API was FCB based and
>worked almost exactly like CP/M.  IIRC, the "byte stream" API
>showed up (in the OS) sever versions later.  The byte stream
>API was implemented by many compiler vendor's C libraries on
>top of the block-oriented FCB API.

My programming reference manual for MSDOS 6.0 (1993)
states the FCB stuff as "superseded" (not obsolete or obsolescent).
It states:
"A programmer should not use a superseded function except to
maintain compatibility with versions of MS-DOS earlier than
version 2.0."

FCB did *not* support paths, but you could access the current
directory.

>--
>Grant Edwards   grant.b.edwardsYow! I had a lease on an

Groetjes Albert

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

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


Re: file seek is slow

2010-03-11 Thread Steve Holden
Metalone wrote:
> I just tried the seek test with Cython.
> Cython fseek() : 1.059 seconds.  30% slower than 'C'
> Python f.seek  : 1.458 secondds. 80% slower than 'C'.
> 
> It is amazing to me that Cython generates a 'C' file that is 1478
> lines.
> 
And what response are you seeking to your amazement?

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: EOFError: EOF when reading a line

2010-03-11 Thread Gabriel Genellina
En Thu, 11 Mar 2010 06:12:02 -0300, Mihir Patel   
escribió:



I am trying to use the subprocess to send the data to child process. I
am not sure why i keep getting  "EOFError: EOF when reading a line"

command_line = 'python test_input.py'
p =subprocess.Popen(command_line, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
print 'output:', p.communicate()[0]

o,e = p.communicate('test')


You may call communicate at most once.

--
Gabriel Genellina

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


Re: inspect.stack() and frame

2010-03-11 Thread Félix-Antoine Fortin
On Mar 11, 6:22 pm, "Alf P. Steinbach"  wrote:
> * F lix-Antoine Fortin:
>
> > Given this code :
> > # Experience with frame
> > import sys
> > import inspect
>
> > def foo():
> >     stack = inspect.stack()
> >     print "foo frame : " + str(hex(id(sys._getframe(
>
> hex returns a string. applying str is therefore redundant.
>

My bad.

>
>
>
>
> > def foo2():
> >     inspect.stack()
> >     print "foo2 frame : " + str(hex(id(sys._getframe(
>
> > def bar():
> >     print "bar frame : " + str(hex(id(sys._getframe(
>
> > foo()
> > foo()
>
> > foo2()
> > foo2()
>
> > bar()
> > bar()
>
> > Output example :
> > foo frame : 0x84d2c0
> > foo frame : 0x844bf0
> > foo2 frame : 0x898c90
> > foo2 frame : 0x898c90
> > bar frame : 0x898f70
> > bar frame : 0x898f70
>
> > Why are the ids (address) of the frame for each foo call not the same?
>
> You're dealing with Python objects. You're not dealing with the computer's
> machine stack. Whether you get the same id for two objects whose lifetimes 
> don't
> overlap depends on the implementation's memory and id allocation strategy.
>

Okay, I thought I got that when I read the id documentation, but now I
get it.
So the only to compare two ids, is by making sure their lifetimes
overlap. In
this case, instead of keeping the ids, I have to keep a reference on
the frame
to make sure it is still alive when I will compare it with a second
one.

Thanks!

> > Or why the call to "stack = inspect.stack()" change the address of the
> > frame?
>
> Does it?
>

Yeah it does... I always get N different id when I run foo() N times
in a row.
Actually, what you said about lifetime applies here too. Here is
another quick
snippet :

import sys
import inspect

def foo():
stack = inspect.stack()
return sys._getframe()

def foo2():
stack = inspect.stack()
del stack
return sys._getframe()

def bar():
inspect.stack()
return sys._getframe()

frame_foo = foo()
frame_foo2 = foo2()
frame_bar = bar()

print sys.getrefcount(frame_foo)
print sys.getrefcount(frame_foo2)
print sys.getrefcount(frame_bar)

Output :
3
2
2

So it seems that there is one more reference to the foo frame because
only because of "stack = inspect.stack()", so its lifetime isn't done
contrary to foo2 and bar frame, and the frame id of a foo frame is
different for each call.

Now, what is keeping a reference on foo frame?

Thanks Alf,

Felix

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


Re: pivot() equivalent

2010-03-11 Thread MRAB

gundlach wrote:

I *know* this already exists, but I can't remember where:

def pivot(func, seq):
  # I know, a good implementation shouldn't call func() twice per item
  return ( (x for x in seq if func(x)), (x for x in seq if not
func(x)) )

I feel like I read a thread in which this was argued to death, and I
can't find that either.

The scenario: I have a sequence of lines from a file.  I want to split
it into those lines that contain a substring, and those that don't.  I
want it to be more efficient and prettier than

with = [x for x in lines if substring in x]
without = [x for x in lines if substring not in x]

Does this exist?


The clearest way is just:

def pivot(func, seq):
with, without = [], []
for x in seq:
if func(x):
with.append(x)
else:
without.append(x)
return with, without
--
http://mail.python.org/mailman/listinfo/python-list


Re: pivot() equivalent

2010-03-11 Thread John Posner

On 3/11/2010 6:16 PM, gundlach wrote:

I *know* this already exists, but I can't remember where:

def pivot(func, seq):
   # I know, a good implementation shouldn't call func() twice per item
   return ( (x for x in seq if func(x)), (x for x in seq if not
func(x)) )

I feel like I read a thread in which this was argued to death, and I
can't find that either.

The scenario: I have a sequence of lines from a file.  I want to split
it into those lines that contain a substring, and those that don't.  I
want it to be more efficient and prettier than

with = [x for x in lines if substring in x]
without = [x for x in lines if substring not in x]

Does this exist?

TIA,
Michael


Try this:

> type lines.txt
the
quick
brown
fox
jumps
over
the
lazy
dog

> type pivot.py

def pivot(pivot_function, filename):
a_list = []
b_list = []
for line in open(filename):
target_list = a_list if pivot_function(line) else b_list
target_list.append(line[:-1])
return (a_list, b_list)

print pivot(lambda line: 'e' in line, "lines.txt")

> python pivot.py
(['the', 'over', 'the'], ['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog'])

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


Re: Bluetooth

2010-03-11 Thread PythonAB

On 10 mrt 2010, at 16:17, Jeff Peck wrote:

> On 3/7/2010 1:26 PM, PythonAB wrote:
>> 
>> Hello,
>> 
>> 
>> I'm writing a script that has to connect a bluetooth device
>> with a 3D application.
>> On my search for a bluetooth module i ran into this:
>> http://www.python.org/download/releases/2.4/notes/
>> where it says:
>> "The socket module now supports Bluetooth sockets,
>> if the system has "
>> 
>> Yet I can't find anything at all in the socket manuals
>> nor anywhere else on the site...
>> 
>> Does the socket module really support Bluetooth out of the box?
>> I've used socket before but never read anything about bluetooth...
>> 
>> Maybe I need to get the bluetooth.h file, but i have no idea what I
>> am supposed to do with it...
>> 
>> Can anyone shed some light on this for me please?
>> 
>> thanks in advance,
>> 
>> gr
>> Arno
> 
> Have you come across the PyBluez library yet? I recently used it in a project 
> and it worked very well.
> 
> Hth,
> Jeff

Hi Jeff,

Yes I ran into it, but couldn't find an OSX version...
I'm using the lightblue module now but would have preferred
to use the socket module without compiling python myself.

Sorry for not mentioning the platform.

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


Re: inspect.stack() and frame

2010-03-11 Thread Alf P. Steinbach

* Félix-Antoine Fortin:

Given this code :
# Experience with frame
import sys
import inspect

def foo():
stack = inspect.stack()
print "foo frame : " + str(hex(id(sys._getframe(


hex returns a string. applying str is therefore redundant.



def foo2():
inspect.stack()
print "foo2 frame : " + str(hex(id(sys._getframe(

def bar():
print "bar frame : " + str(hex(id(sys._getframe(

foo()
foo()

foo2()
foo2()

bar()
bar()

Output example :
foo frame : 0x84d2c0
foo frame : 0x844bf0
foo2 frame : 0x898c90
foo2 frame : 0x898c90
bar frame : 0x898f70
bar frame : 0x898f70

Why are the ids (address) of the frame for each foo call not the same?


You're dealing with Python objects. You're not dealing with the computer's 
machine stack. Whether you get the same id for two objects whose lifetimes don't 
overlap depends on the implementation's memory and id allocation strategy.




Or why the call to "stack = inspect.stack()" change the address of the
frame?


Does it?


Cheers,

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


Re: Parsing Email Headers

2010-03-11 Thread T
Thanks for your suggestions!  Here's what seems to be working - it's
basically the same thing I originally had, but first checks to see if
the line is blank

response, lines, bytes = M.retr(i+1)
# For each line in message
for line in lines:
if not line.strip():
M.dele(i+1)
break

emailMessage = email.message_from_string(line)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")
-- 
http://mail.python.org/mailman/listinfo/python-list


pivot() equivalent

2010-03-11 Thread gundlach
I *know* this already exists, but I can't remember where:

def pivot(func, seq):
  # I know, a good implementation shouldn't call func() twice per item
  return ( (x for x in seq if func(x)), (x for x in seq if not
func(x)) )

I feel like I read a thread in which this was argued to death, and I
can't find that either.

The scenario: I have a sequence of lines from a file.  I want to split
it into those lines that contain a substring, and those that don't.  I
want it to be more efficient and prettier than

with = [x for x in lines if substring in x]
without = [x for x in lines if substring not in x]

Does this exist?

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


Re: Platform Requirement Checker (was Does this already exists?: A module that checks if the used platform is supported)

2010-03-11 Thread MRAB

Martin P. Hellwig wrote:

On 03/11/10 01:37, Gabriel Genellina wrote:

En Wed, 10 Mar 2010 10:54:27 -0300, Martin P. Hellwig
 escribió:


Before I start reinventing a squared wheel, I have the following
question:
Is there already a (standard) module that wraps around the various
os/sys information which checks if the platform + version is supported
for what I want to do with it.


In case you were not aware of it: see the platform module. But you'll 
have

to do the checks yourself (based on the info it returns).


Thanks for the reminder, it indeed slipped my mind.

As Python features are luckily mostly platform independent, I am not 
sure if a convenient 'platform requirement check' module would be worth 
the energy creating it, any thoughts on that?


For clarity purpose I re'added (and adapted) in what lines I was thinking:
-
test = RequirePlatform(do_not_raise=True)
# If do_not_raise is not set or False, an error will be raised
# after a failed require line, otherwise just continue.

test.require(key='test1', platform='freebsd', version_min='2.2')
# If the platform is anything else but FreeBSD 2.2 onwards return False
# and store the result of the test.
# Result of the require (and any additional tests
# can be accessed using the index operators;

if test['test1']:
print('supported')
-
Other requirements like architecture, python vm type/version, cpu 
features, etc. Might also be nice to have.



It might be useful for up-front checking in those platform-specific
scripts, although the platform module might already be fulfilling that
need.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing Email Headers

2010-03-11 Thread MRAB

T wrote:

On Mar 11, 3:13 pm, MRAB  wrote:

T wrote:

All I'm looking to do is to download messages from a POP account and
retrieve the sender and subject from their headers.  Right now I'm 95%
of the way there, except I can't seem to figure out how to *just* get
the headers.  Problem is, certain email clients also include headers
in the message body (i.e. if you're replying to a message), and these
are all picked up as additional senders/subjects.  So, I want to avoid
processing anything from the message body.
Here's a sample of what I have:
# For each line in message
for j in M.retr(i+1)[1]:
# Create email message object from returned string
emailMessage = email.message_from_string(j)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")
I also tried using the following, but got the same results:
 emailMessage =
email.Parser.HeaderParser().parsestr(j, headersonly=True)
Any help would be appreciated!

If you're using poplib then use ".top" instead of ".retr".


I'm still having the same issue, even with .top.  Am I missing
something?

for j in M.top(i+1, 0)[1]:
emailMessage = email.message_from_string(j)
#emailMessage =
email.Parser.HeaderParser().parsestr(j, headersonly=True)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")

Is there another way I should be using to retrieve only the headers
(not those in the body)?


The documentation does say:

  """unfortunately, TOP is poorly specified in the RFCs and is
frequently broken in off-brand servers."""

All I can say is that it works for me with my ISP! :-)
--
http://mail.python.org/mailman/listinfo/python-list


inspect.stack() and frame

2010-03-11 Thread Félix-Antoine Fortin
Given this code :
# Experience with frame
import sys
import inspect

def foo():
stack = inspect.stack()
print "foo frame : " + str(hex(id(sys._getframe(

def foo2():
inspect.stack()
print "foo2 frame : " + str(hex(id(sys._getframe(

def bar():
print "bar frame : " + str(hex(id(sys._getframe(

foo()
foo()

foo2()
foo2()

bar()
bar()

Output example :
foo frame : 0x84d2c0
foo frame : 0x844bf0
foo2 frame : 0x898c90
foo2 frame : 0x898c90
bar frame : 0x898f70
bar frame : 0x898f70

Why are the ids (address) of the frame for each foo call not the same?
Or why the call to "stack = inspect.stack()" change the address of the
frame?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: file seek is slow

2010-03-11 Thread Metalone
I just tried the seek test with Cython.
Cython fseek() : 1.059 seconds.  30% slower than 'C'
Python f.seek  : 1.458 secondds. 80% slower than 'C'.

It is amazing to me that Cython generates a 'C' file that is 1478
lines.


#Cython code

import time

cdef int SEEK_SET = 0

cdef extern from "stdio.h":
void* fopen(char* filename, char* mode)
int fseek(void*, long, int)

def main():
cdef void* f1 = fopen('video.txt', 'rb')
cdef int i=100
t0 = time.clock()
while i > 0:
   fseek(f1, 0, SEEK_SET)
   i -= 1
delta = time.clock() - t0
print "%.3f" % delta

if __name__ == '__main__':
main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where can I find documentation for data[:,9]

2010-03-11 Thread Cal Who

"Martin P. Hellwig"  wrote in message 
news:hnbq8q$vg...@news.eternal-september.org...
> On 03/11/10 22:08,  Cal Who wrote:
> 
>> Thanks, that helped a lot.
>>
>> I'm having trouble knowing what to search for to find documenatation. For
>> example, is print a Python command, a numpy command or a java command?
>>
>> I like to read the documentation even if the command is working for me.
>>
>>
>> Thanks again
>>
>>
> Probably for you the right way would be to familiarize yourself with the 
> namespace concept of Python, this makes it easier to identify whether 
> something is built-in, a standard module or an external module.
> Which makes it much easier to feed google the right clues.
>
> -- 
> mph

Thanks a lot, I'll look that up now. 


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


Platform Requirement Checker (was Does this already exists?: A module that checks if the used platform is supported)

2010-03-11 Thread Martin P. Hellwig

On 03/11/10 01:37, Gabriel Genellina wrote:

En Wed, 10 Mar 2010 10:54:27 -0300, Martin P. Hellwig
 escribió:


Before I start reinventing a squared wheel, I have the following
question:
Is there already a (standard) module that wraps around the various
os/sys information which checks if the platform + version is supported
for what I want to do with it.


In case you were not aware of it: see the platform module. But you'll have
to do the checks yourself (based on the info it returns).


Thanks for the reminder, it indeed slipped my mind.

As Python features are luckily mostly platform independent, I am not 
sure if a convenient 'platform requirement check' module would be worth 
the energy creating it, any thoughts on that?


For clarity purpose I re'added (and adapted) in what lines I was thinking:
-
test = RequirePlatform(do_not_raise=True)
# If do_not_raise is not set or False, an error will be raised
# after a failed require line, otherwise just continue.

test.require(key='test1', platform='freebsd', version_min='2.2')
# If the platform is anything else but FreeBSD 2.2 onwards return False
# and store the result of the test.
# Result of the require (and any additional tests
# can be accessed using the index operators;

if test['test1']:
print('supported')
-
Other requirements like architecture, python vm type/version, cpu 
features, etc. Might also be nice to have.



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


Re: Parsing Email Headers

2010-03-11 Thread T
On Mar 11, 3:13 pm, MRAB  wrote:
> T wrote:
> > All I'm looking to do is to download messages from a POP account and
> > retrieve the sender and subject from their headers.  Right now I'm 95%
> > of the way there, except I can't seem to figure out how to *just* get
> > the headers.  Problem is, certain email clients also include headers
> > in the message body (i.e. if you're replying to a message), and these
> > are all picked up as additional senders/subjects.  So, I want to avoid
> > processing anything from the message body.
>
> > Here's a sample of what I have:
>
> >                 # For each line in message
> >                 for j in M.retr(i+1)[1]:
> >                     # Create email message object from returned string
> >                     emailMessage = email.message_from_string(j)
> >                     # Get fields
> >                     fields = emailMessage.keys()
> >                     # If email contains "From" field
> >                     if emailMessage.has_key("From"):
> >                         # Get contents of From field
> >                         from_field = emailMessage.__getitem__("From")
>
> > I also tried using the following, but got the same results:
> >                  emailMessage =
> > email.Parser.HeaderParser().parsestr(j, headersonly=True)
>
> > Any help would be appreciated!
>
> If you're using poplib then use ".top" instead of ".retr".

I'm still having the same issue, even with .top.  Am I missing
something?

for j in M.top(i+1, 0)[1]:
emailMessage = email.message_from_string(j)
#emailMessage =
email.Parser.HeaderParser().parsestr(j, headersonly=True)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")

Is there another way I should be using to retrieve only the headers
(not those in the body)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where can I find documentation for data[:,9]

2010-03-11 Thread Martin P. Hellwig

On 03/11/10 22:08,  Cal Who wrote:


Thanks, that helped a lot.

I'm having trouble knowing what to search for to find documenatation. For
example, is print a Python command, a numpy command or a java command?

I like to read the documentation even if the command is working for me.


Thanks again


Probably for you the right way would be to familiarize yourself with the 
namespace concept of Python, this makes it easier to identify whether 
something is built-in, a standard module or an external module.

Which makes it much easier to feed google the right clues.

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


Re: Where can I find documentation for data[:,9]

2010-03-11 Thread Cal Who

"Robert Kern"  wrote in message 
news:mailman.631.1268335358.23598.python-l...@python.org...
> On 2010-03-11 13:01 PM,  Cal Who wrote:
>> data = readdata( 'data/input.dat', delimiter = ',' )
>>
>> input = data[:, :9]#nine data columns
>>
>>
>>
>> Where can I find documentation for the
>>
>> data[:,9]
>>
>> in the code above.
>>
>> Been looking and found many examples but would like to know the 
>> definition.
>
> When asking questions like this, it helps *a lot* to provide a complete 
> example, not just a snippet. If I weren't already intimately familiar with 
> the library you are using, I would have no idea how to help you.
>
> However, I do know that input object is a numpy array, and the syntax you 
> are asking about is multidimensional slicing.
>
> http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
>
>> I need to skip the first column and then read 9
>
> data[:, 1:10]
>
>> I would also like to print the data in ihe variable "input"
>
> print input
>
> -- 
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless 
> enigma
>  that is made terrible by our own mad attempt to interpret it as though it 
> had
>  an underlying truth."
>   -- Umberto Eco
>
Thanks, that helped a lot.

I'm having trouble knowing what to search for to find documenatation. For 
example, is print a Python command, a numpy command or a java command?

I like to read the documentation even if the command is working for me.


Thanks again 


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


Re: Reverse engineering CRC?

2010-03-11 Thread Gregory Ewing

Steve Howell wrote:


Hi Greg.  I would at least flip one bit at a time on the first byte of
your data to see if the transformation is bitwise.


I'm actually making good progress on this -- it turns out
there *is* a way of deducing the polynomial by looking at
the effect of single-bit flips. It's actually quite simple,
with no brute-force searching needed at all.

Things get a bit tricky when you don't quite know all
of the data that goes into the CRC, though, which seems
to be the case here...

I'm writing up an essay on my experiences. I'll post a
link when it's finished.

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


Re: Python, Reportlabs, Pil and Windows 7 (64bit)

2010-03-11 Thread Astley Le Jasper
@Robin
Thanks. I thought that this seemed to be a general python thing
because it was effecting both installs. However, after also reading
Martin's comments ...

@Martin
> This is somewhat imprecise: is it
> a) that your CPU is AMD64, and thus supports 64-bit mode, or
> b) that *in addition*, your Windows 7 installation is a 64-bit
> installation, or
> c) that *in addition*, your Python installation is also a 64-bit
>installation.
>
> Unless you have a specific need for 64-bit mode, I recommend that you
> use the 32-bit version of Windows (unless you have more than 4GB of
> main memory), and (even if you have a 64-bit Windows) you install the
> 32-bit version of Python on it (unless you have the need to access more
> than 2GB of objects in your Python applications.


Sorry. I have Windows 7 (64-bit) installed on a machine with an AMD
cpu (which supports 64-bit mode), with a 64-bit version of
(Activestate) python 2.6  although I didn't realise the later
until I looked just now.

> Install the 32-bit version of Python, and these installers should work fine.
Well, I uninstalled the 64-bit version and the installers did indeed
work.

I’m sorry everyone. I didn’t realise I had installed the 64-bit
version of Python. Well, at least someone else might find have the
same problem. But I think that there is going to be a bit of a rough
patch as everyone moves over to 64-bit.

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


Re: Parsing Email Headers

2010-03-11 Thread Grant Edwards
On 2010-03-11, T  wrote:
> All I'm looking to do is to download messages from a POP account and
> retrieve the sender and subject from their headers.  Right now I'm 95%
> of the way there, except I can't seem to figure out how to *just* get
> the headers.

The headers are saparated from the body by a blank line.

> Problem is, certain email clients also include headers in the message
> body (i.e. if you're replying to a message), and these are all picked
> up as additional senders/subjects.  So, I want to avoid processing
> anything from the message body.

Then stop when you see a blank line.

Or retreive just the headers.

-- 
Grant Edwards   grant.b.edwardsYow! My life is a patio
  at   of fun!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Visual Python programming and decompilers?

2010-03-11 Thread Stef Mientki

On 11-03-2010 19:38, Ludolph wrote:

Hi Guys

At work I have been exposed to a Agile Platform called OutSystems. It
allows you to visually program your web applications
http://i.imgur.com/r2F0i.png and I find the idea very intriguing.

   

Although not as low level as you want,
http://mientki.ruhosting.nl/data_www/pylab_works/pw_animations_screenshots.html
http://code.google.com/p/pylab-works/
and here an overview of similar packages

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


Re: Visual Python programming and decompilers?

2010-03-11 Thread geremy condra
On Thu, Mar 11, 2010 at 1:38 PM, Ludolph  wrote:
> Hi Guys
>
> At work I have been exposed to a Agile Platform called OutSystems. It
> allows you to visually program your web applications
> http://i.imgur.com/r2F0i.png and I find the idea very intriguing.
>
> So I have started to play around with the idea on how will I be able
> to visually represent Python code as in the above image and then allow
> the programmer to change some of the flow/code/logic visually and then
> get it back as python source code. I don't know if this have been
> tried before and after some googling I can't find anything like this,
> so maybe I'm just lacking basic googling skills or a python solution
> like the above does not exist yet.
>
> If anybody knows of such solution please let me know, so that I don't
> spend a lot of time recreating the wheel. Otherwise help me out on the
> following problem:
>
> I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to
> disassemble the code to workable objects, It even allows me to rebuild
> the objects to bytecode. So if I define patterns on how python
> interrupts the source code to bytecode I can visually represent this
> and also so convert my visual representations back to bytecode.
>
> The only problem I have at the moment is how will I get this bytecode
> back to python source code. I have googled for python decompiler but
> only found old projects like unpyc, decompyle and some online
> services. I would like to know if anybody know of a well maintained or
> at least recent module that can help me accomplish the above
> mentioned, because I'm hoping I can implement this in Python 3.1.
>
> So any input or suggestion would be greatly appreciated.
>
> Kind Regards,
>
> --
> Ludolph Neethling
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Maybe something like http://www.pypes.org/?

They were at pycon and sounded pretty into what they were
talking about- I'd send them an email.

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


Re: Parsing Email Headers

2010-03-11 Thread MRAB

T wrote:

All I'm looking to do is to download messages from a POP account and
retrieve the sender and subject from their headers.  Right now I'm 95%
of the way there, except I can't seem to figure out how to *just* get
the headers.  Problem is, certain email clients also include headers
in the message body (i.e. if you're replying to a message), and these
are all picked up as additional senders/subjects.  So, I want to avoid
processing anything from the message body.

Here's a sample of what I have:

# For each line in message
for j in M.retr(i+1)[1]:
# Create email message object from returned string
emailMessage = email.message_from_string(j)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")

I also tried using the following, but got the same results:
 emailMessage =
email.Parser.HeaderParser().parsestr(j, headersonly=True)

Any help would be appreciated!


If you're using poplib then use ".top" instead of ".retr".
--
http://mail.python.org/mailman/listinfo/python-list


Re: importing modules from subdirs

2010-03-11 Thread Alex Hall
Halfway there. It imports now, but it says that the module does not
have functions which I know it does have. I will just leave it all in
one folder for now and play with organization after I get the project
working better.

On 3/11/10, Steve Holden  wrote:
> Alex Hall wrote:
>> Hi all,
>> The manual says, for modules in a project stored in subdirectories, you
>> can do:
>> import folderName.module
>>
>> I have a couple questions, though:
>> 1. Do I then have to call functions from module like
>> folder.module.function, or can I still use the normal module.function?
>>
>> 2. When I try to do this, it fails. I have an sw folder. Inside that I
>> have a modes folder, holding weather.pyw. Main.pyw, back in the sw
>> folder, is trying to import modes.weather, but nothing happens. I have
>> tried putting weather.pyw in its own weather folder under the modes
>> folder, but that also fails. I have placed an empty __init__.py file
>> in both the modes folder and the weather subfolder, but I cannot get
>> main.pyw to import weather!
>>
>> 3. How does weather import from a folder above or beside it? For
>> example, if a config directory is at the same level as the modes
>> directory, how can weather import something from config?
>>
>> Thanks!
>>
> I haven't checked this, but I believe .pyw names are only for main
> programs. Try renaming weather.pyw as weather.py and see if it makes any
> difference.
>
> regards
>  Steve
> --
> Steve Holden   +1 571 484 6266   +1 800 494 3119
> See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
> Holden Web LLC http://www.holdenweb.com/
> UPCOMING EVENTS:http://holdenweb.eventbrite.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


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


Parsing Email Headers

2010-03-11 Thread T
All I'm looking to do is to download messages from a POP account and
retrieve the sender and subject from their headers.  Right now I'm 95%
of the way there, except I can't seem to figure out how to *just* get
the headers.  Problem is, certain email clients also include headers
in the message body (i.e. if you're replying to a message), and these
are all picked up as additional senders/subjects.  So, I want to avoid
processing anything from the message body.

Here's a sample of what I have:

# For each line in message
for j in M.retr(i+1)[1]:
# Create email message object from returned string
emailMessage = email.message_from_string(j)
# Get fields
fields = emailMessage.keys()
# If email contains "From" field
if emailMessage.has_key("From"):
# Get contents of From field
from_field = emailMessage.__getitem__("From")

I also tried using the following, but got the same results:
 emailMessage =
email.Parser.HeaderParser().parsestr(j, headersonly=True)

Any help would be appreciated!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where can I find documentation for data[:,9]

2010-03-11 Thread Robert Kern

On 2010-03-11 13:01 PM,  Cal Who wrote:

data = readdata( 'data/input.dat', delimiter = ',' )

input = data[:, :9]#nine data columns



Where can I find documentation for the

data[:,9]

in the code above.

Been looking and found many examples but would like to know the definition.


When asking questions like this, it helps *a lot* to provide a complete example, 
not just a snippet. If I weren't already intimately familiar with the library 
you are using, I would have no idea how to help you.


However, I do know that input object is a numpy array, and the syntax you are 
asking about is multidimensional slicing.


http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html


I need to skip the first column and then read 9


data[:, 1:10]


I would also like to print the data in ihe variable "input"


print input

--
Robert Kern

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

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


Where can I find documentation for data[:,9]

2010-03-11 Thread Cal Who
data = readdata( 'data/input.dat', delimiter = ',' )

input = data[:, :9]#nine data columns



Where can I find documentation for the

data[:,9]

in the code above.

Been looking and found many examples but would like to know the definition.

I need to skip the first column and then read 9



I would also like to print the data in ihe variable "input"



Thanks


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


Re: file seek is slow

2010-03-11 Thread Metalone
I am assuming that Python delegates the f.seek call to the seek call
in the MS C runtime library msvcrt.dll.
Does anybody know a nice link to the Python source like was posted
above for the BSD 'C' library?

Ok, I ran some more tests.
C, seek: 0.812 seconds   // test from original post
Python, f.seek : 1.458 seconds.  // test from original post

C, time(&tm)   : 0.671 seconds
Python, time.time(): 0.513 seconds.
Python, ctypes.msvcrt.time(ctypes.byref(tm)): 0.971 seconds.   #
factored the overhead to be outside the loop, so really this was
func_ptr(ptr).

Perhaps I am just comparing apples to oranges.
I never tested the overhead of ctypes like this before.
Most of my problem timings involve calls through ctypes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I get the error: ImportError: No module named ffnet

2010-03-11 Thread Cal Who
I found out I had something installed wrong.

" Cal Who"  wrote in message 
news:hnb339$g2...@news.eternal-september.org...
>
>
> I have the .py file in Eclipse
> #...@pydevcodeanalysisignore
> from ffnet import ffnet, mlgraph
> topology = mlgraph( (2, 3, 1) )
> nn = ffnet(topology)
>
> I select RunAs / Python Run
>
> I get the error
> from ffnet import ffnet, mlgraph
> ImportError: No module named ffnet
>
> In the folder Python26\lib\site-packages I see
> ffnet-0.6.2-py2.6.egg-info
>
> also a folder ffnet
> in that folder there is a folder Examples
> If I click one of the examples pyrhon.exe opens and runs it OK.
>
> Is there enough in the above for you to tell me how to procede?
>
> Thanks in advance for any help at all
>
>
> 


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


Re: Python 2.6 and modules dbi AND odbc

2010-03-11 Thread Kev Dwyer
On Wed, 10 Mar 2010 16:17:29 -0800, robert somerville wrote:

> hi;
>  i am trying to get some legacy python code (which i no nothing about)
> working with tries to import dbi and odbc (the import fails ...) it
> looks like these modules are deprecated ?? if so is there a work around
> , if not deprecated, what am i doing wrong ?? i see no Ubuntu packages
> that look promising ..
 
Hello Robert,

These modules are distributed as part of the PyWin32 Python for Windows
 extensions, so they need to run on a Windows box.  They'll run fine 
there regardless of the deprecation of dbi (though "import dbi" will 
provide a useful warning message).

Pyodbc is a possible alternative.

Cheers,

Kev

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


Re: Get a method instance through 'getattr' but not superclass's method

2010-03-11 Thread Gabriel Genellina

En Thu, 11 Mar 2010 01:47:30 -0300, Radhakrishna Bhat
 escribió:


I am using getattr to get a method instance from a class. But it also
returns methods from the superclass. How to detect if an attribute is  
from

superclass?


You may look it up directly in the class dictionary (__dict__)

--
Gabriel Genellina

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


Visual Python programming and decompilers?

2010-03-11 Thread Ludolph
Hi Guys

At work I have been exposed to a Agile Platform called OutSystems. It
allows you to visually program your web applications
http://i.imgur.com/r2F0i.png and I find the idea very intriguing.

So I have started to play around with the idea on how will I be able
to visually represent Python code as in the above image and then allow
the programmer to change some of the flow/code/logic visually and then
get it back as python source code. I don't know if this have been
tried before and after some googling I can't find anything like this,
so maybe I'm just lacking basic googling skills or a python solution
like the above does not exist yet.

If anybody knows of such solution please let me know, so that I don't
spend a lot of time recreating the wheel. Otherwise help me out on the
following problem:

I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to
disassemble the code to workable objects, It even allows me to rebuild
the objects to bytecode. So if I define patterns on how python
interrupts the source code to bytecode I can visually represent this
and also so convert my visual representations back to bytecode.

The only problem I have at the moment is how will I get this bytecode
back to python source code. I have googled for python decompiler but
only found old projects like unpyc, decompyle and some online
services. I would like to know if anybody know of a well maintained or
at least recent module that can help me accomplish the above
mentioned, because I'm hoping I can implement this in Python 3.1.

So any input or suggestion would be greatly appreciated.

Kind Regards,

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


Re: Python Script to get Info from Site not working

2010-03-11 Thread Gabriel Genellina

En Wed, 10 Mar 2010 23:26:05 -0300, Jimbo  escribió:


On Mar 11, 12:38 pm, "Gabriel Genellina" 
wrote:

En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo  escribió:

> I found a semi Python & internet tutorial here if anyone else would
> like ithttp://www.upriss.org.uk/python/session6.html

Replace the last line with something like this, to see what you are  
getting exactly:


print("Sorry, no %r (%r) available." % (
   drink,
   cgi.escape(repr(type(drink)

BTW, which Python version are you using? The tutorial you linked to is  
aimed at Python 2.x, but your print syntax suggests you're using Python  
3.x



I am using 3.x, do you think I should be using something else?


Yes, you should use the same version as the tutorial you're following.
Learning how to program, CGI, HTML and Python at the same time is hard
enough to complicate it with incompatible version differences. I suggest
Python 2.6.4


Also
the your code I put in does the some thing, just now it says "Sorry no
none drink available" all the time.


As a general rule, always directly copy and paste the error messages you
get and the source code you execute. Do not retype or paraphrase them.
That's important for the rest of us to be able to help you - now and in
the future.

In this case, the message should read "Sorry, no None ()
available." (None, not none, is a built-in special object in Python). So
we know that drink is None instead of one of the expected values, and we
could start investigating why. But there is no point in digging further -
instead, downgrade to Python 2.6.4 and try again.
This might be a server issue, unrelated to your script. BTW, you didn't
mention the server software.

--
Gabriel Genellina

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


unable to run wxPython script: dll errors

2010-03-11 Thread Alex Hall
Hi all,
I am trying to run a file that should pop up a dialog. The dialog is
fine (I used XRCed to create it and running it from within that editor
brings up the dialog I want). When I run my file, though, I get this
traceback:

C:\Users\Alex>c:\python26\python.exe i:\arm\dictionary.py
Traceback (most recent call last):
  File "i:\arm\dictionary.py", line 2, in 
import wx
  File "c:\python26\lib\site-packages\wx-2.8-msw-unicode\wx\__init__.py", line 4
5, in 
from wx._core import *
  File "c:\python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 4, i
n 
import _core_
ImportError: DLL load failed: The specified procedure could not be found.

I am running win7x64, wxPython2.8, python2.6. Any ideas why this would
be happening? I found a forum post that talked about a manifest file
and some dll files being needed, but it did not make a lot of sense.
Thanks!


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


Re: use of multiple versions of python

2010-03-11 Thread David Robinow
On Thu, Mar 11, 2010 at 12:40 AM, Bujji  wrote:
> hi all,
> I have installed python 2.6 in addition to python 2.5 in my system
> Now for some modules(while installing ) it needs to use python 2.6
> how can i do that
> in case of easy_install what should i do to it to use python 2.6
 You should have an easy_install-2.5 and easy_install-2.6.  One of
them will be linked to easy_install, depending on how you did the
install. Just use the one you need.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: importing modules from subdirs

2010-03-11 Thread Steve Holden
Alex Hall wrote:
> Hi all,
> The manual says, for modules in a project stored in subdirectories, you can 
> do:
> import folderName.module
> 
> I have a couple questions, though:
> 1. Do I then have to call functions from module like
> folder.module.function, or can I still use the normal module.function?
> 
> 2. When I try to do this, it fails. I have an sw folder. Inside that I
> have a modes folder, holding weather.pyw. Main.pyw, back in the sw
> folder, is trying to import modes.weather, but nothing happens. I have
> tried putting weather.pyw in its own weather folder under the modes
> folder, but that also fails. I have placed an empty __init__.py file
> in both the modes folder and the weather subfolder, but I cannot get
> main.pyw to import weather!
> 
> 3. How does weather import from a folder above or beside it? For
> example, if a config directory is at the same level as the modes
> directory, how can weather import something from config?
> 
> Thanks!
> 
I haven't checked this, but I believe .pyw names are only for main
programs. Try renaming weather.pyw as weather.py and see if it makes any
difference.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Python, Reportlabs, Pil and Windows 7 (64bit)

2010-03-11 Thread Martin v. Loewis

>> I have a Windows 7 (64bit AMD) machine

This is somewhat imprecise: is it
a) that your CPU is AMD64, and thus supports 64-bit mode, or
b) that *in addition*, your Windows 7 installation is a 64-bit
installation, or
c) that *in addition*, your Python installation is also a 64-bit
   installation.

Unless you have a specific need for 64-bit mode, I recommend that you
use the 32-bit version of Windows (unless you have more than 4GB of
main memory), and (even if you have a 64-bit Windows) you install the
32-bit version of Python on it (unless you have the need to access more
than 2GB of objects in your Python applications.

>> 1. Reportlabs / Pil 32 installers - I've tried using these but they
>> can't find python. I also tried registering Python (http://effbot.org/
>> zone/python-register.htm) but this also fails.

Install the 32-bit version of Python, and these installers should work fine.

> Perhaps some expert on the python list knows which versions of VS
> support 64bit; I do have VS 2005/2008 etc, but I'll probably need to set
> up a 64bit machine to see if they will install on a 64bit architecture.

For Python 2.6 and later, use VS 2008. This comes with an AMD64
compiler. You technically don't need a 64-bit Windows, as it supports
cross-compilation (but you would need a 64-bit Windows to test it).

I personally build Python on a 32-bit machine, and move the MSI to a
64-bit machine for testing.

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


Re: importing modules from subdirs

2010-03-11 Thread Gary Herron

Alex Hall wrote:

Hi all,
The manual says, for modules in a project stored in subdirectories, you can do:
import folderName.module

I have a couple questions, though:
1. Do I then have to call functions from module like
folder.module.function, or can I still use the normal module.function?
  


Either, depending on how you do the import:

import folder.module
folder.module.function()

or

from folder.module import function
function()

or

from folder import module
module.function()


2. When I try to do this, it fails. I have an sw folder. Inside that I
have a modes folder, holding weather.pyw. Main.pyw, back in the sw
folder, is trying to import modes.weather, but nothing happens. I have
tried putting weather.pyw in its own weather folder under the modes
folder, but that also fails. I have placed an empty __init__.py file
in both the modes folder and the weather subfolder, but I cannot get
main.pyw to import weather!
  


Show us some code and a diagram of your forcer hierarchy, and we'll look 
at it.

3. How does weather import from a folder above or beside it? For
example, if a config directory is at the same level as the modes
directory, how can weather import something from config?
  


You don't import from up the hierarchy.  You can put a higher folder on 
sys.path, and get to it that way.


Gary Herron



Thanks!

  


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


importing modules from subdirs

2010-03-11 Thread Alex Hall
Hi all,
The manual says, for modules in a project stored in subdirectories, you can do:
import folderName.module

I have a couple questions, though:
1. Do I then have to call functions from module like
folder.module.function, or can I still use the normal module.function?

2. When I try to do this, it fails. I have an sw folder. Inside that I
have a modes folder, holding weather.pyw. Main.pyw, back in the sw
folder, is trying to import modes.weather, but nothing happens. I have
tried putting weather.pyw in its own weather folder under the modes
folder, but that also fails. I have placed an empty __init__.py file
in both the modes folder and the weather subfolder, but I cannot get
main.pyw to import weather!

3. How does weather import from a folder above or beside it? For
example, if a config directory is at the same level as the modes
directory, how can weather import something from config?

Thanks!

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


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread mk

kj wrote:



Subject line pretty much says it all: is there a book like "Effective
Java" for Python.  I.e. a book that assumes that readers are
experienced programmers that already know the basics of the language,
and want to focus on more advanced programming issues?


I'm surprised nobody mentioned Dive Into Python:

http://diveintopython.org/

Available for free online. Most Python books contain a lot of 'hello 
world' material which for someone who knows at least one programming 
language is boring, this one doesn't, it cuts straight to the point. I 
found it very readable.


Regards,
mk



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


Re: Named loops for breaking

2010-03-11 Thread bartc

James Harris wrote:

On 10 Mar, 06:29, "Gabriel Genellina"  wrote:

En Tue, 09 Mar 2010 18:41:10 -0300, Daniel Klein 
escribi :


Basically I'm wondering if there are any plans to implemented named
loops in Python, so I can tell a break command to break out of a
specific loop in the case of nested loops.


See PEP3136 [1] and its rejection note [2]
I think you may find some more discussion in the python-ideas list.



Breaking out of an inner loop is just as natural as breaking out of
the immediately enclosing loop. ISTM that if one is allowed the other
should be also.


Exactly. Python already has single-level break (probably some baggage 
carried over from C), and some of the same arguments can be applied to that 
too.


Given that break does exist, it's annoying that in the followed contrived 
example:


def f(n):
   return n==2

for i in range(10):
   print i
   if f(0): break
   if f(1): break
   if f(2): break
   if f(3): break

I can't turn the list of ifs into a loop. I can use break embedded inside a 
statement inside a loop, provided it's not another loop! That's 
discrimination...



There are often times when it *is* better to factor out the code to a
different function but adding a function just to enable a break from
an inner loop doesn't seem to me a good reason.


Multi-level breaks should just have been part of the language, so they are 
available to those who want them, and ignored by everyone else.


(And in languages where I've implemented them -- all generally faster than 
Python -- they have no impact on performance. I also had 4 loop controls 
rather than just 2, all multi-level, and the world hasn't ended yet.)


My experience of these break statements is that they are used infrequently 
in final code. But they are handy when developing code too: you don't want 
to waste time refactoring, and generally turning code upside-down, when the 
code has to be rewritten a dozen times anyway.


--
Bartc 


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


Re: I get the error: ImportError: No module named ffnet

2010-03-11 Thread Cal Who
I ran this:
import sys

from pprint import pprint as pp

pp(sys.path)



The output included:

'E:\\Program Files\\Python26',

'E:\\Program Files\\Python26\\DLLs',

'E:\\Program Files\\Python26\\lib',

'E:\\Program Files\\Python26\\lib\\lib-tk',

'E:\\Program Files\\Python26\\lib\\plat-win',

'E:\\Program Files\\Python26\\lib\\site-packages',



Python is at

E:\Python26

but other things like java are at E:\Program Files

Showhow it is looking in the wrong place and I don't know how to fix it.

I checked the registry. The entries all look good there.



Thanks







" Cal Who"  wrote in message 
news:hnb339$g2...@news.eternal-september.org...
>
>
> I have the .py file in Eclipse
> #...@pydevcodeanalysisignore
> from ffnet import ffnet, mlgraph
> topology = mlgraph( (2, 3, 1) )
> nn = ffnet(topology)
>
> I select RunAs / Python Run
>
> I get the error
> from ffnet import ffnet, mlgraph
> ImportError: No module named ffnet
>
> In the folder Python26\lib\site-packages I see
> ffnet-0.6.2-py2.6.egg-info
>
> also a folder ffnet
> in that folder there is a folder Examples
> If I click one of the examples pyrhon.exe opens and runs it OK.
>
> Is there enough in the above for you to tell me how to procede?
>
> Thanks in advance for any help at all
>
>
> 


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


Re: Behavior of default parameter in a function

2010-03-11 Thread Gary Herron

This is addressed in the FAQ.

 
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects


jitendra gupta wrote:


def foo(x = [0]):
x[0] = x[0] + 1
return x[0]

def soo(x = None):
if x is None:
x = [0]
x[0] = x[0] + 1
return x[0]

>>> foo()
1
>>>foo()  #See the behavior incremented by one 
2
>>>foo([1]) # but here based on given number 
2
>>>foo() 
3

>>>foo([1])
2
>>>foo()
4

>>>soo()
1
>>>soo()
1
>>>soo([1])
2
>>>soo()
1

Why foo() is incremented by 1 always when we are not passing any argument,
 but this is not happening in soo() case, In which scenario 
we will use these type of  function.'


Thanks 
Jitendra Kumar




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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Peter Otten
Steve Howell wrote:

> On Mar 10, 7:18 pm, Steven D'Aprano
>  wrote:

>> (2) special methods like __call__ are only called on the class, not the
>> instance, so you can't give each instance its own method.

> Are you sure about that?  This program prints 1, 2, 1, 2.
 
You are using a classic class while the behaviour described above applies to 
newstyle classes:

>>> class Foo:
... def __init__(self):
... self.__call__ = lambda: 42
...
>>> Foo()()
42
>>> class Bar(object):
... def __init__(self):
... self.__call__ = lambda: 42
...
>>> Bar()()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'Bar' object is not callable

I don't think it's a good idea to write new code that requires a classic 
class.

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


bypass UAC control through python script (to be run from batchfile)

2010-03-11 Thread rizwanahme...@gmail.com
Hi

here is my problem.

i want to install a program through python script. The python scripts
is called from a batch file. The UAC control is not allowing the
python script to install a msi (installer). I need to somehow by pass
the UAC control. i dont want to turn it off permanently. Once i have
run the batch file, i do not interact with the system manually,
therefore Clicking allow on UAC dialog is not the solution for me.

i just want to achieve the goal, whether i run batch file in Admin
mode, or do it form python, doesnt matter.
any ideas?

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


Re: Insert missing keys using defaultdict

2010-03-11 Thread Gnarlodious
On Mar 11, 9:20 am, George Sakkis wrote:

> > How do create a dict assigning every missing key with a default
> > string?
>
> "%(Text)s" %
> defaultdict(lambda:'_MISSING_', plistDict)

Brilliant, I love Python.

-- Gnarlie

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


Re: Python, Reportlabs, Pil and Windows 7 (64bit)

2010-03-11 Thread Robin Becker

On 11/03/2010 13:55, Astley Le Jasper wrote:

I have a Windows 7 (64bit AMD) machine and am having quite a lot of
problems installing Reportlabs and Pil. I wondered if anyone else has
had the same issues and what the best way of dealing with it.

So far I've tried:

1. Reportlabs / Pil 32 installers - I've tried using these but they
can't find python. I also tried registering Python (http://effbot.org/
zone/python-register.htm) but this also fails.
2. Reportlabs / Pil Source - I downloaded each of these and tried to
do a "python setup.py install". However, both complain that they can't
find "vcvarsall.bat". I've done some checking and it's because the
compiler isn't present. Everyone is suggesting downloading Visual
Studio Express C++, but this only comes with the 32bit compiler. There
seems to be quite a lot of work to get 64bit VSE working on a 64bit
machine (http://jenshuebel.wordpress.com/2009/02/12/visual-c-2008-
express-edition-and-64-bit-targets/).

But before I start down that path, I wondered if anyone had any advice
( and no I don't mean suggesting I swap to Linux).

ALJ


Hi,

you might get more assistance on the reportlab users mailing list at

http://two.pairlist.net/mailman/listinfo/reportlab-users

We do have users that run both reportlab & pil on 64 bit linux architectures, 
but I don't think I have ever compiled any of the extensions for 64bit windows. 
The vcvarsall.bat reference is the distutils package desperately  looking for a 
suitable compiler (and not finding it).


Perhaps some expert on the python list knows which versions of VS support 64bit; 
I do have VS 2005/2008 etc, but I'll probably need to set up a 64bit machine to 
see if they will install on a 64bit architecture.

--
Robin Becker

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


Re: Insert missing keys using defaultdict

2010-03-11 Thread George Sakkis
On Mar 11, 5:02 pm, Gnarlodious  wrote:

> I am trying to grok this documentation but need 
> help:http://docs.python.org/library/collections.html#defaultdict-examples
>
> In a perfect world the dict looks like this:
> plistDict={'Style':'ExternalURL', 'Ref':'http://Gnarlodious.com/',
> 'Tip':'Opens in a new window', 'Text':'Gnarlodious.com'}
>
> Let's say I want to prep a dict from a plist to insert the values into
> an HTML link string:
> "%(Text)s" %
> plistDict
>
> However, in this imperfect world the dict might look like this:
> plistDict={'Ref':'http://Gnarlodious.com/', 'Text':'Gnarlodious.com'}
>
> which would error:
> KeyError: 'Style'
>
> So using defaultdict:
> from collections import defaultdict
>
> How do create a dict assigning every missing key with a default
> string?

"%(Text)s" %
defaultdict(lambda:'_MISSING_', plistDict)

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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Steve Howell
On Mar 10, 7:18 pm, Steven D'Aprano
 wrote:
> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote:
> > Want to switch __call__ behavior.  Why doesn't this work?  What is the
> > correct way to write this?
>
> > class X (object):
> >     def __init__(self, i):
> >         if i == 0:
> >             def __call__ (self):
> >                 return 0
> >         else:
> >             def __call_ (self):
> >                 return 1
>
> Others have already pointed out that there are two reasons that won't
> work:
>
> (1) you define __call__ as a local variable of the __init__ method which
> then disappears as soon as the __init__ method completes; and
>
> (2) special methods like __call__ are only called on the class, not the
> instance, so you can't give each instance its own method.
>

Are you sure about that?  This program prints 1, 2, 1, 2.

class Foo:
def __init__(self, a):
if a == 1:
self.__call__ = lambda: 1
else:
self.__call__ = lambda: 2

foo1 = Foo(1)
print foo1()

foo2 = Foo(2)
print foo2()

print foo1()
print foo2()

See here:

http://docs.python.org/reference/datamodel.html

Class instances
Class instances are described below. Class instances are callable
only when the class has a __call__() method; x(arguments) is a
shorthand for x.__call__(arguments).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: show image in python

2010-03-11 Thread mohamed issolah
hey
#!/usr/bin/python
  2 import PIL
  3 import numpy
  4 import Image
   import ImageOps
   import sys

   def Matimg(path):
   """transforme image en matrice"""
  Img = Image.open(str(path))
  Img1 = ImageOps.grayscale(Img)
  largeur,hauteur = Img1.size
  imdata = Img1.getdata()
  tab = numpy.array(imdata)
  matrix = numpy.reshape(tab,(hauteur,largeur))
  return matrix

  def Creeimg():
  """transforme matrice en image"""
  img = Image.new ("L",(8,8))
  matrix = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  img.putdata(matrix)
  img.show()
  img.save(fp="./ana.bmp")

  if  __name__== '__main__':
  if len(sys.argv) < 2 :
  print "Usage: img.py "
  sys.exit(0)
 path = sys.argv[1]
 matrix = Matimg(path)
 print matrix
 Creeimg()


2010/3/11 Philip Semanchuk 

>
> On Mar 10, 2010, at 5:03 PM, mohamed issolah wrote:
>
>  Hey, This is my program
>>
>> 1 #!/usr/bin/python
>>  2 import PIL
>>  3 import numpy
>>  4 import Image
>>  5 import ImageOps
>>  6 import sys
>>  7
>>  8 def Matimg(path):
>>  9 """transforme image en matrice"""
>> 10 Img = Image.open(str(path))
>> 11 Img1 = ImageOps.grayscale(Img)
>> 12 largeur,hauteur = Img1.size
>> 13 imdata = Img1.getdata()
>> 14 tab = numpy.array(imdata)
>> 15 matrix = numpy.reshape(tab,(hauteur,largeur))
>> 16 return matrix
>> 17
>> 18 def Creeimg():
>> 19 """transforme matrice en image"""
>> 20 img = Image.new ("L",(8,8))
>> 21 matrix = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
>> 22 img.putdata(matrix)
>> 23 img.show()
>> 24 img.save(fp="./ana.bmp")
>> 25
>> 26 if  __name__== '__main__':
>> 27 if len(sys.argv) < 2 :
>> 28 print "Usage: img.py "
>> 29 sys.exit(0)
>> 30 path = sys.argv[1]
>> 31 matrix = Matimg(path)
>> 32 print matrix
>> 33 Creeimg()
>>
>> My probeleme : In line 23 "img.show()" Don't work, normally I show the
>> image
>> but it's not work, but strangely in line 24 "img.save(fp="./ana.bmp")"
>> it's
>> work
>> WHERE IS THE PROBLEME.
>>
>> I have this error in shell : "(eog:3176): GLib-WARNING **: GError set over
>> the top of a previous GError or uninitialized memory.
>> This indicates a bug in someone's code. You must ensure an error is NULL
>> before it's set.
>> The overwriting error message was: Error in getting image file info "
>>
>>
>> os: ubuntu 9.10
>>
>
> Hi issolah,
> I don't know what your problem is but I have a few suggestions --
> 1) You say that img.show() doesn't work. How does it fail? Is that where
> you get the GLib warning?
> 2) I'm glad you posted your code, but because it has line numbers, it's
> awkward to copy & paste into a local example. Please show your code without
> line numbers.
>
> I'm unfamiliar with PIL, so this is just a wild guess, but based on the
> GLib error it seems like you haven't initialized something properly. Sorry I
> couldn't be more helpful. Maybe someone who knows more will answer.
>
> Good luck
> Philip
>
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Insert missing keys using defaultdict

2010-03-11 Thread Gnarlodious
I am trying to grok this documentation but need help:
http://docs.python.org/library/collections.html#defaultdict-examples

In a perfect world the dict looks like this:
plistDict={'Style':'ExternalURL', 'Ref':'http://Gnarlodious.com/',
'Tip':'Opens in a new window', 'Text':'Gnarlodious.com'}

Let's say I want to prep a dict from a plist to insert the values into
an HTML link string:
"%(Text)s" %
plistDict

However, in this imperfect world the dict might look like this:
plistDict={'Ref':'http://Gnarlodious.com/', 'Text':'Gnarlodious.com'}

which would error:
KeyError: 'Style'

So using defaultdict:
from collections import defaultdict

How do create a dict assigning every missing key with a default
string?

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


Re: NoSQL Movement?

2010-03-11 Thread dkeeney
On Mar 8, 12:14 pm, Duncan Booth  wrote:

> You've totally missed the point. It isn't the size of the data you have
> today that matters, it's the size of data you could have in several years'
> time.
>
> Maybe today you've got 10 users each with 10 megabytes of data, but you're
> aspiring to become the next twitter/facebook or whatever. It's a bit late
> as you approach 100 million users (and a petabyte of data) to discover that
> your system isn't scalable: scalability needs to be built in from day one.

Do you have examples of sites that got big by planning their site
architecture from day 0 to be big?

Judging from published accounts, even Facebook and Twitter did not
plan to be  'the next twitter/facebook'; each started with routine
LAMP stack architecture and successfully re-engineered the
architecture multiple times on the way up.

Is there compelling reason to think the 'next twitter/facebook' can't
and won't climb a very similar path?

I see good reasons to think that they *will* follow the same path, in
that there are motivations at both ends of the path for re-engineering
as you go.  When the site is small, resources commited to the backend
are not spent on making the frontend useful, so business-wise the best
backend is the cheapest one.  When the site becomes super-large, the
backend gets re-engineered based on what that organization learned
while the site was just large; Facebook, Twitter, and Craigslist all
have architectures custom designed to support their specific needs.
Had they tried to design for large size while they were small, they
would have failed; they couldn't have known enough then about what
they would eventually need.

The only example I can find of a large site that architected large
very early is Google, and they were aiming for a market (search) that
was already known to be huge.

Its reasonable to assume that the 'next twitter/facebook' will *not*
be in web search, social-networking, broadcast instant messaging, or
classified ads, just because those niches are taken already.  So
whichever 'high-scalability' model the aspiring site uses will be the
wrong one.  They might as well start with a quick and cheap LAMP
stack, and re-engineer as they go.

Just one internet watcher's biased opinion...

David


www.rdbhost.com -> SQL databases via a web-service

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


Re: Can't define __call__ within __init__?

2010-03-11 Thread MRAB

Andre Engels wrote:

On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden  wrote:


The example I showed was just a toy problem.  The real problem is
I expect to call a function many times, and I want to avoid the overhead of
the 'if blah' everytime.


This is a premature optimization. First, make it work. Then (if it
doesn't work fast enough) make it work faster.


Corrolary: When you do make it faster, make it faster where it is slow.
Second corrolary: If making it fast is so important that these two
rules do not apply, Python is not your language of choice.


Addendum: a bad algorithm is bad, whatever language it's written in.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for creating processes

2010-03-11 Thread bobicanprogram
On Mar 10, 4:52 pm, J  wrote:
> I'm working on a project and thought I'd ask for a suggestion on how
> to proceed (I've got my own ideas, but I wanted to see if I was on the
> right track)
>
> For now, I've got this:
>
> def main():
> ## get our list of directories to refresh
> releases=sys.argv[1:]
> if len(releases) < 1:
> print "You need to provide at least one dir to update"
> sys.exit()
>
> ## Lets figure out what there is to update
> updateDirs = []
> for rel in releases:
> currentDir = os.path.join(homedir, rel)
> for item in os.listdir(currentDir):
> updateDirs += [os.path.join(homedir, rel, item)]
>
> which returns a list of full pathnames to directories that need to be
> updated (updates will be carried out by calling rsync or zsync
> eventually)
>
> The directory hierarchy looks like this:
>
> /home/user/files
> /home/user/files/version1
> /home/user/files/version1/type1
> /home/user/files/version1/type2
> /home/user/files/version2
> /home/user/files/version2/type1
>
> and the list ends up looking like this:
>
> ['/home/user/files/version1/type1','/home/user/files/version1/type2','/home/user/files/version2/type1','/home/user/files/version2/type2']
>
> the next thing I need to do is figure out how to update those.
>
> the quick and dirty would be (as I'm imagining it at the moment):
> for path in pathlist:
> chdir into path
> execute rsync or zsync
>
> but that gets me moving into one dir, updating, then moving into another.
>
> What I was wondering about though, is spawning off separate rsync
> processes to run concurrently (each rsync will hit a different remote
> dir for each local dir)
>
> so I'm trying to figure out a good way to do this:
>
> for path in pathlist:
> kick off an individual rsync|zsync process to update path
>
> wait for all child processes to end
> exit program.
>
> I've been looking at subprocess because at the moment, that's all I
> really know...
> But is there a better way of kicking off multiple simultaneous
> processes so I can update all dirs at once instead of one at a time?
>
> No, this isn't homework, it's something I'm working on to sync a
> couple directories of ISO images to grab nightly builds
> Yes, there are plenty of pre-made scripts out there, but I don't want
> to even look at those because I figured this would be a good learning
> experience, and I want to try to solve this as much on my own as I can
> without just cut and pasting from someone elses program.
>
> So, with that said, any ideas on the best way to proceed?  I'm going
> to start looking at ways to use subprocess to do this, or would there
> be a better way (multi-threading maybe?)
>
> Or am I even in the right ballpark?
>
> Cheers
> Jeff


You might be able to use the SIMPL toolkit for this one.
(http://www.icanprogram.com/06py/lesson1/lesson1.html)

You could wrap the rsync executable as a SIMPL receiver module and
then message to that from inside your Python script to kick it off and
synchronize actions.

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


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread Kevin Walzer

On 3/10/10 10:19 AM, kj wrote:

Subject line pretty much says it all: is there a book like "Effective
Java" for Python.  I.e. a book that assumes that readers are
experienced programmers that already know the basics of the language,
and want to focus on more advanced programming issues?

~K
I haven't read "Effective Java," but I have found Lutz's "Programming 
Python" to be a very useful guide to solving various programming issues 
with the language. It's a big, big book, so there's lots to look at.


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


I get the error: ImportError: No module named ffnet

2010-03-11 Thread Cal Who


I have the .py file in Eclipse
#...@pydevcodeanalysisignore
from ffnet import ffnet, mlgraph
topology = mlgraph( (2, 3, 1) )
nn = ffnet(topology)

I select RunAs / Python Run

I get the error
from ffnet import ffnet, mlgraph
ImportError: No module named ffnet

In the folder Python26\lib\site-packages I see
ffnet-0.6.2-py2.6.egg-info

also a folder ffnet
in that folder there is a folder Examples
If I click one of the examples pyrhon.exe opens and runs it OK.

Is there enough in the above for you to tell me how to procede?

Thanks in advance for any help at all



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


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread Steve Howell
On Mar 11, 7:05 am, kj  wrote:
> In  Chris Withers 
>  writes:
>
> >kj wrote:
>
> >> Subject line pretty much says it all: is there a book like "Effective
> >> Java"
> >oxymoronic, no?
> >Sorry, couldn't resist ;-)
>
> I hear you, but still: read "Effective Java" some day; it will make
> you a better programmer, whatever your language preference.  I'm
> certainly no fan of Java, but I still rank Effective Java as one
> of my best reads on programming ever.  I felt that my maturity as
> a programmer went up a notch or two after digesting this book.
>

I'll second that.  I much prefer Python to Java, but Effective Java is
an excellent book.

Many of its topics would be applicable to Python, although the
solutions in Python would obviously be different.

  Eliminate obsolete object references (use None to break reference
counts)
  Avoid finalizers (use the "with" statement)
  Favor immutability
  Design method signatures carefully (including the x=[] gotcha)
  Optimize judiciously
  Don't ignore exceptions
  Avoid excessive synchronization

There are also some topics in Effective Java where the advice is
almost the opposite of Python tradition.



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


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread kj
In  Chris Withers 
 writes:

>kj wrote:
>> 
>> 
>> Subject line pretty much says it all: is there a book like "Effective
>> Java" 

>oxymoronic, no?

>Sorry, couldn't resist ;-)

I hear you, but still: read "Effective Java" some day; it will make
you a better programmer, whatever your language preference.  I'm
certainly no fan of Java, but I still rank Effective Java as one
of my best reads on programming ever.  I felt that my maturity as
a programmer went up a notch or two after digesting this book.

Cheers,

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


Re: show image in python

2010-03-11 Thread Philip Semanchuk


On Mar 10, 2010, at 5:03 PM, mohamed issolah wrote:


Hey, This is my program

1 #!/usr/bin/python
 2 import PIL
 3 import numpy
 4 import Image
 5 import ImageOps
 6 import sys
 7
 8 def Matimg(path):
 9 """transforme image en matrice"""
10 Img = Image.open(str(path))
11 Img1 = ImageOps.grayscale(Img)
12 largeur,hauteur = Img1.size
13 imdata = Img1.getdata()
14 tab = numpy.array(imdata)
15 matrix = numpy.reshape(tab,(hauteur,largeur))
16 return matrix
17
18 def Creeimg():
19 """transforme matrice en image"""
20 img = Image.new ("L",(8,8))
21 matrix = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
22 img.putdata(matrix)
23 img.show()
24 img.save(fp="./ana.bmp")
25
26 if  __name__== '__main__':
27 if len(sys.argv) < 2 :
28 print "Usage: img.py "
29 sys.exit(0)
30 path = sys.argv[1]
31 matrix = Matimg(path)
32 print matrix
33 Creeimg()

My probeleme : In line 23 "img.show()" Don't work, normally I show  
the image
but it's not work, but strangely in line 24 "img.save(fp="./ 
ana.bmp")" it's

work
WHERE IS THE PROBLEME.

I have this error in shell : "(eog:3176): GLib-WARNING **: GError  
set over

the top of a previous GError or uninitialized memory.
This indicates a bug in someone's code. You must ensure an error is  
NULL

before it's set.
The overwriting error message was: Error in getting image file info "


os: ubuntu 9.10


Hi issolah,
I don't know what your problem is but I have a few suggestions --
1) You say that img.show() doesn't work. How does it fail? Is that  
where you get the GLib warning?
2) I'm glad you posted your code, but because it has line numbers,  
it's awkward to copy & paste into a local example. Please show your  
code without line numbers.


I'm unfamiliar with PIL, so this is just a wild guess, but based on  
the GLib error it seems like you haven't initialized something  
properly. Sorry I couldn't be more helpful. Maybe someone who knows  
more will answer.


Good luck
Philip





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


Need advice on starting a Python group

2010-03-11 Thread gb345



I'm hoping to get advice from anyone with prior experience setting
up a Python group. 

A friend of mine and I have been trying to start a
scientific-programming-oriented Python group in our school (of
medecine and bio research), with not much success.

The main problem is attendance.  Even though a *ton* of people have
told us that it's a great idea, that they're *very* interested,
and have asked to be added to our mailing list, the attendance to
our first few meeting has never been more than 5, including my
friend and I.  Last time just he and I showed up.

The second problem is getting content.  The format we'd envisioned
for this group was centered around code review (though not limited
to it).  The idea was that at every meeting a different member
would show some code.  This could be for any of a number of reasons,
such as, for example, 1) illustrate a cool module or technique; 2)
present a scientific research problem and how they used Python to
solve it, or get help solving it; 3) get general feedback (e.g. on
code clarity, software usability, module architecture, etc.).  But
in principle just about anything is OK: e.g. a talk on favorite
Python resources, or a comparison of Python with some other language,
or an overview of Python gotchas would all be fair game.

Also, we stressed that the talks were not expected to be polished:
no need for PowerPoint slides, etc.  Just project any old code onto
the screen, and talk about it, or scribble stuff on the chalkboard.

Still, we have a hard time finding volunteers.

And even when we've had volunteers, hardly anyone shows up!

Any suggestions would be appreciated.

GB

P.S.  There's a Python Meetup we could go to, but it does not fit
the bill for us: it doesn't meet often enough, it's sort of out of
the way, and has practically no one doing scientific programming.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread Stefan Behnel

James Harris, 11.03.2010 09:30:

On 10 Mar, 15:19, kj  wrote:

Subject line pretty much says it all: is there a book like "Effective
Java" for Python.  I.e. a book that assumes that readers are
experienced programmers that already know the basics of the language,
and want to focus on more advanced programming issues?


I don't know about the Java book you mention but I find Python in a
Nutshell published by O'Reilly to be a good reference.


There's also the Python Cookbook that has a couple of useful recipes that 
can become handy at times.


http://code.activestate.com/recipes/langs/python/

Stefan

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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Andre Engels
On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden  wrote:

>> The example I showed was just a toy problem.  The real problem is
>> I expect to call a function many times, and I want to avoid the overhead of
>> the 'if blah' everytime.
>>
> This is a premature optimization. First, make it work. Then (if it
> doesn't work fast enough) make it work faster.

Corrolary: When you do make it faster, make it faster where it is slow.
Second corrolary: If making it fast is so important that these two
rules do not apply, Python is not your language of choice.


-- 
André Engels, andreeng...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Python, Reportlabs, Pil and Windows 7 (64bit)

2010-03-11 Thread Astley Le Jasper
I have a Windows 7 (64bit AMD) machine and am having quite a lot of
problems installing Reportlabs and Pil. I wondered if anyone else has
had the same issues and what the best way of dealing with it.

So far I've tried:

1. Reportlabs / Pil 32 installers - I've tried using these but they
can't find python. I also tried registering Python (http://effbot.org/
zone/python-register.htm) but this also fails.
2. Reportlabs / Pil Source - I downloaded each of these and tried to
do a "python setup.py install". However, both complain that they can't
find "vcvarsall.bat". I've done some checking and it's because the
compiler isn't present. Everyone is suggesting downloading Visual
Studio Express C++, but this only comes with the 32bit compiler. There
seems to be quite a lot of work to get 64bit VSE working on a 64bit
machine (http://jenshuebel.wordpress.com/2009/02/12/visual-c-2008-
express-edition-and-64-bit-targets/).

But before I start down that path, I wondered if anyone had any advice
( and no I don't mean suggesting I swap to Linux).

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


Re: Interacting With Another Script

2010-03-11 Thread Victor Subervi
On Wed, Mar 10, 2010 at 10:16 PM, alex23  wrote:

> Victor Subervi  wrote:
> > > There's a program (vpopmail) that has commands which, when called,
> request
> > > input ("email address", "password", etc.) from the command line. I
> would
> > > like to build a TTW interface for my clients to use that interacts with
> > > these commands.
>
> The Pexpect[1] module is pretty much aimed at doing exactly this.
>
> 1: http://www.noah.org/wiki/Pexpect


THIS looks PERFECT! Thanks!
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't define __call__ within __init__?

2010-03-11 Thread Steve Holden
Neal Becker wrote:
> Steven D'Aprano wrote:
> 
>> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote:
>>
>>> Want to switch __call__ behavior.  Why doesn't this work?  What is the
>>> correct way to write this?
>>>
>>> class X (object):
>>> def __init__(self, i):
>>> if i == 0:
>>> def __call__ (self):
>>> return 0
>>> else:
>>> def __call_ (self):
>>> return 1
>>
>> Others have already pointed out that there are two reasons that won't
>> work:
>>
>> (1) you define __call__ as a local variable of the __init__ method which
>> then disappears as soon as the __init__ method completes; and
>>
>> (2) special methods like __call__ are only called on the class, not the
>> instance, so you can't give each instance its own method.
>>
>>
>> Perhaps the best way to solve this is to use delegation:
>>
>>
>> def zero_returner():
>> return 0
>>
>> def one_returner():
>> return 1
>>
>>
>> class X (object):
>> def __init__(self, i):
>> if i == 0:
>> self.func = zero_returner
>> else:
>> self.func = one_returner
>> def __call__(self, *args, **kwargs):
>> return self.func(*args, **kwargs)
>>
>>
>> zero_returner and one_returner can be any callable object, not
>> necessarily a function.
>>
>> Of course, all this assumes that your solution isn't even simpler:
>>
>> class X (object):
>> def __init__(self, i):
>> self.i = i
>> def __call__(self):
>> return self.i
>>
>> but I assume if it was that simple, you would have done that already.
>>
>>
>>
> The example I showed was just a toy problem.  The real problem is
> I expect to call a function many times, and I want to avoid the overhead of 
> the 'if blah' everytime.
> 
This is a premature optimization. First, make it work. Then (if it
doesn't work fast enough) make it work faster.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Get a method instance through 'getattr' but not superclass's method

2010-03-11 Thread Radhakrishna Bhat
Thanks. I just have to check in that string if method1 is from present in
subclass's __dict__ or not.
This is important for me because in my code, I am dynamically calling
methods of 2 classes where one is superclass of the other. And I was getting
duplicate results because of this problem.

-Radhakrishna

On Thu, Mar 11, 2010 at 6:12 PM, Steve Holden  wrote:

> Radhakrishna Bhat wrote:
> > I am using getattr to get a method instance from a class. But it also
> > returns methods from the superclass. How to detect if an attribute is
> > from superclass?
> >
> You could try, if x is in instance, looking to see whether the name is
> defined in x.__class__.__dict__.
>
> >>> class A:
> ...   def method1(self):
> ...   print "A1"
> ...   def method2(self):
> ...   print "A2"
> ...
> >>> class B(A):
> ...   def method2(self):
> ...   print"B2"
> ...   def method3(self):
> ...   print "B3"
> ...
> >>> a = A()
> >>> b = B()
> >>> b.method2()
> B2
> >>> b.method1()
> A1
> >>> b.method3()
> B3
> >>> b.__class__.__dict__
> {'__module__': '__main__', 'method2': ,
> 'method3': , '__doc__': None}
> >>> a.__class__.__dict__
> {'__module__': '__main__', 'method2': ,
> '__doc__': None, 'method1': }
> >>>
>
> I'd be interested to know why this is so important.
>
> regards
>  Steve
> --
> Steve Holden   +1 571 484 6266   +1 800 494 3119
> See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
> Holden Web LLC http://www.holdenweb.com/
> UPCOMING EVENTS:http://holdenweb.eventbrite.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a newbie's question

2010-03-11 Thread Jean-Michel Pichavant

PEYMAN ASKARI wrote:

Hello

I need some help dynamically reloading modules. As it turns out, it is 
not as simple as calling reload, as outlined here

http://pyunit.sourceforge.net/notes/reloading.html

Is there builtin support for this? The example they gave does not seem 
to work for me, and I am unclear as to what PyUnit is.


Thanks


Peyman



Please don't top post.
As for your question, there's no built-in method to reload a module 
because it is not possible (in a generic manner).


However if you *really* want to do it you can do it for your particular 
module (and only for this one).
It is tough to explain though. You have to know perfectly the python 
object model and I'm not familiar enough with it to do the explanation.


So:

1/ try to live without dynamic reload
2/ if you *really* require dynamic reload, you can try to execute the 
module in a subprocess. That way, every time you start a new subprocess, 
the last up to date version of the module is used
3/ reload the module and all it's related objects currently in memory, 
this is painfull and there are many traps to fall into. I'll let someone 
else answer this point.


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


Re: Can't define __call__ within __init__?

2010-03-11 Thread Neal Becker
Steven D'Aprano wrote:

> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote:
> 
>> Want to switch __call__ behavior.  Why doesn't this work?  What is the
>> correct way to write this?
>> 
>> class X (object):
>> def __init__(self, i):
>> if i == 0:
>> def __call__ (self):
>> return 0
>> else:
>> def __call_ (self):
>> return 1
> 
> 
> Others have already pointed out that there are two reasons that won't
> work:
> 
> (1) you define __call__ as a local variable of the __init__ method which
> then disappears as soon as the __init__ method completes; and
> 
> (2) special methods like __call__ are only called on the class, not the
> instance, so you can't give each instance its own method.
> 
> 
> Perhaps the best way to solve this is to use delegation:
> 
> 
> def zero_returner():
> return 0
> 
> def one_returner():
> return 1
> 
> 
> class X (object):
> def __init__(self, i):
> if i == 0:
> self.func = zero_returner
> else:
> self.func = one_returner
> def __call__(self, *args, **kwargs):
> return self.func(*args, **kwargs)
> 
> 
> zero_returner and one_returner can be any callable object, not
> necessarily a function.
> 
> Of course, all this assumes that your solution isn't even simpler:
> 
> class X (object):
> def __init__(self, i):
> self.i = i
> def __call__(self):
> return self.i
> 
> but I assume if it was that simple, you would have done that already.
> 
> 
> 
The example I showed was just a toy problem.  The real problem is
I expect to call a function many times, and I want to avoid the overhead of 
the 'if blah' everytime.

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


Re: a newbie's question

2010-03-11 Thread PEYMAN ASKARI
Hello

I need some help dynamically reloading modules. As it turns out, it is not as 
simple as calling reload, as outlined here
http://pyunit.sourceforge.net/notes/reloading.html

Is there builtin support for this? The example they gave does not seem to work 
for me, and I am unclear as to what PyUnit is.

Thanks


Peyman

--- On Thu, 3/11/10, Lan Qing  wrote:

From: Lan Qing 
Subject: Re: a newbie's question
To: "Python List" 
Received: Thursday, March 11, 2010, 4:56 AM

hi Cheers,     Think you, that helps me a lot.

On Tue, Mar 9, 2010 at 10:00 PM, Simon Brunning  
wrote:

On 9 March 2010 13:51, Lan Qing  wrote:


> Hi all,

>       I'm a newbie of python programming language.



Welcome!



> I have used c/c++ for 5

> years, and one year experience in Lua programming language. Can any one give

> me some advice on learning python. Think you for any help!!



You'll find some useful starting points here -

.



--

Cheers,

Simon B.




-Inline Attachment Follows-

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


Re: Get a method instance through 'getattr' but not superclass's method

2010-03-11 Thread Steve Holden
Radhakrishna Bhat wrote:
> I am using getattr to get a method instance from a class. But it also
> returns methods from the superclass. How to detect if an attribute is
> from superclass? 
> 
You could try, if x is in instance, looking to see whether the name is
defined in x.__class__.__dict__.

>>> class A:
...   def method1(self):
...   print "A1"
...   def method2(self):
...   print "A2"
...
>>> class B(A):
...   def method2(self):
...   print"B2"
...   def method3(self):
...   print "B3"
...
>>> a = A()
>>> b = B()
>>> b.method2()
B2
>>> b.method1()
A1
>>> b.method3()
B3
>>> b.__class__.__dict__
{'__module__': '__main__', 'method2': ,
'method3': , '__doc__': None}
>>> a.__class__.__dict__
{'__module__': '__main__', 'method2': ,
'__doc__': None, 'method1': }
>>>

I'd be interested to know why this is so important.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Python Script to get Info from Site not working

2010-03-11 Thread Steve Holden
Jimbo wrote:
> On Mar 11, 12:38 pm, "Gabriel Genellina" 
> wrote:
>> En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo  escribió:
>>
>>> I found a semi Python & internet tutorial here if anyone else would
>>> like ithttp://www.upriss.org.uk/python/session6.html
>>> My script is meant to find which radio button is selected & tell me
>>> that. But it just keeps saying "No Drink Selected!"
>> Are you sure? From the code you posted, the message should read "You need  
>> to select a drink!", not that one.
> 
>> Replace the last line with something like this, to see what you are  
>> getting exactly:
>>
>> print("Sorry, no %r (%r) available." % (
>>drink,
>>cgi.escape(repr(type(drink)
>>
>> BTW, which Python version are you using? The tutorial you linked to is  
>> aimed at Python 2.x, but your print syntax suggests you're using Python 3.x
>>
>> --
>> Gabriel Genellina
> 
> Yeah that("You need to select a drink!") was the string I was getting
> but I should be getting hot chocolate or something else(which ever one
> I have selected)
> 
> I am using 3.x, do you think I should be using something else? Also
> the your code I put in does the some thing, just now it says "Sorry no
> none drink available" all the time.

It may not seem like it, but you *are* making progress here (of a sort).

>From the information you have given us so far it appears that your call
to form.getvalue() is returning None (which is of type 
- your browser is probably not displaying this because it sees it as a
bogus HTML tag.

Perhaps you haven't yet realised the importance of exact copy-and-paste
here. What I'd like you to do is change the line

drink = form.getvalue("drink")

to

drink = form.getfirst("drink", "No drink selected")

then have you browser display the HTML source of the response page your
program is returning and paste that into a reply to this mail.

You might also want to take a look at

  http://docs.python.org/3.1/library/cgi.html

for further information about the Python library your are trying to use.
In particular I'd recommend heeding its advice about use of the cgitb
module.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: logging: local functions ==> loss of lineno

2010-03-11 Thread Peter Otten
Hellmut Weber wrote:

> Logging works very well giving the filename and line number of the point
> where it is called. As long as I use the loggers directly.
> BUT when I have to wrap the logger call in some other function, I always
> get file name and line number of the call of the logger inside the
> wrapping function.
> 
> Is there a possibility to get this information in this situation too?

The official way is probably to write a custom Logger class that overrides 
the findCaller() method.

Below is a hack that monkey-patches the logging._srcfile attribute to ignore 
user-specified modules in the call stack:

$ cat wrapper.py
import logging
import os
import sys

logger = logging.getLogger()

class SrcFile(object):
def __init__(self, exclude_files):
self.files = set(exclude_files)
def __eq__(self, other):
return other in self.files

def fixname(filename):
if filename.lower().endswith((".pyc", ".pyo")):
filename = filename[:-4] + ".py"
return os.path.normcase(filename)

if "--monkey" in sys.argv:
print "patching"
logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])

def warn(*args, **kw):
logger.warn(*args, **kw)

$ cat main.py
import logging
logging.basicConfig(format="%(filename)s <%(lineno)s>: %(message)s")
import wrapper
wrapper.warn("foo")
wrapper.warn("bar")
wrapper.warn("baz")

$ python main.py
wrapper.py <23>: foo
wrapper.py <23>: bar
wrapper.py <23>: baz

$ python main.py --monkey
patching
main.py <4>: foo
main.py <5>: bar
main.py <6>: baz

$ python -V
Python 2.6.4

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


Re: logging: local functions ==> loss of lineno

2010-03-11 Thread Jean-Michel Pichavant

Hellmut Weber wrote:

Hi Vinay Sajip,
I'm very glad discoverd your logging module ;-)
(That's what I would have liked 25 years ago when I was working as a 
technical software developper!)


Now I'm writing just some personal tools, I like python and want to 
use logging on a regular basis.


Logging works very well giving the filename and line number of the 
point where it is called. As long as I use the loggers directly.
BUT when I have to wrap the logger call in some other function, I 
always get file name and line number of the call of the logger inside 
the wrapping function.


Is there a possibility to get this information in this situation too?

TIA

Hellmut

You have to specify the file and line number by yourself, the logging 
feature can only assume that you want the line number of the logger call.


i.e.

in test.py:

import logging
import inspect

_logger = logging.getLogger(__name__)

class Foo:
   def __init__(self):
   self._logger = _logger

   def info(self, msg): 
   previousFrame = inspect.currentframe().f_back
   self._logger.info(msg, 
extra={'custom_lineno':previousFrame.f_lineno, 'custom_filename': 
previousFrame.f_code.co_filename})


if __name__ == '__main__':
   _logger.handlers=[]
   _logger.addHandler(logging.StreamHandler())
   _logger.handlers[-1].setFormatter(logging.Formatter('file 
%(custom_filename)s line %(custom_lineno)d : %(message)s'))

   _logger.setLevel(logging.DEBUG)
   foo = Foo()
   foo.info('a foo info')

In [3]: run test.py
file test.py line 20 : a foo info


note that you cannot override the logging builtin 'lineno' key with the 
extra dictionary, that is why I'm using the 'custom_lineno' key. I don't 
know why the logger forbids it though, would have been nice to override 
lineno without the need of adding a new key.


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


How to handle file uploads with http.server

2010-03-11 Thread Neil Blue
Hello,

I have a basic http.server instance running (class
HTTPHandler(http.server.BaseHTTPRequestHandler), with python 3.1, and I
would like to upload files with multipart forms.

def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
if ctype=='multipart/form-data':
print('parsing...')
query=cgi.parse_multipart(self.rfile, pdict)
print(query)

However the file never seems to finish being parsed. There are no errors,
but the call hangs at: query=cgi.parse_multipart(self.rfile, pdict)

Please can anyone offer some insight into this or somewhere else I may find
some more information.

Thanks
Neil

* 

The information contained in this message is likely to be confidential.  It is 
intended only for the person named above.  Any dissemination, distribution, 
copying, disclosure or use of this message or its contents unless authorised by 
BioWisdom Ltd is strictly prohibited. Any views or opinions expressed within 
this e-mail are those of the author and do not necessarily represent those of 
BioWisdom Ltd. If you have received this message in error, please immediately 
notify us and delete it.  Thank you.  Registered Office: BioWisdom Ltd, Harston 
Mill, Harston, Cambridge, CB22 7GG.  Registered in England: (GB) 3861669.  VAT 
registered: (GB) 750899881.  Tel: +44 (0)1223 874800, Fax: +44 (0) 1223 874801, 
Internet:www.biowisdom.com 

*

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


Multiple import of the same module under different names

2010-03-11 Thread George Sakkis
The import mechanism is not very smart in identifying whether two
modules imported under different name are actually the same module, at
least when dealing with implicit relative imports and sys.path
manipulation. However, at least in cases of plain file modules, the
module's __file__ would be adequate (or at least better than __name__)
in determining this. Below is an illustration of the different
situations (absolute imports, explicit relative imports, implicit
relative import, sys.path tweaking). So why does not import consult
__file__ before deciding to create a new module instance ?

George


=== File structure =

~/pkg/
   __init__.py  # empty
   mod1.py
   mod2.py
   mod3.py
   mod4.py
   main.py
   subpkg/
  __init__.py  # empty
  foo.py # empty

=== Run  =

~$ PYTHONPATH=. python pkg/main.py

=== Output  =

Imported foo from pkg.subpkg:   
Imported foo from subpkg:   
Imported foo from pkg.mod1: 
Imported foo from mod1: 
Imported foo from pkg.mod2: 
Failed to import foo from mod2:Attempted relative import in non-package
Imported foo from pkg.mod3: 
Imported foo from mod3: 
Imported foo from pkg.mod4: 
Imported foo from mod4: 

* 9 total module(s)
* 3 distinct module(s)



* 1 distinct file(s)
/home/george/pkg/subpkg/foo.py

=== Code  =

### mod1.py ###
# implicit relative import
from subpkg import foo

### mod2.py ###
# explicit relative import
from .subpkg import foo

### mod3.py ###
# absolute import
from pkg.subpkg import foo

### mod4.py ###
# absolute import after tweaking sys.path
import sys
from os.path import dirname,join
sys.path.append(join(dirname(__file__), 'subpkg'))
import foo

### main.py ###
#!/usr/bin/env python

from os.path import abspath, normpath

def test(*modules):
module_set = set(modules)
file_set = set(module_file(m) for m in modules)
print '* %d total module(s)' % len(modules)
print '* %d distinct module(s)' % len(module_set)
for m in module_set:
print '\t', m
print '* %d distinct file(s)' % len(file_set)
for f in file_set:
print '\t', f

def module_file(mod):
f = abspath(normpath(mod.__file__))
if f.endswith('.pyc'):
f = f[:-1]
return f

if __name__ == '__main__':
from pkg.subpkg import foo
print 'Imported foo from pkg.subpkg:\t', foo
from subpkg import foo as rel_foo
print 'Imported foo from subpkg:\t\t', rel_foo

from pkg.mod1 import foo as foo1
print 'Imported foo from pkg.mod1:\t', foo1
from mod1 import foo as rel_foo1
print 'Imported foo from mod1:\t\t', rel_foo1

from pkg.mod2 import foo as foo2
print 'Imported foo from pkg.mod2:\t', foo2
try: from mod2 import foo as rel_foo2
except ValueError, ex:
print 'Failed to import foo from mod2:', ex

from pkg.mod3 import foo as foo3
print 'Imported foo from pkg.mod3:\t', foo3
from mod3 import foo as rel_foo3
print 'Imported foo from mod3:\t\t', rel_foo3

from pkg.mod4 import foo as foo4
print 'Imported foo from pkg.mod4:\t', foo4
from mod4 import foo as rel_foo4
print 'Imported foo from mod4:\t\t', rel_foo4

print
test(foo, rel_foo,
 foo1, rel_foo1,
 foo2, # rel_foo2,
 foo3, rel_foo3,
 foo4, rel_foo4)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Behavior of default parameter in a function

2010-03-11 Thread Shashank Singh
quoting from docs:http://docs.python.org/reference/compound_stmts.html

*Default parameter values are evaluated when the function definition is
executed.* This means that the expression is evaluated once, when the
function is defined, and that that same “pre-computed” value is used for
each call. This is especially important to understand when a default
parameter is a mutable object, such as a list or a dictionary: if the
function modifies the object (e.g. by appending an item to a list), the
default value is in effect modified

so default object for x in foo is precomputed once and is modified with each
call.

hth
--shashank

On Thu, Mar 11, 2010 at 3:02 PM, jitendra gupta wrote:

>
> def foo(x = [0]):
> x[0] = x[0] + 1
>  return x[0]
>
> def soo(x = None):
> if x is None:
> x = [0]
> x[0] = x[0] + 1
>  return x[0]
>
> >>> foo()
> 1
> >>>foo()  #See the behavior incremented by one
> 2
> >>>foo([1]) # but here based on given number
> 2
> >>>foo()
> 3
> >>>foo([1])
> 2
> >>>foo()
> 4
>
> >>>soo()
> 1
> >>>soo()
> 1
> >>>soo([1])
> 2
> >>>soo()
> 1
>
> Why foo() is incremented by 1 always when we are not passing any argument,
>  but this is not happening in soo() case, In which scenario we will use
> these type of  function.'
>
> Thanks
> Jitendra Kumar
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Regards
Shashank Singh
Senior Undergraduate, Department of Computer Science and Engineering
Indian Institute of Technology Bombay
shashank.sunny.si...@gmail.com
http://www.cse.iitb.ac.in/~shashanksingh
-- 
http://mail.python.org/mailman/listinfo/python-list


Behavior of default parameter in a function

2010-03-11 Thread jitendra gupta
def foo(x = [0]):
x[0] = x[0] + 1
return x[0]

def soo(x = None):
if x is None:
x = [0]
x[0] = x[0] + 1
return x[0]

>>> foo()
1
>>>foo()  #See the behavior incremented by one
2
>>>foo([1]) # but here based on given number
2
>>>foo()
3
>>>foo([1])
2
>>>foo()
4

>>>soo()
1
>>>soo()
1
>>>soo([1])
2
>>>soo()
1

Why foo() is incremented by 1 always when we are not passing any argument,
 but this is not happening in soo() case, In which scenario we will use
these type of  function.'

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


EOFError: EOF when reading a line

2010-03-11 Thread Mihir Patel
I am trying to use the subprocess to send the data to child process. I
am not sure why i keep getting  "EOFError: EOF when reading a line"

i am using Python 2.4.3, GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on
64bit linux ,centos

Thanks

output :
=
Traceback (most recent call last):
  File "test_input.py", line 3, in ?
x = raw_input()
EOFError: EOF when reading a line
output: hello



Main Program:
===

command_line = 'python test_input.py'
p =subprocess.Popen(command_line, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
print 'output:', p.communicate()[0]

o,e = p.communicate('test')

print "out:",o
print "error:",e
p.stdout.close()

print "Exit Success"



test_input.py
==
print 'hello\n'

x = raw_input()

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


Re: pexpect and logging integration

2010-03-11 Thread Lars Stavholm
It works like a charm, thank you!
/Lars

Jean-Michel Pichavant wrote:
> Lars Stavholm wrote:
>> Hi all,
>>
>> has anyone managed to integrate pexpect and logging?
>>
>> I.e., I'd like to be able to pick up the dialog,
>> commands sent and responses received, in my logging.
>> I know about the pexpect logfile, and I can log things
>> to stdout or stderr, but I really need to log using the
>> python logging library.
>>
>> Any thoughts appreciated
>> /Lars
>>
>>   
> I had to implement this.
> It's a bit of a hack, but it does the job.
> 
> The following code is tested with python 2.5, I remember pexpect behaves
> slightly differently in python 2.3.
> 
> import logging
> import pexpect
> import re
> 
> # this will be the method called by the pexpect object to log
> def _write(*args, **kwargs):
>content = args[0]
># let's ignore other params, pexpect only use one arg AFAIK
>if content in [' ', '', '\n', '\r', '\r\n']:
>return # don't log empty lines
>for eol in ['\r\n', '\r', '\n']:
># remove ending EOL, the logger will add it anyway
>content = re.sub('\%s$' % eol, '', content)
>return logger.info(content) # call the logger info method with the
> reworked content
> 
> 
> # our flush method
> def _doNothing():
>pass
> 
> # get the logger
> logger = logging.getLogger('foo')
> 
> # configure the logger
> logger.handlers=[]
> logger.addHandler(logging.StreamHandler())
> logger.handlers[-1].setFormatter(logging.Formatter("%(asctime)s -
> %(name)s - %(levelname)s - %(message)s"))
> logger.setLevel(logging.INFO)
> 
> # give the logger the methods required by pexpect
> logger.write = _write
> logger.flush = _doNothing
> 
> p = pexpect.spawn('echo "hello world !!"', logfile=logger)
> p.expect('!!')
> 
> ... 2010-03-10 15:01:31,234 - foo - INFO - hello world !!
> 
> Hope it helps.
> 
> JM
> 
> 


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


Re: Anything like "Effective Java" for Python?

2010-03-11 Thread James Harris
On 10 Mar, 15:19, kj  wrote:
> Subject line pretty much says it all: is there a book like "Effective
> Java" for Python.  I.e. a book that assumes that readers are
> experienced programmers that already know the basics of the language,
> and want to focus on more advanced programming issues?

I don't know about the Java book you mention but I find Python in a
Nutshell published by O'Reilly to be a good reference.

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