Re: Functional schmunctional...

2009-02-10 Thread andrew cooke
r0g wrote:
 def ip2inet(a):
   li = a.split('.')
   assert len(li) == 4 or len(li) == 6
   return reduce(add,[int(li[e])*(256**((len(li)-1)-e)) for e in
 xrange(0,len(li))])

what a mess.

i don't use this extreme a functional style in python (it's not really how
the language is intended to be used), but i think you can do better than
that.

how about:

from itertools import count

def ip2inet(a):
blocks = a.split('.')
assert len(blocks) in (4, 6)
return sum(map(lambda (i, n): int(i) * 256**n,
   zip(reversed(blocks), count(0

i haven't looked at the other function, but as a general comment it sounds
me like you are in such a hurry to point out fp is bad that you haven't
bothered to master it first.

andrew


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


Re: Functional schmunctional...

2009-02-10 Thread Paul Rubin
r0g aioe@technicalbloke.com writes:
 def inet2ip(n):
   p = (n/16777216)
   q = ((n-(p*16777216))/65536)
   r = ((n-((p*16777216)+(q*65536)))/256)
   s = ((n-((p*16777216)+(q*65536)+(r*256
   return str(p)+.+str(q)+.+str(r)+.+str(s)

from struct import pack
def inet2ip(n):
  xs = pack('L',n)
  return '.'.join(str(ord(x)) for x in xs)
--
http://mail.python.org/mailman/listinfo/python-list


Replace unknow string varible in file.

2009-02-10 Thread namire
Hey .python first time poster here. I'm pretty good with python so
far, but I keep needed a function in my program but not knowing how to
build it. =( Here's the problem:

Imagine a html file full of 100's of these strings all mooshed
together onto many lines;
!--@@MARKER@@; id=ITEM--ITEMbr
Where the word 'MARKER' is a constant, it stay the same in each string
and the word 'ITEM' is a random combination of ascii characters of an
unknown length. So for example a:
!--@@MARKER@@; id=CATFISH--CATFISHbrh1Text text text/
h1!--@@MARKER@@; id=SPAM--SPAMbr and so on...

What I need to do it replace each instance of the random letter with a
constant and/or delete them. The file is a html file so the stuff
inside of !-- -- is ok to keep and I need that data to identify
where the strings are in the file (it's full of other stuff too).  I'm
tired making a bruteforcer but with unknown length and 26 letters of
the alphabet I gave up because it would take too long (it was
something like; read file; if '@@MARKER@@; id='+str(gen_string)+'--
+sr(gen_string)+'br' in file then replace with '', but I'm
paraphrasing code and it's not the best solution anyway).

Just as a comparison in the Windows OS this seems easy to do when
managing files, say for the file a-blah-b-blah.tmp where blah is an
unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
python and a string in text file I don't have a clue. @_@ could
someone please help me?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Module: nift

2009-02-10 Thread r0g
J wrote:
 Thanks for your answers, especially Chris Rebert and Paul McGuire's. I
 have a question:
 How far does Python go in the Game Development field? (Using Python
 only, no extensions)


Hey J,

Python's all about the libraries (extensions), you won't be able to do
much without them but that's no big whoop. AFAIK most python libs are
released under LGPL or BSD style licenses so there's pretty much no
restriction on what you can use them for. A lot of games are written in
C for performance reasons but most python gaming libs are just wrappers
round C gaming libs and share the same names and methods so your
knowledge should travel with you.

As for which libs to use...

There's PyGame which is a a good mature library for 2D stuff.

Pyglet seem to be the most popular OpenGL lib for 3D stuff.

You might also want to look at the Blender Game Engine and the free
game Yo Frankie, they use C for the heavy lifting and Python for
scripting levels and stuff, it might be a good way to test the waters.

Regards,

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


zlib interface semi-broken

2009-02-10 Thread Travis
Hello all,

The zlib interface does not indicate when you've hit the end of a compressed 
stream.

The underlying zlib functionality provides for this.

With python's zlib, you have to read past the compressed data and into
the uncompressed, which gets stored in Decompress.unused_data.

As a result, if you've got a network protocol which mixes compressed
and non-compressed output, you may find a compressed block ending with
no uncompressed data following until you send another command -- which
a synchronous (non-pipelined) client will not send, because it is waiting
for the [compressed] data from the previous command to be finished.

As a result, you get a protocol deadlock.

A simple way to fix this would be to add a finished attribute to the
Decompress object.

However, perhaps this would be a good time to discuss how this library
works; it is somewhat awkward and perhaps there are other changes which
would make it cleaner.

What does the python community think?
-- 
Crypto ergo sum.  http://www.subspacefield.org/~travis/
Do unto other faiths as you would have them do unto yours.
If you are a spammer, please email j...@subspacefield.org to get blacklisted.
--
http://mail.python.org/mailman/listinfo/python-list


optparse versus getopt

2009-02-10 Thread Matthew Sacks
does anyone have any arguments against optparse vs getopt
--
http://mail.python.org/mailman/listinfo/python-list


pySerial help please!

2009-02-10 Thread bmaschino
Hello all,

I am very new to Python and I am using it because I needed an easy
language to control a piece of equipment that connects to my computer
via a serial cable. I am running Python 2.6 with pySerial 2.4 under
Windows. I can get Python to create a serial port on COM1, but when I
try to write to the device, nothing happens. Here is an example:

import serial
ser = serial.Serial(0)
ser.write(otpm 2 16 0)
ser.close()

If I connect to the device in Hyperterminal the device will behave as
expected when I give it the command 'otpm 2 16 0' but not in Python. I
have confirmed Python is actually controlling the port because
Hyperterminal will not open the port while Python has it open,
although I have not been able to try a loopback connector as of yet.
Any suggestions out there? Thanks in advance!

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


Re: optparse versus getopt

2009-02-10 Thread Robert Kern

On 2009-02-10 15:06, Matthew Sacks wrote:

does anyone have any arguments against optparse vs getopt


As the getopt docs say: A more convenient, flexible, and powerful alternative 
is the optparse module.


I have found all three statements to be true.

But I've found argparse to be even better. The main reason is that argparse will 
help you parse, verify and generate help text for positional arguments in 
addition to --options.


  http://argparse.python-hosting.com/
  http://argparse.python-hosting.com/wiki/ArgparseVsOptparse

--
Robert Kern

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

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


Re: Functional schmunctional...

2009-02-10 Thread Scott David Daniels

For expressiveness, try something like:

def ip2in(dotted_ip_addr):
result = 0
assert dotted_ip_addr.count('.') in (3, 7)
for chunk in dotted_ip_addr.split('.'):
result = (result  8) + int(chunk)
return result

def inet2ip(ip_number):
assert 0  ip_number  1  48
bytes = []
while ip_number:
bytes.append(ip_number  255)
ip_number = 8
assert len(bytes) in (4, 6)
return '.'.join(str(n) for n in reversed(bytes))

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


Re: Replace unknow string varible in file.

2009-02-10 Thread Vlastimil Brom
2009/2/10 namire nam...@gmail.com:
 Hey .python first time poster here. I'm pretty good with python so
 far, but I keep needed a function in my program but not knowing how to
 build it. =( Here's the problem:

 Imagine a html file full of 100's of these strings all mooshed
 together onto many lines;
 !--@@MARKER@@; id=ITEM--ITEMbr
 Where the word 'MARKER' is a constant, it stay the same in each string
 and the word 'ITEM' is a random combination of ascii characters of an
 unknown length. So for example a:
 !--@@MARKER@@; id=CATFISH--CATFISHbrh1Text text text/
 h1!--@@MARKER@@; id=SPAM--SPAMbr and so on...

 What I need to do it replace each instance of the random letter with a
 constant and/or delete them.
...
 Just as a comparison in the Windows OS this seems easy to do when
 managing files, say for the file a-blah-b-blah.tmp where blah is an
 unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
 python and a string in text file I don't have a clue. @_@ could
 someone please help me?
 --
 http://mail.python.org/mailman/listinfo/python-list


Hi,
It is not quite clear to me, what should be achieved with the given
file, but an example with wildcard characters in windows implies, that
the regular expressions can be of some use here (given the file is as
regular as the samples, especially without nesting the comments etc.)

the segments in examples can be matched eg. with the expression:

!--@@MARKER@@; id=([^]+)--\1br

the ITEM, CATFISH, SPAM ... elements are captured in the parethesised
group and can be used foe matching or replacing.

check the re module in the python library:
http://docs.python.org/library/re.html

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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Albert Hopkins
On Tue, 2009-02-10 at 12:50 -0800, Josh Dukes wrote:

 The thing I don't understand is why a generator that has no iterable
 values is different from an empty list. Why shouldn't bool ==
 has_value?? Technically a list, a tuple, and a string are also objects
 but if they lack values they're evaluated as False. It seems to me that
 a generator is an object that intends to replace lists where lazy
 evaluation would be more efficent. Here is one place where that's
 definitely true. 

Well, I did not implement generators in python, but my guess would be
that lists and tuples can be checked with len() to see if it is
non-empty.  Generators don't have length.  You would at least need to
call .next() which changes the generator so every time you'd want to
evaluate the boolean of the generator you'd potentially lose the next
item.

Generators are meant to replace lists where you don't want/can't put the
entire list in memory or for which there is no (known) end to the
list. You don't know the next value a generator will return (if any)
until you evaluate it.  Don't think of generators as containers like
lists, tuples and strings are.  Generators don't contain values.
Generators are objects that return the next value.  It has no idea how
many values it contains (it's not a container).  It only knows the
.next() value when it's called.  It forgets the value once it's
returned. And it has no idea how far it is in the iteration until it's
finished (StopIteration).

 The main reason I'm interested in this is that it improves performance
 immensely over boolean evaluation of large lists (as the attached code
 shows). It seems to me if I could use find a good use for it in my
 experimentation that someone else might also want to do the same thing
 in real-world code. 

I don't understand what you mean by this.  But if you really want to
know if a generator is non-empty:

def non_empty(virgin_generator):
try:
virgin_generator.next() # note you just lost the first value
return True
except StopIteration:
return False

The only way to get around this is to put all the values of a generator
inside a container (e.g. a list):

l = list(generator_object)

but in doing so you've (obviously) lost the advantages of the generator.


 Is there another list I should be asking these questions on?

I don't know. Sorry I wasn't able to help you.

-a


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


Re: zlib interface semi-broken

2009-02-10 Thread Scott David Daniels

Travis wrote:

The zlib interface does not indicate when you've hit the

 end of a compressed stream

The underlying zlib functionality provides for this.

With python's zlib, you have to read past the compressed data and into
the uncompressed, which gets stored in Decompress.unused_data.
... [good explanation of why this is problematic] ...
A simple way to fix this would be to add a finished attribute to the
Decompress object.

Perhaps you could submit a patch with such a change?


However, perhaps this would be a good time to discuss how this library
works; it is somewhat awkward and perhaps there are other changes which
would make it cleaner.

Well, it might be improvable, I haven't really looked.  I personally
would like it and bz2 to get closer to each other in interface, rather
than to spread out.  SO if you are really opening up a can of worms,
I vote for two cans.

--Scott David Daniels
scott.dani...@acm.org



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


Re: optparse versus getopt

2009-02-10 Thread Tim Chase

Matthew Sacks wrote:

does anyone have any arguments against optparse vs getopt


I've found that the optparse module beats getopt on *every* 
aspect except in the event that you have experience with the C 
getopt libraries *and* just want something that behaves like 
those libraries.  Optparse is easy to learn, easy to read, and 
part of the Python stdlib.


-tkc




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


Re: Logging in Python

2009-02-10 Thread aha
Thanks for your suggestions.  I've also figured that I can test if
logging.RootLogger.manager.loggerDict has any items in it.  Or if it
has a logger for the module that I wish to start.  I like basicLogger
idea though as it seems like the cleanest implementation.

On Feb 10, 3:21 pm, Vinay Sajip vinay_sa...@yahoo.co.uk wrote:
 On Feb 10, 5:50 pm, aha aquil.abdul...@gmail.com wrote:



  Hello All,

  I have an application whereloggingmay need to be configured in
  multiple places.  I've used the PythonLoggingFramework for sometime,
  but I'm still not sure how to test iflogginghas configured.  For
  example, I have modules A, B, and C.

  Below is some pseudo code...
  moduleA

  class A(object):
    def __init__(self):
      ...

  startLogging(config):
    # Configurelogging
    # global logger
    ...

  moduleB
  import moduleA
  from myconfig import MyConfig
  class B(object):
    def __init__(self):
      # self.config = MyConfig()
      # iflogginghas started [HOW DO YOU DO THIS?]
      #   self.logger =logging.getLogger(moduleB)
      # else
      #   self.logger = moduleA.startLogging(self.config)
      # moduleA.startLogging
      ...

  Where I need help is determining if a logger has already been
  configured.  Any advice?

  Aquil

 It depends upon how complicated your logging requirements are. For
 example, each module can have the following code in it:

 import logging

 logging.basicConfig(level=logging.DEBUG, filename=/tmp/myapp.log,
 filemode=w) # An example

 logger = logging.getLogger(__name__)

 ... your code, involving logger.debug(...) statements

 basicConfig() attaches a FileLogger to the root logger, so all logging
 output would be routed to the file /tmp/myapp.log in the example.
 However, basicConfig() does nothing if the root logger already has
 handlers, so calling it in each module shouldn't cause problems. It's
 also nice to use the module name (__name__) as the logger name.

 Another pattern is to configure logging in your main module, if there
 is one, and then the other modules just assume logging is configured
 and log away. If there isn't a main module, have all the modules
 import a common module which, when imported, configures logging how
 you want it. Under normal circumstances, the import code will only run
 once, so your logging only gets configured the first time the module
 gets imported by any of the others.

 Regards,

 Vinay Sajip

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


Re: optparse versus getopt

2009-02-10 Thread Matthew Sacks
it seems as if optparse isn't in my standard library.
is there a way to add a lib like ruby gems?

On Tue, Feb 10, 2009 at 1:38 PM, Tim Chase
python.l...@tim.thechases.com wrote:
 Matthew Sacks wrote:

 does anyone have any arguments against optparse vs getopt

 I've found that the optparse module beats getopt on *every* aspect except in
 the event that you have experience with the C getopt libraries *and* just
 want something that behaves like those libraries.  Optparse is easy to
 learn, easy to read, and part of the Python stdlib.

 -tkc





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


Cannot get python to read a CAP file link in an ATOM feed

2009-02-10 Thread Brian Kaplan
Hi List,
I'm trying to get python to parse a CAP file in an ATOM feed from the
National Weather Service.  Here's one states ATOM feed.
http://www.weather.gov/alerts-beta/ky.php?x=0.  If you view the source
of this file, there is a reference to another web feed. For instance,
href=http://www.weather.gov/alerts-beta/wwacapget.php?x=KY20090210210200ILNNPWILNHighWindWarning2009021210KY;
(note, this latter file changes hourly).  I can retrieve and parse the
first file but cannot get the second file read or parsed as an xml. I
am able to read it into memory using urlopen.  Can python read and
parse as an xml or do I need to read it into memory.  If it is in
memory, is there a similar parsing function.  Thanks  Brian Kaplan
--
http://mail.python.org/mailman/listinfo/python-list


Re: optparse versus getopt

2009-02-10 Thread Robert Kern

On 2009-02-10 15:42, Matthew Sacks wrote:

it seems as if optparse isn't in my standard library.


How did you install your Python? It has been part of the standard library for a 
very long time.



is there a way to add a lib like ruby gems?


http://docs.python.org/install/index.html

But optparse (named Optik when it was a separate package) has not been 
distributed separately from Python for a long time.


--
Robert Kern

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

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


Re: Using TK and the canvas.

2009-02-10 Thread Kalibr
On Feb 11, 5:51 am, r rt8...@gmail.com wrote:
 On Feb 10, 1:27 pm, Kalibr space.captain.f...@gmail.com wrote:
 [snip]

 You should really check out wxPython, there is support for just this
 type of thing. of course you could do this with Tkinter, but just
 thinking about it makes my head hurt :).

Alright, I shall investigate. Thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: GAE read binary file into db.BlobProperty()

2009-02-10 Thread alex goretoy
was not able to use open to open a binary file so what I did was use
urlfetch to fetch the image for me and read the content into BlobProperty()
Not sure why it took me so long to figure this out. Hope it helps someone.
thx

  def post(self,key):
k=db.get(key)
  img=images.Image(urlfetch.Fetch(http://www.example.com/
+k.image).content)
  img.rotate(90)
  jpg_data= img.execute_transforms(images.JPEG)
  k.image_blob=jpg_data
  k.put()

-Alex Goretoy
http://www.goretoy.com



On Mon, Feb 9, 2009 at 11:07 AM, alex goretoy
aleksandr.gore...@gmail.comwrote:

 How to read Binary file into GAE(Google App Engine) db.BlobProperty()
 datastore?

 -Alex Goretoy
 http://www.goretoy.com


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


Re: zlib interface semi-broken

2009-02-10 Thread Paul Rubin
Travis travis+ml-pyt...@subspacefield.org writes:
 However, perhaps this would be a good time to discuss how this library
 works; it is somewhat awkward and perhaps there are other changes which
 would make it cleaner.
 
 What does the python community think?

It is missing some other features too, like the ability to preload
a dictionary.  I'd support extending the interface.  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Functional schmunctional...

2009-02-10 Thread bearophileHUGS
Here a small benchmark:

def ip2inet01(a): # can't be used with 6
li = a.split('.')
assert len(li) == 4
a = int(li[0])*16777216
b = int(li[1])*65536
c = int(li[2])*256
d = int(li[3])
return a+b+c+d

from itertools import count

def ip2inet02(a):
blocks = a.split('.')
assert len(blocks) in (4, 6)
return sum(map(lambda (i, n): int(i) * 256**n, zip(reversed
(blocks), count(0

def ip2inet03(iptxt):
bytes = map(int, iptxt.strip().split(.))[::-1]
assert len(bytes) in (4, 6)
return sum(by * 256**po for po, by in enumerate(bytes))

def ip2inet04(iptxt):
bytes = map(int, reversed(iptxt.strip().split(.)))
assert len(bytes) in (4, 6)
powers256 = [1, 256, 65536, 16777216, 4294967296, 1099511627776]
return sum(by * powers256[po] for po, by in enumerate(bytes))

def ip2inet05(iptxt):
bytes = (int(by) for by in reversed(iptxt.strip().split(.)))
parts = [by * ip2inet05.powers256[po] for po, by in enumerate
(bytes)]
assert len(parts) in (4, 6)
return sum(parts)
ip2inet05.powers256 = [1, 256, 65536, 16777216, 4294967296,
1099511627776]

def ip2inet06(a):
li = a.split('.')
n = len(li)
assert n == 4 or n == 6
if n == 4:
return (int(li[0]) * 16777216 +
 int(li[1]) * 65536 +
 int(li[2]) * 256 +
 int(li[3]))
else:
return (int(li[0]) * 1099511627776 +
 int(li[1]) * 4294967296 +
 int(li[2]) * 16777216 +
 int(li[3]) * 65536 +
 int(li[4]) * 256 +
 int(li[5]))

def ip2inet07(iptxt):
bytes = map(int, iptxt.strip().split(.))[::-1]
assert len(bytes) in (4, 6)
return sum(by * ip2inet07.pows[po] for po, by in enumerate(bytes))
ip2inet07.pows = [1, 256, 65536, 16777216, 4294967296, 1099511627776]

from struct import pack
def ip2inet08(n): # error
  xs = pack('L', n)
  return '.'.join(str(ord(x)) for x in xs)

def ip2inet09(iptxt): # short and readable
bytes = map(int, iptxt.strip().split(.))[::-1]
assert len(bytes) in (4, 6)
return sum(by  (8*po) for po, by in enumerate(bytes))

def ip2inet10(iptxt):
count = 0
result = 0
for byte in iptxt.strip().split(.):
result = 8
result += int(byte)
count += 1
assert count in (4, 6)
return result

def ip2inet11(iptxt):
bytes = iptxt.strip().split(.)
assert len(bytes) in (4, 6)
result = 0
for byte in bytes:
result = 8
result += int(byte)
return result

def ip2inet12(iptxt):
bytes = iptxt.strip().split(.)
assert len(bytes) in (4, 6)
result = 0
for byte in bytes:
result = (result  8) + int(byte)
return result

def ip2inet13(a): # fastest and readable
li = a.split('.')
n = len(li)
assert n == 4 or n == 6
if n == 4:
return ((int(li[0])  24) +
 (int(li[1])  16) +
 (int(li[2])  8) +
 int(li[3]))
else:
return ((int(li[0])  40) +
 (int(li[1])  32) +
 (int(li[2])  24) +
 (int(li[3])  16) +
 (int(li[4])  8) +
 int(li[5]))



def main():
from timeit import default_timer as clock

ip4 = 155.16.187.87
ip6 = 7.155.16.187.87.255
algos = [ip2inet02, ip2inet03, ip2inet04, ip2inet05, ip2inet06,
 ip2inet07, ip2inet09, ip2inet10, ip2inet11, ip2inet12,
 ip2inet13]
for algo in algos:
assert algo(ip4) == 2601565015 and algo(ip6) == 8362582038527
t0 = clock()
for i in xrange(1):
algo(ip4)
algo(ip6)
print algo.__name__, round(clock() - t0, 2), s

import psyco; psyco.full()
print With Psyco:
for algo in algos:
t0 = clock()
for i in xrange(1):
algo(ip4)
algo(ip6)
print algo.__name__, round(clock() - t0, 2), s


main()

ip2inet02 2.68 s
ip2inet03 1.79 s
ip2inet04 1.72 s
ip2inet05 1.88 s
ip2inet06 0.98 s
ip2inet07 1.57 s
ip2inet09 1.5 s
ip2inet10 1.07 s
ip2inet11 1.07 s
ip2inet12 1.03 s
ip2inet13 0.95 s

With Psyco (indipendent run! Not normally successive):
ip2inet02 2.66 s
ip2inet03 1.66 s
ip2inet04 1.97 s
ip2inet05 1.66 s
ip2inet06 0.57 s
ip2inet07 1.5 s
ip2inet09 1.54 s
ip2inet10 0.53 s
ip2inet11 0.76 s
ip2inet12 0.52 s
ip2inet13 0.8 s

So if you need speed you the version #13 is better, if you want short
and readable code (but not too much slow) you can use #9, and if you
want code easy to understand by every one and easy to translate to
other languages then you can use #12.

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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:

 The thing I don't understand is why a generator that has no iterable
 values is different from an empty list.

How do you know it has no iterable values until you call next() on it and 
get StopIteration?

By the way, your has_values function is just a slower version of the 
built-in any().


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


Re: can multi-core improve single funciton?

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 12:43:20 +, Lie Ryan wrote:

 Of course multi-core processor can improve single function using
 multiprocessing, as long as the function is parallelizable. The
 Fibonacci function is not a parallelizable function though.

As I understand it, there's very little benefit to multi-cores in Python 
due to the GIL. 


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


Re: generator object or 'send' method?

2009-02-10 Thread Terry Reedy

Aaron Brady wrote:


I guess a generator that counts, but skips K numbers, where K can be
varied.  For instance, you initialize it with N, the starting number,
and K, the jump size.  Then, you can change either one later on.


This specification is incomplete as to the timing of when changes to N 
take effect and when variable K is applied.



This gets two birds with one stone, but is it possible?


If you write an iterator class instead of trying to stretch the 
abbreviated form beyond its intention, and the specification is 
coherent, it should be trivial.


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


Re: Replace unknow string varible in file.

2009-02-10 Thread r0g
namire wrote:
 Just as a comparison in the Windows OS this seems easy to do when
 managing files, say for the file a-blah-b-blah.tmp where blah is an
 unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
 python and a string in text file I don't have a clue. @_@ could
 someone please help me?

Hi Namire,

The equivalent thing in programming languages is Regular Expressions,
also known as regex. It's like a small pattern matching sub-language.

There quite a lot more to it than the odd * so it might take a bit of
googling around and study to really understand it but the principle is
the same.

In python you need to import the 're' module.

Have a look at this, the replace method is called 'sub'...

http://www.amk.ca/python/howto/regex/regex.html

Take your time with it though, it can be confusing until you get used to
it and when you're building an expression don't try and do it all at
once, start small and build it up a little at a time.

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


Re: can multi-core improve single funciton?

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 22:41:25 +1000, Gerhard Weis wrote:

 btw. the timeings are not that different for the naive recursion in OP's
 version and yours.
 fib(500) on my machine:
   OP's: 0.00116 (far away from millions of years) 
 This here: 0.000583

I don't believe those timings are credible. On my machine, it took a 
minute to calculate fib(38), and while my machine isn't exactly the 
fastest box possible, nor is it especially slow.

I don't wish to imply that you are deliberately lying, but your result of 
0.00116 seconds for the naive version of fib(500) is so unrealistic in my 
experience that I believe you must be confused. Perhaps you've timed a 
less naive fib() but thought it was the naive version.

Unless somebody can point out an error in my analysis, I'm sticking to my 
earlier claim that the naive version of fib(500) requires an unbelievably 
huge number of function calls: significantly more than the value of fib
(500) itself. See my earlier post in this thread for details.



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


Re: Scanning a file character by character

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 12:06:06 +, Duncan Booth wrote:

 Steven D'Aprano ste...@remove.this.cybersource.com.au wrote:
 
 On Mon, 09 Feb 2009 19:10:28 -0800, Spacebar265 wrote:
 
 How would I do separate lines into words without scanning one
 character at a time?
 
 Scan a line at a time, then split each line into words.
 
 
 for line in open('myfile.txt'):
 words = line.split()
 
 
 should work for a particularly simple-minded idea of words.
 
 Or for a slightly less simple minded splitting you could try re.split:
 
 re.split((\w+), The quick brown fox jumps, and falls over.)[1::2]
 ['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']


Perhaps I'm missing something, but the above regex does the exact same 
thing as line.split() except it is significantly slower and harder to 
read.

Neither deal with quoted text, apostrophes, hyphens, punctuation or any 
other details of real-world text. That's what I mean by simple-minded.


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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Chris Rebert
On Tue, Feb 10, 2009 at 1:57 PM, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:

 The thing I don't understand is why a generator that has no iterable
 values is different from an empty list.

 How do you know it has no iterable values until you call next() on it and
 get StopIteration?

 By the way, your has_values function is just a slower version of the
 built-in any().

nitpick
Not quite: if the generator produces one or more elements but those
elements happen to be boolean false according to Python, then any()
will be false but has_values() will be true. The functions serve
different purposes (produces at least 1 value vs. has at least one
true value).

Cheers,
Chris

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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Josh Dukes
ahhh any! ok, yeah, I guess that's what I was looking for. Thanks.


On 10 Feb 2009 21:57:56 GMT
Steven D'Aprano ste...@remove.this.cybersource.com.au wrote:

 On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:
 
  The thing I don't understand is why a generator that has no iterable
  values is different from an empty list.
 
 How do you know it has no iterable values until you call next() on it
 and get StopIteration?
 
 By the way, your has_values function is just a slower version of
 the built-in any().
 
 


-- 

Josh Dukes
MicroVu IT Department
--
http://mail.python.org/mailman/listinfo/python-list


Re: Logging in Python

2009-02-10 Thread Vinay Sajip
On Feb 10, 9:38 pm, aha aquil.abdul...@gmail.com wrote:
 Thanks for your suggestions.  I've also figured that I can test 
 iflogging.RootLogger.manager.loggerDict has any items in it.  Or if it
 has a logger for the module that I wish to start.  I like basicLogger
 idea though as it seems like the cleanest implementation.


It's best not to dig into the implementation details, as these might
change across versions of Python. Any of the suggestions that Robert
and I made should be able to solve your problem in the right way.

Best regards,

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


Re: Python Launcher.app on OS X

2009-02-10 Thread kpp9c
So how does this effect the install instructions found on the link:

http://wiki.python.org/moin/MacPython/Leopard

do you trash that when you do an install on OS X? I am so hesitant to
delete anything that resides in /System



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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Terry Reedy

Josh Dukes wrote:



I was actually aware of that (thank you, though, for trying to help).
What I was not clear on was if the boolean evaluation is a method of an
object that can be modified via operatior overloading (in the same way
+ is actually .__add__()) or not. Clearly __nonzero__ is the operator I
was curious about. Thanks for that info. 


.__bool__ in 3.0.



The thing I don't understand is why a generator that has no iterable
values is different from an empty list. Why shouldn't bool ==
has_value?? Technically a list, a tuple, and a string are also objects
but if they lack values they're evaluated as False. It seems to me that
a generator is an object that intends to replace lists where lazy
evaluation would be more efficent. Here is one place where that's
definitely true. 


Generator functions are abbreviated iterator classes.  If you want 
iterators with more functionality, write an iterator class.  In 
particular, you can add a .__bool__ method for empty or not or even a 
.__len__ method if you can accurately calculate the number of items 
remaining.



The main reason I'm interested in this is that it improves performance
immensely over boolean evaluation of large lists (as the attached code
shows). It seems to me if I could use find a good use for it in my
experimentation that someone else might also want to do the same thing
in real-world code. 


Terry Jan Reedy

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


Re: can multi-core improve single funciton?

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 02:05:35 -0800, Niklas Norrthon wrote:

 According to the common definition of fibonacci numbers fib(0) = 0, fib
 (1) = 1 and fib(n) = fib(n-1) + fib(n-2) for n  1. So the number above
 is fib(501).

So it is. Oops, off by one error!

Or, if you prefer, it's the right algorithm for a Lucas sequence with 
first two values 1 and 1 instead of 0 and 1. :)



 timeit.Timer('fib(500)', 'from __main__ import fib').timeit(1)

 0.00083398818969726562
 
 And now for my version (which admitedly isn't really mine, and returns
 slightly incorrect fib(n) for large values of n, due to the limited
 floating point precision).

The floating point version is nice, but it starts giving incorrect 
answers relatively early, from n=71. But if you don't need accurate 
results (a relative error of 3e-15 for n=71), it is very fast.



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


RE: Python binaries with VC++ 8.0?

2009-02-10 Thread Delaney, Timothy (Tim)
bearophileh...@lycos.com wrote:

 Paul Rubin:
 Gideon Smeding of the University of
 Utrecht has written a masters' thesis titled An executable
 operational semantics for Python.
 
 A significant part of Computer Science is a waste of time and money.

The same can be said for any research. Can you predict ahead of time
which research will be useful, and which won't? If so (and you can prove
it) why aren't you making unbelievable amounts of money?

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


Re: Working with propositional formulae in Python

2009-02-10 Thread Terry Reedy

nnp wrote:

Hey,

I'm currently working with propositional boolean formulae of the type
'A  (b - c)' (for example). I was wondering if anybody knows of a
Python library to create parse trees and convert such formulae to
conjunctive, disjunctive and Tseitin normal forms?


You would probably do better with Google.
Python conjunctive normal form
gave some potentially interesting hits.

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


Re: zlib interface semi-broken

2009-02-10 Thread Scott David Daniels

Paul Rubin wrote:

Travis travis+ml-pyt...@subspacefield.org writes:

However, perhaps this would be a good time to discuss how [zlib] works...

It is missing some other features too, like the ability to preload
a dictionary.  I'd support extending the interface.


The trick to defining a preload interface is avoiding creating a brittle
interface -- the saved preload should be usable across machines and
versions.  I suspect that is why such an interface never came up (If
you can clone states, then you can say: compress this, then use the
resultant state to compress/decompress others.  Suddenly there is no
nasty problem guessing what to parameterize and what to fix in stone.


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


Re: can multi-core improve single funciton?

2009-02-10 Thread Gerhard Weis
On 2009-02-11 08:01:29 +1000, Steven D'Aprano 
ste...@remove.this.cybersource.com.au said:



On Tue, 10 Feb 2009 22:41:25 +1000, Gerhard Weis wrote:


btw. the timeings are not that different for the naive recursion in OP's
version and yours.
fib(500) on my machine:
OP's: 0.00116 (far away from millions of years)
This here: 0.000583


I don't believe those timings are credible. On my machine, it took a
minute to calculate fib(38), and while my machine isn't exactly the
fastest box possible, nor is it especially slow.

I don't wish to imply that you are deliberately lying, but your result of
0.00116 seconds for the naive version of fib(500) is so unrealistic in my
experience that I believe you must be confused. Perhaps you've timed a
less naive fib() but thought it was the naive version.

Unless somebody can point out an error in my analysis, I'm sticking to my
earlier claim that the naive version of fib(500) requires an unbelievably
huge number of function calls: significantly more than the value of fib
(500) itself. See my earlier post in this thread for details.


I am sorry for the wrong timing, I mixed up the function names. The 
naive version used partly your version and partly the naive recursion. 
So less naive is a good description :)


after fixing it:
naive fib(38): ~40seconds

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


Re: pySerial help please!

2009-02-10 Thread MRAB

bmasch...@gmail.com wrote:

Hello all,

I am very new to Python and I am using it because I needed an easy
language to control a piece of equipment that connects to my computer
via a serial cable. I am running Python 2.6 with pySerial 2.4 under
Windows. I can get Python to create a serial port on COM1, but when I
try to write to the device, nothing happens. Here is an example:

import serial
ser = serial.Serial(0)
ser.write(otpm 2 16 0)
ser.close()

If I connect to the device in Hyperterminal the device will behave as
expected when I give it the command 'otpm 2 16 0' but not in Python. I
have confirmed Python is actually controlling the port because
Hyperterminal will not open the port while Python has it open,
although I have not been able to try a loopback connector as of yet.
Any suggestions out there? Thanks in advance!


Have you checked that the baud rate, parity, etc, are the same as in
Hyperterminal?
--
http://mail.python.org/mailman/listinfo/python-list


Re: pySerial help please!

2009-02-10 Thread Diez B. Roggisch

bmasch...@gmail.com schrieb:

Hello all,

I am very new to Python and I am using it because I needed an easy
language to control a piece of equipment that connects to my computer
via a serial cable. I am running Python 2.6 with pySerial 2.4 under
Windows. I can get Python to create a serial port on COM1, but when I
try to write to the device, nothing happens. Here is an example:

import serial
ser = serial.Serial(0)
ser.write(otpm 2 16 0)
ser.close()

If I connect to the device in Hyperterminal the device will behave as
expected when I give it the command 'otpm 2 16 0' but not in Python. I
have confirmed Python is actually controlling the port because
Hyperterminal will not open the port while Python has it open,
although I have not been able to try a loopback connector as of yet.
Any suggestions out there? Thanks in advance!


You need to give a newline, you press return on the terminal as well, 
don't you?


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


Re: Scanning a file character by character

2009-02-10 Thread Tim Chase

Or for a slightly less simple minded splitting you could try re.split:


re.split((\w+), The quick brown fox jumps, and falls over.)[1::2]

['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']



Perhaps I'm missing something, but the above regex does the exact same 
thing as line.split() except it is significantly slower and harder to 
read.


Neither deal with quoted text, apostrophes, hyphens, punctuation or any 
other details of real-world text. That's what I mean by simple-minded.


   s = The quick brown fox jumps, and falls over.
   import re
   re.split(r(\w+), s)[1::2]
  ['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']
   s.split()
  ['The', 'quick', 'brown', 'fox', 'jumps,', 'and', 'falls', 
'over.']


Note the difference in jumps vs. jumps,  (extra comma in the 
string.split() version) and likewise the period after over. 
Thus not quite the exact same thing as line.split().


I think an easier-to-read variant would be

   re.findall(r\w+, s)
  ['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']

which just finds words.  One could also just limit it to letters with

  re.findall([a-zA-Z], s)

as \w is a little more encompassing (letters and underscores) 
if that's a problem.


-tkc




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


Re: Scanning a file character by character

2009-02-10 Thread Rhodri James
On Tue, 10 Feb 2009 22:02:57 -, Steven D'Aprano  
ste...@remove.this.cybersource.com.au wrote:



On Tue, 10 Feb 2009 12:06:06 +, Duncan Booth wrote:


Steven D'Aprano ste...@remove.this.cybersource.com.au wrote:


On Mon, 09 Feb 2009 19:10:28 -0800, Spacebar265 wrote:


How would I do separate lines into words without scanning one
character at a time?


Scan a line at a time, then split each line into words.


for line in open('myfile.txt'):
words = line.split()


should work for a particularly simple-minded idea of words.


Or for a slightly less simple minded splitting you could try re.split:


re.split((\w+), The quick brown fox jumps, and falls over.)[1::2]

['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']



Perhaps I'm missing something, but the above regex does the exact same
thing as line.split() except it is significantly slower and harder to
read.

Neither deal with quoted text, apostrophes, hyphens, punctuation or any
other details of real-world text. That's what I mean by simple-minded.


You're missing something :-)  Specifically, the punctuation gets swept
up with the whitespace, and the extended slice skips it.  Apostrophes
(and possibly hyphenation) are still a bit moot, though.



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


Re: Functional schmunctional...

2009-02-10 Thread Terry Reedy

r0g wrote:



def inet2ip(n):
  p = (n/16777216)
  q = ((n-(p*16777216))/65536)
  r = ((n-((p*16777216)+(q*65536)))/256)
  s = ((n-((p*16777216)+(q*65536)+(r*256
  return str(p)+.+str(q)+.+str(r)+.+str(s)


Beyond what other wrote:
To future-proof code, use // instead of / for integer division.
To get both quotient and remainder, use divmod(num,den)
For future reading (and generalization) documenting magic constants helps.

In 3.0:

def inet2ip(n):
  p = (n//16777216)
  q = ((n-(p*16777216))//65536)
  r = ((n-((p*16777216)+(q*65536)))//256)
  s = ((n-((p*16777216)+(q*65536)+(r*256
  return str(p)+.+str(q)+.+str(r)+.+str(s)

def inet2ip2(n):
p,n=divmod(n,16777216) # 124
q,n=divmod(n,65536) # 116
r,s=divmod(n,256) # 18
return str(p)+.+str(q)+.+str(r)+.+str(s)

print(inet2ip(10), inet2ip2(10))


59.154.202.0 59.154.202.0

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


Re: Super() confusion

2009-02-10 Thread Paul Boddie
On 10 Feb, 20:45, Jean-Paul Calderone exar...@divmod.com wrote:

 It replaces one kind of repetition with another.  I think each kind is
 about as unpleasant.  Has anyone gathered any data on the frequency of
 changes of base classes as compared to the frequency of classes being
 renamed?  I don't think either happens very often, but it might be
 interesting to see some numbers.

Base class changes are less important than common derived class
functionality. For example, I have employed the following style of
class hierarchy in at least one system:

class ResourceUsingClass:


Stuff using some resource, like a file.
Should we close the file when we're finished with it?
Is that rude? Best not do it!


def close(self):
pass # Don't close anything!

class SelfContainedResourceUsingClass(ResourceUsingClass):


We don't care about keeping the resource open in this class.
The user of the class should just need to call the close method.


def close(self):
ResourceUsingClass.close(self)
# Now close the resource!

I know that there would be other ways of solving this problem, but in
this case, for every class we want to subclass and provide such
functionality, we need to write a specific close method. With super,
we can avoid being specific about the superclass, but we still need to
write the method unless we define it in a mix-in which appears before
the superclass in the method resolution order.

I think this is the only real use I've found for super, mostly
because, as you say, in most other situations it doesn't actually save
anyone very much effort.

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


Re: Replace unknow string varible in file.

2009-02-10 Thread Terry Reedy

namire wrote:

Hey .python first time poster here. I'm pretty good with python so
far, but I keep needed a function in my program but not knowing how to
build it. =( Here's the problem:

Imagine a html file full of 100's of these strings all mooshed
together onto many lines;
!--@@MARKER@@; id=ITEM--ITEMbr
Where the word 'MARKER' is a constant, it stay the same in each string
and the word 'ITEM' is a random combination of ascii characters of an
unknown length. So for example a:
!--@@MARKER@@; id=CATFISH--CATFISHbrh1Text text text/
h1!--@@MARKER@@; id=SPAM--SPAMbr and so on...

What I need to do it replace each instance of the random letter with a
constant and/or delete them. The file is a html file so the stuff
inside of !-- -- is ok to keep


I cannot understand what you want to do where.  The last phrase implies 
'leave the comments alone' but you only talked about random letters 
within the comments.  I suggest a minimal but complete example of 
possible input and desired output.


 and I need that data to identify

where the strings are in the file (it's full of other stuff too).  I'm
tired making a bruteforcer but with unknown length and 26 letters of
the alphabet I gave up because it would take too long (it was
something like; read file; if '@@MARKER@@; id='+str(gen_string)+'--

+sr(gen_string)+'br' in file then replace with '', but I'm

paraphrasing code and it's not the best solution anyway).

Just as a comparison in the Windows OS this seems easy to do when
managing files, say for the file a-blah-b-blah.tmp where blah is an
unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
python and a string in text file I don't have a clue. @_@ could
someone please help me?
--
http://mail.python.org/mailman/listinfo/python-list



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


Re: zlib interface semi-broken

2009-02-10 Thread Travis
On Tue, Feb 10, 2009 at 01:36:21PM -0800, Scott David Daniels wrote:
 A simple way to fix this would be to add a finished attribute to the
 Decompress object.
 Perhaps you could submit a patch with such a change?

Yes, I will try and get to that this week.

 However, perhaps this would be a good time to discuss how this library
 works; it is somewhat awkward and perhaps there are other changes which
 would make it cleaner.
 Well, it might be improvable, I haven't really looked.  I personally
 would like it and bz2 to get closer to each other in interface, rather
 than to spread out.  SO if you are really opening up a can of worms,
 I vote for two cans.

Well, I like this idea; perhaps this is a good time to discuss the
equivalent of some abstract base classes, or interfaces, for
compression.

As I see it, the fundamental abstractions are the stream-oriented
de/compression routines.  Given those, one should easily be able to
implement one-shot de/compression of strings.  In fact, that is the
way that zlib is implemented; the base functions are the
stream-oriented ones and there is a layer on top of convenience
functions that do one-shot compression and decompression.

After examining the bz2 module, I notice that it has a file-like
interface called bz2file, which is roughly analogous to the gzip
module.  That file interface could form a third API, and basically
conform to what python expects of files.

So what I suggest is a common framework of three APIs; a sequential
compression/decompression API for streams, a layer (potentially
generic) on top of those for strings/buffers, and a third API for
file-like access.  Presumably the file-like access can be implemented
on top of the sequential API as well.

If the sequential de/compression routines are indeed primitive, and
sufficient for the implementation of the other two APIs, then that
gives us the option of implementing the other upper two layers in
pure python, potentially simplifying the amount of extension code that
has to be written.  I see that as desirable, since it gives us options
for writing the upper two layers; in pure python, or by writing
extensions to the C code where available.

I seem to recall a number of ancilliary functions in zlib, such as
those for loading a compression dictionary.  There are also options
such as flushing the compression in order to be able to resynchronize
should part of the archive become garbled.  Where these functions are
available, they could be implemented, though it would be desirable to
give them the same name in each module to allow client code to test
for their existence in a compression-agnostic way.

For what it's worth, I would rather see a pythonic interface to the
libraries than a simple-as-can-be wrapper around the C functions.  I
personally find it annoying to have to drop down to non-OOP styles in
a python program in order to use a C library.  It doesn't matter to me
whether the OOP layer is added atop the C library in pure python or in
the C-to-python binding; that is an implementation detail to me, and I
suspect to most python programmers.  They don't care, they just want
it easy to use from python.  If performance turns out to matter, and
the underlying compression library supports an upper layer in C,
then we have the option for using that code.

So my suggestion is that we (the python users) brainstorm on how we
want the API to look, and not focus on the underlying library except
insofar as it informs our discussion of the proper APIs - for example,
features such as flushing state, setting compression levels/windows,
or for resynchronization points.

My further suggestion is that we start with the sequential
de/compression, since it seems like a fundamental primitive.
De/compressing strings will be trivial, and the file-like interface is
already described by Python.

So my first suggestion on the stream de/compression API thread is:

The sequential de/compression needs to be capable of returning
more than just the de/compressed data.  It should at least be
capable of returning end-of-stream conditions and possibly
other states as well.  I see a few ways of implementing this:

1) The de/compression object holds state in various members such as
data input buffers, data output buffers, and a state for indicating
states such as synchronization points or end-of-stream states.  Member
functions are called and primarily manipulate the data members of the
object.

2) The de/compression object has routines for reading de/compressed
data and states such as end-of-stream or resynchronization points as
exceptions, much like the file class can throw EOFError.  My problem
with this is that client code has to be cognizant of the possible
exceptions that might be thrown, and so one cannot easily add new
exceptions should the need arise.  For example, if we add an exception
to indicate a possible resynchronization point, client code may not
be capable of handling it as a non-fatal exception.

Thoughts?
-- 

Re: Scanning a file character by character

2009-02-10 Thread Steven D'Aprano
On Tue, 10 Feb 2009 16:46:30 -0600, Tim Chase wrote:

 Or for a slightly less simple minded splitting you could try re.split:

 re.split((\w+), The quick brown fox jumps, and falls
 over.)[1::2]
 ['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']
 
 
 Perhaps I'm missing something, but the above regex does the exact same
 thing as line.split() except it is significantly slower and harder to
 read.

...

 Note the difference in jumps vs. jumps,  (extra comma in the
 string.split() version) and likewise the period after over. Thus not
 quite the exact same thing as line.split().

Um... yes. I'll just slink away quietly now... nothing to see here...


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


Re: Scanning a file character by character

2009-02-10 Thread MRAB

Steven D'Aprano wrote:

On Tue, 10 Feb 2009 16:46:30 -0600, Tim Chase wrote:


Or for a slightly less simple minded splitting you could try re.split:


re.split((\w+), The quick brown fox jumps, and falls
over.)[1::2]

['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']


Perhaps I'm missing something, but the above regex does the exact same
thing as line.split() except it is significantly slower and harder to
read.


...


Note the difference in jumps vs. jumps,  (extra comma in the
string.split() version) and likewise the period after over. Thus not
quite the exact same thing as line.split().


Um... yes. I'll just slink away quietly now... nothing to see here...


You could've used str.translate to strip out the unwanted characters.
--
http://mail.python.org/mailman/listinfo/python-list


embedding Python in a shared library

2009-02-10 Thread Deepak Chandran
I have embedded Python in a shared library. This works fine in Windows
(dll), but I get the following error is Ubuntu when I try to load modules:

/usr/lib/python2.5/lib-dynload/*time.so*: error: symbol lookup error: *
undefined* symbol: PyExc_ValueError


I found many postings on this issue on the internet, but I was not able to
find a solution that worked. I tried to load libpython2.5.so.1 into my
program using dlopen, but that crashed the program for some reason. I tried
building my library using the libpython2.5.a, but the same error was there.

I am sure someone has a solution to this, since it seems like a general
issue.
--
http://mail.python.org/mailman/listinfo/python-list


Re: optparse versus getopt

2009-02-10 Thread Matthew Sacks
its a debian package. 2.5

importing optparse works with interactive python, but not through the
jython interpreter i an using.

is there some way i can force the import based on the the absolute
path to the module?

On Tue, Feb 10, 2009 at 1:48 PM, Robert Kern robert.k...@gmail.com wrote:
 On 2009-02-10 15:42, Matthew Sacks wrote:

 it seems as if optparse isn't in my standard library.

 How did you install your Python? It has been part of the standard library
 for a very long time.

 is there a way to add a lib like ruby gems?

 http://docs.python.org/install/index.html

 But optparse (named Optik when it was a separate package) has not been
 distributed separately from Python for a long time.

 --
 Robert Kern

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

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

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


Re: optparse versus getopt

2009-02-10 Thread Robert Kern

On 2009-02-10 17:32, Matthew Sacks wrote:

its a debian package. 2.5

importing optparse works with interactive python, but not through the
jython interpreter i an using.


Ah, yes. The current version of Jython is still based off of Python 2.2 whereas 
optparse was introduced in Python 2.3.



is there some way i can force the import based on the the absolute
path to the module?


Better would be for you to copy the optparse.py module onto your Jython's import 
path. I'm not particularly familiar with the details of Jython, so you will need 
to consult with the Jython documentation unless if a Jython expert can jump in 
here. Here is one message describing this procedure:


  http://osdir.com/ml/lang.jython.user/2004-01/msg00086.html

You may want to ask Jython specific questions on the Jython mailing list. You 
will probably get more on-target answers faster there.


--
Robert Kern

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

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


Re: Functional schmunctional...

2009-02-10 Thread r0g
bearophileh...@lycos.com wrote:
 Here a small benchmark:
 
 def ip2inet01(a): # can't be used with 6
 li = a.split('.')
snip

Wow, thanks everybody for all the suggestions, v.interesting esp as I
didn't even ask for any suggestions!  This is a fantastically didactic
newsgroup: you start off just musing about functional programming, you
end up learning python has a rich set of augmented assignment operators,
brilliant :-)


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


Re: zlib interface semi-broken

2009-02-10 Thread Paul Rubin
Scott David Daniels scott.dani...@acm.org writes:
 I suspect that is why such an interface never came up (If
 you can clone states, then you can say: compress this, then use the
 resultant state to compress/decompress others. 

The zlib C interface supports something like that.  It is just not
exported to the python application.  It should be.
--
http://mail.python.org/mailman/listinfo/python-list


Escaping my own chroot...

2009-02-10 Thread r0g
I'm writing a linux remastering script in python where I need to chroot
into a folder, run some system commands and then come out and do some
tidying up, un-mounting proc  sys etc.

I got in there with os.chroot() and I tried using that to get back out
but that didn't work so... is my script trapped in there forever now or
is there an un-hacky way to escape?

If it is an OS restriction rather than my ignorance of python is there a
canonical way of dealing with it? Should I maybe fork a process, have it
do the chroot and wait for it to terminate before continuing?

Apologies if this turn out to be OT!


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


Re: pySerial help please!

2009-02-10 Thread bmaschino
On Feb 10, 5:41 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 bmasch...@gmail.com schrieb:





  Hello all,

  I am very new to Python and I am using it because I needed an easy
  language to control a piece of equipment that connects to my computer
  via a serial cable. I am running Python 2.6 with pySerial 2.4 under
  Windows. I can get Python to create a serial port on COM1, but when I
  try to write to the device, nothing happens. Here is an example:

  import serial
  ser = serial.Serial(0)
  ser.write(otpm 2 16 0)
  ser.close()

  If I connect to the device in Hyperterminal the device will behave as
  expected when I give it the command 'otpm 2 16 0' but not in Python. I
  have confirmed Python is actually controlling the port because
  Hyperterminal will not open the port while Python has it open,
  although I have not been able to try a loopback connector as of yet.
  Any suggestions out there? Thanks in advance!

 You need to give a newline, you press return on the terminal as well,
 don't you?

 Diez- Hide quoted text -

 - Show quoted text -

Yes! That was exactly the problem. I replaced ser.write(otpm x x x)
to (otpm x x x\n) and it WORKS!! Thank you!
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-10 Thread Aahz
In article 4c7158d2-5663-46b9-b950-be81bd799...@z6g2000pre.googlegroups.com,
Emanuele D'Arrigo man...@gmail.com wrote:

I'm having a ball with the power of regular expression but I stumbled
on something I don't quite understand:

Book recommendation: _Mastering Regular Expressions_, Jeffrey Friedl
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Super() confusion

2009-02-10 Thread Gabriel Genellina
En Tue, 10 Feb 2009 18:01:53 -0200, Daniel Fetchinson  
fetchin...@googlemail.com escribió:

On 2/9/09, Gabriel Genellina gagsl-...@yahoo.com.ar wrote:

En Mon, 09 Feb 2009 23:34:05 -0200, Daniel Fetchinson
fetchin...@googlemail.com escribió:


Consider whether you really need to use super().
http://fuhm.net/super-harmful/


Because throwing around that link carries about the same amount of
information as perl is better than python, my IDE is better than
yours, vim rulez!, emacs is cooler than vim, etc, etc.


Not at all. It contains accurate and valuable information that isn't
available elsewhere.


But does not contain other valuable information which would demystify
super. If only this source is shown to a person who wants to learn the
proper usage of super, the impression he/she gets will be distorted.


Why so? At the end there is a best practices recipe that pretty much  
summarizes the proper usage of super, and AFAIK it's the right way to use  
it. Don't you agree with any of the conclusions?



Example: 1. somebody asks about threading 2. reply comes: there is a
GIL! you can't do real threading. Now this reply might be technically
more-or-less correct, it is misleading and does not help the poster.
An informed discussion of the various solutions that does not involve
a rant about the GIL would be very useful though.


As I said, the article presents a recipe for super() usage, and I'd  
consider it very helpful. It's far from just saying super s*cks!, or  
the GIL s*cks! or something like that.



You might want to start with

http://www.artima.com/weblogs/viewpost.jsp?thread=236275


...which, although the author says it was written a long time ago, was not  
published until less than six months ago, and has very low visibility.



You are right, it's not in the documentation. But if somebody asks on
c.l.p the appropriate answer, I think, is to point to information such
as the one above, and not the misleading harmful essay.


According to Google, nobody has menctioned the harmful essay in this  
group since April 2008 [1], months before Simionato's article were  
available... So this is the *first* time the reference to the former essay  
could have been replaced by M.S.' one... don't be so strict!


Anyway, the right thing to do, IMHO, is to publish correct and accurate  
documentation in the main Python site. Not everybody knows about this  
group existence, nor has the time/experience/interest on subscribe here,  
post a question and wait for an answer. I've seen some posts in python-dev  
saying something like this is confusing, we should evangelize people on  
c.l.p. on the right way to do it and I completely disagree; the right  
place for such things is the product documentation, or -to a much lesser  
degree because it's online only- some article collection linked from the  
main site (like the Other resources left bar, already present).



Honestly, I don't understand how this thing got so much out of
control. If anyone starts an intelligent question or remark about
super, this essay is thrown in no matter what. Anyone can explain why?


Because for a very loong time (seven years, 2001-2008) super was
almost undocumented. The Library Reference -before release 2.6- only  
had a short paragraph, the [...]


You are right, the documentation needs some work in this regard. But
again, just because some sources are not in the documentation doesn't
mean that the most opinionated essay is the best source. A little
search turns up much more useful ones.


(not according to Google [2]: I get the harmful article in the top, the  
thread in python-dev, a couple threads in c.l.p including posts by M.S.,  
his article in artima, and nothing more relevant than Monty Python Super  
Star up to the 3rd page)


*Now* that *I* am aware of the recent article series by M.S., the next  
time someone asks *me* about super(), probably I'll refer her to both M.S.  
and J.K.'s articles. Last time I checked (perhaps one or two years ago),  
the harmful article was almost the only relevant source of info about  
super().


[1]  
http://groups.google.com/group/comp.lang.python/search?q=super+harmfulstart=0scoring=d;

[2] http://www.google.com/search?q=python+super

--
Gabriel Genellina

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


relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC

2009-02-10 Thread tkevans
Found a couple of references to this in the newsgroup, but no
solutions.

I'm trying to build libsbml-3.3.0 with python 2.5.4 support on RHEL
5.3.  This RedHat distro has python 2.4.5, and libsbml builds ok with
that release.

After building 2.5.4 (./configure CFLAGS=-fPIC , as the error message
suggests), ld still croaks here:

g++ -L../../ -L/usr/local/lib/python2.5/config   -shared -o
_libsbml.so libsbml_wrap.o -lsbml -lpython2.5 -lxml2 -lz -lm -lm   -lz
-lbz2
/usr/bin/ld: /usr/local/lib/python2.5/config/libpython2.5.a
(abstract.o): relocation R_X86_64_32 against `a local symbol' can not
be used when making a shared object; recompile with -fPIC
/usr/local/lib/python2.5/config/libpython2.5.a: could not read
symbols: Bad value
collect2: ld returned 1 exit status

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


Propagating function calls

2009-02-10 Thread Noam Aigerman
Suppose I have a python object X, which holds inside it a python object
Y. How can I propagate each function call to X so the same function call
in Y will be called, i.e:

X.doThatFunkyFunk()

Would cause

Y.doThatFunkyFunk()

Thanks, Noam

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


Re: Functional schmunctional...

2009-02-10 Thread python
 This is a fantastically didactic newsgroup: you start off just musing about 
 fill-in-the-blank, you end up learning python has some great feature, 
 brilliant :-)

+1 !!

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


Re: Propagating function calls

2009-02-10 Thread Chris Rebert
On Tue, Feb 10, 2009 at 5:02 PM, Noam Aigerman no...@answers.com wrote:
 Suppose I have a python object X, which holds inside it a python object Y.
 How can I propagate each function call to X so the same function call in Y

That'd be a method call actually, not a function call.

 will be called, i.e:

 X.doThatFunkyFunk()

 Would cause

 Y.doThatFunkyFunk()

Use a simple proxy (this will forward attribute accesses too, but that
doesn't usually matter and can be worked around if necessary):

class Delegator(object):
def __init__(self, delegate):
self.delegate = delegate
def __getattr__(self, attr):
return getattr(self.delegate, attr)

Example:
 a=Delegator([])
 a.append(5)
 a.delegate
[5]

Note that this won't forward the operator special methods (e.g. __add__).

Cheers,
Chris

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


getopt

2009-02-10 Thread Matthew Sacks
if anyone can have a look at this code and offer suggestions i would
appreciate it.
i am forced to use getopt, so i cant use something good like optparse

passedArgs = sys.argv[1:]
optlist, args = getopt.getopt(str(passedArgs), [connectPassword=,
adminServerURL=, action=, targets=, appDir=])


for some reason this does not work in assigning args to opts.


i want to take all of the command line options and assign them to strings

here is how i am currently going about it

for o,a in optlist:
if o == --connectPassword:
connectPassword = a
print Connect password assigned as  + a
continue
elif o == --adminServerURL:
adminServerURL = a
print adminServerURL  + a
continue
else:
print No connection Arguments Specified


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


Re: Propagating function calls

2009-02-10 Thread James Mills
On Wed, Feb 11, 2009 at 11:02 AM, Noam Aigerman no...@answers.com wrote:
 Suppose I have a python object X, which holds inside it a python object Y.
 How can I propagate each function call to X so the same function call in Y
 will be called, i.e:

 X.doThatFunkyFunk()

 Would cause

 Y.doThatFunkyFunk()

Noam, using circuits [1] you could do this:


from circuits import Event, Component, Manager

class X(Component):

   def doThatFunkyFunk(self):
  ...

class Y(Component):

   def doThatFunkyFunk(self):
  ...

manager = Manager()
manager += X()
manager += Y()

manager.send(Event(), doThatFunkyFunk)


cheers
James

[1] http://pypi.python.org/pypi/circuits/
--
http://mail.python.org/mailman/listinfo/python-list


Re: getopt

2009-02-10 Thread John Machin
On Feb 11, 12:12 pm, Matthew Sacks ntw...@gmail.com wrote:
 if anyone can have a look at this code and offer suggestions i would
 appreciate it.
 i am forced to use getopt, so i cant use something good like optparse

 passedArgs = sys.argv[1:]
 optlist, args = getopt.getopt(str(passedArgs), [connectPassword=,

Dunno where you acquired the str() ... just lose it. Consider checking
with the documentation when something does not work.

 adminServerURL=, action=, targets=, appDir=])

 for some reason this does not work in assigning args to opts.

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


Re: Upgrade 2.6 to 3.0

2009-02-10 Thread Aahz
In article 2ba4f763-79fa-423e-b082-f9de829ae...@i20g2000prf.googlegroups.com,
Giampaolo Rodola' gne...@gmail.com wrote:

Just out of curiosity, am I the only one who think that switching to
3.x right now is not a good idea?

Hardly.  I certainly wouldn't consider it for production software, but
installing it to play with probably is a Good Idea -- it's the future,
after all.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


Avoiding argument checking in recursive calls

2009-02-10 Thread Steven D'Aprano
I sometimes write recursive functions like this simple factorial:


def fact(n):
if n  0: raise ValueError
if n = 0: return 1
return fact(n-1)*n 

At the risk of premature optimization, I wonder if there is an idiom for 
avoiding the unnecessary test for n = 0 in the subsequent recursive 
calls? For the sake of the argument, let's pretend the test is expensive 
and I have a good reason for wanting to avoid it on subsequent calls :)



I've done this:

def _fact(n):
if n = 0: return 1
return _fact(n-1)*n 

def fact(n):
if n  0: raise ValueError
return _fact(n)

but that's ugly. What else can I do?



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


Re: Super() confusion

2009-02-10 Thread Daniel Fetchinson
 Consider whether you really need to use super().
 http://fuhm.net/super-harmful/

 Because throwing around that link carries about the same amount of
 information as perl is better than python, my IDE is better than
 yours, vim rulez!, emacs is cooler than vim, etc, etc.

 Not at all. It contains accurate and valuable information that isn't
 available elsewhere.

 But does not contain other valuable information which would demystify
 super. If only this source is shown to a person who wants to learn the
 proper usage of super, the impression he/she gets will be distorted.

 Why so? At the end there is a best practices recipe that pretty much
 summarizes the proper usage of super, and AFAIK it's the right way to use
 it. Don't you agree with any of the conclusions?

 Example: 1. somebody asks about threading 2. reply comes: there is a
 GIL! you can't do real threading. Now this reply might be technically
 more-or-less correct, it is misleading and does not help the poster.
 An informed discussion of the various solutions that does not involve
 a rant about the GIL would be very useful though.

 As I said, the article presents a recipe for super() usage, and I'd
 consider it very helpful. It's far from just saying super s*cks!, or
 the GIL s*cks! or something like that.

 You might want to start with

 http://www.artima.com/weblogs/viewpost.jsp?thread=236275

 ...which, although the author says it was written a long time ago, was not
 published until less than six months ago, and has very low visibility.

 You are right, it's not in the documentation. But if somebody asks on
 c.l.p the appropriate answer, I think, is to point to information such
 as the one above, and not the misleading harmful essay.

 According to Google, nobody has menctioned the harmful essay in this
 group since April 2008 [1], months before Simionato's article were
 available... So this is the *first* time the reference to the former essay
 could have been replaced by M.S.' one... don't be so strict!

 Anyway, the right thing to do, IMHO, is to publish correct and accurate
 documentation in the main Python site. Not everybody knows about this
 group existence, nor has the time/experience/interest on subscribe here,
 post a question and wait for an answer. I've seen some posts in python-dev
 saying something like this is confusing, we should evangelize people on
 c.l.p. on the right way to do it and I completely disagree; the right
 place for such things is the product documentation, or -to a much lesser
 degree because it's online only- some article collection linked from the
 main site (like the Other resources left bar, already present).

 Honestly, I don't understand how this thing got so much out of
 control. If anyone starts an intelligent question or remark about
 super, this essay is thrown in no matter what. Anyone can explain why?

 Because for a very loong time (seven years, 2001-2008) super was
 almost undocumented. The Library Reference -before release 2.6- only
 had a short paragraph, the [...]

 You are right, the documentation needs some work in this regard. But
 again, just because some sources are not in the documentation doesn't
 mean that the most opinionated essay is the best source. A little
 search turns up much more useful ones.

 (not according to Google [2]: I get the harmful article in the top, the
 thread in python-dev, a couple threads in c.l.p including posts by M.S.,
 his article in artima, and nothing more relevant than Monty Python Super
 Star up to the 3rd page)

 *Now* that *I* am aware of the recent article series by M.S., the next
 time someone asks *me* about super(), probably I'll refer her to both M.S.
 and J.K.'s articles. Last time I checked (perhaps one or two years ago),
 the harmful article was almost the only relevant source of info about
 super().

 [1]
 http://groups.google.com/group/comp.lang.python/search?q=super+harmfulstart=0scoring=d;
 [2] http://www.google.com/search?q=python+super

Okay, I think we converged to a common denominator. I agree with you
that the documentation needs additions about super and I also agree
with you that referring to both MS and JK articles is appropriate when
a question about super comes up.

It's good to have a discussion when views actually converge and not
diverge at the end :)

Cheers,
Daniel


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


Re: Super() confusion

2009-02-10 Thread Benjamin Kaplan
On Tue, Feb 10, 2009 at 9:25 PM, Daniel Fetchinson 
fetchin...@googlemail.com wrote:

  Consider whether you really need to use super().
  http://fuhm.net/super-harmful/
 
  Because throwing around that link carries about the same amount of
  information as perl is better than python, my IDE is better than
  yours, vim rulez!, emacs is cooler than vim, etc, etc.
 
  Not at all. It contains accurate and valuable information that isn't
  available elsewhere.
 
  But does not contain other valuable information which would demystify
  super. If only this source is shown to a person who wants to learn the
  proper usage of super, the impression he/she gets will be distorted.
 
  Why so? At the end there is a best practices recipe that pretty much
  summarizes the proper usage of super, and AFAIK it's the right way to use
  it. Don't you agree with any of the conclusions?
 
  Example: 1. somebody asks about threading 2. reply comes: there is a
  GIL! you can't do real threading. Now this reply might be technically
  more-or-less correct, it is misleading and does not help the poster.
  An informed discussion of the various solutions that does not involve
  a rant about the GIL would be very useful though.
 
  As I said, the article presents a recipe for super() usage, and I'd
  consider it very helpful. It's far from just saying super s*cks!, or
  the GIL s*cks! or something like that.
 
  You might want to start with
 
  http://www.artima.com/weblogs/viewpost.jsp?thread=236275
 
  ...which, although the author says it was written a long time ago, was
 not
  published until less than six months ago, and has very low visibility.
 
  You are right, it's not in the documentation. But if somebody asks on
  c.l.p the appropriate answer, I think, is to point to information such
  as the one above, and not the misleading harmful essay.
 
  According to Google, nobody has menctioned the harmful essay in this
  group since April 2008 [1], months before Simionato's article were
  available... So this is the *first* time the reference to the former
 essay
  could have been replaced by M.S.' one... don't be so strict!
 
  Anyway, the right thing to do, IMHO, is to publish correct and accurate
  documentation in the main Python site. Not everybody knows about this
  group existence, nor has the time/experience/interest on subscribe here,
  post a question and wait for an answer. I've seen some posts in
 python-dev
  saying something like this is confusing, we should evangelize people on
  c.l.p. on the right way to do it and I completely disagree; the right
  place for such things is the product documentation, or -to a much lesser
  degree because it's online only- some article collection linked from the
  main site (like the Other resources left bar, already present).
 
  Honestly, I don't understand how this thing got so much out of
  control. If anyone starts an intelligent question or remark about
  super, this essay is thrown in no matter what. Anyone can explain why?
 
  Because for a very loong time (seven years, 2001-2008) super was
  almost undocumented. The Library Reference -before release 2.6- only
  had a short paragraph, the [...]
 
  You are right, the documentation needs some work in this regard. But
  again, just because some sources are not in the documentation doesn't
  mean that the most opinionated essay is the best source. A little
  search turns up much more useful ones.
 
  (not according to Google [2]: I get the harmful article in the top, the
  thread in python-dev, a couple threads in c.l.p including posts by M.S.,
  his article in artima, and nothing more relevant than Monty Python Super
  Star up to the 3rd page)
 
  *Now* that *I* am aware of the recent article series by M.S., the next
  time someone asks *me* about super(), probably I'll refer her to both
 M.S.
  and J.K.'s articles. Last time I checked (perhaps one or two years ago),
  the harmful article was almost the only relevant source of info about
  super().
 
  [1]
 
 http://groups.google.com/group/comp.lang.python/search?q=super+harmfulstart=0scoring=d;
  [2] http://www.google.com/search?q=python+super

 Okay, I think we converged to a common denominator. I agree with you
 that the documentation needs additions about super and I also agree
 with you that referring to both MS and JK articles is appropriate when
 a question about super comes up.

 It's good to have a discussion when views actually converge and not
 diverge at the end :)


Wait, that's not supposed to happen. This is Usenet after all. Quick,
someone comment on the GIL!

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

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


Iterable Ctypes Struct

2009-02-10 Thread mark . seagoe
I like the ability to access elements of a struct such as with ctypes
Structure:
myStruct.elementName1
4

What I like about it is there are no quotes needed.

What I don't like about it is that it's not iterable:
for n in myStruct:  == gives error
print n

I don't want to force the end user to have preknowledge of the element
names.
Has anyone subclassed ctypes Structure based class to be iterable?
Before a noob starts trying to do this, is it possible?  How to
approach it?

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


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Jervis Whitley
 I've done this:

 def _fact(n):
if n = 0: return 1
return _fact(n-1)*n

 def fact(n):
if n  0: raise ValueError
return _fact(n)

 but that's ugly. What else can I do?


Hello, an idea is optional keyword arguments.

def fact(n, check=False):
  if not check:
if n  0: raise ValueError
  if n == 0: return 1
  return fact(n - 1, check=True) * n

essentially hiding an expensive check with a cheap one. It saves you
duplicating code in a separate function like in your example.
--
http://mail.python.org/mailman/listinfo/python-list


Re: pySerial help please!

2009-02-10 Thread Grant Edwards
On 2009-02-10, bmasch...@gmail.com bmasch...@gmail.com wrote:
 Hello all,

 I am very new to Python and I am using it because I needed an
 easy language to control a piece of equipment that connects to
 my computer via a serial cable. I am running Python 2.6 with
 pySerial 2.4 under Windows. I can get Python to create a
 serial port on COM1, but when I try to write to the device,
 nothing happens. Here is an example:

 import serial
 ser = serial.Serial(0)
 ser.write(otpm 2 16 0)
 ser.close()

Are you sure you don't need some sort of line-terminator (e.g
\r or \n) to tell the device to go ahead and execute the command?

 If I connect to the device in Hyperterminal the device will
 behave as expected when I give it the command 'otpm 2 16 0'
 but not in Python.

In Hyperterminal did you have to press [enter] to get the
command to execute?

If you close the port immediately after calling write(), it's
possible that the data never actually got sent.  You should
probably wait until the data has been sent before closing the
port.

If you're running on Unix, I you should be able to call
ser.flush() to wait until the output buffer has been sent.

However, I wouldn't count too heavily on the underlying Unix
serial driver properly implimenting the tcdrain call.  And, I
think the flush() call on Windows is a noop.  So, putting an
appropriate delay between the write and the close is probably
the safest thing to do.

-- 
Grant

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


Re: Super() confusion

2009-02-10 Thread Gabriel Genellina
En Wed, 11 Feb 2009 00:31:06 -0200, Benjamin Kaplan  
benjamin.kap...@case.edu escribió:

On Tue, Feb 10, 2009 at 9:25 PM, Daniel Fetchinson 
fetchin...@googlemail.com wrote:


Okay, I think we converged to a common denominator. I agree with you
that the documentation needs additions about super and I also agree
with you that referring to both MS and JK articles is appropriate when
a question about super comes up.

It's good to have a discussion when views actually converge and not
diverge at the end :)


Wait, that's not supposed to happen. This is Usenet after all. Quick,
someone comment on the GIL!


Our resident trolls are sleeping, it seems...

--
Gabriel Genellina

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


Difference between vars() and locals() and use case for vars()

2009-02-10 Thread python
Can someone explain the difference between vars() and locals()?
I'm also trying to figure out what the use case is for vars(),
eg. when does it make sense to use vars() in a program?
Thank you,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between vars() and locals() and use case for vars()

2009-02-10 Thread Gabriel Genellina

En Wed, 11 Feb 2009 01:38:49 -0200, pyt...@bdurham.com escribió:


Can someone explain the difference between vars() and locals()?
I'm also trying to figure out what the use case is for vars(),
eg. when does it make sense to use vars() in a program?


Without arguments, vars() returns the current namespace -- same as  
locals() inside a function, same as locals() *and* globals() outside any  
function (i.e., in a module or running in the interactive interpreter)


With an argument (that is, vars(x)) it returns the names defined by the  
object itself; for normal class instances, that means its __dict__


It's useful in the interactive interpreter, to examine some object's  
contents. Maybe in other special cases. Certainly not in everyday's  
programming.


--
Gabriel Genellina

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


Re: Difference between vars() and locals() and use case for vars()

2009-02-10 Thread python
Thank you Gabriel!

Malcolm


- Original message -
From: Gabriel Genellina gagsl-...@yahoo.com.ar
To: python-list@python.org
Date: Wed, 11 Feb 2009 02:04:47 -0200
Subject: Re: Difference between vars() and locals() and use case for
vars()

En Wed, 11 Feb 2009 01:38:49 -0200, pyt...@bdurham.com escribió:

 Can someone explain the difference between vars() and locals()?
 I'm also trying to figure out what the use case is for vars(),
 eg. when does it make sense to use vars() in a program?

Without arguments, vars() returns the current namespace -- same as  
locals() inside a function, same as locals() *and* globals() outside any 
function (i.e., in a module or running in the interactive interpreter)

With an argument (that is, vars(x)) it returns the names defined by the  
object itself; for normal class instances, that means its __dict__

It's useful in the interactive interpreter, to examine some object's  
contents. Maybe in other special cases. Certainly not in everyday's  
programming.

-- 
Gabriel Genellina

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


Re: can multi-core improve single funciton?

2009-02-10 Thread oyster
Hi, guys, my fib(xx) is just an example to show what is a single
function and what is the effect I expect to see when enable
multi-core.

My real purpose is to know whether multi-core can help to improve the
speed of a common function. But I know definitely that the answer is
NO.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Gabriel Genellina
En Tue, 10 Feb 2009 23:58:07 -0200, Steven D'Aprano  
ste...@remove.this.cybersource.com.au escribió:



I sometimes write recursive functions like this simple factorial:


def fact(n):
if n  0: raise ValueError
if n = 0: return 1
return fact(n-1)*n

At the risk of premature optimization, I wonder if there is an idiom for
avoiding the unnecessary test for n = 0 in the subsequent recursive
calls? For the sake of the argument, let's pretend the test is expensive
and I have a good reason for wanting to avoid it on subsequent calls :)



I've done this:

def _fact(n):
if n = 0: return 1
return _fact(n-1)*n

def fact(n):
if n  0: raise ValueError
return _fact(n)

but that's ugly. What else can I do?


I don't think it's ugly; you have an implementation (_fact) and its public  
interfase (fact). In 'fact' you could check that n is actually an integer  
(an implicit precondition, your algorithm doesn't finish in other case)  
and whatever validation is also required. Perhaps its arguments come from  
user input and you need stricter tests, or convert from other type (like  
float-int).
On the other hand, '_fact' is private and you can assume their arguments  
are exactly what you require.
In Pascal I would have used a nested _fact function; in Python it isn't as  
efficient so I'd write it as your own example.


This is a rather used idiom - in the Python C API, by example, usually you  
see a public function PyFoo_DoSomething(PyObject* obj) and a private one  
_PyFoo_DoSomething(double x) (assume a function like sqrt, number -  
float). The public one takes a generic Python object, checks its type,  
converts it to a C double value, and if all went OK, finally calls the  
private implementation passing that value.


--
Gabriel Genellina

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


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread afriere
On Feb 11, 1:48 pm, Jervis Whitley jervi...@gmail.com wrote:

 Hello, an idea is optional keyword arguments.

 def fact(n, check=False):
   if not check:
     if n  0: raise ValueError
   if n == 0: return 1
   return fact(n - 1, check=True) * n

 essentially hiding an expensive check with a cheap one. It saves you
 duplicating code in a separate function like in your example.

Given the original read:

def fact(n):
if n  0: raise ValueError
if n = 0: return 1
return fact(n-1)*n

You've merely replaced the 'test n0' with 'not check' at the expense
of an additional parameter that has to be passed each time (and the
additional test 'n0' for the first iteration).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Super() confusion

2009-02-10 Thread Michele Simionato
On Feb 10, 9:19 am, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:
 You really should push them to be included in python.org, even in their  
 unfinished form. (At least a link in the wiki pages). Their visibility is  
 almost null now.

It looks like I made an unfortunate choice with the
title (Things to Know about Super). I have just changed
it to Things to Know about *Python* Super so that now
googling for python super should hopefully find them.

I agree that the real solution would be to publish on python.org, but
for that I have to write yet another paper about Python 3.0 super, cut
things about old version/bugs of Python, re-read the whole beast, and
convince the core developers to publish it (which is not automatic).
That is some work, and I have many other projects going on right now.
Truth is, discussions about super do not come out so often, and most
people don't care.

 They're very clearly written - I wish you had published them years ago!

Me too, but you know what happens when working for free and without
deadlines ;)

 again, you really should publish the  
 series in a more prominent place.

Soon or later I will go back to super in Python 3.0, but now I am
working on my Scheme series, I have yet to translate two articles of
my Mixins considered harmful series, and I have at least two other
articles which has been waiting publication for more than one year.
Plus, I have a full time job ;)
But if you and others keep bugging me something may happen ...

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


Re: can multi-core improve single funciton?

2009-02-10 Thread James Mills
On Wed, Feb 11, 2009 at 2:21 PM, oyster lepto.pyt...@gmail.com wrote:
 My real purpose is to know whether multi-core can help to improve the
 speed of a common function. But I know definitely that the answer is
 NO.

As stated by others, and even myself,
it is not possible to just automagically
improve the execution speed of a single
function - let alone an application.

Your problem must be capable of being divided up
into work units that can be parallelized. If this is not
possible, multiple cores (no matter how many you have)
-will not- help you.

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


Re: can multi-core improve single funciton?

2009-02-10 Thread Chris Rebert
On Tue, Feb 10, 2009 at 8:57 PM, James Mills
prolo...@shortcircuit.net.au wrote:
 On Wed, Feb 11, 2009 at 2:21 PM, oyster lepto.pyt...@gmail.com wrote:
 My real purpose is to know whether multi-core can help to improve the
 speed of a common function. But I know definitely that the answer is
 NO.

 As stated by others, and even myself,
 it is not possible to just automagically
 improve the execution speed of a single
 function - let alone an application.

 Your problem must be capable of being divided up
 into work units that can be parallelized. If this is not
 possible, multiple cores (no matter how many you have)
 -will not- help you.

See also Amdahl's Law -- http://en.wikipedia.org/wiki/Amdahl%27s_law

Cheers,
Chris

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


Re: bool evaluations of generators vs lists

2009-02-10 Thread Gabriel Genellina

On Tue, 2009-02-10 at 12:50 -0800, Josh Dukes wrote:


The thing I don't understand is why a generator that has no iterable
values is different from an empty list. Why shouldn't bool ==
has_value?? Technically a list, a tuple, and a string are also objects
but if they lack values they're evaluated as False. It seems to me that
a generator is an object that intends to replace lists where lazy
evaluation would be more efficent. Here is one place where that's
definitely true.


Just in case it's not perfectly clear: until you call next() there is no  
way to know whether the generator will yield any value or not -- and once  
it does, it's lost until you explicitely save it.


This generator doesn't yield any value - but you have to wait for a while  
if you call .next() on it, until eventually raises StopIteration:

(x for x in xrange(20) if x1000)


En Tue, 10 Feb 2009 19:25:47 -0200, Albert Hopkins  
mar...@letterboxes.org escribió:



The main reason I'm interested in this is that it improves performance
immensely over boolean evaluation of large lists (as the attached code
shows). It seems to me if I could use find a good use for it in my
experimentation that someone else might also want to do the same thing
in real-world code.


I don't understand what you mean by this.  But if you really want to
know if a generator is non-empty:

def non_empty(virgin_generator):
try:
virgin_generator.next() # note you just lost the first value
return True
except StopIteration:
return False

The only way to get around this is to put all the values of a generator
inside a container (e.g. a list):


For a long generator you may not want to do that, also you may not want to  
lose the next element. A variation of your function above is useful in  
such cases:


py def end_of_gen(g):
...   returns (False, next_element) when it exists or (True, None) when  
it's

empty
...   try: return False, g.next()
...   except StopIteration: return True, None
...
py g = (c for c in Python if c in aeiou)
py eog, c = end_of_gen(g)
py eog
False
py c
'o'
py eog, c = end_of_gen(g)
py eog
True
py c


--
Gabriel Genellina

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


Re: getopt

2009-02-10 Thread Matthew Sacks
The documentation leaves lack for want, especially the examples.

On Tue, Feb 10, 2009 at 5:25 PM, John Machin sjmac...@lexicon.net wrote:
 On Feb 11, 12:12 pm, Matthew Sacks ntw...@gmail.com wrote:
 if anyone can have a look at this code and offer suggestions i would
 appreciate it.
 i am forced to use getopt, so i cant use something good like optparse

 passedArgs = sys.argv[1:]
 optlist, args = getopt.getopt(str(passedArgs), [connectPassword=,

 Dunno where you acquired the str() ... just lose it. Consider checking
 with the documentation when something does not work.

 adminServerURL=, action=, targets=, appDir=])

 for some reason this does not work in assigning args to opts.

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

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


Re: Iterable Ctypes Struct

2009-02-10 Thread Gabriel Genellina

En Wed, 11 Feb 2009 00:31:26 -0200, mark.sea...@gmail.com escribió:


I like the ability to access elements of a struct such as with ctypes
Structure:

myStruct.elementName1

4

What I like about it is there are no quotes needed.

What I don't like about it is that it's not iterable:

for n in myStruct:  == gives error
   print n


I don't want to force the end user to have preknowledge of the element
names.


Note that those names are available as the _fields_ class attribute


Has anyone subclassed ctypes Structure based class to be iterable?
Before a noob starts trying to do this, is it possible?  How to
approach it?


The easiest way would be to define __getitem__ accepting index 0, 1, 2...  
until the last defined field.

See http://docs.python.org/reference/datamodel.html#object.__getitem__

code
from ctypes import Structure

class IterableStructure(Structure):
  def __getitem__(self, i):
if not isinstance(i, int):
  raise TypeError('subindices must be integers: %r' % i)
return getattr(self, self._fields_[i][0])

/code

This was tested as much as you see here:

py from ctypes import c_int
py
py class POINT(IterableStructure):
... _fields_ = [(x, c_int),
... (y, c_int)]
...
py point = POINT(10, 20)
py print point.x, point.y
10 20
py for field in point:
...   print field
...
10
20
py print list(point)
[10, 20]

--
Gabriel Genellina

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


Re: can multi-core improve single funciton?

2009-02-10 Thread Paul Rubin
oyster lepto.pyt...@gmail.com writes:
 Hi, guys, my fib(xx) is just an example to show what is a single
 function and what is the effect I expect to see when enable
 multi-core.
 
 My real purpose is to know whether multi-core can help to improve
 the speed of a common function. But I know definitely that the
 answer is NO.

Well, it depends on the implementation.  Python isn't at the moment
designed for using multicores but as it evolves that may change.

The blog post http://cgi.cse.unsw.edu.au/~dons/blog/2007/11/29
may be of interest re parallelizing that fibonacci function.
--
http://mail.python.org/mailman/listinfo/python-list


Re: getopt

2009-02-10 Thread John Machin
On Feb 11, 12:25 pm, John Machin sjmac...@lexicon.net wrote:
 On Feb 11, 12:12 pm, Matthew Sacks ntw...@gmail.com wrote:

  if anyone can have a look at this code and offer suggestions i would
  appreciate it.
  i am forced to use getopt, so i cant use something good like optparse

  passedArgs = sys.argv[1:]
  optlist, args = getopt.getopt(str(passedArgs), [connectPassword=,

 Dunno where you acquired the str() ... just lose it. Consider checking
 with the documentation when something does not work.

Rule 2: read further in the documentation than you think you need
to :-)

You have omitted the second arg, the one that defines short options.
As for the second part of your question, assigning to locals is
tedious as you have found. The classical solution is to jam the
options into an object that acts like a 'record' or a 'struct'.
Another possibility is the newfangled 'named tuple'. The following
illustrates working code for getting your optlist going, and using the
'record' idea. Keep on going (next step: add a function for each arg
that will convert the value from str to int/float/bool/etc as/if
appropriate) and you will have rewritten optparse.

HTH,
John


C:\junktype getopt_demo.py
import getopt, sys
long_arg_defns = { # sets up defaults
cp:  None,
asu: xyzzy,
act: do_nothing,
tgts:,
ad: 1.23,
}
long_arg_list = [x + '=' for x in long_arg_defns.keys()]
passedArgs = sys.argv[1:]
print 'passedArgs', passedArgs

print '\nattempt 1'
try:
optlist, args = getopt.getopt(str(passedArgs), long_arg_list)
print optlist, args
except Exception, e:
print %s: %s % (e.__class__.__name__, e)

print '\nattempt 2'
try:
optlist, args = getopt.getopt(passedArgs, long_arg_list)
print optlist, args
except Exception, e:
print %s: %s % (e.__class__.__name__, e)

print '\nattempt 3'
try:
optlist, args = getopt.getopt(passedArgs, '', long_arg_list)
print optlist, args
except Exception, e:
print %s: %s % (e.__class__.__name__, e)

class Record(object):

def __init__(self, initial_dict, optlist):
for attr, default in initial_dict.items():
setattr(self, attr, default)
for attr, value in optlist:
setattr(self, attr.lstrip('-'), value)

def __str__(self):
return Record: %s % self.__dict__

r = Record(long_arg_defns, optlist)
print
print r
print
print r.cp, r.act, r.ad


C:\junkgetopt_demo.py --cp xxxcp --ad yad extra1 extra2
passedArgs ['--cp', 'xxxcp', '--ad', 'yad', 'extra1', 'extra2']

attempt 1
[] ['--cp', 'xxxcp', '--ad', 'yad', 'extra1', 'extra2']

attempt 2
GetoptError: option --cp not recognized

attempt 3
[('--cp', 'xxxcp'), ('--ad', 'yad')] ['extra1', 'extra2']

Record: {'tgts': '', 'cp': 'xxxcp', 'ad': 'yad', 'asu': 'xyzzy',
'act': 'do _nothing'}

xxxcp do_nothing yad

C:\junk
--
http://mail.python.org/mailman/listinfo/python-list


Re: python3 tutorial for newbie

2009-02-10 Thread Gabriel Genellina

En Tue, 10 Feb 2009 16:22:36 -0200, Gary Wood python...@sky.com escribió:

Can someone recommend a good tutorial for Python 3, ideally that has  
tasks or assignments at the end of each chapter.


I don't know of any specifically targetted to Python 3, except the  
official one at http://www.python.org/doc/3.0/


For the most part, any Python tutorial should be fine. Perhaps the only  
visible change (at the tutorial level) is that print became a function:


# 2.x syntax:
print Hello, world!

# 3.x syntax:
print(Hello, world!)

That said, Python 3.0 is so recent that isn't widely used yet, and many  
third party libraries aren't available for 3.0 at this moment. This  
certainly will change in the future, but in the meantime, perhaps you  
should stick to Python 2.6 for a while.


--
Gabriel Genellina

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


Re: getopt

2009-02-10 Thread John Machin
On Feb 11, 4:36 pm, Matthew Sacks ntw...@gmail.com wrote:
 The documentation leaves lack for want, especially the examples.

You had two problems:

(1) str(passedArgs): The docs make it plain that args is a list, not
a str instance: args is the argument list to be parsed, without the
leading reference to the running program. Typically, this means
sys.argv[1:]. The 1st and 2nd examples spell out the same story;
here's the 2nd:

 Using long option names is equally easy:
  s = '--condition=foo --testing --output-file abc.def -x a1 a2'
  args = s.split()
  args
 ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x',
'a1', 'a2']


(2) omitting the *required* options arg: (a) it's not wrapped in [] so
it's required (b) the docs say To accept only long options, options
should be an empty string.

IOW, it may leave lack for want, but not in the areas causing you a
bother.
--
http://mail.python.org/mailman/listinfo/python-list


Browse Dialog

2009-02-10 Thread kamath86
Hi ,

I am using TKinter for creating a GUI. As of now i am using
tkFileDialog module for selection of file/directory.But i see that i
can use either of these at one go.Is there a way i can select a file
or a directory through a single dialog box??
--
http://mail.python.org/mailman/listinfo/python-list


Programmatically changing network proxy settings on the Mac

2009-02-10 Thread arunasunil
Hi,

Anybody have suggestions of how network proxy settings can be changed
programmatically on the Mac, Tiger and Leopard. Are there any helpful
python api's that can be used.

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


Re: python3 tutorial for newbie

2009-02-10 Thread Akira Kitada
http://wiki.python.org/moin/Python3.0Tutorials

On Wed, Feb 11, 2009 at 3:22 AM, Gary Wood python...@sky.com wrote:
 Can someone recommend a good tutorial for Python 3, ideally that has tasks
 or assignments at the end of each chapter.
 Please,

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


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


Re: getopt

2009-02-10 Thread Matthew Sacks
I didn't realize that the no-value arguments, -b, -h, etc are required?
This seems to make things a bit more difficult considering unless I
use the GNU style getopt all arguments are required to be passed?

I could be mistaken.

I will have a look at what you have posted here and report my results.
I appreciate the response.

M

On Tue, Feb 10, 2009 at 10:36 PM, John Machin sjmac...@lexicon.net wrote:
 On Feb 11, 4:36 pm, Matthew Sacks ntw...@gmail.com wrote:
 The documentation leaves lack for want, especially the examples.

 You had two problems:

 (1) str(passedArgs): The docs make it plain that args is a list, not
 a str instance: args is the argument list to be parsed, without the
 leading reference to the running program. Typically, this means
 sys.argv[1:]. The 1st and 2nd examples spell out the same story;
 here's the 2nd:
 
  Using long option names is equally easy:
   s = '--condition=foo --testing --output-file abc.def -x a1 a2'
   args = s.split()
   args
  ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x',
 'a1', 'a2']
 

 (2) omitting the *required* options arg: (a) it's not wrapped in [] so
 it's required (b) the docs say To accept only long options, options
 should be an empty string.

 IOW, it may leave lack for want, but not in the areas causing you a
 bother.
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Scott David Daniels

Steven D'Aprano wrote:

I sometimes write recursive functions like this simple factorial:
def fact(n):
if n  0: raise ValueError
if n = 0: return 1
return fact(n-1)*n 

At the risk of premature optimization, I wonder if there is an idiom for 
avoiding the unnecessary test for n = 0 in the subsequent recursive 
calls? For the sake of the argument, let's pretend the test is expensive 
and I have a good reason for wanting to avoid it on subsequent calls :)

How about:

 def fact(n):
 if n  2:
 if n  0:
 raise ValueError
 return 1
 return fact(n - 1) * n

But really, iteration is the solution to this, and avoiding the
right answer is a mistake.  I couldn't resist fixing your test
so you do one less layer of recursion.

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


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Paul Rubin
Steven D'Aprano ste...@remove.this.cybersource.com.au writes:
 def fact(n):
 if n  0: raise ValueError
 if n = 0: return 1
 return fact(n-1)*n 
 
 At the risk of premature optimization, I wonder if there is an idiom for 
 avoiding the unnecessary test for n = 0 in the subsequent recursive 
 calls?

I'd write nested functions:

def fact(n):
   if n  0: raise ValueError
   def f1(n):
  return 1 if n==0 else n*f1(n-1)
   return f1(n)

If the language implementation supports tail recursion optimization
there's an accumulation-parameter style that takes a little getting
used to but is familiar in functional programming:

def fact(n):
if n  0: raise ValueError
def f1(k,n):
   return k if n==0 else f1(k*n, n-1)
return f1(1, n)

This won't do any good in CPython but maybe PyPy or Pyrex or whatever
can make use of it.  In this case the nested function is already
there, and the n0 check naturally lifts out of it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Avoiding argument checking in recursive calls

2009-02-10 Thread Paul Rubin
Paul Rubin http://phr...@nospam.invalid writes:
 I'd write nested functions:
 
 def fact(n):
if n  0: raise ValueError
def f1(n):
   return 1 if n==0 else n*f1(n-1)
return f1(n)

I forgot to add: all these versions except your original one should
add a type check if you are trying to program defensively.  Otherwise
they can recurse infinitely if you give a positive non-integer arg like
fact(3.5).
--
http://mail.python.org/mailman/listinfo/python-list


[issue4804] Python on Windows disables all C runtime library assertions

2009-02-10 Thread Martin v. Löwis

Martin v. Löwis mar...@v.loewis.de added the comment:

The revised patch looks fine to me, please apply.

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue4804
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5200] unicode.normalize gives wrong result for some characters

2009-02-10 Thread Peter Landgren

New submission from Peter Landgren peter.tal...@telia.com:

If any of the Swedish characters åäöÅÄÖ are input to
unicode.normalize(form, ustr) with form = NFD or NFKD the result
will be aaoAAO. åäöÅÄÖ are normal character and should be the same
after normalize. They are not connected to aaoAAO other than for
historic reasons, but not in modern languages. It's a common
misinterpretation that the dots and circle above them are diacritic
signs, but those letters should behave as the (Danish)
Ø which is normalized correctly.

From Wikipedia:
Å is often perceived as an A with a ring, interpreting the ring as a
diacritical mark. However, in the languages that use it, the ring is not
considered a diacritic but part of the letter.
The letter Ö in the Swedish and Icelandic alphabets historically arises
from the Germanic umlaut, but it is considered a separate letter from O.
See http://en.wikipedia.org/wiki/%C3%85

I think this is pobably impossible to solve as it will be mixed up with
umlaut and you don't know what language the specific word is connected to.

--
components: Library (Lib)
messages: 81536
nosy: PeterL
severity: normal
status: open
title: unicode.normalize gives wrong result for some characters
type: behavior
versions: Python 2.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5200
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1818] Add named tuple reader to CSV module

2009-02-10 Thread Jervis Whitley

Jervis Whitley jervi...@gmail.com added the comment:

Updated NamedTupleReader to give a rename=False keyword argument.
rename is passed directly to the namedtuple factory function to enable
automatic handling of invalid fieldnames.

Two new tests for the rename keyword.

Cheers,

Added file: http://bugs.python.org/file13009/ntreader4.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1818
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5201] Using LDFLAGS='-rpath=\$$LIB:/some/other/path' ./configure breaks the build

2009-02-10 Thread Floris Bruynooghe

New submission from Floris Bruynooghe floris.bruynoo...@gmail.com:

When specifying an RPATH with -rpath or -R you can use the special
tokens `$LIB' and `$ORIGIN' which the runtime linker interprets as
normal search path and relative to current sofile respectively.  To
get these correctly to the gcc command line you need to specify this in
LDFLAGS as `\$$LIB' to work around escapes of both the makefile and
shell, so in the Python Makefile this will appear somewhere as (this is
on one line):

CONFIG_ARGS= '--prefix=/opt/example.com/python25'
'LDFLAGS=-Wl,-rpath=\$$LIB:/opt/example.com/lib,--enable-new-dtags'

This works for compiling the main python binary.  But when the extension
modules get compiled distutils chokes on this. 
distutils.sysconfig.parse_makefile() does think that any value of a
variable that contains `$' in it refers to an other variable in the
makefile.  It will fail to find the value and CONFIG_ARGS will not be
defined.  This then fails in setup.py for the _ctypes extension:

if not '--with-system-ffi' in sysconfig.get_config_var(CONFIG_ARGS):
return

Where `None' is returned instead of a list by .get_config_var().

It seems that distutils.sysconfig.parse_makefile() needs to understand
more of the makefile synatax to deal with this.

--
assignee: tarek
components: Distutils
messages: 81538
nosy: flub, tarek
severity: normal
status: open
title: Using LDFLAGS='-rpath=\$$LIB:/some/other/path' ./configure breaks the 
build
type: compile error
versions: Python 2.5, Python 2.6, Python 2.7, Python 3.0

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5201
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5202] wave.py cannot write wave files into a shell pipeline

2009-02-10 Thread David Jones

New submission from David Jones d...@pobox.com:

When using the wave module to output wave files, the output file cannot 
be a Unix pipeline.

Example.  The following program outputs a (trivial) wave file on stdout:

#!/usr/bin/env python
import sys
import wave
w = wave.open(sys.stdout, 'w')
w.setnchannels(1)
w.setsampwidth(1)
w.setframerate(32000)
w.setnframes(0)
w.close()


It can create a wave file like this:

$ ./bugex  foo.wav

When used in a pipeline we get:

$ ./bugex | wc
Traceback (most recent call last):
  File ./bugex, line 9, in module
w.close()
  File 
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
, line 437, in close
self._ensure_header_written(0)
  File 
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
, line 458, in _ensure_header_written
self._write_header(datasize)
  File 
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
, line 465, in _write_header
self._form_length_pos = self._file.tell()
IOError: [Errno 29] Illegal seek
Exception exceptions.IOError: (29, 'Illegal seek') in bound method 
Wave_write.__del__ of wave.Wave_write instance at 0x71418 ignored
   0   1   8

The wave module has almost all it needs to work around this problem.  
The wave module will only seek the output if it needs to patch the 
header.  If you use setnframes to write the correct number of frames 
before writing them with writeframesraw then the header will not be 
patched upon calling close.  However...

The problem is that the tell method is invoked on the output stream 
(to record where the header is, in the event that we need to patch it); 
the tell method fails with an exception when the output is a pipeline 
(see example, above).

Exceptions from tell when writing the header initially (in 
_write_header) should be ignored.  If _patchheader is later invoked it 
will fail due to lack of pos.

--
components: Library (Lib)
messages: 81539
nosy: drj
severity: normal
status: open
title: wave.py cannot write wave files into a shell pipeline
type: behavior
versions: Python 2.5, Python 2.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5202
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5175] negative PyLong - C unsigned integral, TypeError or OverflowError?

2009-02-10 Thread Lisandro Dalcin

Lisandro Dalcin dalc...@gmail.com added the comment:

Mark, here you have a patch. I've only 'make test' on a 32bit Linux box

Just two comments:

- in docs: perhaps the 'versionchanged' stuff should be added.

- in tests: I did not touch Modules/_testcapimodule.c, as it seems the
test is covered. However, note that in all these tests, actual exception
types are not checked)

--
keywords: +patch
Added file: http://bugs.python.org/file13010/negative-to-unsigned.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5175
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



<    1   2   3   4   >