Re: How to write replace string for object which will be substituted? [regexp]

2009-08-05 Thread ryniek
On 5 Sie, 00:55, MRAB  wrote:
> ryniek90 wrote:
> > Hi.
> > I started learning regexp, and some things goes well, but most of them
> > still not.
>
> > I've got problem with some regexp. Better post code here:
>
> > "
> >  >>> import re
> >  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> > mail [$dot$] com\n'
> >  >>> mail
> > '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> > com\n'
> >  >>> print mail
>
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> >  >>> maail = re.sub('^\n|$\n', '', mail)
> >  >>> print maail
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
> >  >>> maail = re.sub(' ', '', maail)
> >  >>> print maail
> > n...@mail.com
> > name1[at]mail[dot]com
> > name2[$at$]mail[$dot$]com
> >  >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
> >  >>> print maail
> > n...@mail.com
> > na...@mail[dot]com
> > na...@mail[$dot$]com
> >  >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
> >  >>> print maail
> > n...@mail.com
> > na...@mail.com
> > na...@mail.com
> >  >>> #How must i write the replace string to replace all this regexp's
> > with just ONE command, in string 'mail' ?
> >  >>> maail = re.sub('^\n|$\n| |\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]',
> > *?*, mail)
> > "
>
> > How must i write that replace pattern (look at question mark), to maek
> > that substituion work? I didn't saw anything helpful while reading Re
> > doc and HowTo (from Python Doc). I tried with 'MatchObject.group()' but
> > something gone wrong - didn't wrote it right.
> > Is there more user friendly HowTo for Python Re, than this?
>
> > I'm new to programming an regexp, sorry for inconvenience.
>
> I don't think you can do it in one regex, nor would I want to. Just use
> the string's replace() method.
>
>  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> mail [$dot$] com\n'
>  >>> mail
> '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> com\n'
>  >>> print mail
>
> n...@mail.com
> name1 [at] mail [dot] com
> name2 [$at$] mail [$dot$] com
>
>  >>> maail = mail.strip()
> n...@mail.com
> name1 [at] mail [dot] com
> name2 [$at$] mail [$dot$] com
>
>  >>> maail = maail.replace(' ', '')
>  >>> print maail
> n...@mail.com
> name1[at]mail[dot]com
> name2[$at$]mail[$dot$]com
>  >>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
>  >>> print maail
> n...@mail.com
> na...@mail[dot]com
> na...@mail[$dot$]com
>  >>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
>  >>> print maail
> n...@mail.com
> na...@mail.com
> na...@mail.com

Too bad, I thought that the almighty re module could do anything, but
it failed with this (or maybe re can do what i want, but only few
people knows how to force him to that?  :P).
But with help of MRAB, i choose The 3rd Point of Python's Zen -
"Simple is better than complex."

"
>>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail 
>>> [$dot$] com\n'
>>> mail

'\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
com\n'

>>> print mail

n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = mail.lstrip().rstrip().replace(' ', '').replace('[dot]', 
>>> '.').replace('[$dot$]', '.').replace('[at]', '@').replace('[$at$]', '@')
>>> print maail
n...@mail.com
na...@mail.com
na...@mail.com
>>> #Did it  :)

"

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread casevh
On Aug 4, 10:49 pm, Mensanator  wrote:
> I hadn't noticed this before, but the overhaul of Turtle Graphics
> dating
> back to 2.6 has been broken as far as gmpy is concerned.
> The reason is that code in turtle.py was chabged from
>
> v2.5
>         if self._drawing:
>             if self._tracing:
>                 dx = float(x1 - x0)
>                 dy = float(y1 - y0)
>                 distance = hypot(dx, dy)
>                 nhops = int(distance)
>
> to
>
> v3.1
>        if self._speed and screen._tracing == 1:
>             diff = (end-start)
>             diffsq = (diff[0]*screen.xscale)**2 + (diff[1]
> *screen.yscale)**2
>             nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)
> *self._speed))
>
> Unfortunately, that calculation of nhops is illegal if diffsq is
> an .mpf (gmpy
> floating point). Otherwise, you get
>
> Traceback (most recent call last):
>   File "K:\user_python26\turtle\turtle_xy_Py3.py", line 95, in
> 
>     tooter.goto(the_coord)
>   File "C:\Python31\lib\turtle.py", line 1771, in goto
>     self._goto(Vec2D(*x))
>   File "C:\Python31\lib\turtle.py", line 3165, in _goto
>     nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
> ValueError: mpq.pow fractional exponent, inexact-root
>
Warning: Completely untested fix ahead!

What happens if you change turtle.py to use

nhops=1+int((math.sqrt(diffsq)/(3*math.pow(1.1, self._speed)
*self._speed))

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Steven D'Aprano
On Wed, 5 Aug 2009 03:49 pm Mensanator wrote:

> In 3.1, tracing is now a screen attribute, not a turtle atribute.
> I have no idea why
> 
>   tooter = turtle.Turtle()
>   tooter.tracer(False)
> 
> doesn't give me an error (I thought silent errors were a bad thing).

What makes it an error? Do you consider the following an error?

>>> class Test:
... pass
...
>>> t = Test()
>>> t.tracer = 5
>>>

Perhaps you mean, it's an API change you didn't know about, and you wish to
protest that Turtle Graphics made an incompatible API change without
telling you?


> Naturally, having tracing on caused my program to crash. 

It seg faulted or raised an exception?


[...] 
> Unfortunately, that calculation of nhops is illegal if diffsq is
> an .mpf (gmpy floating point). Otherwise, you get

How does diffsq get to be a mpf? Are gmpy floats supposed to be supported?



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


Re: The Perils of PyContract (and Generators)

2009-08-05 Thread Steven D'Aprano
On Wed, 5 Aug 2009 03:06 pm Nick Daly wrote:

> The problem actually lies in the contract.  Generally, the PyContract
> shouldn't affect the return values or in any way modify the code, which
> it doesn't, as long as the function returns a list values (the way the
> code had in fact originally been written).  However, the contract
> mentioned above is actually quite wrong for a generator.

Yes, because you are conflating the items yielded from the generator with
the generator object returned from the generator function "find_files". You
can't look inside the generator object without using up whichever items you
look at.

[...]
> Correcting the above example involves doing nothing more than
> simplifying the contract:
> 
> post:
> name in __return__

That can't be right, not unless PyContract is doing something I don't
expect. I expect that would be equivalent of:

'fish' in 

which should fail:

>>> __return__ = find_files('fish')  # a generator object
>>> 'fish' in __return__
False
>>>
>>> __return__ = find_files('fish')
>>> __return__ = list(__return__)
>>> 'fish' in __return__
False
>>> __return__
['one fish', 'two fish', 'red fish', 'blue fish']

Of course, I may be mistaking what PyContract is doing.



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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Gregor Lingl

Mensanator schrieb:

I hadn't noticed this before, but the overhaul of Turtle Graphics
dating
back to 2.6 has been broken as far as gmpy is concerned.

I hadn't noticed because I usually have tracing turned off (tracing
off
takes 3-4 seconds, tracing on takes 6-7 minutes).

In 3.1, tracing is now a screen attribute, not a turtle atribute.
I have no idea why

  tooter = turtle.Turtle()
  tooter.tracer(False)

doesn't give me an error (I thought silent errors were a bad thing).



Hi,

on my machine I get:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit 
(Intel)] on win32

Type "copyright", "credits" or "license()" for more information.
>>> import turtle
>>> tooter = turtle.Turtle()
>>> tooter.tracer(False)
Traceback (most recent call last):
  File "", line 1, in 
tooter.tracer(False)
AttributeError: 'Turtle' object has no attribute 'tracer'
>>>

I'd like to help with your problem but I'd much better be
able to analyze it, if I had a version of your script,
which works as you intended with Python 2.5 and the old
turtle module. Could you please post it?

(The one you posted below uses some commands of the
turtle 2.6 module which are not present in 2.5, so it
doesn't run with Python 2.5)

Regards,
Gregor



Had to change to

  tooter = turtle.Turtle()
  tooter.screen.tracer(False) # tracer now a screen attribute

to turn tracing off in 3.1.

Naturally, having tracing on caused my program to crash. The 2.6
version
seemed to work, but only because turning off tracing as a turtle
attribute
works in 2.6.

So I turned it back on and it crashed too.

2.5 worked okay.

The reason is that code in turtle.py was chabged from

v2.5
if self._drawing:
if self._tracing:
dx = float(x1 - x0)
dy = float(y1 - y0)
distance = hypot(dx, dy)
nhops = int(distance)

to

v3.1
   if self._speed and screen._tracing == 1:
diff = (end-start)
diffsq = (diff[0]*screen.xscale)**2 + (diff[1]
*screen.yscale)**2
nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)
*self._speed))

Unfortunately, that calculation of nhops is illegal if diffsq is
an .mpf (gmpy
floating point). Otherwise, you get

Traceback (most recent call last):
  File "K:\user_python26\turtle\turtle_xy_Py3.py", line 95, in

tooter.goto(the_coord)
  File "C:\Python31\lib\turtle.py", line 1771, in goto
self._goto(Vec2D(*x))
  File "C:\Python31\lib\turtle.py", line 3165, in _goto
nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
ValueError: mpq.pow fractional exponent, inexact-root

So when using gmpy, you have to convert the .mpz to int before calling
turtle
functions. (if tracing is on).

demo code (fixed code commented out)

import gmpy

##   (even) hi|
##|
##lo (odd)
## or
##
##   (even) lo
##   |
##   |
##   hi (odd)
##
##
##
##

import turtle

tooter = turtle.Turtle()

tooter.hideturtle()
tooter.speed('fast')
turtle.update()
# make tracer false and it works
#tooter.screen.tracer(False) # tracer now a screen attribute
tooter.penup()
tooter.color('black')

s = ['1','0']
while len(s[0])<1:
s = [''.join(s), s[0]]


origin = [0,0]
if s[0] == '0':
  tooter.goto(origin)
  tooter.dot(1)
if s[1] == '0':
  tooter.goto([1,0])
  tooter.dot(1)

print(len(s[0]))

for i,j in enumerate(s[0]):
  the_coord=[]
  cur_root = gmpy.sqrt(i)
  lo__root = gmpy.sqrt(i)**2
  hi__root = ((gmpy.sqrt(i)+1)**2)
##  cur_root = int(gmpy.sqrt(i))
##  lo__root = int(gmpy.sqrt(i)**2)
##  hi__root = int(((gmpy.sqrt(i)+1)**2))

  if hi__root%2==0:
side = 'northeast'
  else:
side = 'southwest'

  elbow = (hi__root - lo__root)//2 + lo__root + 1

  if i>= elbow:

side_len = i - elbow
elbow_plus = True
  else:
side_len = elbow - i
elbow_plus = False

  if side == 'northeast':
elbow_offset = [(gmpy.sqrt(elbow)-1)//2 +1,-((gmpy.sqrt(elbow)-1)//
2) +1]
  else:
elbow_offset = [-((gmpy.sqrt(elbow)-1)//2 +1),((gmpy.sqrt
(elbow)-1)//2 +1)]
##  if side == 'northeast':
##elbow_offset = [int((gmpy.sqrt(elbow)-1)//2 +1),-int(((gmpy.sqrt
(elbow)-1)//2) +1)]
##  else:
##elbow_offset = [-int(((gmpy.sqrt(elbow)-1)//2 +1)),int
(((gmpy.sqrt(elbow)-1)//2 +1))]

  elbow_coord = [origin[0]+elbow_offset[0],origin[1]+elbow_offset[1]]

  if i != hi__root and i != lo__root:
if i == elbow:
  the_coord = elbow_coord
else:
  if elbow_plus:
if side == 'northeast':
  the_coord = [elbow_coord[0]-side_len,elbow_coord[1]]
else:
  the_coord = [elbow_coord[0]+side_len,elbow_coord[1]]
  else:
if side == 'northeast':
  the_coord = [elbow_coord[0],elbow_coord[1]+side_len]
else:
  the_coord = [elbow_coord[0],elbow_coord[1]-side_len]
  else:
if i % 2 == 0:  # even square
  n = gmpy.sqrt(i)//2 - 1
##  n = int(gmpy.sqrt(i)//2) - 1
  the_coord = [-n, -n-1]
else:
  n = (gmpy.s

Cypress FX2 - py libusb code?

2009-08-05 Thread RayS


I'm looking for a Python example for the FX2 USB
chip (I'm using the SerMod-100 board
http://www.acquiredevices.com/sermod100.jsp).
Does anyone have a simple "hello" script to re-enumerate the
chip, and then do control or bulk reads with only Python and
libusb?
I've found C code and some Python modules that use the FX2, however I'm
now using ActivePython 2.6.2.2 and don't have an appropriate compiler (I
haven't found out which they use ?).
I've used pure Python/libusb to work with a Ti ADS1271 kit
http://focus.ti.com/docs/toolsw/folders/print/ads1271evm.html

 (Ti's demo developer also liked Python BTW!) and it seem that I should be able with this FX2 as well. The catch is that the chip can re-enumerate and needs to be put in command state to accept the binary string, then stopped and re-enumerated.
http://lea.hamradio.si/~s57uuu/uuusb/simple_prg_rd.c looks like I can base work on it. So that's where I'll start if no one pipes up. There was a thread Wander replied in, but no code snip in it.
So far I've read:
http://lea.hamradio.si/~s57uuu/uuusb/uuusb_software.htm 
http://github.com/mulicheng/fx2lib/tree/master 
http://allmybrain.com/tag/fx2/ 
http://volodya-project.sourceforge.net/fx2_programmer.php 
http://www.fpgaz.com/usbp/ 
http://www.triplespark.net/elec/periph/USB-FX2/software/ 

Ray Schumacher


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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Gregor Lingl

Steven D'Aprano schrieb:

On Wed, 5 Aug 2009 03:49 pm Mensanator wrote:


In 3.1, tracing is now a screen attribute, not a turtle atribute.
I have no idea why

  tooter = turtle.Turtle()
  tooter.tracer(False)

doesn't give me an error (I thought silent errors were a bad thing).


What makes it an error? Do you consider the following an error?


class Test:

... pass
...

t = Test()
t.tracer = 5



Perhaps you mean, it's an API change you didn't know about, and you wish to
protest that Turtle Graphics made an incompatible API change without
telling you?



It didn't form 2.5 to 2.6 (at least not intentionally). But with the 
indroduction of the TurtleScreen class and the Screen class/object 
(singleton) a few of the turtle methods were also implemented as screen 
methods and as turtle methods declared deprecated (see docs of Python 
2.6). These deprecated turtle methods do not occur as turtle methods any 
more in Python 3.x.


Among them is the tracer method, which in fact does not control single 
turtle objects but all the turtles on a given screen.


So there is an icompatibility beween 2.6 and 3.x

But as far as I have understood, this doesn't concern the problem 
reported by mensator.


Regards,
Gregor




Naturally, having tracing on caused my program to crash. 


It seg faulted or raised an exception?


[...] 

Unfortunately, that calculation of nhops is illegal if diffsq is
an .mpf (gmpy floating point). Otherwise, you get


How does diffsq get to be a mpf? Are gmpy floats supposed to be supported?




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


Re: merge two png pic

2009-08-05 Thread News123
You didn't really tell us why you need one big file, which contains more
pixel, than you can probably store in RAM

Is it for printing?
Not knwoing your goal makes it difficult to suggest solutions.


Can't you use other image formats like raw RGB or
What other image formats are supported?
tiff? Raw? PPM ( http://en.wikipedia.org/wiki/Portable_pixmap )?

concatenating raw/ppm files with the same width would be really trivial.

so you could use python to create horizontal raw/ppm stripes.

then you could concatenate them without having to have the whole image
in RAM.


Perhaps there's even converters from ppm to png, that don't need the
whole image in RAM.



cocobear wrote:
> On Aug 4, 3:24 pm, News123  wrote:
>> Hi,
>>
>>
>>
>>
>>
>> cocobear wrote:
>>> Map = Image.new("RGB", (x,y))
>>> Map.paste(im, box)
>>> Map.paste(im1,box)
>>> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
>>> But if thetwopngpicis too big , or if I have tomergemorepic
>>> together, I will get MemoryError:
> Image.new("RGB",(44544,38656))
 What do you want to do with such a big image? You will run into the same
 limitation when you are trying to display it.
>>> I want to download a map from google map with high precision
>> For me it sounds strange to handle such big files.
>>
>> Is the map you download from Google really that big?
>> Mostly Google maps uses normally tiles of sizes 256x256 pixel, wich you
>> have to compose to get the  big image.
>>
> 
> The map I downloaded from Google is small(256x256). These small maps
> will compose a big image.
> 
> 
> 
>> If your problem is to combine tiles with different palettes, then you
>> should pobably look at a way to  'unify' the palette of all tiles befor
>> combining them.
>>
> 
> I think it is impossible to 'unify' all this tiles.
> 
> 
>> Being no expert of PIL I don't know whether this is easily possible though?
>>
>> bye
>>
>> N
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python to automate builds

2009-08-05 Thread Hendrik van Rooyen
On Tuesday 04 August 2009 21:13:10 Kosta wrote:
> I am a Python newbie, tasked with automating (researching) building
> Windows drivers using the WDK build environment.  I've been looking
> into Python for this (instead of writing a bunch of batch files).
>
Why do you not use make and a makefile - it was designed to 
do exactly this.

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


Special chars with HTMLParser

2009-08-05 Thread Fafounet
Hello,

I am parsing a web page with special chars such as é (which
stands for é).
I know I can have the unicode character é from unicode
("\xe9","iso-8859-1")
but with those extra characters I don' t know.

I tried to implement handle_charref within HTMLParser without success.
Furthermore, if I have the data abécd, handle_data will get "ab",
handle_charref will get xe9 and then handle_data doesn't have the end
of the string ("cd").

Thank you for your help,
Fabien
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to have the python color in the terminal ?

2009-08-05 Thread aurelien
 
> You might try posting to this thread: 
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/58df7b77394e4666/f4c13766a1e09380
> I don't know much about the question myself, though.
> 
> Marcus


Thanks for your help !

aurelien

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


trouble with complex numbers

2009-08-05 Thread Dr. Phillip M. Feldman

When I try to compute the phase of a complex number, I get an error message:

In [3]: from cmath import *
In [4]: x=1+1J
In [5]: phase(x)

NameError: name 'phase' is not defined

AttributeError: 'complex' object has no attribute 'phase'

Any advice will be appreciated.
-- 
View this message in context: 
http://www.nabble.com/trouble-with-complex-numbers-tp24821423p24821423.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: trouble with complex numbers

2009-08-05 Thread Chris Rebert
On Tue, Aug 4, 2009 at 11:28 PM, Dr. Phillip M.
Feldman wrote:
>
> When I try to compute the phase of a complex number, I get an error message:
>
> In [3]: from cmath import *
> In [4]: x=1+1J
> In [5]: phase(x)
> 
> NameError: name 'phase' is not defined
> 
> AttributeError: 'complex' object has no attribute 'phase'

I suspect the problem lies in what you snipped away:
In [1]: from cmath import phase

In [2]: x=1+1J

In [3]: phase(x)
Out[3]: 0.78539816339744828

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


Re: trouble with complex numbers

2009-08-05 Thread alex23
On Aug 5, 4:28 pm, "Dr. Phillip M. Feldman" 
wrote:
> When I try to compute the phase of a complex number, I get an error message:
> [...]
> Any advice will be appreciated.

1. What version of Python are you using, and on what platform?

2. What you snipped is necessary to help debug your problem.

3. Do you have a cmath.py in the folder you're running the interpreter
in?

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


Launch my application if its not running

2009-08-05 Thread sanju ps
How to check whether my gui application is running or not.If its already
running i have to quit my program other wise i need to launch my program.
Can anybody suggest a solution to this

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


Re: Launch my application if its not running

2009-08-05 Thread Chris Rebert
On Wed, Aug 5, 2009 at 2:03 AM, sanju ps wrote:
> How to check whether my gui application is running or not.If its already
> running i have to quit my program other wise i need to launch my program.
> Can anybody suggest a solution to this

Use a lock file? http://en.wikipedia.org/wiki/Lock_file#Lock_files

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


Re: socket.inet_ntop, and pton question

2009-08-05 Thread Mahesh Poojary S



Martin-298 wrote:
> 
> Hi
> 
> Are these functions (inet_ntop(), inet_pton()) from the socket library 
> supported on Windows.
> 
> If not is there an equivalent for them using Windows
> 
> Ive seen mention of people creating their own in order to use them
> 
> Appreciate the help
> 
> ty
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

You can use the below code:
def inet_ntop(address_family, packed_ip):
  if address_family != AF_INET:
raise socket.error, (97, 'Address family not supported by protocol')
  lIP = []
  for ch in packed_ip:
 lIP.append(str(ord(ch)))
  strIP = string.join(lIP,'.')
  return strIP

def inet_pton(address_family, ip_string):
  if address_family != AF_INET:
raise socket.error, (97, 'Address family not supported by protocol')
  lIP = ip_string.split('.')
  strHexIP = ""
  for i in lIP:
if i == '':
  continue
strHex = "%x" % int(i)
strHex = strHex.zfill(2)
strHexIP += "\\x"+strHex
  return strHexIP

-- 
View this message in context: 
http://www.nabble.com/socket.inet_ntop%2C-and-pton-question-tp8677935p24823395.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: trouble with complex numbers

2009-08-05 Thread Christian Heimes

Dr. Phillip M. Feldman wrote:

When I try to compute the phase of a complex number, I get an error message:

In [3]: from cmath import *
In [4]: x=1+1J
In [5]: phase(x)

NameError: name 'phase' is not defined

AttributeError: 'complex' object has no attribute 'phase'

Any advice will be appreciated.


phase() has been added to Python 2.6 and 3.0. It's not available in 
Python 2.5 and earlier. If you'd used cmath.phase() instead of the ugly 
"from cmath import *" statement you'd have seen the correct error message.


You can write your own phase() function. This function is mostly correct 
unless either the real and/or the imag part is NaN or INF.


from math import atan2

def phase(z):
z += 1j # convert int, long, float to complex
return atan2(z.imag, z.real)

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


intricated functions: how to share a variable

2009-08-05 Thread TP
Hi everybody,

See the following example:

#
def tutu():

def toto():

print a
a = 4
print a

a=2
toto()

tutu()
##

I obtain the following error:
"UnboundLocalError: local variable 'a' referenced before assignment"

This is because Python looks in the local context before looking in the
global context.

The use of "global a" in toto() does not help because global allows to force
Python to look for the variable at the module level.

So, how to share a variable between intricated functions?

Thanks a lot

Julien
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overlap in python

2009-08-05 Thread Albert van der Horst
In article ,
Jay Bird   wrote:
>Hi everyone,
>
>I've been trying to figure out a simple algorithm on how to combine a
>list of parts that have 1D locations that overlap into a non-
>overlapping list.  For example, here would be my input:
>
>part name   location
>a  5-9
>b  7-10
>c  3-6
>d  15-20
>e  18-23
>
>And here is what I need for an output:
>part name   location
>c.a.b3-10
>d.e   15-23
>
>I've tried various methods, which all fail.  Does anyone have an idea
>how to do this?

That is an algorithmic question and has little to do with Python.
You could proceed as follows:
-  Order your data by the lower limit
-  Combine everything with the same lower limit.
-  Then combine every pair of consecutive entries that overlap.

(In you case the second step is not needed.)

>
>Thank you very much!
>Jay


--
-- 
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: intricated functions: how to share a variable

2009-08-05 Thread Diez B. Roggisch
TP wrote:

> Hi everybody,
> 
> See the following example:
> 
> #
> def tutu():
> 
> def toto():
> 
> print a
> a = 4
> print a
> 
> a=2
> toto()
> 
> tutu()
> ##
> 
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenced before assignment"
> 
> This is because Python looks in the local context before looking in the
> global context.
> 
> The use of "global a" in toto() does not help because global allows to
> force Python to look for the variable at the module level.
> 
> So, how to share a variable between intricated functions?

You could use a class :)

Another often used trick is to have a mutable container-object, like this:

def tutu():
   a = [2]

   def toto():
   a[0] = 4

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


Re: intricated functions: how to share a variable

2009-08-05 Thread Chris Rebert
On Wed, Aug 5, 2009 at 3:13 AM, TP wrote:
> Hi everybody,
>
> See the following example:
>
> #
> def tutu():
>
>    def toto():
>

nonlocal a #note: this requires a rather recent version of python

>        print a
>        a = 4
>        print a
>
>    a=2
>    toto()
>
> tutu()
> ##
>
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenced before assignment"
>
> This is because Python looks in the local context before looking in the
> global context.
>
> The use of "global a" in toto() does not help because global allows to force
> Python to look for the variable at the module level.
>
> So, how to share a variable between intricated functions?

Details: http://www.python.org/dev/peps/pep-3104/

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


Re: Overlap in python

2009-08-05 Thread Bearophile
Albert van der Horst:
>That is an algorithmic question and has little to do with Python.<

Yes, but comp.lang.python isn't comp.lang.c, that kind of questions
are perfectly fine here. They help keep this place from becoming
boring.

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: intricated functions: how to share a variable

2009-08-05 Thread Peter Otten
TP wrote:

> Hi everybody,
> 
> See the following example:
> 
> #
> def tutu():
> 
> def toto():
> 
> print a
> a = 4
> print a
> 
> a=2
> toto()
> 
> tutu()
> ##
> 
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenced before assignment"
> 
> This is because Python looks in the local context before looking in the
> global context.
> 
> The use of "global a" in toto() does not help because global allows to
> force Python to look for the variable at the module level.
> 
> So, how to share a variable between intricated functions?

This limitation is removed in Python 3 with the 'nonlocal' statement:

>>> def outer():
... def inner():
... nonlocal a
... print(a)
... a = 4
... print(a)
... a = 2
... inner()
... print(a)
...
>>> outer()
2
4
4

Peter

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


Re: intricated functions: how to share a variable

2009-08-05 Thread Bearophile
TP:
> def tutu():
>
>     def toto():
>
>         print a
>         a = 4
>         print a
>
>     a=2
>     toto()
>
> tutu()
> ##
>
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenced before assignment"
>
> This is because Python looks in the local context before looking in the
> global context.
>
> The use of "global a" in toto() does not help because global allows to force
> Python to look for the variable at the module level.
>
> So, how to share a variable between intricated functions?

Generally your purpose as a programmer is to write the least intricate
code. Because the less tricky it is, the more readable it becomes and
also the bug count decreases.

So probably a better solution is to just use the normal function
semantics: you pass them an argument and you take an argument as
return value. Such return value will be the new version of the value
you talk about.

Python3+ has the "nonlocal" statement that may do what you ask for.
But as many other things in Python it must be used with judgment, to
avoid writing intricate code.

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python to automate builds

2009-08-05 Thread Marcus Wanner

On 8/4/2009 5:52 PM, Philip Semanchuk wrote:


On Aug 4, 2009, at 5:40 PM, Kosta wrote:


On Aug 4, 2:34 pm, Dave Angel  wrote:


+ I have released pyKook 0.0.2.
+http://pypi.python.org/pypi/Kook/0.0.2
+http://www.kuwata-lab.com/kook/
+http://www.kuwata-lab.com/kook/pykook-users-guide.html

Other possibilities:
+  http://pypi.python.org/pypi/vellum/  flexible small 'make'
alternative

+  http://code.google.com/p/waf/

+  http://code.google.com/p/fabricate/

DaveA- Hide quoted text -

- Show quoted text -


Thanks Dave.  I had thought about those three options, and was
honestly hoping for a foruth (I know, some people are never
satisfied ;).  I'll look into pyKook.  Thank you for your help.


Poof! Your wish is granted!  =)

http://www.scons.org/

Dunno if you'll find it better, worse or different than the 
alternatives, but there it is.


have fun
P

 
I can highly recommend scons. At dolphin-emu, we use it to daily compile 
 a project containing over 500,000 lines of c/c++ code, which is 
modified very frequently. It works like a charm, and is seamlessly 
cross- platform (in my experience). However, it might not be exactly the 
thing you're looking for.


Marcus

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


Re: Overlap in python

2009-08-05 Thread Marcus Wanner

On 8/4/2009 6:09 PM, MRAB wrote:
 >>> parts = [(5, 9, "a"), (7, 10, "b"), (3, 6, "c"), (15, 20, "d"), 
(18, 23, "e")]

 >>> parts.sort()
 >>> parts
[(3, 6, 'c'), (5, 9, 'a'), (7, 10, 'b'), (15, 20, 'd'), (18, 23, 'e')]
 >>> # Merge overlapping intervals.
 >>> pos = 1
 >>> while pos < len(parts):
# Merge the pair in parts[pos - 1 : pos + 1] if they overlap.
p, q = parts[pos - 1 : pos + 1]
if p[1] >= q[0]:
parts[pos - 1 : pos + 1] = [(p[0], max(p[1], q[1]), p[2] 
+ "." + q[2])]

else:
# They don't overlap, so try the next pair.
pos += 1


 >>> parts
[(3, 10, 'c.a.b'), (15, 23, 'd.e')]

That's the best solution I've seen so far. It even has input/output 
formatted as close as is reasonably possible to the format specified.


As we would say in googlecode, +1.

Marcus

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


Re: How to write replace string for object which will be substituted? [regexp]

2009-08-05 Thread Jon Clements
On 5 Aug, 07:53, ryniek  wrote:
> On 5 Sie, 00:55, MRAB  wrote:
>
>
>
> > ryniek90 wrote:
> > > Hi.
> > > I started learning regexp, and some things goes well, but most of them
> > > still not.
>
> > > I've got problem with some regexp. Better post code here:
>
> > > "
> > >  >>> import re
> > >  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> > > mail [$dot$] com\n'
> > >  >>> mail
> > > '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> > > com\n'
> > >  >>> print mail
>
> > > n...@mail.com
> > > name1 [at] mail [dot] com
> > > name2 [$at$] mail [$dot$] com
>
> > >  >>> maail = re.sub('^\n|$\n', '', mail)
> > >  >>> print maail
> > > n...@mail.com
> > > name1 [at] mail [dot] com
> > > name2 [$at$] mail [$dot$] com
> > >  >>> maail = re.sub(' ', '', maail)
> > >  >>> print maail
> > > n...@mail.com
> > > name1[at]mail[dot]com
> > > name2[$at$]mail[$dot$]com
> > >  >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
> > >  >>> print maail
> > > n...@mail.com
> > > na...@mail[dot]com
> > > na...@mail[$dot$]com
> > >  >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
> > >  >>> print maail
> > > n...@mail.com
> > > na...@mail.com
> > > na...@mail.com
> > >  >>> #How must i write the replace string to replace all this regexp's
> > > with just ONE command, in string 'mail' ?
> > >  >>> maail = re.sub('^\n|$\n| |\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]',
> > > *?*, mail)
> > > "
>
> > > How must i write that replace pattern (look at question mark), to maek
> > > that substituion work? I didn't saw anything helpful while reading Re
> > > doc and HowTo (from Python Doc). I tried with 'MatchObject.group()' but
> > > something gone wrong - didn't wrote it right.
> > > Is there more user friendly HowTo for Python Re, than this?
>
> > > I'm new to programming an regexp, sorry for inconvenience.
>
> > I don't think you can do it in one regex, nor would I want to. Just use
> > the string's replace() method.
>
> >  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> > mail [$dot$] com\n'
> >  >>> mail
> > '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> > com\n'
> >  >>> print mail
>
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> >  >>> maail = mail.strip()
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> >  >>> maail = maail.replace(' ', '')
> >  >>> print maail
> > n...@mail.com
> > name1[at]mail[dot]com
> > name2[$at$]mail[$dot$]com
> >  >>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
> >  >>> print maail
> > n...@mail.com
> > na...@mail[dot]com
> > na...@mail[$dot$]com
> >  >>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> >  >>> print maail
> > n...@mail.com
> > na...@mail.com
> > na...@mail.com
>
> Too bad, I thought that the almighty re module could do anything, but
> it failed with this (or maybe re can do what i want, but only few
> people knows how to force him to that?  :P).
> But with help of MRAB, i choose The 3rd Point of Python's Zen -
> "Simple is better than complex."
>
> "
>
> >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail 
> >>> [$dot$] com\n'
> >>> mail
>
> '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> com\n'
>
> >>> print mail
>
> n...@mail.com
> name1 [at] mail [dot] com
> name2 [$at$] mail [$dot$] com
>
> >>> maail = mail.lstrip().rstrip().replace(' ', '').replace('[dot]', 
> >>> '.').replace('[$dot$]', '.').replace('[at]', '@').replace('[$at$]', '@')
> >>> print maail
>
> n...@mail.com
> na...@mail.com
> na...@mail.com
>
> >>> #Did it  :)
>
> "
>
> Thanks again   :)

Short of writing a dedicated function I might be tempted to write this
as:

EMAIL_REPLACEMENTS = (
('[at]', '@'),
('[dot]', '.'),
...
)

for src, dest in EMAIL_REPLACEMENTS:
mail = mail.replace(src, dest)

Apart from taste reasons, it keeps the replaces more obvious (and
accessible via a variable rather than embedded in the code), enables
swapping the order or adding/removing easier.

Jon

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


Re: How to write replace string for object which will be substituted? [regexp]

2009-08-05 Thread Anthra Norell

MRAB wrote:

ryniek90 wrote:

Hi.
I started learning regexp, and some things goes well, but most of 
them still not.


I've got problem with some regexp. Better post code here:

"
 >>> import re
 >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] 
mail [$dot$] com\n'

 >>> mail
'\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail 
[$dot$] com\n'

 >>> print mail

n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

 >>> maail = re.sub('^\n|$\n', '', mail)
 >>> print maail
n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com
 >>> maail = re.sub(' ', '', maail)
 >>> print maail
n...@mail.com
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
 >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
 >>> print maail
n...@mail.com
na...@mail[dot]com
na...@mail[$dot$]com
 >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
 >>> print maail
n...@mail.com
na...@mail.com
na...@mail.com
 >>> #How must i write the replace string to replace all this 
regexp's with just ONE command, in string 'mail' ?
 >>> maail = re.sub('^\n|$\n| 
|\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]', *?*, mail)

"

How must i write that replace pattern (look at question mark), to 
maek that substituion work? I didn't saw anything helpful while 
reading Re doc and HowTo (from Python Doc). I tried with 
'MatchObject.group()' but something gone wrong - didn't wrote it right.

Is there more user friendly HowTo for Python Re, than this?

I'm new to programming an regexp, sorry for inconvenience.


I don't think you can do it in one regex, nor would I want to. Just use
the string's replace() method.

>>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] 
mail [$dot$] com\n'

>>> mail
'\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$] 
com\n'

>>> print mail

n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = mail.strip()
n...@mail.com
name1 [at] mail [dot] com
name2 [$at$] mail [$dot$] com

>>> maail = maail.replace(' ', '')
>>> print maail
n...@mail.com
name1[at]mail[dot]com
name2[$at$]mail[$dot$]com
>>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
>>> print maail
n...@mail.com
na...@mail[dot]com
na...@mail[$dot$]com
>>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
>>> print maail
n...@mail.com
na...@mail.com
na...@mail.com
This is a good learning exercise demonstrating the impracticality of 
regular expressions in a given situation. In the light of the 
fascination regular expressions seem to exert in general, one might 
conclude that knowing regular expressions in essence is knowing when not 
to use them.


There is nothing wrong with cascading substitutions through multiple 
expressions. The OP's solution wrapped up in a function and streamlined 
for needless regex overkill might look something like this:


def translate (s):
  s1 = s.strip () # Instead of: s1 = re.sub ('^\n|$\n', '', s)
  s2 = s1.replace (' ', '')# Instead of: s2 = re.sub (' ', '', s1)
  s3 = re.sub ('\[at\]|\[\$at\$\]', '@', s2)
  s4 = re.sub ('\[dot\]|\[\$dot\$\]', '.', s3)
  return s4

print translate (mail)   # Tested

MRAB's solution using replace () avoids needless regex complexity, but 
doesn't simplify tedious coding if the number of substitutions is 
significant. Some time ago I proposed a little module I made to 
alleviate the tedium. It would handle this case like this:


import SE
Translator = SE.SE ( ' (32)= [at]=@ [$at$]=@ [dot]=. [$dot$]=. ' )
print Translator (mail.strip ())   # Tested

So SE.SE compiles a string composed of any number of substitution 
definitions into an object that translates anything given it. In a 
running speed contest it would surely come in last, although in most 
cases the disadvantage would be imperceptible. Another matter is coding 
speed. Here the advantage is obvious, even with a set of substitutions 
as small as this one, let alone with sets in the tens or even hundreds. 
One inconspicuous but significant feature of SE is that it handles 
precedence correctly if targets overlap (upstream over downstream and 
long over short). As far as I know there's nothing in the Python system 
handling substitution precedence. It always needs to be hand-coded from 
one case to the next and that isn't exactly trivial.


SE can be downloaded from http://pypi.python.org/pypi/SE/2.3.

Frederic




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


Re: Using Python to automate builds

2009-08-05 Thread David Cournapeau
On Wed, Aug 5, 2009 at 4:56 PM, Hendrik van
Rooyen wrote:
> On Tuesday 04 August 2009 21:13:10 Kosta wrote:
>> I am a Python newbie, tasked with automating (researching) building
>> Windows drivers using the WDK build environment.  I've been looking
>> into Python for this (instead of writing a bunch of batch files).
>>
> Why do you not use make and a makefile - it was designed to
> do exactly this.

Because make is a pain on windows (lack of a proper shell) ?

cheers,

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


Do anyone here use Python *embedded* in a database?

2009-08-05 Thread Jonathan Fine

Hi

I'm writing a talk that compares embed and extend, and wondered if 
anyone here ever used the Python *embedded* in a database server.

http://twistedmatrix.com/users/glyph/rant/extendit.html

Web frameworks (such as Django) use extend, to import an extension 
module that makes a connection to a database.


If you have used embed, you might have consulted a page such as:
http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html

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


Re: How to write replace string for object which will be substituted? [regexp]

2009-08-05 Thread ryniek
On 5 Sie, 13:28, Anthra Norell  wrote:
> MRAB wrote:
> > ryniek90 wrote:
> >> Hi.
> >> I started learning regexp, and some things goes well, but most of
> >> them still not.
>
> >> I've got problem with some regexp. Better post code here:
>
> >> "
> >>  >>> import re
> >>  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> >> mail [$dot$] com\n'
> >>  >>> mail
> >> '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail
> >> [$dot$] com\n'
> >>  >>> print mail
>
> >> n...@mail.com
> >> name1 [at] mail [dot] com
> >> name2 [$at$] mail [$dot$] com
>
> >>  >>> maail = re.sub('^\n|$\n', '', mail)
> >>  >>> print maail
> >> n...@mail.com
> >> name1 [at] mail [dot] com
> >> name2 [$at$] mail [$dot$] com
> >>  >>> maail = re.sub(' ', '', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> name1[at]mail[dot]com
> >> name2[$at$]mail[$dot$]com
> >>  >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> na...@mail[dot]com
> >> na...@mail[$dot$]com
> >>  >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> na...@mail.com
> >> na...@mail.com
> >>  >>> #How must i write the replace string to replace all this
> >> regexp's with just ONE command, in string 'mail' ?
> >>  >>> maail = re.sub('^\n|$\n|
> >> |\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]', *?*, mail)
> >> "
>
> >> How must i write that replace pattern (look at question mark), to
> >> maek that substituion work? I didn't saw anything helpful while
> >> reading Re doc and HowTo (from Python Doc). I tried with
> >> 'MatchObject.group()' but something gone wrong - didn't wrote it right.
> >> Is there more user friendly HowTo for Python Re, than this?
>
> >> I'm new to programming an regexp, sorry for inconvenience.
>
> > I don't think you can do it in one regex, nor would I want to. Just use
> > the string's replace() method.
>
> > >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> > mail [$dot$] com\n'
> > >>> mail
> > '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> > com\n'
> > >>> print mail
>
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> > >>> maail = mail.strip()
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> > >>> maail = maail.replace(' ', '')
> > >>> print maail
> > n...@mail.com
> > name1[at]mail[dot]com
> > name2[$at$]mail[$dot$]com
> > >>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
> > >>> print maail
> > n...@mail.com
> > na...@mail[dot]com
> > na...@mail[$dot$]com
> > >>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> > >>> print maail
> > n...@mail.com
> > na...@mail.com
> > na...@mail.com
>
> This is a good learning exercise demonstrating the impracticality of
> regular expressions in a given situation. In the light of the
> fascination regular expressions seem to exert in general, one might
> conclude that knowing regular expressions in essence is knowing when not
> to use them.
>
> There is nothing wrong with cascading substitutions through multiple
> expressions. The OP's solution wrapped up in a function and streamlined
> for needless regex overkill might look something like this:
>
> def translate (s):
>    s1 = s.strip ()     # Instead of: s1 = re.sub ('^\n|$\n', '', s)
>    s2 = s1.replace (' ', '')    # Instead of: s2 = re.sub (' ', '', s1)
>    s3 = re.sub ('\[at\]|\[\$at\$\]', '@', s2)
>    s4 = re.sub ('\[dot\]|\[\$dot\$\]', '.', s3)
>    return s4
>
> print translate (mail)   # Tested
>
> MRAB's solution using replace () avoids needless regex complexity, but
> doesn't simplify tedious coding if the number of substitutions is
> significant. Some time ago I proposed a little module I made to
> alleviate the tedium. It would handle this case like this:
>
> import SE
> Translator = SE.SE ( ' (32)= [at]=@ [$at$]=@ [dot]=. [$dot$]=. ' )
> print Translator (mail.strip ())   # Tested
>
> So SE.SE compiles a string composed of any number of substitution
> definitions into an object that translates anything given it. In a
> running speed contest it would surely come in last, although in most
> cases the disadvantage would be imperceptible. Another matter is coding
> speed. Here the advantage is obvious, even with a set of substitutions
> as small as this one, let alone with sets in the tens or even hundreds.
> One inconspicuous but significant feature of SE is that it handles
> precedence correctly if targets overlap (upstream over downstream and
> long over short). As far as I know there's nothing in the Python system
> handling substitution precedence. It always needs to be hand-coded from
> one case to the next and that isn't exactly trivial.
>
> SE can be downloaded fromhttp://pypi.python.org/pypi/SE/2.3.
>
> Frederic

Thanks again.  :)

I saw that MRAB is actively developing new implementation of re
module.
MRAB: You think it'd be good idea adding to Your project some best
features

Re: How to write replace string for object which will be substituted? [regexp]

2009-08-05 Thread ryniek
On 5 Sie, 13:28, Anthra Norell  wrote:
> MRAB wrote:
> > ryniek90 wrote:
> >> Hi.
> >> I started learning regexp, and some things goes well, but most of
> >> them still not.
>
> >> I've got problem with some regexp. Better post code here:
>
> >> "
> >>  >>> import re
> >>  >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> >> mail [$dot$] com\n'
> >>  >>> mail
> >> '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail
> >> [$dot$] com\n'
> >>  >>> print mail
>
> >> n...@mail.com
> >> name1 [at] mail [dot] com
> >> name2 [$at$] mail [$dot$] com
>
> >>  >>> maail = re.sub('^\n|$\n', '', mail)
> >>  >>> print maail
> >> n...@mail.com
> >> name1 [at] mail [dot] com
> >> name2 [$at$] mail [$dot$] com
> >>  >>> maail = re.sub(' ', '', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> name1[at]mail[dot]com
> >> name2[$at$]mail[$dot$]com
> >>  >>> maail = re.sub('\[at\]|\[\$at\$\]', '@', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> na...@mail[dot]com
> >> na...@mail[$dot$]com
> >>  >>> maail = re.sub('\[dot\]|\[\$dot\$\]', '.', maail)
> >>  >>> print maail
> >> n...@mail.com
> >> na...@mail.com
> >> na...@mail.com
> >>  >>> #How must i write the replace string to replace all this
> >> regexp's with just ONE command, in string 'mail' ?
> >>  >>> maail = re.sub('^\n|$\n|
> >> |\[at\]|\[\$at\$\]|\[dot\]|\[\$dot\$\]', *?*, mail)
> >> "
>
> >> How must i write that replace pattern (look at question mark), to
> >> maek that substituion work? I didn't saw anything helpful while
> >> reading Re doc and HowTo (from Python Doc). I tried with
> >> 'MatchObject.group()' but something gone wrong - didn't wrote it right.
> >> Is there more user friendly HowTo for Python Re, than this?
>
> >> I'm new to programming an regexp, sorry for inconvenience.
>
> > I don't think you can do it in one regex, nor would I want to. Just use
> > the string's replace() method.
>
> > >>> mail = '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$]
> > mail [$dot$] com\n'
> > >>> mail
> > '\nn...@mail.com\nname1 [at] mail [dot] com\nname2 [$at$] mail [$dot$]
> > com\n'
> > >>> print mail
>
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> > >>> maail = mail.strip()
> > n...@mail.com
> > name1 [at] mail [dot] com
> > name2 [$at$] mail [$dot$] com
>
> > >>> maail = maail.replace(' ', '')
> > >>> print maail
> > n...@mail.com
> > name1[at]mail[dot]com
> > name2[$at$]mail[$dot$]com
> > >>> maail = maail.replace('[at]', '@').replace('[$at$]', '@')
> > >>> print maail
> > n...@mail.com
> > na...@mail[dot]com
> > na...@mail[$dot$]com
> > >>> maail = maail.replace('[dot]', '.').replace('[$dot$]', '.')
> > >>> print maail
> > n...@mail.com
> > na...@mail.com
> > na...@mail.com
>
> This is a good learning exercise demonstrating the impracticality of
> regular expressions in a given situation. In the light of the
> fascination regular expressions seem to exert in general, one might
> conclude that knowing regular expressions in essence is knowing when not
> to use them.
>
> There is nothing wrong with cascading substitutions through multiple
> expressions. The OP's solution wrapped up in a function and streamlined
> for needless regex overkill might look something like this:
>
> def translate (s):
>    s1 = s.strip ()     # Instead of: s1 = re.sub ('^\n|$\n', '', s)
>    s2 = s1.replace (' ', '')    # Instead of: s2 = re.sub (' ', '', s1)
>    s3 = re.sub ('\[at\]|\[\$at\$\]', '@', s2)
>    s4 = re.sub ('\[dot\]|\[\$dot\$\]', '.', s3)
>    return s4
>
> print translate (mail)   # Tested
>
> MRAB's solution using replace () avoids needless regex complexity, but
> doesn't simplify tedious coding if the number of substitutions is
> significant. Some time ago I proposed a little module I made to
> alleviate the tedium. It would handle this case like this:
>
> import SE
> Translator = SE.SE ( ' (32)= [at]=@ [$at$]=@ [dot]=. [$dot$]=. ' )
> print Translator (mail.strip ())   # Tested
>
> So SE.SE compiles a string composed of any number of substitution
> definitions into an object that translates anything given it. In a
> running speed contest it would surely come in last, although in most
> cases the disadvantage would be imperceptible. Another matter is coding
> speed. Here the advantage is obvious, even with a set of substitutions
> as small as this one, let alone with sets in the tens or even hundreds.
> One inconspicuous but significant feature of SE is that it handles
> precedence correctly if targets overlap (upstream over downstream and
> long over short). As far as I know there's nothing in the Python system
> handling substitution precedence. It always needs to be hand-coded from
> one case to the next and that isn't exactly trivial.
>
> SE can be downloaded fromhttp://pypi.python.org/pypi/SE/2.3.
>
> Frederic

Thanks again.  :)

I saw that MRAB is actively developing new implementation of re
module.
MRAB: You think it'd be good idea adding to Your project some best
features

Re: Special chars with HTMLParser

2009-08-05 Thread Piet van Oostrum
> Fafounet  (F) wrote:

>F> Hello,
>F> I am parsing a web page with special chars such as é (which
>F> stands for é).
>F> I know I can have the unicode character é from unicode
>F> ("\xe9","iso-8859-1")
>F> but with those extra characters I don' t know.

>F> I tried to implement handle_charref within HTMLParser without success.
>F> Furthermore, if I have the data abécd, handle_data will get "ab",
>F> handle_charref will get xe9 and then handle_data doesn't have the end
>F> of the string ("cd").

The character references indicate Unicode ordinals, not iso-8859-1
characters. In your example it will give the proper character because
iso-8859-1 coincides with the first part of the Unicode ordinals, but
for character outside of iso-8859-1 it will fail.

This should give you an idea:

from htmlentitydefs import name2codepoint
...
def handle_charref(self, name):
if name.startswith('x'):
num = int(name[1:], 16)
else:
num = int(name, 10)
print 'char:', repr(unichr(num))

def handle_entityref(self, name):
print 'char:', unichr(name2codepoint[name])

If your HTML may be illegal you should add some exception handling.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trouble with complex numbers

2009-08-05 Thread Piet van Oostrum
> Christian Heimes  (CH) wrote:

>CH> You can write your own phase() function. This function is mostly correct
>CH> unless either the real and/or the imag part is NaN or INF.

>CH> from math import atan2

>CH> def phase(z):
>CH> z += 1j # convert int, long, float to complex

That should be z += 0j

>CH> return atan2(z.imag, z.real)

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: intricated functions: how to share a variable

2009-08-05 Thread TP
Bearophile wrote:

> So probably a better solution is to just use the normal function
> semantics: you pass them an argument and you take an argument as
> return value. Such return value will be the new version of the value
> you talk about.

Thanks for your answer.
Yes, it is better like this. My problem is that I cannot get the return
values of the function and affect it to some variable, because the function
is called via a "signal-slot" connection of PyQt4, not by an explicit
inline function call.

For example:
#
def tutu():

def toto():

print a
a = 4
print a

a=2

# ...
# definition of some graphical QPushButton "button"
# ...
# ...

self.connect( button, SIGNAL( "clicked" ), toto )

tutu()
##

Then, as advised Diez, perhaps the best solution is to have "true" global
variables by using a class and defining the variable a as a member self.a
of the class. By doing like this, a will be known everywhere.

Julien
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
-- 
http://mail.python.org/mailman/listinfo/python-list


Datetime with float seconds

2009-08-05 Thread kpal
Hello Everybody,

The standard datetime has 1 microsecond granularity. My application
needs finer time resolution, preferably float seconds. Is there an
alternative to the out-of-the-box datetime? Timezone support is not
essential.

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


Re: The Perils of PyContract (and Generators)

2009-08-05 Thread Nick Daly
On Wed, 5 Aug 2009 03:06 pm Nick Daly wrote:

> The problem actually lies in the contract.  Generally, the PyContract
> shouldn't affect the return values or in any way modify the code, which
> it doesn't, as long as the function returns a list values (the way the
> code had in fact originally been written).  However, the contract
> mentioned above is actually quite wrong for a generator.

Yes, because you are conflating the items yielded from the
generator with the generator object returned from the generator
function "find_files". You can't look inside the generator object
without using up whichever items you look at.

[...]
> Correcting the above example involves doing nothing more than
> simplifying the contract:
>
> post:
> name in __return__

That can't be right, not unless PyContract is doing something I don't
expect. I expect that would be equivalent of:

'fish' in 

which should fail:

>>> __return__ = find_files('fish')  # a generator object
>>> 'fish' in __return__
False
>>>
>>> __return__ = find_files('fish')
>>> __return__ = list(__return__)
>>> 'fish' in __return__
False
>>> __return__
['one fish', 'two fish', 'red fish', 'blue fish']

Of course, I may be mistaking what PyContract is doing.

-

No, you're absolutely right, I realized the error in my email moments
after sending it, I was just hoping no one was paying enough attention
to notice.  I'm essentially asking PyContract to operate outside of the
generator (it needs to check the return value of a generator, without
changing its internal state), which doesn't seem to have an easy
solution unless generators have some fantastic reverse_states function
that I haven't heard about.  I suppose one could be built, saving off
the values of all local variables for later restoration, but that would
still be dependent on the state of the rest of the machine and could not
guarantee giving the same answer twice in a row.

So, the moral of the story seems to be that PyContract's post-return
value checking doesn't mix with generators at all.  Sort of sad, because
they're two neat flavors that would go great together but are instead
technically incompatible.  A decent set of unit-tests would accomplish
the same thing much less destructively anyway.

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


Re: Special chars with HTMLParser

2009-08-05 Thread Fafounet
Thank you, now I can get the correct character.

Now when I have the string abécd I can get ab then é thanks to
your function and then cd. But how is it possible to know that cd is
still the same word ?


Fabien


> The character references indicate Unicode ordinals, not iso-8859-1
> characters. In your example it will give the proper character because
> iso-8859-1 coincides with the first part of the Unicode ordinals, but
> for character outside of iso-8859-1 it will fail.
>
> This should give you an idea:
>
> from htmlentitydefs import name2codepoint
> ...
>     def handle_charref(self, name):
>         if name.startswith('x'):
>             num = int(name[1:], 16)
>         else:
>             num = int(name, 10)
>         print 'char:', repr(unichr(num))
>
>     def handle_entityref(self, name):
>         print 'char:', unichr(name2codepoint[name])
>
> If your HTML may be illegal you should add some exception handling.
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

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


Re: Datetime with float seconds

2009-08-05 Thread Xavier Ho
On Wed, Aug 5, 2009 at 10:50 PM, kpal  wrote:

> Hello Everybody,
>
> The standard datetime has 1 microsecond granularity. My application
> needs finer time resolution, preferably float seconds. Is there an
> alternative to the out-of-the-box datetime? Timezone support is not
> essential.


This is a little outdated, but:

>>>from time import time

Usually gets me things like

#output
Run time: 0.0279998779297 seconds.

So... that might be what you want.

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


Re: intricated functions: how to share a variable

2009-08-05 Thread alex23
TP  wrote:
> Then, as advised Diez, perhaps the best solution is to have "true" global
> variables by using a class and defining the variable a as a member self.a
> of the class. By doing like this, a will be known everywhere.

Or, as Bearophile suggested, you could use the standard way of passing
arguments into a function:

>>> def tutu():
...   a = 2
...   def toto(a=a):
... print 'toto', a
... a = 4
... print 'toto', a
...   print 'tutu', a
...   toto()
...   print 'tutu', a
...
>>> tutu()
tutu 2
toto 2
toto 4
tutu 2

You could also just do 'toto(a)', but as you're trying to create a
closure here, binding the external scope 'a' to toto via the default
argument will (probably) do what you need.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug in multiprocessing or in my script?

2009-08-05 Thread Jesse Noller
On Aug 5, 1:21 am, sturlamolden  wrote:
> On Aug 5, 4:37 am, erikcw  wrote:
>
> > It's not always the same traceback, but they are always short like
> > this.  I'm running Python 2.6.2 on Ubuntu 9.04.
>
> > Any idea how I can debug this?
>
> In my experience,multiprocessingis fragile. Scripts tend fo fail for
> no obvious reason, case processes to be orphaned and linger, system-
> wide resource leaks, etc. For example,multiprocessinguses os._exit
> to stop a spawned process, even though it inevitably results in
> resource leaks on Linux (it should use sys.exit). Gaël Varoquaux and I
> noticed this when we implemented shared memory ndarrays for numpy; we
> consistently got memory leaks with System V IPC for no obvious reason.
> Even after Jesse Noller was informed of the problem (about half a year
> ago), the bug still lingers. It is easy editmultiprocessing's
> forking.py file on you own, but bugs like this is a pain in the ass,
> and I suspectmultiprocessinghas many of them. Of course unless you
> show us you whole script, identifying the source of your bug will be
> impossible. But it may very likely be inmultiprocessingas well. The
> quality of this module is not impressing. I am beginning to think 
> thatmultiprocessingshould never have made it into the Python standard
> library. The GIL cannot be that bad! If you can't stand the GIL, get a
> Unix (or Mac, Linux, Cygwin) and use os.fork. Or simply switch to a
> non-GIL Python: IronPython or Jython.
>
> Allow me to show you something better. With os.fork we can write code
> like this:
>
> class parallel(object):
>
>    def __enter__(self):
>        # call os.fork
>
>    def __exit__(self, exc_type, exc_value, traceback):
>        # call sys.exit in the child processes and
>        # os.waitpid in the parent
>
>    def __call__(self, iterable):
>        # return different sub-subsequences depending on
>        # child or parent status
>
> with parallel() as p:
>     # parallel block starts here
>
>     for item in p(iterable):
>         # whatever
>
>     # parallel block ends here
>
> This makes parallel code a lot cleaner than anything you can do 
> withmultiprocessing, allowing you to use constructs similar to OpenMP.
> Further, if you make 'parallel' a dummy context manager, you can
> develop and test the algorithms serially. The only drawback is that
> you have to use Cygwin to get os.fork on Windows, and forking will be
> less efficient (no copy-on-write optimization). Well, this is just one
> example of why Windows sucks from the perspective of the programmer.
> But it also shows that you can do much better by notusingmultiprocessingat 
> all.
>
> The only case I can think of wheremultiprocessingwould be usesful,
> is I/O bound code on Windows. But here you will almost always resort
> to C extension modules. For I/O bound code, Python tends to give you a
> 200x speed penalty over C. If you are resorting to C anyway, you can
> just use OpenMP in C for your parallel processing. We can thus forget
> aboutmultiprocessinghere as well, given that we have access to the C
> code. If we don't, it is still very likely that the C code releases
> the GIL, and we can get away withusingPython threads instead 
> ofmultiprocessing.
>
> IMHO, if you areusingmultiprocessing, you are very likely to have a
> design problem.
>
> Regards,
> Sturla

Sturla;

That bug was fixed unless I'm missing something. Also, patches and
continued bug reports are welcome.

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


Re: trouble with complex numbers

2009-08-05 Thread alex23
Piet van Oostrum  wrote:
> That should be z += 0j

Pardon my ignorance, but could anyone explain the rationale behind
using 'j' to indicate the imaginary number (as opposed to the more
intuitive 'i')?

(Not that I've had much call to use complex numbers but I'm
curious)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Announcing PythonTurtle

2009-08-05 Thread John Posner



OK, then why the statements "from turtle import *" in the modules
turtleprocess.py and turtlewidget.py?



Yes, I see that now (he said meekly, too sheepish to complain about 
being misled by an unfortunate naming choice).


Tx,
John

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread casevh
On Aug 5, 12:19 am, Steven D'Aprano  wrote:
> On Wed, 5 Aug 2009 03:49 pm Mensanator wrote:
>
> > In 3.1, tracing is now a screen attribute, not a turtle atribute.
> > I have no idea why
>
> >   tooter = turtle.Turtle()
> >   tooter.tracer(False)
>
> > doesn't give me an error (I thought silent errors were a bad thing).
>
> What makes it an error? Do you consider the following an error?
>
> >>> class Test:
>
> ...     pass
> ...
>
> >>> t = Test()
> >>> t.tracer = 5
>
> Perhaps you mean, it's an API change you didn't know about, and you wish to
> protest that Turtle Graphics made an incompatible API change without
> telling you?
>
> > Naturally, having tracing on caused my program to crash.
>
> It seg faulted or raised an exception?
>
> [...]
>
> > Unfortunately, that calculation of nhops is illegal if diffsq is
> > an .mpf (gmpy floating point). Otherwise, you get
>
> How does diffsq get to be a mpf? Are gmpy floats supposed to be supported?
>
> --
> Steven

The root cause of the error is that GMP, the underlying library for
gmpy, provides only the basic floating point operations. gmpy
implements a very limited exponentiation function. Python's math
library will convert an mpf to a float automatically so I think the
revised calculation for nhops should work with either any numerical
type that supports __float__.

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


Subclassing Python's dict

2009-08-05 Thread Sergey Simonenko

Hi,

I subclass builtin 'dict' in my application and experience some problems  
with it.


The whole issue is that I should redefine 'setdefault' and 'update'  
methods after redefining '__setitem__' or/and '__delitem__',
otherwise 'update' and 'setdefault' ignore redefined '__setitem__' and use  
builtin dict's one so dict looks kinda like a black box.


Another guy have reported me that he experiences similar problems with  
subclassing builtin 'list'.


Kind regards, Sergey.

--
Написано в почтовом клиенте браузера Opera: http://www.opera.com/mail/

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


Re: Using Python to automate builds

2009-08-05 Thread Hendrik van Rooyen
On Wednesday 05 August 2009 14:08:18 David Cournapeau wrote:
> On Wed, Aug 5, 2009 at 4:56 PM, Hendrik van
>
> Rooyen wrote:
> > On Tuesday 04 August 2009 21:13:10 Kosta wrote:
> >> I am a Python newbie, tasked with automating (researching) building
> >> Windows drivers using the WDK build environment.  I've been looking
> >> into Python for this (instead of writing a bunch of batch files).
> >
> > Why do you not use make and a makefile - it was designed to
> > do exactly this.
>
> Because make is a pain on windows (lack of a proper shell) ?

Well, you could always use Dosbox - I am doing exactly that
on Linux to be able to run some old DOS based assembler
tools.  It will run on windows too, they claim.

And it is very simple to use too - no fancy fidgeting, all you have 
to do is to point it at a directory that becomes your "C" drive.

Bit slow - but hey, nobody's perfect.

- Hendrik

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


Re: Subclassing Python's dict

2009-08-05 Thread Xavier Ho
On Thu, Aug 6, 2009 at 11:51 AM, Sergey Simonenko wrote:

> I subclass builtin 'dict' in my application and experience some problems
> with it.
>

You should subclass collections.UserDict, and not the default dict class.
Refer to the collections module.

Also, the ABC MutableMapping might be of your interest.


> Another guy have reported me that he experiences similar problems with
> subclassing builtin 'list'.


Similarly, UserList is what you should subclass.

HTH,

Ching-Yun "Xavier" Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do anyone here use Python *embedded* in a database?

2009-08-05 Thread Jon Clements
On 5 Aug, 13:17, Jonathan Fine  wrote:
> Hi
>
> I'm writing a talk that compares embed and extend, and wondered if
> anyone here ever used the Python *embedded* in a database server.
>      http://twistedmatrix.com/users/glyph/rant/extendit.html
>
> Web frameworks (such as Django) use extend, to import an extension
> module that makes a connection to a database.
>
> If you have used embed, you might have consulted a page such as:
>      http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html
>
> --
> Jonathan

Yup, have used plpythonu within postgres without too many problems.
Although I do recall once that using the CSV module to load and filter
external data, did consume a massive amount of memory. Even though the
entire procedure was iterator based -- never did work out if it was
postgres caching, or some other weird stuff. [That was about 2 years
ago though]

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


Re: Subclassing Python's dict

2009-08-05 Thread Bruno Desthuilliers

Sergey Simonenko a écrit :

Hi,

I subclass builtin 'dict' in my application and experience some problems 
with it.


The whole issue is that I should redefine 'setdefault' and 'update' 
methods after redefining '__setitem__' or/and '__delitem__',
otherwise 'update' and 'setdefault' ignore redefined '__setitem__' and 
use builtin dict's one so dict looks kinda like a black box.


Another guy have reported me that he experiences similar problems with 
subclassing builtin 'list'.


I indeed notice this behaviour here (Python 2.6.2). I'm afraid it has to 
do with some optimization tricks (dict being the very fundamental data 
structure in Python, it has to be higly optimized).


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


http access produces 503

2009-08-05 Thread Rog
I am porting a simple code from Perl, the website asks for usr/pwd and
the server's side Perl script makes atemp ftp dir for file upload.

The original Perl script connects okay, does its job. The same URL
stuffed into FF3 performs the same way.
My Python script I am sweating out for past four days (noob!) gets
consistently "503", even with user agen set to: Mozilla/5.0 (Windows;
U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11.
Snippets of relevant code below.

Please, help me understand how this same code lets me log in into my
router usr/pwd running .asp, but this !...@#$% perl script returns to me
w/503 c*ap?
Thank you.

Do I need to set any proxy? The server is on intranet and the FF3 is
set to proxy. The original Perl script did not use any proxy setting.



url = http://example.com/ftpsetup.pl?username=boofa&nodeid=42
#
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
password_mgr.add_password(None, url, uid, pcode)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
class Mopener(URLopener): version = "Mozilla/5.0 (Windows; U;
Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"

opener = Mopener()

# create "opener" (OpenerDirector instance)

opener = urllib2.build_opener(handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U;
Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')]

opener.version = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:
1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"

print opener.version
# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# Install the opener all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)

try:
response = opener.open(url)
# "http://www.useragent.org/"; tested okay!!!
print"ok = 1"
except:
print "error 1"
#

output from the above:

Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/
20071127 Firefox/2.0.0.11
error 1
Error code:  503
('Service Unavailable', 'The server cannot process the request due to
a high load')

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


unicode() vs. s.decode()

2009-08-05 Thread Michael Ströder
HI!

These both expressions are equivalent but which is faster or should be used
for any reason?

u = unicode(s,'utf-8')

u = s.decode('utf-8') # looks nicer

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


Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Martin P. Hellwig

Hi List,

On several occasions I have needed (and build) a parser that reads a 
binary piece of data with custom structure. For example (bogus one):


BE
+-+-+-+-+--++
| Version | Command | Instruction | Data Length | Data | Filler |
+-+-+-+-+--++
Version: 6 bits
Command: 4 bits
Instruction: 5 bits
Data Length: 5 bits
Data: 0-31 bits
Filler: filling 0 bits to make the packet dividable by 8

what I usually do is read the packet in binary mode, convert the output 
to a concatenated 'binary string'(i.e. '0101011000110') and then use 
slice indeces to get the right data portions.
Depending on what I need to do with these portions I convert them to 
whatever is handy (usually an integer).


This works out fine for me. Most of the time I also put the ASCII art 
diagram of this 'protocol' as a comment in the code, making it more 
readable/understandable.


Though there are a couple of things that bothers me with my approach:
- This seems such a general problem that I think that there must be 
already a general pythonic solution.
- Using a string for binary representation takes at least 8 times more 
memory for the packet than strictly necessary.

- Seems to need a lot of prep work before doing the actual parsing.

Any suggestion is greatly appreciated.

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Is it possible to produce spider plots?

2009-08-05 Thread dp_pearce
I want to be able to use Python to produce Spider plots (perhaps you
know them as radar plots or star plots). Does anyone know how to
achieve this? Are there existing libraries?

Direction to any examples, especially those with tutorials, would be
greatly appreciated.

Cheers,
Dan

http://www.answers.com/topic/spider-plot
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overlap in python

2009-08-05 Thread nn
On Aug 5, 7:13 am, Marcus Wanner  wrote:
> On 8/4/2009 6:09 PM, MRAB wrote:
>
> >  >>> parts = [(5, 9, "a"), (7, 10, "b"), (3, 6, "c"), (15, 20, "d"),
> > (18, 23, "e")]
> >  >>> parts.sort()
> >  >>> parts
> > [(3, 6, 'c'), (5, 9, 'a'), (7, 10, 'b'), (15, 20, 'd'), (18, 23, 'e')]
> >  >>> # Merge overlapping intervals.
> >  >>> pos = 1
> >  >>> while pos < len(parts):
> >         # Merge the pair in parts[pos - 1 : pos + 1] if they overlap.
> >         p, q = parts[pos - 1 : pos + 1]
> >         if p[1] >= q[0]:
> >                 parts[pos - 1 : pos + 1] = [(p[0], max(p[1], q[1]), p[2]
> > + "." + q[2])]
> >         else:
> >                 # They don't overlap, so try the next pair.
> >                 pos += 1
>
> >  >>> parts
> > [(3, 10, 'c.a.b'), (15, 23, 'd.e')]
>
> That's the best solution I've seen so far. It even has input/output
> formatted as close as is reasonably possible to the format specified.
>
> As we would say in googlecode, +1.
>
> Marcus

How does it compare to this one?

http://groups.google.com/group/comp.lang.python/browse_frm/thread/1a1d2ed9d05d11d0/56684b795fc527cc#56684b795fc527cc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overlap in python

2009-08-05 Thread Scott David Daniels

Jay Bird wrote:

Hi everyone,

I've been trying to figure out a simple algorithm on how to combine a
list of parts that have 1D locations that overlap into a non-
overlapping list.  For example, here would be my input:

part name   location
a  5-9
b  7-10
c  3-6
d  15-20
e  18-23

And here is what I need for an output:
part name   location
c.a.b3-10
d.e   15-23

I've tried various methods, which all fail.  Does anyone have an idea
how to do this?

Thank you very much!
Jay


I once had to do this for finding nested block structure.
The key for me was a sort order:  start, -final.

Having not seen it here (though I looked a bit), here's one:

class Entry(object):
'''An entry is a name and range'''
def __init__(self, line):
self.name, startstop = line.split()
start, stop = startstop.split('-')
self.start, self.stop = int(start), int(stop)


def combined_ranges(lines):
'''Create Entries in "magic order", and produce ranges.

The "magic order" makes least element with longest range first, so
overlaps show up in head order, with final tail first among equals.
'''
# Fill in our table (ignoring blank lines), then sort by magic order
elements = [Entry(line) for line in lines if line.strip()]
elements.sort(key=lambda e: (e.start, -e.stop))

# Now produce resolved ranges.  Grab the start
gen = iter(elements)
first = gen.next()

# For the remainder, combine or produce
for v in gen:
if v.start <= first.stop:
# on overlap, merge in new element (may update stop)
first.name += '.' + v.name
if first.stop < v.stop:
first.stop = v.stop
else:
yield first
first = v
# And now produce the last element we covered
yield first

# Demo:
sample = '''part name   location
a  5-9
b  7-10
c  3-6
d  15-20
e  18-23
'''
source = iter(sample.split('\n')) # source of lines, opened file?
ignored = source.next() # discard heading
for interval in combined_range(source):
print '%s  %s-%s' % (interval.name, interval.start, interval.stop)

Prints:
c.a.b  3-10
d.e  15-23


--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


web access yields "503"

2009-08-05 Thread roger_lynx
Hi, I am porting a simple code from Perl, the website asks for 
usr/pwd and the server's side Perl script makes a temp ftp dir 
for a file upload.

The original Perl script connects okay, does its job. The same URL
stuffed into FF3 performs the same way.
My Python script I am sweating over for past four days (noob!) gets
consistently "503", even with user agent set to: 
Mozilla/5.0 (Windows;U; Windows NT 5.1; it; rv:1.8.1.11)Gecko/20071127 
Firefox/2.0.0.11.

Snippets of relevant code below, heavily copied from this author:
http://www.voidspace.org.uk/python/articles/urllib2.shtml

Please, help me understand how this same code lets me log in into my
router usr/pwd running .asp, but this !...@#$% perl script returns to me
w/503 c*ap?
Thank you.

Do I need to set any proxy? 
The server is on intranet and the FF3 is
set to proxy. 
The original Perl script did not use any proxy setting! 
LWP::



url = http://bad_example.com/ftpsetup.pl?username=boofa&nodeid=42
#
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
password_mgr.add_password(None, url, uid, pcode)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
class Mopener(URLopener): version = "Mozilla/5.0 (Windows; U;
Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"

opener = Mopener()

# create "opener" (OpenerDirector instance)

opener = urllib2.build_opener(handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U;
Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')]

opener.version = "Mozilla/5.0 (Windows; U; 
Windows NT 5.1; it; rv:
1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"

print opener.version
# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# Install the opener all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)

try:
response = opener.open(url)
# "http://www.useragent.org/"; tested okay!!!
print"ok = 1"
except:
print "error 1"
#

output from the above:

Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/
20071127 Firefox/2.0.0.11
error 1
Error code: 503
('Service Unavailable', 'The server cannot process the request due to
a high load')


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


Re: trouble with complex numbers

2009-08-05 Thread Scott David Daniels

alex23 wrote:

Piet van Oostrum  wrote:

That should be z += 0j


Pardon my ignorance, but could anyone explain the rationale behind
using 'j' to indicate the imaginary number (as opposed to the more
intuitive 'i')?

(Not that I've had much call to use complex numbers but I'm
curious)

I think it explained in the complex math area, but basically EE types
use j, math types use i for exactly the same thing.  Since i is so
frequently and index in CS, and there is another strong convention,
why not let the EE types win?

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


error return from urlopen

2009-08-05 Thread Rustom Mody
When I direct urlopen to a non-existent server process I get

IOError: [Errno socket error] (10061, 'Connection refused')
The connection refused is as expected but whats the 10061?
strerror(10061) says 'unknown error'

So its like an errno but not quite an errno?
Can I find out more about this number like I can about errno?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to produce spider plots?

2009-08-05 Thread Robin Becker

dp_pearce wrote:

I want to be able to use Python to produce Spider plots (perhaps you
know them as radar plots or star plots). Does anyone know how to
achieve this? Are there existing libraries?

Direction to any examples, especially those with tutorials, would be
greatly appreciated.

Cheers,

..
reportlab has a spider chart in reportlab/graphics/charts/spider.py


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


Re: trouble with complex numbers

2009-08-05 Thread alex23
On Aug 6, 1:18 am, Scott David Daniels  wrote:
> I think it explained in the complex math area, but basically EE types
> use j, math types use i for exactly the same thing.  Since i is so
> frequently and index in CS, and there is another strong convention,
> why not let the EE types win?

That 'i' tends to be used as index did occur to me, I just wasn't
aware of what conventions were used in other contexts.

Thanks, Scott, much appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error return from urlopen

2009-08-05 Thread Albert Hopkins
On Wed, 2009-08-05 at 20:48 +0530, Rustom Mody wrote:
> When I direct urlopen to a non-existent server process I get
> 
> IOError: [Errno socket error] (10061, 'Connection refused')
> The connection refused is as expected but whats the 10061?
> strerror(10061) says 'unknown error'
> 
> So its like an errno but not quite an errno?
> Can I find out more about this number like I can about errno?

http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx




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


Re: Is it possible to produce spider plots?

2009-08-05 Thread jkn
On Aug 5, 3:51 pm, dp_pearce  wrote:
> I want to be able to use Python to produce Spider plots (perhaps you
> know them as radar plots or star plots). Does anyone know how to
> achieve this? Are there existing libraries?
>

The 'wxPython in Action' book has some simple example code which
produces this sort of graphic. If you are after a GUI application
rather than creating a paper copy it might be worth checking this out.


HTH
J^n
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: intricated functions: how to share a variable

2009-08-05 Thread Jason Tackaberry
On Wed, 2009-08-05 at 12:39 +0200, Diez B. Roggisch wrote:
> Another often used trick is to have a mutable container-object, like this:
> 
> def tutu():
>a = [2]
> 
>def toto():
>a[0] = 4
> 
>toto()

When you need a writable bound variable inside a closure, I prefer this
idiom:

def foo():
   def bar():
  print bar.a
  bar.a = 4
  print bar.a
   bar.a = 2
   bar()

Python 3's nonlocal is obviously much more elegant.

Cheers,
Jason.

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Jon Clements
On 5 Aug, 15:46, "Martin P. Hellwig" 
wrote:
> Hi List,
>
> On several occasions I have needed (and build) a parser that reads a
> binary piece of data with custom structure. For example (bogus one):
>
> BE
> +-+-+-+-+--++
> | Version | Command | Instruction | Data Length | Data | Filler |
> +-+-+-+-+--++
> Version: 6 bits
> Command: 4 bits
> Instruction: 5 bits
> Data Length: 5 bits
> Data: 0-31 bits
> Filler: filling 0 bits to make the packet dividable by 8
>
> what I usually do is read the packet in binary mode, convert the output
> to a concatenated 'binary string'(i.e. '0101011000110') and then use
> slice indeces to get the right data portions.
> Depending on what I need to do with these portions I convert them to
> whatever is handy (usually an integer).
>
> This works out fine for me. Most of the time I also put the ASCII art
> diagram of this 'protocol' as a comment in the code, making it more
> readable/understandable.
>
> Though there are a couple of things that bothers me with my approach:
> - This seems such a general problem that I think that there must be
> already a general pythonic solution.
> - Using a string for binary representation takes at least 8 times more
> memory for the packet than strictly necessary.
> - Seems to need a lot of prep work before doing the actual parsing.
>
> Any suggestion is greatly appreciated.
>
> --
> MPHhttp://blog.dcuktec.com
> 'If consumed, best digested with added seasoning to own preference.'

IIRC (and I have my doubts) the BitVector module may be of use, but
it's been about 3 years since I had to look at it. I think it used the
C equiv. of short ints to do its work. Otherwise, maybe the array
module, the struct module or even possibly ctypes.

Not much use, but might give a few pointers.

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


Re: Using Python to automate builds

2009-08-05 Thread Kosta
On Aug 5, 7:29 am, Hendrik van Rooyen  wrote:
> On Wednesday 05 August 2009 14:08:18 David Cournapeau wrote:
>
> > On Wed, Aug 5, 2009 at 4:56 PM, Hendrik van
>
> > Rooyen wrote:
> > > On Tuesday 04 August 2009 21:13:10 Kosta wrote:
> > >> I am a Python newbie, tasked with automating (researching) building
> > >> Windows drivers using the WDK build environment.  I've been looking
> > >> into Python for this (instead of writing a bunch of batch files).
>
> > > Why do you not use make and a makefile - it was designed to
> > > do exactly this.
>
> > Because make is a pain on windows (lack of a proper shell) ?
>
> Well, you could always use Dosbox - I am doing exactly that
> on Linux to be able to run some old DOS based assembler
> tools.  It will run on windows too, they claim.
>
> And it is very simple to use too - no fancy fidgeting, all you have
> to do is to point it at a directory that becomes your "C" drive.
>
> Bit slow - but hey, nobody's perfect.
>
> - Hendrik

Sorry for not being more clear.  The WDK provides build.exe, a utility
that calls the compiler & linker with the approprite switches.  In
essence, it manages all the nmake details away from the developer.
For some, this is a bad thing in terms of loss of control.  But in
reality, this is a great thing, as this is the way Microsoft builds
Windows.  As far as I can tell, the WDK build environment is dervied
from the Microsoft Windows build environment, which is great for
developing kernel components in the similar enviornment in which
Microsoft builds its OS.  It is a requirement to use the WDK.

Setenv.bat sets up the path and other environment variables build.exe
needs to compile and link (and even binplace) its utilities.  So
building itself is not the issue.  The problem is that if I call
setenv.bat from Python and then build.exe, but the modifications to
the path (and other environment settings) are not seen by Python, so
the attempt to build without a specified path fails.

Now I can open up a build window that targets a build type (check or
release), an architecture target (x86 or x64) and an OS target (which
will be Windows Xp except if in the future, the driver(s) uses some
infrastructure introduced in later versions of WDM.

However, what I was hoping to do was to have a python script that
would allow me to build 4 versions of the driver: x86 check, x86
release, x64 check, and x64 release.  I may have to resort to using
batch files, but I was hoping as much as possible to use Python.

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


Re: Overlap in python

2009-08-05 Thread Mark Lawrence

Jay Bird wrote:

Hi everyone,

I wanted to thank you all for your help and *excellent* discussion.  I
was able to utilize and embed the script by Grigor Lingl in the 6th
post of this discussion to get my program to work very quickly (I had
to do about 20 comparisons per data bin, with over 40K bins in
total).  I am involved in genomic analysis research and this problem
comes up a lot and I was surprised to not have been able to find a
clear way to solve it.  I will also look through all the tips in this
thread, I have a feeling they may come in handy for future use!

Thank you again,
Jay
I don't know if this is relevant, but http://planet.python.org/ has an 
entry dated this morning which points here 
http://www.logarithmic.net/pfh/blog/01249470842.


HTH.

--
Kindest regards.

Mark Lawrence.

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


Re: unicode() vs. s.decode()

2009-08-05 Thread Jason Tackaberry
On Wed, 2009-08-05 at 16:43 +0200, Michael Ströder wrote:
> These both expressions are equivalent but which is faster or should be used
> for any reason?
>
> u = unicode(s,'utf-8')
> 
> u = s.decode('utf-8') # looks nicer

It is sometimes non-obvious which constructs are faster than others in
Python.  I also regularly have these questions, but it's pretty easy to
run quick (albeit naive) benchmarks to see.

The first thing to try is to have a look at the bytecode for each:

>>> import dis
>>> dis.dis(lambda s: s.decode('utf-8'))
  1   0 LOAD_FAST0 (s)
  3 LOAD_ATTR0 (decode)
  6 LOAD_CONST   0 ('utf-8')
  9 CALL_FUNCTION1
 12 RETURN_VALUE
>>> dis.dis(lambda s: unicode(s, 'utf-8'))
  1   0 LOAD_GLOBAL  0 (unicode)
  3 LOAD_FAST0 (s)
  6 LOAD_CONST   0 ('utf-8')
  9 CALL_FUNCTION2
 12 RETURN_VALUE  

The presence of LOAD_ATTR in the first form hints that this is probably
going to be slower.   Next, actually try it:

>>> import timeit
>>> timeit.timeit('"foobarbaz".decode("utf-8")')
1.698289155960083
>>> timeit.timeit('unicode("foobarbaz", "utf-8")')
0.53305888175964355

So indeed, uncode(s, 'utf-8') is faster by a fair margin.

On the other hand, unless you need to do this in a tight loop several
tens of thousands of times, I'd prefer the slower form s.decode('utf-8')
because it's, as you pointed out, cleaner and more readable code.

Cheers,
Jason.

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


Re: Do anyone here use Python *embedded* in a database?

2009-08-05 Thread Jonathan Fine

Jon Clements wrote:

On 5 Aug, 13:17, Jonathan Fine  wrote:

Hi

I'm writing a talk that compares embed and extend, and wondered if
anyone here ever used the Python *embedded* in a database server.
 http://twistedmatrix.com/users/glyph/rant/extendit.html

Web frameworks (such as Django) use extend, to import an extension
module that makes a connection to a database.

If you have used embed, you might have consulted a page such as:
 http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html

--
Jonathan


Yup, have used plpythonu within postgres without too many problems.
Although I do recall once that using the CSV module to load and filter
external data, did consume a massive amount of memory. Even though the
entire procedure was iterator based -- never did work out if it was
postgres caching, or some other weird stuff. [That was about 2 years
ago though]


Thanks, Jon, for that.

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


Re: Cypress FX2 - py libusb code?

2009-08-05 Thread RayS


Reply to self: code attached. 
As Marco wrote: "To run the single C file USB bulk read program
properly, you must provide a source of data to the CY7C68013 FIFO bus,
like the Simple dual channel A/D system,
otherwise the reading call will time out in one second, you will get a
bunch of zeros, and the ninth status number will be something negative
instead of 512. "
The Python behavior is 
bulkRead error:  ('usb_reap: timeout error',)
when un-connected.
Ray
At 12:25 AM 8/5/2009 -0700, you wrote:
I'm looking
for a Python example for the FX2 USB chip (I'm using the SerMod-100 board
http://www.acquiredevices.com/sermod100.jsp).
Does anyone have a simple "hello" script to re-enumerate the
chip, and then do control or bulk reads with only Python and
libusb?
I've found C code and some Python modules that use the FX2, however I'm
now using ActivePython 2.6.2.2 and don't have an appropriate compiler (I
haven't found out which they use ?).
I've used pure Python/libusb to work with a Ti ADS1271 kit
http://focus.ti.com/docs/toolsw/folders/print/ads1271evm.html

 (Ti's demo developer also liked Python BTW!) and it seem that I should be able with this FX2 as well. The catch is that the chip can re-enumerate and needs to be put in command state to accept the binary string, then stopped and re-enumerated.
http://lea.hamradio.si/~s57uuu/uuusb/simple_prg_rd.c looks like I can base work on it. So that's where I'll start if no one pipes up. There was a thread Wander replied in, but no code snip in it.
So far I've read:
http://lea.hamradio.si/~s57uuu/uuusb/uuusb_software.htm 
http://github.com/mulicheng/fx2lib/tree/master 
http://allmybrain.com/tag/fx2/ 
http://volodya-project.sourceforge.net/fx2_programmer.php 
http://www.fpgaz.com/usbp/ 
http://www.triplespark.net/elec/periph/USB-FX2/software/ 
Ray Schumacher 
-- 
http://mail.python.org/mailman/listinfo/python-list

"""
To run the single C file USB bulk read program properly,
you must provide a source of data to the CY7C68013 FIFO bus, 
like the Simple dual channel A/D system, otherwise the reading 
call will time out in one second, you will get a bunch of zeros, 
and the ninth status number will be something negative instead of 512. 
"""

import sys
import os
import time
import usb


def PrintDevInfo(dev):
"""Print device information."""
print "Device:", dev.filename
print "  Device class:",dev.deviceClass
print "  Device sub class:",dev.deviceSubClass
print "  Device protocol:",dev.deviceProtocol
print "  Max packet size:",dev.maxPacketSize
print "  idVendor:",dev.idVendor
print "  idProduct:",dev.idProduct
print "  Device Version:",dev.deviceVersion
print "  Device SerialNumber:",dev.iSerialNumber

for config in dev.configurations:
print "  Configuration:", config.value
print "Total length:", config.totalLength
print "selfPowered:", config.selfPowered
print "remoteWakeup:", config.remoteWakeup
print "maxPower:", config.maxPower
for intf in config.interfaces:
print "Interface:",intf[0].interfaceNumber
for alt in intf:
print "Alternate Setting:",alt.alternateSetting
print "  Interface class:",alt.interfaceClass
print "  Interface sub class:",alt.interfaceSubClass
print "  Interface protocol:",alt.interfaceProtocol
for ep in alt.endpoints:
print "  Endpoint:",hex(ep.address)
print "Type:",ep.type
print "Max packet size:",ep.maxPacketSize
print "Interval:",ep.interval


firmware =  [0x90, 0xE6, 0x0B, 0x74, 0x03, 0xF0,##REVCTL = 0x03
0x90, 0xE6, 0x04, 0x74, 0x80, 0xF0, ##FIFORESET = 0x80
0x74, 0x08, 0xF0,   ##FIFORESET = 
0x08
0xE4, 0xF0, 
##FIFORESET = 0x00
0x90, 0xE6, 0x01, 0x74, 0xCB, 0xF0, ##IFCONFIG = 0xCB
0x90, 0xE6, 0x1B, 0x74, 0x0D, 0xF0, ##EP8FIFOCFG = 0x0D
#0x90, 0xE6, 0x09, 0x74, 0x10, 0xF0,##FIFOPINPOLAR = 0x10   
TRUST!!!
0x80, 0xFE] 
##while (1) {}
reset = 0x01
er = []
endpoint = 8

# find all of the USB busses
busses = usb.busses()
#print busses
# Find one device
rdev = None
for bus in busses:
for dev in bus.devices:
if dev.idVendor == 0x04B4 and dev.idProduct == 0x8613:
rdev = dev
if rdev==None:
print "Could not find a CY7C68013\ndev.idVendor == 0x04B4 and 
dev.idProduct == 0x8613"
sys.exit()
else:
dev = rdev
#PrintDevInfo(dev)
endpoint=8
current_handle = dev.open()

requestType = 0x40
request = 0xa0
#buffer = ''.zfill(4096)
value=0
index=0
timeout=1000
er.append(('RESET', current_handle.controlMsg(requestType, request, reset, 
value, index, timeou

Re: Is it possible to produce spider plots?

2009-08-05 Thread Mark Lawrence

dp_pearce wrote:

I want to be able to use Python to produce Spider plots (perhaps you
know them as radar plots or star plots). Does anyone know how to
achieve this? Are there existing libraries?

Direction to any examples, especially those with tutorials, would be
greatly appreciated.

Cheers,
Dan

http://www.answers.com/topic/spider-plot
How about 
http://matplotlib.sourceforge.net/examples/api/radar_chart.html?highlight=spider


--
Kindest regards.

Mark Lawrence.

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Martin P. Hellwig

Jon Clements wrote:


IIRC (and I have my doubts) the BitVector module may be of use, but
it's been about 3 years since I had to look at it. I think it used the
C equiv. of short ints to do its work. Otherwise, maybe the array
module, the struct module or even possibly ctypes.

Not much use, but might give a few pointers.

Jon.


I tried struct before (it seemed logical) but it didn't really do what I 
expected:

>>> import struct
>>> struct.unpack('?','x')
(True,)

I expected a list like  (False, True, True, True, True, False, False, 
False). But it is (actually I hope) just an error of my side, so if 
somebody knows how to achieve what I want, with this module, please 
enlighten me :-)


The array module doesn't seem to have an option for boolean values.

BitVector does look very promising, but after peeking in the source it 
does more or less the same as what I do with converting back and forth 
of 'bitstrings'.


I haven't thought about using ctypes for this, excellent suggestion!
Thank you for your feedback.

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


access string data from within cython

2009-08-05 Thread Diez B. Roggisch
Hi,

I'm trying to wrap a C-API which has a call that takes a void* and a size_t
as arguments.

void foo(void *data, size_t length)


The wrapper is supposed to be called with a python (byte)string instance,
which might contain more than one '\0'-character.

So how do I access the raw data of a string? I tried looking into the
buffer-protocol, but to be honest - that's beyond me, I don't see where
that is actually giving me access to the real data.

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Bearophile
Martin P. Hellwig:
> On several occasions I have needed (and build) a parser that reads a
> binary piece of data with custom structure. For example (bogus one):
>
> BE
> +-+-+-+-+--++
> | Version | Command | Instruction | Data Length | Data | Filler |
> +-+-+-+-+--++
> Version: 6 bits
> Command: 4 bits
> Instruction: 5 bits
> Data Length: 5 bits
> Data: 0-31 bits
> Filler: filling 0 bits to make the packet dividable by 8

Have you tried Hachoir? (I think its name may be changed to Fusil, I
don't know).

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Masklinn

On 5 Aug 2009, at 16:46 , Martin P. Hellwig wrote:

Hi List,

On several occasions I have needed (and build) a parser that reads a  
binary piece of data with custom structure. For example (bogus one):


BE
+-+-+-+-+--++
| Version | Command | Instruction | Data Length | Data | Filler |
+-+-+-+-+--++
Version: 6 bits
Command: 4 bits
Instruction: 5 bits
Data Length: 5 bits
Data: 0-31 bits
Filler: filling 0 bits to make the packet dividable by 8

what I usually do is read the packet in binary mode, convert the  
output to a concatenated 'binary string'(i.e. '0101011000110') and  
then use slice indeces to get the right data portions.
Depending on what I need to do with these portions I convert them to  
whatever is handy (usually an integer).


This works out fine for me. Most of the time I also put the ASCII  
art diagram of this 'protocol' as a comment in the code, making it  
more readable/understandable.


Though there are a couple of things that bothers me with my approach:
- This seems such a general problem that I think that there must be  
already a general pythonic solution.
- Using a string for binary representation takes at least 8 times  
more memory for the packet than strictly necessary.

- Seems to need a lot of prep work before doing the actual parsing.

Any suggestion is greatly appreciated.
The gold standard for binary parsing (and serialization) is probably  
Erlang's bit syntax, but as far as Python goes you might be interested  
by Hachoir (http://hachoir.org/ but it seems down right now).


It's not going to match your second point, but it can probably help  
with the rest (caveat: I haven't used hachoir personally).

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Masklinn

On 5 Aug 2009, at 19:17 , Bearophile wrote:

Have you tried Hachoir? (I think its name may be changed to Fusil, I
don't know).
Name hasn't been changed (I think fusil is a subproject, something  
like that) on the other hand the hachoir.org site is dead.


But apparently Hachoir was moved to bitbucket (http://bitbucket.org/haypo/hachoir/wiki/Home 
) just in time so…

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Mensanator
> It didn't form 2.5 to 2.6 (at least not intentionally). But with the
> indroduction of the TurtleScreen class and the Screen class/object
> (singleton) a few of the turtle methods were also implemented as screen
> methods and as turtle methods declared deprecated (see docs of Python
> 2.6). These deprecated turtle methods do not occur as turtle methods any
> more in Python 3.x.

More info.

Yes, there is no tracer attribute...when the object is created.

But watch this (Python 3.1):

>>> import turtle
>>> tooter = turtle.Turtle()
>>> tooter.tracer
Traceback (most recent call last):
  File "", line 1, in 
tooter.tracer
AttributeError: 'Turtle' object has no attribute 'tracer'
>>> tooter.hideturtle()
>>> tooter.speed('fast')
>>> turtle.update()
>>> turtle.tracer


Now, after setting hide, speed, update, a tracer exists.
Is that supposed to happen? That explains why there was no error
when I set the turtle attribute instead of the screen attribute.
And, of course, setting the turtle attribute accomplishes nothing,
as actual tracing is controlled by the screen attribute as you say.

>
> Among them is the tracer method, which in fact does not control single
> turtle objects but all the turtles on a given screen.
>
> So there is an icompatibility beween 2.6 and 3.x
>
> But as far as I have understood, this doesn't concern the problem
> reported by mensator.

Only that the problem is hidden when tracing is off, as the nhops
variable is never evaluated when trace is off.

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Gregor Lingl

Mensanator schrieb:

It didn't form 2.5 to 2.6 (at least not intentionally). But with the
indroduction of the TurtleScreen class and the Screen class/object
(singleton) a few of the turtle methods were also implemented as screen
methods and as turtle methods declared deprecated (see docs of Python
2.6). These deprecated turtle methods do not occur as turtle methods any
more in Python 3.x.


More info.

Yes, there is no tracer attribute...when the object is created.

But watch this (Python 3.1):


import turtle
tooter = turtle.Turtle()
tooter.tracer

Traceback (most recent call last):
  File "", line 1, in 
tooter.tracer
AttributeError: 'Turtle' object has no attribute 'tracer'

tooter.hideturtle()
tooter.speed('fast')
turtle.update()
turtle.tracer



Now, after setting hide, speed, update, a tracer exists.


No,

>>> import turtle
>>> turtle.tracer

>>> help(turtle.tracer)
Help on function tracer in module turtle:

tracer(n=None, delay=None)
Turns turtle animation on/off and set delay for update drawings.

Optional arguments:
n -- nonnegative  integer
delay -- nonnegative  integer

If n is given, only each n-th regular screen update is really 
performed.

(Can be used to accelerate the drawing of complex graphics.)
Second arguments sets delay value.)

Example:
>>> tracer(8, 25)
>>> dist = 2
>>> for i in range(200):
fd(dist)
rt(90)
dist += 2

>>>

The reason for this is, that the turtle module (the new one as well as 
the old one) has a vers special design: The methods of class Turtle are 
also available as functions (which are in fact methods calls of an 
anonymous turtle). The same holds for the methods of TurtleScreen.


The intention behind this design is that you can use the module in an 
OOP way as well as with procedural programming (especially for beginners).


When using objects I normally use

from turtle import Turtle, Screen
screen = Screen()
# that creates  singleton object, the screen the turtle acts on
and I create as many turtles as I need from the Turtle class

So turtle.tracer() doesn't make sense anymore



Is that supposed to happen? That explains why there was no error
when I set the turtle attribute instead of the screen attribute.


You do not 'set the turtle attribute', you call the tracer function of 
the turtle module



And, of course, setting the turtle attribute accomplishes nothing,
as actual tracing is controlled by the screen attribute as you say.


Among them is the tracer method, which in fact does not control single
turtle objects but all the turtles on a given screen.

So there is an icompatibility beween 2.6 and 3.x

But as far as I have understood, this doesn't concern the problem
reported by mensator.


Only that the problem is hidden when tracing is off, as the nhops
variable is never evaluated when trace is off.


Nevertheless I'd like to see a working Python 2.5 version of your script.

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Paul Rubin
"Martin P. Hellwig"  writes:
> what I usually do is read the packet in binary mode, convert the
> output to a concatenated 'binary string'(i.e. '0101011000110') and

Something wrong with reading the data words as an integer and using
old fashioned shifts and masks to get at the bit fields?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode() vs. s.decode()

2009-08-05 Thread 1x7y2z9
unicode() has LOAD_GLOBAL which s.decode() does not.  Is it generally
the case that LOAD_ATTR is slower than LOAD_GLOBAL that lead to your
intuition that the former would probably be slower?  Or some other
intuition?
Of course, the results from timeit are a different thing - I ask about
the intuition in the disassembler output.
Thanks.

>
> The presence of LOAD_ATTR in the first form hints that this is probably
> going to be slower.   Next, actually try it:
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Special chars with HTMLParser

2009-08-05 Thread Piet van Oostrum
> Fafounet  (F) wrote:

>F> Thank you, now I can get the correct character.
>F> Now when I have the string abécd I can get ab then é thanks to
>F> your function and then cd. But how is it possible to know that cd is
>F> still the same word ?

That depends on your definition of `word'. And that is
language-dependent. 

What you normally do is collect the text in a (unicode) string variable.
This happens in handle_data, handle_charref and handle_entityref.
Then you check that the previously collected stuff was a word (e.g.
consisting of Unicode letters), and that the new stuff also consists of
letters. If your language has additional word constituents like - or '
you have to add this.

You can do this with unicodedata.category or with a regular
expression. If your locale is correct \w in a regular expression may be
helpful. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to produce spider plots?

2009-08-05 Thread Devon_Dan

> reportlab has a spider chart in reportlab/graphics/charts/spider.py

> How about
> http://matplotlib.sourceforge.net/examples/api/radar_chart.html?highl...

Both exactly what I was after. Thanks very much.

> The 'wxPython in Action' book has some simple example code which
> produces this sort of graphic. If you are after a GUI application
> rather than creating a paper copy it might be worth checking this out.

Thanks for this one jkn. I actually have this book and went through it
looking to see if it had an answer before posting. Completely missed
it! Clearly I need to RTFM more closely next time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access string data from within cython

2009-08-05 Thread Philip Semanchuk


On Aug 5, 2009, at 1:16 PM, Diez B. Roggisch wrote:


Hi,

I'm trying to wrap a C-API which has a call that takes a void* and a  
size_t

as arguments.

void foo(void *data, size_t length)


The wrapper is supposed to be called with a python (byte)string  
instance,

which might contain more than one '\0'-character.

So how do I access the raw data of a string? I tried looking into the
buffer-protocol, but to be honest - that's beyond me, I don't see  
where

that is actually giving me access to the real data.


Hi Diez,
Would ctypes.create_string_buffer() work for you?


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


Install setup tools for 2.6

2009-08-05 Thread Jeremy Cowles
Ok, I feel like this is a really stupid question, but how do you install
setup tools for Python 2.6 under windows?
The only format is .egg which requires setup tools to install, doesn't it?

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Martin P. Hellwig

Paul Rubin wrote:

"Martin P. Hellwig"  writes:

what I usually do is read the packet in binary mode, convert the
output to a concatenated 'binary string'(i.e. '0101011000110') and


Something wrong with reading the data words as an integer and using
old fashioned shifts and masks to get at the bit fields?


No not at all of course, I just found it more readable to slice it 
instead of shifting and masking; i.e. if the ASCII art schematics of the 
protocol are included as a comment it is almost an 1 to 1  implementation.


Is there an advantage using shifts and masks over my kitchen type solution?

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-08-05 Thread Just Another Victim of the Ambient Morality

"Steven D'Aprano"  wrote in message 
news:pan.2009.08.04.09.28...@remove.this.cybersource.com.au...
> On Tue, 04 Aug 2009 10:03:53 +0200, Bruno Desthuilliers wrote:
>
>>> Disadvantages: your code is filled with line noise. It's an arbitrary
>>> choice between @@ meaning instance attribute and @@ meaning class
>>> attribute -- there's no logical reason for choosing one over the other,
>>> so you have to memorise which is which. It's easy to get it wrong.
>>
>> So far that's something I have no difficulty living with.
>
> I don't like arbitrary symbols. Most people don't -- that's why "line
> noise" is unpopular. It's hard to read, hard to write, hard to maintain,
> and hard to talk about. The more line-noise, the worse the language.

It's not "line noise" if it conveys information...


> Of course, *ultimately* every symbol is arbitrary. There's no reason why
> "2" should mean the integer two, or "list" should mean a sequence type,
> but some symbols have such a long history, or have some other connection
> (say, with human languages), that the arbitrariness is lost. For
> instance, "+" is the obvious, non-arbitrary choice for the addition
> operator in any programming language using Latin symbols, and probably
> any programming language on Earth. (Not the *only* choice, but the
> obvious one.)
>
> I have a similar dislike for decorator syntax, because "@" ("at" in
> English) has nothing to do with decorations. It's an arbitrary symbol.
> One might argue that "$" would have been a more logical choice, because
> we turn numerals into currency by decorating it with a $ sign. (At least
> in the US, Canada, Australia, and a few other countries.) I use
> decorators all the time, and they are a fantastic invention, but the
> arbitrariness of the @ syntax is a negative. Oh well, one negative out of
> a whole lot of positives isn't too bad.

You can think of "@" as describing something being "at" the instance or 
the class.  "$" is totally arbitrary to me 'cause I don't thnk of my code as 
currency...


> At least I only have to deal with *one* such arbitrary symbol that needs
> memorizing. There's no need to distinguish between @@function_decorator
> and @class_decorator (or should it be the other way around?). Similarly,
> Python's choice of syntax for attributes is consistent: object.attribute
> works for everything, whether object is a class, an instance, a module,
> and whether attribute is callable or not. You can even use it on ints,
> provided you are clever about it:

You can think of "@" as being at an instance or "@@" to be more 
emphatically (as the Japanese do in their language) integrated with a class, 
being available to all instances... or you can simply understand that 
instance vairables are more common than class variables so the shorter 
notation is used for the more common case...
You want to talk about arbitrariness?  Why is len() a function you pass 
objects into while objects can have methods that describe themselves to you? 
At least Ruby defines operators using the actual name of the operator 
instead of you having to remember an arbitrary magic incantation that 
corresponds to the operator...

class Test
attr_reader :data

def initialize(data)
@data = data
end

# This is the operator part...
def + right
Test.new @data + right.data
end
end


> Somebody who knows more Ruby than me should try writing the Zen of
> Ruby. Something like:
 (snip childish parody of Python Zen)

 Steven, is that any useful ?
>>>
>>> It made me feel good.
>>
>> Why ???
>>
>> You don't like Ruby ? Fine, don't use it. Period. I can't see the point
>> of all these pissing contests.
>
> Criticism of a language is a pissing contest?
>
> Yeah, okay, I was a tad dismissive. I un-apologetically jump to strong
> impressions about languages based on minimal use -- but I'm also willing
> to change my mind. Ruby certainly looks to me like it has some nice
> features. Syntax that looks like Perl isn't one of them though.

Yeah, that would be the "pissing contest" part.
You could simply have gone to the Ruby newsgroup and posted some 
criticisms to see what was behind those decisions.  However, that would have 
avoided the pissing contest and perhaps you wanted one...


>>> Just because Smalltalk had a particular (mis?)feature
>>
>> You can drop the 'mis' part IMHO. The point of code blocks in Smalltalk
>> is that once you have something as powerful as the message+code blocks
>> combo, you just don't need any other 'special form' for control flow.
>
> Well, maybe, but remember, programming languages are only partly for
> communication to the compiler. They also have the requirement to
> communicate with human programmers as well, and that's even more
> important, because
>
> (1) humans spent a lot more time working with code than compilers do;
>
> (2) human programmers charge much more money than compilers do;
>
> (3) and you can modify the comp

Re: trouble with complex numbers

2009-08-05 Thread pdlemper
On Wed, 05 Aug 2009 08:18:55 -0700, Scott David Daniels
 wrote:

>I think it explained in the complex math area, but basically EE types
>use j, math types use i for exactly the same thing.  Since i is so
>frequently and index in CS, and there is another strong convention,
>why not let the EE types win?
>
>--Scott David Daniels
>scott.dani...@acm.org

   i represents current Dave WB3DWE
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Subclassing Python's dict

2009-08-05 Thread Joshua Kugler
Xavier Ho wrote:

> On Thu, Aug 6, 2009 at 11:51 AM, Sergey Simonenko
> wrote:
> 
>> I subclass builtin 'dict' in my application and experience some problems
>> with it.
>>
> 
> You should subclass collections.UserDict, and not the default dict class.
> Refer to the collections module.

Are you referring to Python 3.0?  Python 2.6 does not have
collections.UserDict

j


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


Re: access string data from within cython

2009-08-05 Thread sturlamolden
On 5 Aug, 19:16, "Diez B. Roggisch"  wrote:

> void foo(void *data, size_t length)
>
> The wrapper is supposed to be called with a python (byte)string instance,
> which might contain more than one '\0'-character.
>
> So how do I access the raw data of a string?

cdef extern void foo(void *data, int length)
cdef char *data
cdef int length
bytestring = ... #whatever

rawdata =  bytestring
length =  len(bytestring)

foo(data, length)


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


Re: access string data from within cython

2009-08-05 Thread sturlamolden
On 5 Aug, 21:23, sturlamolden  wrote:

> foo(data, length)

Oops, that would be

foo( data, length)


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


Re: Do anyone here use Python *embedded* in a database?

2009-08-05 Thread Joshua Kugler
Jonathan Fine wrote:

> Hi
> 
> I'm writing a talk that compares embed and extend, and wondered if
> anyone here ever used the Python *embedded* in a database server.
>  http://twistedmatrix.com/users/glyph/rant/extendit.html
> 
> Web frameworks (such as Django) use extend, to import an extension
> module that makes a connection to a database.
> 
> If you have used embed, you might have consulted a page such as:
>  http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html
> 

Yes, I've used plpython.  We had a web application that had account creation
logic with salting, encryption, etc, on the passwords.  We then wanted to
use the same accounts to log in to a Drupal site.  Instead of trying to
replicate the logic in PHP, I just created a stored procedure with
plpython, and we overrode the drupal login check to call something
like "SELECT check_account(user, pass)" which would return 1 or 0 on
success or failure, respectively.  Worked great.

j


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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Mensanator
On Aug 5, 2:19 am, Steven D'Aprano  wrote:
> On Wed, 5 Aug 2009 03:49 pm Mensanator wrote:
>
> > In 3.1, tracing is now a screen attribute, not a turtle atribute.
> > I have no idea why
>
> >   tooter = turtle.Turtle()
> >   tooter.tracer(False)
>
> > doesn't give me an error (I thought silent errors were a bad thing).
>
> What makes it an error? Do you consider the following an error?
>
> >>> class Test:
>
> ...     pass
> ...
>
> >>> t = Test()
> >>> t.tracer = 5

Come on, even _I_ know this:

>>> class Test:
pass
>>> t = Test()
>>> t.tracer
Traceback (most recent call last):
  File "", line 1, in 
t.tracer
AttributeError: 'Test' object has no attribute 'tracer'
>>> t.tracer = False
>>> t.tracer
False


>
> Perhaps you mean, it's an API change you didn't know about, and you wish to
> protest that Turtle Graphics made an incompatible API change without
> telling you?

What does this mean?

>>> import turtle
>>> tooter = turtle.Turtle()
>>> tooter.tracer
Traceback (most recent call last):
  File "", line 1, in 
tooter.tracer
AttributeError: 'Turtle' object has no attribute 'tracer'
>>> tooter.hideturtle()
>>> tooter.speed('fast')
>>> turtle.update()
>>> turtle.tracer


How did the tracer attribute appear out of thin air?
And more importantly, why, if has been deprecated and
dropped from 3.x?

>
> > Naturally, having tracing on caused my program to crash.
>
> It seg faulted or raised an exception?

Why did you snip it? Should I not refer to this as a crash?

Traceback (most recent call last):
  File "K:\user_python26\turtle\turtle_xy_Py3.py", line 95, in

tooter.goto(the_coord)
  File "C:\Python31\lib\turtle.py", line 1771, in goto
self._goto(Vec2D(*x))
  File "C:\Python31\lib\turtle.py", line 3165, in _goto
nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
ValueError: mpq.pow fractional exponent, inexact-root

>
> [...]
>
> > Unfortunately, that calculation of nhops is illegal if diffsq is
> > an .mpf (gmpy floating point). Otherwise, you get
>
> How does diffsq get to be a mpf?

No idea. I neglected to mention I'm using the new gmpy 1.10.
Don't know if that has any bearing.

> Are gmpy floats supposed to be supported?

Apparently not, as gmpy has only limited exponential capability.
Looks like I need to change turtle.py to use math.sqrt(diffsq)
or float(diffsq)**0.5. Or not pass turtle.py any .mpz since it
can't handle them.

>
> --
> Steven

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


Re: Is this a bug in multiprocessing or in my script?

2009-08-05 Thread sturlamolden
On 5 Aug, 15:40, Jesse Noller  wrote:

> Sturla;
>
> That bug was fixed unless I'm missing something.

It is still in SVN. Change every call to os._exit to sys.exit
please. :)

http://svn.python.org/view/python/branches/release26-maint/Lib/multiprocessing/forking.py?revision=66717&view=markup

http://svn.python.org/view/python/branches/release31-maint/Lib/multiprocessing/forking.py?revision=73579&view=markup







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


Re: Is this a bug in multiprocessing or in my script?

2009-08-05 Thread sturlamolden
On 5 Aug, 21:36, sturlamolden  wrote:

> http://svn.python.org/view/python/branches/release26-maint/Lib/multip...
>
> http://svn.python.org/view/python/branches/release31-maint/Lib/multip...

http://svn.python.org/view/python/trunk/Lib/multiprocessing/forking.py?revision=65864&view=markup

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


Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-05 Thread Martin P. Hellwig

Paul Rubin wrote:

"Martin P. Hellwig"  writes:

Is there an advantage using shifts and masks over my kitchen type solution?


Weren't you complaining about the 8-to-1 expansion from turning each bit
to an ascii char?


Yes you are (of course) right, my 'dream' solution would be something 
that accepts slice indeces on bit level. Your reasoning did reveal some 
flaws in my approach though ;-)


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug in multiprocessing or in my script?

2009-08-05 Thread ryles
On Aug 4, 10:37 pm, erikcw  wrote:
> Traceback (most recent call last):
>   File "scraper.py", line 144, in 
>     print pool.map(scrape, range(10))
>   File "/usr/lib/python2.6/multiprocessing/pool.py", line 148, in map
>     return self.map_async(func, iterable, chunksize).get()
>   File "/usr/lib/python2.6/multiprocessing/pool.py", line 422, in get
>     raise self._value
> TypeError: expected string or buffer

This is almost certainly due to your scrape call raising an exception.
In the parent process, multiprocessing will detect if one of its
workers have terminated with an exception and then re-raise it.
However, only the exception and not the original traceback is made
available, which is making debugging more difficult for you. Here's a
simple example which demonstrates this behavior:

*** from multiprocessing import Pool
*** def evil_on_8(x):
...   if x == 8: raise ValueError("I DONT LIKE THE NUMBER 8")
...   return x + 1
...
*** pool = Pool(processes=4)
>>> pool.map(evil_on_8, range(5))
[1, 2, 3, 4, 5]
*** pool.map(evil_on_8, range(10)) # 8 will cause evilness.
Traceback (most recent call last):
  File "", line 1, in 
  File "/bb/real/3ps/lib/python2.6/multiprocessing/pool.py", line 148,
in map
return self.map_async(func, iterable, chunksize).get()
  File "/bb/real/3ps/lib/python2.6/multiprocessing/pool.py", line 422,
in get
raise self._value
ValueError: I DONT LIKE THE NUMBER 8
***

My recommendation is that you wrap your scrape code inside a try/
except and log any exception. I usually do this with logging.exception
(), or if logging is not in use, the traceback module. After that you
can simply re-raise it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no-clobber dicts?

2009-08-05 Thread kj
In <00027aa9$0$2969$c3e8...@news.astraweb.com> Steven D'Aprano 
 writes:

>No problem. Here you go:

>http://www.cybersource.com.au/users/steve/python/constants.py

Extremely helpful.  Thanks!

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


Re: Turtle Graphics are incompatible with gmpy

2009-08-05 Thread Wolfram Hinderer
On 5 Aug., 21:31, Mensanator  wrote:
>
> >>> import turtle
> >>> tooter = turtle.Turtle()
> >>> tooter.tracer
>
> Traceback (most recent call last):
>   File "", line 1, in 
>     tooter.tracer
> AttributeError: 'Turtle' object has no attribute 'tracer'>>> 
> tooter.hideturtle()
> >>> tooter.speed('fast')
> >>> turtle.update()
> >>> turtle.tracer
>
> 
>
> How did the tracer attribute appear out of thin air?

You seem to confuse the "tooter" Turtle object and the "turtle" module
when talking about "the tracer attribute".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug in multiprocessing or in my script?

2009-08-05 Thread Piet van Oostrum
> sturlamolden  (s) wrote:

>s> On 5 Aug, 15:40, Jesse Noller  wrote:
>>> Sturla;
>>> 
>>> That bug was fixed unless I'm missing something.

>s> It is still in SVN. Change every call to os._exit to sys.exit
>s> please. :)

Calling os.exit in a child process may be dangerous. It can cause
unflushed buffers to be flushed twice: once in the parent and once in
the child. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


How to combine regexps?

2009-08-05 Thread kj


One of the nice things one can do with Perl's regexp's is illustrated
in the following example:

my $gly = qr/gg[ucag]/i
my $ala = qr/gc[ucag]/i;
my $val = qr/gu[ucag]/i;
my $leu = qr/uu[ag]|cu[ucag]/i;
my $ile = qr/au[uca]/i;

my $aliphatic = qr/$gly|$ala|$val|$leu|$ile/;


In other words, one can build regular expressions by re-combining
other regular expressions.

Is there a way to do this with Python's regexps?

TIA!

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


  1   2   >