Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Hendrik van Rooyen
When the days get colder and the nights longer,
then evil things are hatched.

A can is like a pickle, in that it is a string, but anything
can be canned.
Unlike a pickle, a can cannot leave the process, though,
unless the object it points to lives in shared memory.

Here is the output of a test session:

> python -i cantest.py

dir(Can) yields:
['__doc__', '__file__', '__name__', 'can', 'uncan']

Testing string object
s is:  The quick brown fox jumps over the lazy dog
id(s) is:  47794404772392
ps = can(s) :  47794404772392
t = uncan(ps): The quick brown fox jumps over the lazy dog
t is s gives:  True

Testing q = Queue.Queue()
q is:  
id(q) is:  47794404845616
pq = can(q) :  47794404845616
r = uncan(pq): 
r is q gives:  True

Testing banana class object
b = banana()   <__main__.banana object at 0x73d190>
id(b) is:  7590288
pb = can(c) :  7590288
c = uncan(pb): <__main__.banana object at 0x73d190>
c is b gives:  True

That's it, folks!
>>> pcan = can(Can.can)
>>> pcan
'47794404843816'
>>> griz = uncan(pcan)
>>> griz is can
True
>>> z = 'foo'
>>> griz(z)
'7557840'
>>> uncan(_)
'foo'
>>> griz

>>> # uncanny!
>>>


Now I have only tested it on my system, which is 64 bit SuSe Linux.
I suspect it is fragile.
I know it is dangerous - if you uncan a random
number, then if you are lucky, you will get a segfault.
If you are not lucky, your life will be completely
changed.

If you have any interest, contact me and I will
send you the source.

- Hendrik


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


Re: OT: Can;'t find a Mozilla user group

2009-06-04 Thread Anthra Norell

Terry Reedy wrote:

News123 wrote:


Anthra Norell wrote:

I can't run Firefox and Thunderbird without getting these upgrade
ordering windows. I don't touch them, because I have reason to suspect
that they are some (Russian) virus that hijacks my traffic. 
Occasionally

one of these window pops up the very moment I hit a key and next a
confirmation message appears "reassuring" me that the upgrade will be
installed the next time I start. I meant to ask Mozilla about their
upgrade policy and facilities but for all the googling I do I can't 
find

a contact address, nor an active user group. Hints will be greatly
appreciated. Thanks!

Frederic



I don't know a Mozilla news group,
but in Settings Advanced -> Settings ->  Updates
you can choose if updats will be installed automatically or if you want
to be asked.


In my copies of Firefox and Thunderbird:
Tools - Options - Advanced - Update

I have 'ask me' checked because auto updates do not seem to work in 
non-admin accounts.  So at my convenience, I switch to admin, go to 
Help - Check for Updates, and when it finds it, click install.



Additionally you could check your SW version in the Help->About Menu and
then go to the Firefox / Thunderbird web site and look at which version
they are?

So you know at least whether there is a pending update



Thank you all! Great advice!
Frederic


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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Nick Craig-Wood
Esmail  wrote:
>  Scott David Daniels wrote:
> > Esmail wrote:
> >> ... Tk seems a bit more complex .. but I really don't know much about
> >> it and its interface with Python to make any sort of judgments as
> >> to which option would be better.
> > 
> > This should look pretty easy:
> 
>  Thanks Scott for taking the time to share this code with me, it
>  will give me something to study. I'm not sure if I'd say it looks
>  easy (but then again I am not very familiar with Tk :-)

Here is a demo with pygame...

import pygame
from pygame.locals import *
from random import randrange

size = width, height = 640, 480
background_colour = 0x00, 0x00, 0x00
foreground_colour = 0x51, 0xd0, 0x3c

def main():
pygame.init()
screen = pygame.display.set_mode(size, 0, 32)
pygame.display.set_caption("A test of Pygame - press Up and Down")
pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
dots = [ (randrange(width), randrange(height)) for _ in range(100) ]
radius = 10
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
raise SystemExit(0)
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
raise SystemExit(0)
elif event.key == K_UP:
radius += 1
elif event.key == K_DOWN:
radius -= 1
screen.fill(background_colour)
for dot in dots:
pygame.draw.circle(screen, foreground_colour, dot, radius, 1)
dots = [ (dot[0]+randrange(-1,2), dot[1]+randrange(-1,2)) for dot in 
dots ]
pygame.display.flip()

if __name__ == "__main__":
main()

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
Joseph Garvin schrieb:
> So I was curious whether it's possible to use the ctypes module with
> C++ and if so how difficult it is.

There have been some attempts to use ctypes to access C++ objects.
We (Roman Yakovenko and myself) made some progress.  We were able to
handle C++ name mangling, the special C++ calling convention,
access virtual, non-virtual, overloaded functions, but finally gave up
because the binary layout (function tables, member variables, and so on)
of C++ objects is way too complicated and undocumented.

Our attempts are documented in posts to the ctypes-users mailing list,
most of them have the word 'cpptypes' in the subject line.

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


Re: Access from a class attribute

2009-06-04 Thread Ben Finney
Kless  writes:

> Why can not to access from a class attribute to a function of that
> class?
> 
> -
> class Foo(object):
>attr = __class__.__name__
>attr = self.__class__.__name__
> -

The ‘self’ name is not magical. If you want it bound to something, you
have to bind it explicitly; it's exactly like any other name.

You will have noticed this being done in methods of a class:

class Foo(object):
attr = 'spam'

def frobnicate(self, bar):
self.attr = str(bar)

The statements in the method are evaluated in the context of a specific
call to that method, where the parameters have been passed and bound to
the parameter names.

-- 
 \ “I got some new underwear the other day. Well, new to me.” —Emo |
  `\   Philips |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is the biggest number that i can send to Wave_write.writeframes(data)

2009-06-04 Thread '2+
thanx for the example!
somehow on juanty gui comes up but no sound ..
anyway i shortened the script this way and could aplay it

import wave

AMPLITUDE = 2 ** 15

w = wave.open( "out.wav", "w" )
w.setnchannels( 2 )
w.setsampwidth( 2 ) #BYTES
w.setframerate( 22000 )

from array import array
import math

F = 261.626
F2 = F * (2 ** (5 / 12.))
ang = 0.0
ang2 = 0.0
delta = ( math.pi * 2 * F  ) / 22000.0
delta2 = ( math.pi * 2 * F2  ) / 22000.0

for cycle in xrange( 4 ):
data = array( 'h' )
for pos in xrange( 22000 ):
amp = AMPLITUDE * (pos / 22000.0)
amp2 = AMPLITUDE - amp
if cycle & 1:
amp, amp2 = amp2, amp

data.append( int( ( amp * math.sin( ang ) ) ) )
data.append( int( ( amp2 * math.sin( ang2 ) ) ) )

ang += delta
ang2 += delta2

w.writeframes( data.tostring() )

w.close()

On Tue, Jun 02, 2009 at 05:13:59PM -0500, Rob Williscroft wrote:
> '2+ wrote in news:mailman.1017.1243932401.8015.python-l...@python.org in 
> comp.lang.python:
> 
> > would like to take advantage of the wave module
> > found a good example here:
> > http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=10644
> > 
> > hmm .. i don't get how to write a stereo .. i mean i can set nchannels
> > .. but how do i actually take control of each ch individually?
> 
> Interleave the channels, one sample for the left then one sample 
> for the right (or maybe its the other way around).
> 
> > and what's the range(in float) of the data i can set in
> 
> The range of a signed 16 bit int, -2**15 to 2**15 - 1.
> 
> > wav_file.writeframes(struct.pack('h', data))?
> 
> Example:
> 
> import wave
> from StringIO import StringIO
> 
> out = StringIO()
> 
> AMPLITUDE = 2 ** 15
> 
> w = wave.open( out, "w" )
> w.setnchannels( 2 )
> w.setsampwidth( 2 ) #BYTES
> w.setframerate( 22000 )
> 
> from array import array
> import math
> 
> F = 261.626
> F2 = F * (2 ** (5 / 12.))
> ang = 0.0
> ang2 = 0.0
> delta = ( math.pi * 2 * F  ) / 22000.0
> delta2 = ( math.pi * 2 * F2  ) / 22000.0
> 
> for cycle in xrange( 4 ):
>   data = array( 'h' )
>   for pos in xrange( 22000 ):
> amp = AMPLITUDE * (pos / 22000.0)
> amp2 = AMPLITUDE - amp
> if cycle & 1:
>   amp, amp2 = amp2, amp
>   
> data.append( int( ( amp * math.sin( ang ) ) ) )
> data.append( int( ( amp2 * math.sin( ang2 ) ) ) )
> 
> ang += delta
> ang2 += delta2
>   
>   w.writeframes( data.tostring() )
>   
> w.close()
> 
> sample = out.getvalue()
> out.close()
> 
> #a wx player
> 
> import wx
> 
> app = wx.PySimpleApp()
> 
> class Frame( wx.Dialog ):
>   def __init__( self, *args ):
> wx.Dialog.__init__( self, *args )
> b = wx.Button( self, -1, "Ok" )
> b.Bind( wx.EVT_BUTTON, self.button )
>   
>   def button( self, event ):
> self.sound = wx.SoundFromData( sample ) 
> self.sound.Play( wx.SOUND_ASYNC )
> 
> frame = Frame( None )
> frame.Show()
> 
> app.MainLoop()
> 
> Rob.
> -- 
> http://www.victim-prime.dsl.pipex.com/
-- 
'2+
http://sarigama.namaste.jp/
is
podcasting his microtuned music
http://www002.upp.so-net.ne.jp/buyobuyo/micro/rss.xml
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get the hard disk hardware serial number

2009-06-04 Thread Nick Craig-Wood
MRAB  wrote:
>  Jorge wrote:
> > I need to know how to get the hardware serial number of a hard disk in 
> > python.
> > 
>  For Windows, see http://www.daniweb.com/forums/thread187326.html

For linux I'd run this and parse the results.

# smartctl -i /dev/sda
smartctl version 5.38 [i686-pc-linux-gnu] Copyright (C) 2002-8 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF INFORMATION SECTION ===
Model Family: Seagate Momentus 7200.2
Device Model: ST9200420AS
Serial Number:7QW138AK
Firmware Version: 3.AAA
User Capacity:200,049,647,616 bytes
Device is:In smartctl database [for details use: -P show]
ATA Version is:   7
ATA Standard is:  Exact ATA specification draft version not indicated
Local Time is:Thu Jun  4 09:30:23 2009 BST
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

According to the man page smartctl also runs under windows/mac/solaris
etc

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Project source code layout?

2009-06-04 Thread Lawrence D'Oliveiro
In message , Allen 
Fowler wrote:

> I was hoping to keep the dev layout as close to deployment possible.

Completely different purposes. For example, the actual production database 
and config files form no part of your development project, do they? And 
conversely, utility scripts that might be used, for example, to set up a 
database, should not be part of the production installation.

Here's one layout I used in a production system for an online shop:

/home/shop/bin -- binaries (CGI and daemon)
/home/shop/lib -- common libraries for binaries
/home/shop/config -- configuration files, incl format templates
/home/shop/state -- socket for interprocess communication, log files


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


Access from a class attribute

2009-06-04 Thread Kless
Why can not to access from a class attribute to a function of that
class?

-
class Foo(object):
   attr = __class__.__name__
   attr = self.__class__.__name__
-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Access from a class attribute

2009-06-04 Thread Bruno Desthuilliers

Kless a écrit :

Why can not to access from a class attribute to a function of that
class?

-
class Foo(object):
   attr = __class__.__name__
   attr = self.__class__.__name__
-



"class" is an executable statement that instanciate a new class object 
and bind it to the class name in the current namespace. The class object 
doesn't yet exists when the body of the class statement is eval'd, so 
you can't access it, obviously - nor any of it's instances FWIW.


Also, there's nothing magical wrt/ 'self' - it's just a naming 
convention for the "current instance" argument of functions that are 
intented to be used as methods (Python's 'methods' being just thin 
wrappers around the function, the class and the instance).


If you want to mess with the class attributes, you can either do so 
after the class is created (that is, after the end of the class 
statement's body), or use a custom metaclass.

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


Re: nntplib.NNTPTemporaryError: 441 Article has no body -- just headers

2009-06-04 Thread Jon Bendtsen
Jon Bendtsen wrote:
> Dennis Lee Bieber wrote:
>> On Wed, 27 May 2009 14:25:58 +0200, Jon Bendtsen 
>> declaimed the following in gmane.comp.python.general:
>>
>>> 'From: r...@laerdal.dk\nsubject: testing\nNewsgroups: test\nBody:
>>> \n\n\nfoobar\n\n\n.\n\n\n'
>>>
>>  I believe NNTP, like SMTP, requires \r\n line termination.
> 
> I will try it, but why does it then work when it posts the file? The
> file is a std. unix file, so it should also only have \n line
> termination.

I have now tried adding \r infront of all \n. That didnt help.

I tried making a tempfile.TemporaryFile(dir='/tmp') which did not
work either. Message is still:

nntplib.NNTPTemporaryError: 441 Article has no body -- just headers
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Nigel Rantor
Hendrik van Rooyen wrote:
> 
> If you have any interest, contact me and I will
> send you the source.

Maybe you could tell people what the point is...

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


Re: import sqlite3

2009-06-04 Thread willgun

Andrew McNamara 写道:


On 04/06/2009, at 4:14 PM, willgun wrote:

What did you call the .py file? sqlite3.py? If so, you've just 
imported your own module again. 8-)
After the import, try "print sqlite3.__file__", which will tell you 
where the module came from.



Thank you all the same.
I'm a student from China.It's painful for us to read python 
documentation entirely due to poor english.So I often make these 
mistakes.


Don't worry - even experienced Python coders get caught by this one. 
Just remember the "print module.__file__" trick for next time something 
odd happens.


When you import a module in python, it is only imported the first time 
you request it (which is why your import did not become recursive and 
raise an error).
I know it well now.Thanks.It seems that the mailing list is much greater 
than most forums in China.

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


Re: import sqlite3

2009-06-04 Thread willgun

By the way ,what does 'best regards' means at the end of a mail?
--
http://mail.python.org/mailman/listinfo/python-list


Re: nntplib.NNTPTemporaryError: 441 Article has no body -- just headers

2009-06-04 Thread Jon Bendtsen
Jon Bendtsen wrote:
> Jon Bendtsen wrote:
>> Dennis Lee Bieber wrote:
>>> On Wed, 27 May 2009 14:25:58 +0200, Jon Bendtsen 
>>> declaimed the following in gmane.comp.python.general:
>>>
 'From: r...@laerdal.dk\nsubject: testing\nNewsgroups: test\nBody:
 \n\n\nfoobar\n\n\n.\n\n\n'

>>> I believe NNTP, like SMTP, requires \r\n line termination.
>> I will try it, but why does it then work when it posts the file? The
>> file is a std. unix file, so it should also only have \n line
>> termination.
> 
> I have now tried adding \r infront of all \n. That didnt help.
> 
> I tried making a tempfile.TemporaryFile(dir='/tmp') which did not
> work either. Message is still:
> 
> nntplib.NNTPTemporaryError: 441 Article has no body -- just headers

with the help of #python on Freenode i found the problem. I didnt
seek back to 0. With seeking stringio works, and then i will use that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Hendrik van Rooyen
"Nigel Rantor"  wrote:

> Hendrik van Rooyen wrote:
> > 
> > If you have any interest, contact me and I will
> > send you the source.
> 
> Maybe you could tell people what the point is...

Well its a long story, but you did ask...

I am working on an i/o system, running in an ebox -
it is basically a 486 with 128 meg running slackware,
and its job is to serve as an i/o server, reading inputs
and setting relay and other outputs. A nice slow
300 Mhz machine - not fast at all.

Now the thread that does the i/o gets commands over 
a queue from a socket , so they are strings, and they 
look like this:

"A,10101010,00010010"

where the "A" means its an I/O command, and the
ascii binary is stuff that must be written. 
The response looks the same, but it has more fields
as it reflects both the state of the current inputs and
the current outputs. The responses are written to
an output queue that is serviced by another thread.

Now I did not want to waste any cycles deciding how
to unpack the incoming stuff, so I just use split(',').

So then I wanted to add a command to change masters,
passing an alternate output queue, so that the changeover
is confirmed.

But a queue is not a string, and you can also not pickle it.

So I messed around for a while passing the name of the
new queue, and doing exec magic.  This worked, but only
if the new queue was a global, which was kind of yucky.

So then I started thinking - why can't I just pass a simple 
pointer to the object, and the can was the result.

It is not really something that is all that useful - only if
you want to pass the reference "as part of a string".

It is now working in the ebox too, but it does not look
as good - the strings look like negative numbers, but
they uncan fine.

- Hendrik


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


Re: import sqlite3

2009-06-04 Thread Andrew McNamara


On 04/06/2009, at 9:45 PM, willgun wrote:


By the way ,what does 'best regards' means at the end of a mail?


The correspondent is wishing you well. You'll also see things like  
"kind regards", "best wishes" and so on. "Regard" essentially means  
respect.

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


Re: import sqlite3

2009-06-04 Thread pdpi
On Jun 4, 12:45 pm, willgun  wrote:
> By the way ,what does 'best regards' means at the end of a mail?

"regard" means roughly "care".

Its use as "best regards" closing a letter (or, in this case, email),
means that you care for the person you're saying goodbye to. It's just
a polite way to end a letter :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Nigel Rantor
Hendrik van Rooyen wrote:
> "Nigel Rantor"  wrote:
> 
>> Hendrik van Rooyen wrote:
>>> If you have any interest, contact me and I will
>>> send you the source.
>> Maybe you could tell people what the point is...
> 
> Well its a long story, but you did ask...

[snip]

Maybe I should have said

"why should people care"

or

"why would someone use this"

or

"what problem does this solve"

Your explanation doesn't make a whole lot of sense to me, I'm sure it
does to you.

Why, for example, would someone use your system to pass objects between
processes (I think this is the main thing you are providing?) rather
than POSH or some other system?

Regards,

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


Re: Get the hard disk hardware serial number

2009-06-04 Thread Dietmar Schwertberger

MRAB schrieb:

Jorge wrote:
I need to know how to get the hardware serial number of a hard disk in 
python.



For Windows, see http://www.daniweb.com/forums/thread187326.html


This recipe uses the function GetVolumeInformation(), which does not
return the hardware serial number.

From the microsoft documentation:
 This function returns the volume serial number that the operating
 system assigns when a hard disk is formatted. To programmatically
 obtain the hard disk's serial number that the manufacturer assigns,
 use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia
 property SerialNumber.

The WMI method is e.g. described here:
http://www.velocityreviews.com/forums/t359670-wmi-help.html


import wmi
c = wmi.WMI()
for pm in c.Win32_PhysicalMedia():
print pm.Tag, pm.SerialNumber

or to retrieve the serial number for the installation drive:

serial = c.Win32_PhysicalMedia(["SerialNumber"], 
Tag=r"\\.\PHYSICALDRIVE0")[0].SerialNumber.strip()



Regards,

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


Re: hash and __eq__

2009-06-04 Thread Albert van der Horst
In article <0233137f$0$8244$c3e8...@news.astraweb.com>,
Steven D'Aprano   wrote:
>On Sun, 31 May 2009 12:08:33 +0100, Arnaud Delobelle wrote:
>
>> Anyway, it's good to know that quicksort is O(n^2) in the worst case -
>> and that this worst case can crop up very easily in some situations,
>> especially if not too much care has been taken implementing it.
>
>The naive quicksort algorithm, where you recursively split the list into
>two halves, performs badly if the list is already sorted (or nearly so).

No it isn't (in general).
It only does so if you use the first element as a pivot,
resulting in a set with one element and a set with n-1 elements.
It is an incredible long time ago that someone implemented qsort
this very naive way, especially since in the 80's Knuth warned
about it.

A current naive way is use element (m+n)/2 when sorting the range m..n.
This is nearly optimal for a sorted set. For a random set the chance
for a consistent bad choice is astronomically low. I have a QSORT
implemented in the library for my lina Forth and didn't bother to do
better than this. (Used in some of the demanding problems in
projecteuler.net).

More sophisticated qsorts select three elements at random and use
best of three. The worst behaviour is still O(n^2) but the multiplication
factor is lower and the chance is astronomically low to the third power.

>It's easy to fix that: randomise the list before you sort! You can do
>that with a single pass of the list. That has the advantage of ensuring
>that no specific input can cause degraded performance, so an attacker
>can't DOS your application by passing sorted lists to be sorted.

>Another strategy is to randomly exchange the pivot element when sorting.
 choose?

A good sort utility with facilities for records with fields
(using a modified heap sort, and using disk base merge sort for very
large sets) can be find on my site below (called ssort).
Because of the heap sort even the worst case is O(Nlog(N)) if
the O(N^2) behaviour of qsort is your worry.

>--
>Steven

Groetjes Albert

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

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


Re: What text editor is everyone using for Python

2009-06-04 Thread Albert van der Horst
In article ,
Emile van Sebille   wrote:
>On 6/1/2009 4:57 PM Steven D'Aprano said...
>> Having noted that the word "Quit" does appear, how do you then *actually*
>> Quit? Apart from taunting the user, what is it that Ctrl-G is actually
>> doing when it displays the word "Quit" in what seems to be some sort of
>> status bar?
>
>Ahhh.. memories of discovering that F7 gets you out of WordPerfect...

Memories of Atari 260/520/1040 that had a keyboard with a key actually
marked ... HELP.
(Sometimes hitting it provided you with help...)

>
>Emile
>

Groetjes Albert


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

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


Making the case for repeat

2009-06-04 Thread pataphor
This probably has a snowballs change in hell of ending up in builtins or
even some in some module, but such things should not prevent one to
try and present the arguments for what one thinks is right. Else one
would end up with consequentialism and that way lies madness and
hyperreality.

So here is my proposed suggestion for a once and for all reconciliation
of various functions in itertools that can not stand on their own and
keep a straight face. Because of backwards compatibility issues we
cannot remove them but we can boldly jump forward and include the right
repeat in the builtin namespace, which I think would be the best thing.
Alternatively -- the second best solution -- would be to give this
function its own namespace where it can supersede the old incongruencies
in itertools. Combiniter or combinator?

P.

from itertools import count
from functools import partial

def repeat(iterable, cycles = None, times = 1):
L = []
for x in iterable:
for i in xrange(times):
yield x
L.append(x)
counter = count if cycles is None else partial(xrange,cycles-1)
for _ in counter():
for x in L:
for i in xrange(times):
yield x

def test():
#making the case for repeat
from itertools import islice, cycle
times = 2
n = 3
cycles = 2
L = range(n)
#look ma, no islice!
print list(repeat(L, cycles))
print list(repeat(L, cycles, times))
#repeat without extra args works like itertools.cycle:
print list(islice(repeat(L),len(L)*cycles))
print list(islice(cycle(L),len(L)*cycles))
#enclosing a single item in a list emulates
#itertools.repeat functionality:
print list(repeat(['Mr. Anderson'],cycles))

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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Scott David Daniels

Esmail wrote:

Scott David Daniels wrote:

Esmail wrote:

... Tk seems a bit more complex .. but I really don't know much about
it and its interface with Python to make any sort of judgments as
to which option would be better.


This should look pretty easy:


Thanks Scott for taking the time to share this code with me, it
will give me something to study. I'm not sure if I'd say it looks
easy (but then again I am not very familiar with Tk :-)


I threw in too much, I think.  The reason for the "idle -n" trick is to
share the Tkinter mainloop with idle itself, so you can experiment with
using Tkinter and see the effects of a single command, just after you
type it in (and thus get a nice intuitive feel for the possibilities).

The tricky part to understand is the switch from imperative programming
to interrupt driven programming.  gui stuff has to switch to interrupt
driven so that it can respond to things like mouse drags, windows
repaints, and so on when they happen, rather than when the software
asks for them.  Typically, a GUI program sets some stuff up (in
imperative mode), and then switches to event-driven code and drops into
an event loop.  It is also typical of most GUI programs that the display
manipulating code (the actual painting) must all be done in a single
thread (the display thread) where the events happen.  Small calculations
can occur inside these events, but slow calculations leave your GUI
unresponsive and unable to do things like restore hidden parts as you
drag another window across it.  So, big or slow calculations are either
done in a separate threads (or single calculation thread) that
communicate back with the display thread, or the big slow calculation
are done in bite-sized pieces in the display thread, doing a piece and
moving the rest on to another event.

A Tkinter canvas is nice to use because you can draw things on it, move
those things around (separately or in groups), change their shape or
color, make them visible or invisible, and the canvas keeps track of
drawing them.

import Tkinter as tk

# First set up Tkinter and a root window (idle -n fakes part)
root = tk.Tk()

# Set that window's size (400x400) and loc (5,5)
root.geometry('400x400+5+5')

# make a canvas (2-D drawing area) in that window
canvas = tk.Canvas(root)

#make the canvas fill the window (even as the window is resized)
canvas.pack(expand=1, fill=tk.BOTH)

# draw a red filled oval on the canvas bounded by (50,100), (70,140)
a = canvas.create_oval((50, 100, 70, 140), fill='red')

# the hard-to-get part about the example is that the Mover class
# takes a canvas element or tag (such as a above), and provides a
# couple of methods (.start(event) and .move(event)) for associating
# with the mouse.  ".start" when the button goes down, and ".move"
# as the button is dragged with the mouse down.

canvas.itemconfig(a, fill='#55AADD') # Change to oval to near cyan
canvas.move(a, 5, 5) # move it down right 5 pixels

single_mover = Mover(canvas, a) # associate motion with our oval
# make the "left button down" on canvas call our start method
# which just gives u a point to move around from.
canvas.bind("", single_mover.start)

# make motion while left button is down call our move method.
# there we will compare the mouse position to our saved location,
# move our oval (in this case) a corresponding distance, and
# update the saved location.
canvas.bind("", single_mover.move)

# at this point we have our behavior wired up. If in idle, "just
# try it", but if not:
tk.mainloop() # stays there until you click the close window.

Also, a Tkinter Canvas can be saved as an encapsulated postscript file,
thus allowing you pretty pictures into PDFs.

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


Re: Replacing module with a stub for unit testing

2009-06-04 Thread Fuzzyman
On May 23, 2:00 pm, pigmart...@gmail.com wrote:
> Hi,
>
> I'm working on a unit test framework for a module.  The module I'm
> testing indirectly calls another module which is expensive to access
> --- CDLLs whose functions access a database.
>
>     test_MyModule --->MyModule--->IntermediateModule---
>
> >ExpensiveModule
>
> I want to create a stub of ExpensiveModule and have that be accessed
> by IntermediateModule instead of the real version
>
>     test_MyModule --->MyModule--->IntermediateModule---
>
> >ExpensiveModuleStub
>
> I tried the following in my unittest:
>
>     import ExpensiveModuleStub
>     sys.modules['ExpensiveModule'] = ExpensiveModuleStub # Doesn't
> work
>
> But, import statements in the IntermediateModule still access the real
> ExpensiveModule, not the stub.
>
> The examples I can find of creating and using Mock or Stub objects
> seem to all follow a pattern where the fake objects are passed in as
> arguments to the code being tested.  For example, see the "Example
> Usage" section here:http://python-mock.sourceforge.net.  But that
> doesn't work in my case as the module I'm testing doesn't directly use
> the module that I want to replace.
>
> Can anybody suggest something?
>


My Mock module, and in particular the patch decorator is designed
explicitly to do this.

You need to be careful because modules and module level globals
(including things your module imports) are global state. If you change
them for the purpose of a test you must *guarantee* to restore them
after the test.

http://www.voidspace.org.uk/python/mock/
http://www.voidspace.org.uk/python/mock/patch.html

In your case if you are testing a module which imports ExpensiveModule
then by ExpensiveModule lives in the *namespace of the module under
test*. You could patch it like this:

from mock import patch
import module

@patch('module.ExpensiveModule)
def testModule(self, mockExpensiveModule):


Whilst the test is running 'module' has'ExpensiveModule' mocked out
(replaced) with a Mock instance. This is passed into your test so that
you can setup behaviour and make assertions about how it is used.
After the test is completed the patching is undone.

All the best,


Michael Foord
--
http://www.ironpythoninaction.com/

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


Re: Making the case for repeat

2009-06-04 Thread Steven D'Aprano
On Thu, 04 Jun 2009 13:37:45 +, pataphor wrote:

> This probably has a snowballs change in hell of ending up in builtins or
> even some in some module, but such things should not prevent one to try
> and present the arguments for what one thinks is right. Else one would
> end up with consequentialism and that way lies madness and hyperreality.

It would be cruel of me to say "Too late", so I shall merely ask, what on 
earth are you going on about?

> So here is my proposed suggestion for a once and for all reconciliation
> of various functions in itertools that can not stand on their own and
> keep a straight face. Because of backwards compatibility issues we
> cannot remove them but we can boldly jump forward and include the right
> repeat in the builtin namespace, which I think would be the best thing.


What is "the right repeat"? What's wrong with the old one? If you're 
going to make a proposal, you have to actually *make the proposal* and 
not just say "Here, have some code, now put it in the builtins because 
the rest of itertools is teh suxor!!!". That rarely goes down well.

(I don't know if that's exactly what you're trying to say, but it seems 
that way to me.)

I've run your test code, and I don't know what I'm supposed to be 
impressed by.



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


import _sqlite3 no module named error

2009-06-04 Thread Vincent Davis
I volunteered to help Marijo Mihelčić who was looking for someone with
a mac the help him build a mac binary using py2app for his
simpletasktimer
http://code.google.com/p/simpletasktimer/wiki/Installation

when I try to run his app I get the no module named _sqlite3 , I am
not sure what this is caused by as it looks to me like sqlite3 is
trying to import it. Any idea how to fix this? Other than the obvious
of getting _sqlite3 somehow, or maby it is that simple.

here is what i get

Traceback (most recent call last):
  File 
"/Users/vincentdavis/simpletasktimer-read-only/simpletasktimer/KTaskTimer.py",
line 13, in 
from lib import KTaskEditDialog, KTaskReport, KTaskTimerDB
  File 
"/Users/vincentdavis/simpletasktimer-read-only/simpletasktimer/lib/KTaskReport.py",
line 10, in 
from lib import KTaskTimerDB
  File 
"/Users/vincentdavis/simpletasktimer-read-only/simpletasktimer/lib/KTaskTimerDB.py",
line 8, in 
import sqlite3 as lite
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sqlite3/__init__.py",
line 24, in 
from dbapi2 import *
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sqlite3/dbapi2.py",
line 27, in 
from _sqlite3 import *
ImportError: No module named _sqlite3
logout


Thanks
Vincent Davis
720-301-3003
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import sqlite3

2009-06-04 Thread MRAB

Andrew McNamara wrote:


On 04/06/2009, at 9:45 PM, willgun wrote:


By the way ,what does 'best regards' means at the end of a mail?


The correspondent is wishing you well. You'll also see things like "kind 
regards", "best wishes" and so on. "Regard" essentially means respect.


There's also "high regard", which means "much admiration or respect",
and the phrase "to hold someone in high regard" means to admire and
respect that person greatly, for example, "we hold Guido in high
regard". :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Compiling and transporting modules/libraries in python

2009-06-04 Thread Abe
alex23 -
  Thanks for the tips.  I'm using portable python and it's working
great so far.  I'll come back and try virtualenv if I get stuck again.
- Abe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making the case for repeat

2009-06-04 Thread pataphor
Steven D'Aprano wrote:

> I've run your test code, and I don't know what I'm supposed to be 
> impressed by.

Thank you for trying out the code. That you're unimpressed actually is a
huge encouragement because code should just run the way people expect,
without unnecessary surprises.

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


Re: "Exploding" (**myvariable) a dict with unicode keys

2009-06-04 Thread Aahz
In article ,
Samuel Wan   wrote:
>
>I started using python last week and ran into exceptions thrown when
>unicode dictionary keys are exploded into function arguments. In my
>case, decoded json dictionaries did not work as function arguments.
>There was a thread from Oct 2008
>(http://www.gossamer-threads.com/lists/python/python/684379) about the
>same problem. Here is my workaround:

When posting to threads like this, please make sure to specify which
version(s) of Python you're using -- this is a Python 2.x issue.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face.  It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger."  --Roy Smith, c.l.py, 2004.05.23
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Hendrik van Rooyen
"Nigel Rantor"  wrote:


> Hendrik van Rooyen wrote:
> > "Nigel Rantor"  wrote:
> > 
> >> Hendrik van Rooyen wrote:
> >>> If you have any interest, contact me and I will
> >>> send you the source.
> >> Maybe you could tell people what the point is...
> > 
> > Well its a long story, but you did ask...
> 
> [snip]
> 
> Maybe I should have said
> 
> "why should people care"
> 
> or
> 
> "why would someone use this"
> 
> or
> 
> "what problem does this solve"
> 
> Your explanation doesn't make a whole lot of sense to me, I'm sure it
> does to you.
> 
> Why, for example, would someone use your system to pass objects between
> processes (I think this is the main thing you are providing?) rather
> than POSH or some other system?
> 

I can see that my explanation passes you by completely.

I said, in my original post, that a can could not leave a process.
A can is exactly the same as a C pointer, only its value has been
converted to a string, so that you can pass it "in band" as
part of a string. That is all it does. No more, no less.

Passing it to an outside process makes no sense, unless it points
at an object in shared memory - else it guarantees a segfault.

It is not something that would find common use - in fact, I have
never, until I started struggling with my current problem, ever even
considered the possibility of converting a pointer to a string and 
back to a pointer again, and I would be surprised if anybody else
on this list has done so in the past, in a context other than debugging.

Which is why I posted.  This is supposed to be a newsgroup,
and this is a rare occurrence, and hence news.

- Hendrik


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


Re: Project source code layout?

2009-06-04 Thread Allen Fowler




> > I was hoping to keep the dev layout as close to deployment possible.
> 
> Completely different purposes. For example, the actual production database 
> and config files form no part of your development project, do they? And 
> conversely, utility scripts that might be used, for example, to set up a 
> database, should not be part of the production installation.
> 

The data will certainly be different. Good point.  (Though for now even the 
production DB will be sqlite.)

> Here's one layout I used in a production system for an online shop:
> 
> /home/shop/bin -- binaries (CGI and daemon)
> /home/shop/lib -- common libraries for binaries
> /home/shop/config -- configuration files, incl format templates
> /home/shop/state -- socket for interprocess communication, log files
> 
>

Thank you.

Couple of questions:

1) Do you use virtualpython?

2) How do you load the modules in your lib directory?

3) How do you reference your configuration directives from within your modules 
and CGI/daemon scripts?

Thank you,

Allen


  

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


PEP 315: Enhanced While Loop

2009-06-04 Thread Daniel Cotton
During some random surfing I became interested in the below piece of code:

> while :
> 
> and while :
> 
> and while :
> 
> else: 

It strikes me that the 'and while' syntax has some power that you may
not have considered. Consider that if an 'and while' were to be
executed under some condition:

 if :
 and while :
 

you would then have a live loop condition that applied to subsequent
iterations only if conditionX was met. Under this circumstance I
wondered if you might also want a method of cancelling while
conditions? For example:

  cancel while :
  

where the 'cancel while' command removes conditionY from the list of
conditions that the loop will terminate on. You could then also use
this syntax to cancel the initial while condition.

I also wondered if 'or while' might be better, so that you could add
different types of prefix to a while condition, i.e. 'and while'.

I'm a python novice so this could well be something you want to ignore
but I wasn't doing anything anyway. Let me know what you think.

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


Re: Project source code layout?

2009-06-04 Thread Jean-Paul Calderone

On Thu, 04 Jun 2009 21:33:13 +1200, Lawrence D'Oliveiro 
 wrote:

In message , Allen
Fowler wrote:


I was hoping to keep the dev layout as close to deployment possible.


Completely different purposes. For example, the actual production database
and config files form no part of your development project, do they? And
conversely, utility scripts that might be used, for example, to set up a
database, should not be part of the production installation.


If you don't use the "utility scripts" in your development environment, how
do you know they work with the code you're developing?  If production has a
completely different set of configuration files from your development env,
how do you know how your code will work when combined with them?

Any time there is a difference, there is the potential for bugs which you
cannot catch until you deploy your code, and that's a recipe for failure.

Minimizing differences between development and deployment is a *great*
thing.  There are certainly cases where differences are necessary, but
one should strive to make them the exception, not the rule.

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


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Jean-Paul Calderone

On Thu, 4 Jun 2009 16:49:42 +0200, Hendrik van Rooyen  
wrote:

[snip]

It is not something that would find common use - in fact, I have
never, until I started struggling with my current problem, ever even
considered the possibility of converting a pointer to a string and
back to a pointer again, and I would be surprised if anybody else
on this list has done so in the past, in a context other than debugging.


So, do you mind sharing your current problem?  Maybe then it'll make more
sense why one might want to do this.

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


Feedparser problem

2009-06-04 Thread Jonathan Nelson
I'm trying to add a feedreader element to my django project.  I'm
using Mark Pilgrim's great feedparser library.  I've used it before
without any problems.  I'm getting a TypeError I can't figure out.
I've tried searching google, bing, google groups to no avail.

Here's the dpaste of what I'm trying to do and the result I'm
getting:

http://dpaste.com/51406/

I've tried checking my firewall settings.  I'm using Windows 7 and
Python 2.6.  Win 7 is allowing other Python programs through.  I've
tried several different RSS urls with the same result.

Any thoughts would be greatly appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


multi-core software

2009-06-04 Thread Xah Lee
Of interest:

• Why Must Software Be Rewritten For Multi-Core Processors?
  http://xahlee.org/UnixResource_dir/writ/multi-core_software.html

plain text version follows.

--
Why Must Software Be Rewritten For Multi-Core Processors?

Xah Lee, 2009-06-04

I had a revelation today, namely, that it is necessary to rewrite
software to use multi-processor in order to benefit from it.

This may sound stupid, but is a revelation to me. For the past decade,
the question has been on my mind, about why should software needs to
be rewritten to take advantage of multi-processors. Because, in my
mind, i thought that software are at some fundamental level just
algorithms, and algorithms, have nothing to do with hardware
implementation aspects such as number of processors. I always felt,
that those talks about the need or difficulty of rewriting software
for multi-processor (or multi-core these days) must be the product of
idiocy of industrial imperative coding monkies. In particular, some
languages such as java, the way they deal with it, seems to me
extremely stupid. e.g. the concept of threads. In my mind, there
should be a layer between the software and the hardware, such as the
operating system, or the compiler, that should be able to
automatically handle or compile the software so that it FULLY use the
multi-processors when present. In short, i thought that a algorithm
really has nothing to do with hardware details.

I never really thought hard about this issue, but today, since i got a
quad-core PC, so i looked into the issue, and thought about it, and i
realized the answer. The gist is that, algorithm, fundamentally means
manipulating some hardware, in fact, algorithm is a step by step
instruction about some form of hardware, even the hardware may be
abstract or virtual. For example, let's say the simplest case of 1+1.
It is a algorithm, but where is the hardware? You may say it's
abstract concept, or it being a mathematical model. What you call 1+1
depends on the context, but in our context, those numbers are the
hardware. To see this, lets say more complex example of listing primes
by sieve. Again, it comes down to “what is a number”? Here, numbers
can be stones, or arrangement of beads on abacus, it's hardware! As
another example, say sorting. To begin with, you have to have some
something to sort, that's hardware.

Another way to see this is that, for a given computing problem, there
are infinite number of algorithms to achieve the same thing. Some,
will be better ones, requiring less steps, or requiring less storage.
All these are concrete manipulation issues, and the thing being
manipulated, ultimately we have to call it hardware. So, when hardware
changes, say from one cpu to multi-cpu, there's no way for the
algorithm to magically change and adopt the changed hardware. If you
need a algorithm that is catered to the new hardware, you need a new
algorithm.

One might think that there might be algorithm Omega, such that it
takes input of old hardware O, new hardware N, and a algorithm A, and
output a algorithm B, such that B has the same behavior as A, but N+B
performs better than O+A. This is like asking for Strong AI.

One thing we often forgot is that algorithms ultimately translates to
manipulating hardware. In a modern digital computer, that means
software algorithms ultimately becomes machine instructions in CPU,
which manipulate the 1s and 0s in register, or electricity voltage in
transisters.

In a more mundane point of view, a automatic system for software to
work on multi-processors is a problem of breaking a problem into
discrete units (so that they can be computed in parallel). The problem
of finding a algorithm is entirely different from the problem of
breaking a problem into distinct units. The problem of breaking a
problem into distinct units is a entire new branch of mathematics. For
example, let's say factoring. Factoring is a well defined mathematical
problem. There are millions algorithms to do it, each class has
different properties such as number of steps or storage units.
However, if we look at these algorithms from the point of view of
distinct units, it's a new perspective on classification of
algorithms. Software are in general too ill-defined and fuzzy and
complex, the software we use on personal computers such as email,
browsers, games, don't even have mathematical models. They don't even
have mathematical models of their inputs and outputs. To talk about
automatic system of unitizing software, would be more like a AI
fantasy. Roughly, a term that describes this aspect of research is
Parallel computing.

In the Wikipedia article, it talks about types of parallelism: Bit-
level parallelism, Instruction-level parallelism, Data parallelism,
Task parallelism. Then it also discusses hardware aspects classes:
multicore, symmetric multiprocessing, distributed computing, cluster,
grid. The subjects mentioned there more close to this essay are 

Re: Generating all combinations

2009-06-04 Thread Mensanator
On Jun 4, 1:27 am, Raymond Hettinger  wrote:
> > Nice one!
>
> It only does partitions of a sequence.  I haven't yet looked at a way
> to
> do partitions of a set.  Any ideas?
>
> > Raymond, as perhaps *the* principle contributor to itertools, do you feel
> > that the combinatorics-related tools should be in their own module? Or is
> > that dividing the module too fine?
>
> I like having them in itertools because they form part of the
> "iterator algebra"
> for splitting, filtering, joining, and combining streams of iterators.
>
> > Would you consider moving permutations() and friends into a module
> > together with functions for calculating the number of permutations etc?
>
> > e.g. permutations(iterable[, r]) --> permutations object
> >      nPr(n, r) --> int
>
> Looked at this a while back.  If the nPr style functions go in, they
> will
> probably be in the math module (or a new module for integer functions
> like gcd and whatnot).  The reason for not keeping them together is
> that
> the use cases are likely disjoint -- when I go to my calculator for
> nCr
> I don't expect a listing -- when I need to loop over permutations, I
> typically only care about the contents of the permutation, not the
> total
> number of them (except for purposes of estimating how long a search
> will
> take).  People look to the math module for things they find on their
> calculator and to the itertools module for high-speed looping
> constructs.
>
> Also, there is a issue of naming the functions.  For combinations, you
> you might think to look for ncombs() or somesuch, but
> binomial_coefficient()
> is what someone else may be searching for.
>
> What would be nice is to turn the itertool combinatorics into
> classes with a len() method, an ability to index to the n-th
> iteration, or to find at an arbitrary iteration:
>
> >>> c = combinations('monte', 3)
> >>> len(c)
> 10
> >>> c[2]
> ('m', 'o', 'e')
> >>> c.find(('m', 't', 'e'))
> 5
> >>> random.choice(c)
>
> ('o', 'n', 'e')
>
> If the input iterable contains repeats, the find() method
> would find the first match.
>
> These things are interesting and fun, but they also smell
> of feeping-creaturism.

After all, everybody knows that for m items taken n at
a time, the counts are

perm  w/repl = m**n
comb  w/repl = (m+n-1)!/(n!(m-1)!)
perm wo/repl = m!/(m-n)!
comb wo/repl = m!/(n!(m-n)!)

>
> Raymond

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


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread skip

Hendrik> A can is like a pickle, in that it is a string, but anything
Hendrik> can be canned.  Unlike a pickle, a can cannot leave the
Hendrik> process, though, unless the object it points to lives in shared
Hendrik> memory.

Got some use cases?

Thx,

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
America's vaunted "free press" notwithstanding, story ideas that expose
the unseemly side of actual or potential advertisers tend to fall by the
wayside.  Not quite sure why.  -- Jim Thornton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: accessing the XML attribute value noNamespaceSchemaLocation thru Python 2.5

2009-06-04 Thread Stefan Behnel
tooshiny wrote:
> I am currently successfully using lxml and ElementTree to validate and
> to access the XML contained data. I can however not find any
> functional call to access the schema location ie the attribute value
> noNamespaceSchemaLocation.
> 
> A simple function call would be so much nicer than the other route of
> file reading / string chopping etc

I'm not quite sure what you are trying to do here, but to access an
attribute of an Element object, you can use its get() method, as in

root.get("{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation";)

Or did you mean to do something else?

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


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Terry Reedy

Hendrik van Rooyen wrote:


I can see that my explanation passes you by completely.

I said, in my original post, that a can could not leave a process.
A can is exactly the same as a C pointer, only its value has been
converted to a string, so that you can pass it "in band" as
part of a string. That is all it does. No more, no less.


If I understand correctly, your problem and solution was this:

You have multiple threads within a long running process.  One thread 
repeatedly reads a socket.  You wanted to be able to occasionally send 
an object to that thread.  Rather than rewrite the thread to also poll a 
queue.Queue(), which for CPython sends objects by sending a pointer, you 
converted pointers to strings and sent (multiplex) them via the text 
stream the thread was already reading -- and modified the thread to 
decode and act on the new type of message.


And you are willing to share the can code with someone who has a similar 
rare need and understands the danger of interpreting ints as addresses.


Correct?

tjr

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


"where" in python

2009-06-04 Thread Chris
I am a newby in Python and I'm first looking for equivalent to things
I already manage: IDL.
For example, I want to plot a sub-set of points, selected from a
bigger set by applying some filter. In my example, I want to select
only the values > 0.
I succeed to write 5 different ways to do this, which one is the more
efficient, the quicker, the more Python?
It seems the 4th method give wrong results, but I don't know why...
Thanks for your tips,
Christophe
--
import pylab as pylab
import numpy as numpy
#Read the data
d=pylab.load('Tabla_General2.cvs',comments='#',delimiter=';')
# Define the columns to plot
i_x = 10
i_y = 5

# Select the points to plot, 1rst method
b_where = (d[:,i_x]>0)  & (d[:,i_y]>0)
xb = d[b_where,i_x]
yb = d[b_where,i_y]

# 2nd method
xb2=pylab.compress(b_where,d[:,i_x])
yb2=pylab.compress(b_where,d[:,i_y])

# 3rd method
i_where = pylab.where((d[:,i_x]>0) & (d[:,i_y]>0),1,0)
xi = d[i_where,i_x]
yi = d[i_where,i_y]

# 4th method
xi2=pylab.compress(i_where,d[:,i_x])
yi2=pylab.compress(i_where,d[:,i_y])

#5th method
in_where = numpy.transpose(numpy.where((d[:,i_x]>0) & (d[:,i_y]>0)))
xin = d[in_where,i_x]
yin = d[in_where,i_y]
--
-- 
http://mail.python.org/mailman/listinfo/python-list


python way to automate IE8's File Download dialog

2009-06-04 Thread monogeo
Hello,

I am able to use PAMIE 2.0 to automate IE7's File Download dialog, but
the same approach/code fails on IE8. You can see the details and code
at http://tech.groups.yahoo.com/group/Pamie_UsersGroup/message/675

Please help if you are able to automate IE8's File Download dialog
(with three buttons; Open, Save, Cancel) via python script.

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


Re: Get the hard disk hardware serial number

2009-06-04 Thread Terry Reedy

Dietmar Schwertberger wrote:


The WMI method is e.g. described here:
http://www.velocityreviews.com/forums/t359670-wmi-help.html


import wmi


Not in the stdlib, but available here:
http://pypi.python.org/pypi/WMI/1.3
and requires in turn pywin32:
http://pypi.python.org/pypi/pywin32/210


c = wmi.WMI()
for pm in c.Win32_PhysicalMedia():
print pm.Tag, pm.SerialNumber

or to retrieve the serial number for the installation drive:

serial = c.Win32_PhysicalMedia(["SerialNumber"], 
Tag=r"\\.\PHYSICALDRIVE0")[0].SerialNumber.strip()



Regards,

Dietmar


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


Re: Winter Madness - Passing Python objects as Strings

2009-06-04 Thread Nigel Rantor

Hendrik van Rooyen wrote:

It is not something that would find common use - in fact, I have
never, until I started struggling with my current problem, ever even
considered the possibility of converting a pointer to a string and 
back to a pointer again, and I would be surprised if anybody else

on this list has done so in the past, in a context other than debugging.


Okay, well, I think that's probably because it sounds like a fairly good 
way of making things slower and hard to port to another interpreter. 
Obviously depending on how you're achieving this.


If you need to pass infomation to a thread then I would suggest there's 
better, quicker, more reliable, portable and comprehensible ways of 
doing it.


It just smells to me that you've created this elaborate and brittle hack 
to work around the fact that you couldn't think of any other way of 
getting the thread to change it's behaviour whilst waiting on input.


Just my 0.02p

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


Re: Get the hard disk hardware serial number

2009-06-04 Thread Paul Boddie
On 4 Jun, 11:29, Nick Craig-Wood  wrote:
>
> For linux I'd run this and parse the results.
>
> # smartctl -i /dev/sda

Also useful is hdparm, particularly with the drive identification and
detailed information options shown respectively below:

# hdparm -i /dev/sda
# hdparm -I /dev/sda

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


Re: import _sqlite3 no module named error

2009-06-04 Thread Ned Deily
In article 
<77e831100906040708l1a8bf638n19bbff05607b3...@mail.gmail.com>,
 Vincent Davis  wrote:

> I volunteered to help Marijo Mihelčić who was looking for someone with
> a mac the help him build a mac binary using py2app for his
> simpletasktimer
> http://code.google.com/p/simpletasktimer/wiki/Installation
> 
> when I try to run his app I get the no module named _sqlite3 , I am
> not sure what this is caused by as it looks to me like sqlite3 is
> trying to import it. Any idea how to fix this? Other than the obvious
> of getting _sqlite3 somehow, or maby it is that simple.
> 
> here is what i get

[...]

>   "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/s
>   qlite3/dbapi2.py",
> line 27, in 
> from _sqlite3 import *
> ImportError: No module named _sqlite3
> logout

>From the path names, it appears you are using the MacPorts python2.5.  
Try:

sudo port install py25-sqlite3

which will bring along sqlite3 if not already installed.

It should be that simple.

-- 
 Ned Deily,
 n...@acm.org

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


Re: import _sqlite3 no module named error

2009-06-04 Thread Vincent Davis
>> when I try to run his app I get the no module named _sqlite3 , I am
>> not sure what this is caused by as it looks to me like sqlite3 is
>> trying to import it. Any idea how to fix this? Other than the obvious

>> of getting _sqlite3 somehow, or maby it is that simple
>>   
>> "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/s
>>   qlite3/dbapi2.py",
>> line 27, in 
>>     from _sqlite3 import *
>> ImportError: No module named _sqlite3
>> logout


> >From the path names, it appears you are using the MacPorts python2.5.
> Try:
>
>    sudo port install py25-sqlite3
>
> which will bring along sqlite3 if not already installed.


Yes I am using macports I think sqlite is installed? here is what I
get when I run
sudo port install py25-sqlite3

vincent-daviss-macbook-pro-2:~ vmd$ sudo port install py25-sqlite3
Skipping org.macports.activate (py25-sqlite3 ) since this port is already active
--->  Cleaning py25-sqlite3
vincent-daviss-macbook-pro-2:~ vmd$
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multi-core software

2009-06-04 Thread Kaz Kylheku
["Followup-To:" header set to comp.lang.lisp.]
On 2009-06-04, Roedy Green  wrote:
> On Thu, 4 Jun 2009 09:46:44 -0700 (PDT), Xah Lee 
> wrote, quoted or indirectly quoted someone who said :
>
>>• Why Must Software Be Rewritten For Multi-Core Processors?
>
> Threads have been part of Java since Day 1.

Unfortunately, not sane threads designed by people who actually understand
multithreading.

> The nice thing about Java is whether you are on a single core
> processor or a 256 CPU machine (We got to run our Athena Integer Java
> spreadsheet engine on such a beast), does not concern your code.

You are dreaming if you think that there are any circumstances (other than
circumstances in which performance doesn't matter) in which you don't have to
concern yourself about the difference between a uniprocessor and a 256 CPU
machine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
[Please keep the discussion on the list]

Joseph Garvin schrieb:
> On Thu, Jun 4, 2009 at 3:43 AM, Thomas Heller  wrote:
>> There have been some attempts to use ctypes to access C++ objects.
>> We (Roman Yakovenko and myself) made some progress.  We were able to
>> handle C++ name mangling, the special C++ calling convention,
>> access virtual, non-virtual, overloaded functions, but finally gave up
>> because the binary layout (function tables, member variables, and so on)
>> of C++ objects is way too complicated and undocumented.
> 
> Have you read the book Inside The C++ Object Model?:

I haven't, but considered to buy it ;-)

> http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/ref=sr_1_1?ie=UTF8&s=books&qid=1244139929&sr=8-1
> 
> It's probably out of date now, but I'm about halfway through it and it
> documents a ton of the little unexpected optimizations and such that
> cause the binary layout to be complex. How did you get as far as you
> did without having figured out the layout? (e.g. if you could access
> virtual functions you must have known how to get at the virtual table)

I found a lot of material on the web, also I used the (very good) visual
studio debugger, and finally I did a lot of experimentation.  We were only
able to access some very simple C++ objects.

There is also a patent or patents from MS about the vtable.

All in all, as I said, IMO it is too complicated to figure out the binary
layout of the C++ objects (without using a C++ compiler), also there are
quite some Python packages for accessing them.

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


Re: import _sqlite3 no module named error

2009-06-04 Thread Ned Deily
In article 
<77e831100906041151g70868dbre1546cdb01082...@mail.gmail.com>,
 Vincent Davis  wrote:
> Yes I am using macports I think sqlite is installed? here is what I
> get when I run
> sudo port install py25-sqlite3
> 
> vincent-daviss-macbook-pro-2:~ vmd$ sudo port install py25-sqlite3
> Skipping org.macports.activate (py25-sqlite3 ) since this port is already 
> active
> --->  Cleaning py25-sqlite3
> vincent-daviss-macbook-pro-2:~ vmd$

Hmm!  This is on 10.5.7, BTW.

$ sudo port version
Version: 1.710
$ sudo port info py25-sqlite3
py25-sqlite3 @2.5.4 (python, databases)
[...]
$ /opt/local/bin/python2.5
Python 2.5.4 (r254:67916, May  4 2009, 01:40:08) 
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from _sqlite3 import *
>>> 

A quick web search shows that there were apparently some issues with 
MacPort's py25-sqlite3 in the not too distant past.  Perhaps your ports 
need to be upgraded? 

$ sudo port selfupdate
or
$ sudo port sync

$ sudo port upgrade installed

-- 
 Ned Deily,
 n...@acm.org

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


Is socket.shutdown(1) useless

2009-06-04 Thread Mohamed Garrana
Hello

This is is in answer for Is socket.shutdown(1) useless

Shutdown(1) , forces the socket no to send any more data

This is usefull in

Buffer flushing

Strange error detection

Safe guarding

Let me explain more , when you send a data , it's not guaranteed to be
sent to your peer , it's only guaranteed to be sent to the os buffer ,
which in turn sends it to the peer os buffer

So by doing shutdown(1) , you flush your buffer and an error is raised
if the buffer is not empty ie: data has not been sent to the peer yet

Howoever this is irrevesable , so you can do that after you completely
sent all your data and you want to be sure that it's atleast at the peer
os buffer

 

 

Everything's ok in the end...if it's not ok, then its not the end.

 

P save paper...

 

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Philip Semanchuk


On Jun 4, 2009, at 3:35 PM, Thomas Heller wrote:


[Please keep the discussion on the list]

Joseph Garvin schrieb:
On Thu, Jun 4, 2009 at 3:43 AM, Thomas Heller   
wrote:

There have been some attempts to use ctypes to access C++ objects.
We (Roman Yakovenko and myself) made some progress.  We were able to
handle C++ name mangling, the special C++ calling convention,
access virtual, non-virtual, overloaded functions, but finally  
gave up
because the binary layout (function tables, member variables, and  
so on)

of C++ objects is way too complicated and undocumented.


Have you read the book Inside The C++ Object Model?:

It's probably out of date now, but I'm about halfway through it and it


documents a ton of the little unexpected optimizations and such that
cause the binary layout to be complex. How did you get as far as you
did without having figured out the layout? (e.g. if you could access
virtual functions you must have known how to get at the virtual  
table)


I found a lot of material on the web, also I used the (very good)  
visual
studio debugger, and finally I did a lot of experimentation.  We  
were only

able to access some very simple C++ objects.

There is also a patent or patents from MS about the vtable.

All in all, as I said, IMO it is too complicated to figure out the  
binary
layout of the C++ objects (without using a C++ compiler), also there  
are

quite some Python packages for accessing them.


Hi Thomas,
We're weighing options for accessing C++ objects via Python. I know of  
SIWG and Boost; are there others that you think deserve consideration?


I've been happy with ctypes in my limited exposure to it and would  
love to find a way to make that work. This thread has been very  
interesting to me.


Thanks
Philip








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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Thomas Heller
Philip Semanchuk schrieb:

> Hi Thomas,
> We're weighing options for accessing C++ objects via Python. I know of  
> SIWG and Boost; are there others that you think deserve consideration?

I haven't used any of them myself.  A common suggestion is SIP,
less known are pybindgen and Robin.  But there may be many more,
and others with more experience might have much more to say about them.
Also there is Roman Yokavenko's pygccxml which has a lot of stuff.

> I've been happy with ctypes in my limited exposure to it and would  
> love to find a way to make that work. This thread has been very  
> interesting to me.

Unfortunately there is no solution in sight, with ctypes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import sqlite3

2009-06-04 Thread Gabriel Genellina

En Thu, 04 Jun 2009 03:14:18 -0300, willgun  escribió:

I'm a student from China.It's painful for us to read python  
documentation entirely due to poor english.So I often make these  
mistakes.


Try "chinese python group" at Google - I see some promising results at  
least...


--
Gabriel Genellina

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


Re: Illegal seek with os.popen

2009-06-04 Thread pruebauno
On Jun 3, 3:36 pm, a...@pythoncraft.com (Aahz) wrote:
> In article 
> <7c93031a-235e-4e13-bd37-7c9dbc6e8...@r16g2000vbn.googlegroups.com>,
>
>
>
>   wrote:
> >Should I open a bug report for this?
>
> >Python 2.5.1 (r251:54863, Sep 19 2007, 14:58:06) [C] on aix5
> >Type "help", "copyright", "credits" or "license" for more information.
>  import os
>  os.popen('cat','w')
> >
>
> >Python 3.1rc1 (r31rc1:73054, Jun  1 2009, 10:49:24) [C] on aix5
> >Type "help", "copyright", "credits" or "license" for more information.
>  os.popen('cat','w')
> >Traceback (most recent call last):
> >  File "", line 1, in 
> >  File "/Python-3.1rc1/Lib/os.py", line 641, in popen
> >    return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
> >IOError: [Errno 29] Illegal seek
>
> What happens in 2.6 and 3.0?
> --
> Aahz (a...@pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> "Given that C++ has pointers and typecasts, it's really hard to have a
> serious conversation about type safety with a C++ programmer and keep a
> straight face.  It's kind of like having a guy who juggles chainsaws
> wearing body armor arguing with a guy who juggles rubber chickens wearing
> a T-shirt about who's in more danger."  --Roy Smith, c.l.py, 2004.05.23

Python 2.6.2 (r262:71600, Jun  4 2009, 16:07:26) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.popen('cat','w')

>>>

Python 3.0.1 (r301:69556, Jun  4 2009, 16:07:22) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.popen('cat','w')


So it seems to be something in 3.1 that causes it to fail.
BTW it is not like I use os.popen a lot. I found this problem while
trying to use help().

Python 3.1rc1 (r31rc1:73054, Jun  1 2009, 10:49:24) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> help(open)
Traceback (most recent call last):
  File "", line 1, in 
  File "/Python-3.1rc1/Lib/site.py", line 429, in __call__
return pydoc.help(*args, **kwds)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1709, in __call__
self.help(request)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1756, in help
else: doc(request, 'Help on %s:')
  File "/Python-3.1rc1/Lib/pydoc.py", line 1505, in doc
pager(render_doc(thing, title, forceload))
  File "/Python-3.1rc1/Lib/pydoc.py", line 1320, in pager
pager(text)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1340, in 
return lambda text: pipepager(text, 'less')
  File "/Python-3.1rc1/Lib/pydoc.py", line 1359, in pipepager
pipe = os.popen(cmd, 'w')
  File "/Python-3.1rc1/Lib/os.py", line 641, in popen
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
IOError: [Errno 29] Illegal seek
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Brian
What is the goal of this conversation that goes above and beyond what
Boost.Python + pygccxml achieve? Boost has published a variety of libraries
that will be included into the next c++ standard. It's hard to imagine a
better designed python/c++ interface library than Boost.Python. Further,
pygccxml is amazing with regards to scanning your source code using gcc
itself and then using that xml representation to write out the Boost.Python
code. What more do people in this conversation want?

On Thu, Jun 4, 2009 at 2:07 PM, Thomas Heller  wrote:

> Philip Semanchuk schrieb:
>
> > Hi Thomas,
> > We're weighing options for accessing C++ objects via Python. I know of
> > SIWG and Boost; are there others that you think deserve consideration?
>
> I haven't used any of them myself.  A common suggestion is SIP,
> less known are pybindgen and Robin.  But there may be many more,
> and others with more experience might have much more to say about them.
> Also there is Roman Yokavenko's pygccxml which has a lot of stuff.
>
> > I've been happy with ctypes in my limited exposure to it and would
> > love to find a way to make that work. This thread has been very
> > interesting to me.
>
> Unfortunately there is no solution in sight, with ctypes.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-04 Thread Nick Craig-Wood
Steven D'Aprano  wrote:
>  On Tue, 02 Jun 2009 10:54:48 +1200, Lawrence D'Oliveiro wrote:
> 
> > In message , Albert van der Horst wrote:
> > 
> >> An indication of how one can see one is in emacs is also appreciated.
> > 
> > How about, hit CTRL/G and see if the word "Quit" appears somewhere.
> 
>  Ah, one has to love user interfaces designed with mnemonic keyboard 
>  commands so as to minimize the burden of rote learning on the user. 
>  Presumably it is G for "Get me the frack outta here!".
> 
>  Having noted that the word "Quit" does appear, how do you then *actually* 
>  Quit? Apart from taunting the user, what is it that Ctrl-G is actually 
>  doing when it displays the word "Quit" in what seems to be some sort of 
>  status bar?

I love the idea of emacs taunting the user - it is like all that lisp
code suddenly became self aware and decided to join in ;-)

Ctrl-G is the emacs interrupt sequence.  It cancels what you are
doing.  Kind of like Ctrl-C in the shell.

You quit emacs with Ctrl-X Ctrl-C.

Save a document with Ctrl-X Ctrl-S that that is probably enough for
the emacs survival guide!

If you run emacs in a windowing environment (eg X-Windows or Windows)
you actually get menus you can choose save and quit off!

Anyway, wrenching the thread back on topic - I use emacs for all my
python editing needs (on linux, windows and mac) with pymacs or
python-mode (depending on distro). I use the subversion integration
extensively.

The interactive features of emacs are really useful for testing python
code - even more useful than the python interactive prompt for bits of
code longer than a line or too.  (Open a new window in python mode,
type stuff, press Ctrl-C Ctrl-C and have the output shown in a
different window.  If you messed up, clicking on the error will put
the cursor in the right place in the code).

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Illegal seek with os.popen

2009-06-04 Thread Terry Reedy

prueba...@latinmail.com wrote:


Python 3.0.1 (r301:69556, Jun  4 2009, 16:07:22) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.

import os
os.popen('cat','w')



So it seems to be something in 3.1 that causes it to fail.
BTW it is not like I use os.popen a lot. I found this problem while
trying to use help().

Python 3.1rc1 (r31rc1:73054, Jun  1 2009, 10:49:24) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.

help(open)


On Windows xp, this works fine.


Traceback (most recent call last):
  File "", line 1, in 
  File "/Python-3.1rc1/Lib/site.py", line 429, in __call__
return pydoc.help(*args, **kwds)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1709, in __call__
self.help(request)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1756, in help
else: doc(request, 'Help on %s:')
  File "/Python-3.1rc1/Lib/pydoc.py", line 1505, in doc
pager(render_doc(thing, title, forceload))
  File "/Python-3.1rc1/Lib/pydoc.py", line 1320, in pager
pager(text)
  File "/Python-3.1rc1/Lib/pydoc.py", line 1340, in 
return lambda text: pipepager(text, 'less')
  File "/Python-3.1rc1/Lib/pydoc.py", line 1359, in pipepager
pipe = os.popen(cmd, 'w')
  File "/Python-3.1rc1/Lib/os.py", line 641, in popen
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
IOError: [Errno 29] Illegal seek


Now is the time to file a bug report for this and os.popen problem;)

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


Re: multi-core software

2009-06-04 Thread MRAB

Kaz Kylheku wrote:

["Followup-To:" header set to comp.lang.lisp.]
On 2009-06-04, Roedy Green  wrote:

On Thu, 4 Jun 2009 09:46:44 -0700 (PDT), Xah Lee 
wrote, quoted or indirectly quoted someone who said :


• Why Must Software Be Rewritten For Multi-Core Processors?

Threads have been part of Java since Day 1.


Unfortunately, not sane threads designed by people who actually understand
multithreading.


The nice thing about Java is whether you are on a single core
processor or a 256 CPU machine (We got to run our Athena Integer Java
spreadsheet engine on such a beast), does not concern your code.


You are dreaming if you think that there are any circumstances (other than
circumstances in which performance doesn't matter) in which you don't have to
concern yourself about the difference between a uniprocessor and a 256 CPU
machine.


If you're interested in parallel programming, have a look at Flow-Based
Programming:

http://www.jpaulmorrison.com/fbp/

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Philip Semanchuk


On Jun 4, 2009, at 4:23 PM, Brian wrote:


What is the goal of this conversation that goes above and beyond what
Boost.Python + pygccxml achieve? Boost has published a variety of  
libraries
that will be included into the next c++ standard. It's hard to  
imagine a
better designed python/c++ interface library than Boost.Python.  
Further,
pygccxml is amazing with regards to scanning your source code using  
gcc
itself and then using that xml representation to write out the  
Boost.Python

code. What more do people in this conversation want?


Hi Brian,
I've only experimented with SWIG (and ctypes for wrapping a C  
library). We're not yet sold on using SWIG to wrap our C++ libraries  
and so we're exploring alternatives. Before I started with SWIG, I did  
some research to see what other folks were using. The arguments I  
recall reading that swayed me to try SWIG before Boost were --
- Boost involves more "magic" than SWIG which means it does a bit more  
work for you, but when things go wrong (i.e. interface won't compile  
or doesn't work as expected) it is very difficult to debug.
- Boost-ed code requires a Boost runtime library. This isn't a  
showstopper problem, obviously, but it's a  mark against since it adds  
yet another prerequisite to our install process.

- Boost's generated code can take a long time to compile.

I don't know what pygccxml adds to the equation, so perhaps some of  
those disadvantages disappear with Boost.Python + pygccxml versus just  
plain Boost.Python. If you'd like to expound on this, feel free. I'd  
appreciate the education.


I don't know about what Boost (or any other tool) generates, but the  
interface I got out of SWIG was not pretty. That's no knock on SWIG --  
I'm amazed at what it can do. Nevertheless the generated interface has  
all the charm of a Babelfish translation. I imagine lots of  
autogenerated code looks "babelfish-ed": meaning is preserved, albeit  
crudely, but all the idioms are lost and it's eminently clear that it  
was not created by a native speaker.


Until there's an interface generation tool that can build an interface  
that makes the wrapped library look completely Pythonic, then choosing  
a tool will be a matter of choosing which compromises you want to  
make. As long as that's true, I think there's room for multiple  
library-wrapping packages, just like there's room for more than one  
programming language in the world.


Cheers
Philip







On Thu, Jun 4, 2009 at 2:07 PM, Thomas Heller   
wrote:



Philip Semanchuk schrieb:


Hi Thomas,
We're weighing options for accessing C++ objects via Python. I  
know of
SIWG and Boost; are there others that you think deserve  
consideration?


I haven't used any of them myself.  A common suggestion is SIP,
less known are pybindgen and Robin.  But there may be many more,
and others with more experience might have much more to say about  
them.

Also there is Roman Yokavenko's pygccxml which has a lot of stuff.


I've been happy with ctypes in my limited exposure to it and would
love to find a way to make that work. This thread has been very
interesting to me.


Unfortunately there is no solution in sight, with ctypes.
--
http://mail.python.org/mailman/listinfo/python-list


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


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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Brian
Well you'll just have to try Boost.Python. There is a pygccxml gui gets you
started in about 15 minutes. You'll be able to see how well it groks your
code and what that generated code is.

Boost is the best. People complain about it because they don't understand
C++ templates and they don't realize how incredibly well designed it is.

If you don't know the basics it's this: Boost.Python uses meta template
programming. This allows it to use the C++ preprocessor for what other
binding systems generally write a custom tool for. Long compile times are
not a real problem for anyone - it's easy to set up a compile farm and the
benefit is that your runtime code is blazing fast.

In my opinion Boost is more sophisticated, SWIG, etc.. is more of a hack. Of
course they all help you get the job done.

On Thu, Jun 4, 2009 at 2:54 PM, Philip Semanchuk wrote:

>
> On Jun 4, 2009, at 4:23 PM, Brian wrote:
>
>  What is the goal of this conversation that goes above and beyond what
>> Boost.Python + pygccxml achieve? Boost has published a variety of
>> libraries
>> that will be included into the next c++ standard. It's hard to imagine a
>> better designed python/c++ interface library than Boost.Python. Further,
>> pygccxml is amazing with regards to scanning your source code using gcc
>> itself and then using that xml representation to write out the
>> Boost.Python
>> code. What more do people in this conversation want?
>>
>
> Hi Brian,
> I've only experimented with SWIG (and ctypes for wrapping a C library).
> We're not yet sold on using SWIG to wrap our C++ libraries and so we're
> exploring alternatives. Before I started with SWIG, I did some research to
> see what other folks were using. The arguments I recall reading that swayed
> me to try SWIG before Boost were --
> - Boost involves more "magic" than SWIG which means it does a bit more work
> for you, but when things go wrong (i.e. interface won't compile or doesn't
> work as expected) it is very difficult to debug.
> - Boost-ed code requires a Boost runtime library. This isn't a showstopper
> problem, obviously, but it's a  mark against since it adds yet another
> prerequisite to our install process.
> - Boost's generated code can take a long time to compile.
>
> I don't know what pygccxml adds to the equation, so perhaps some of those
> disadvantages disappear with Boost.Python + pygccxml versus just plain
> Boost.Python. If you'd like to expound on this, feel free. I'd appreciate
> the education.
>
> I don't know about what Boost (or any other tool) generates, but the
> interface I got out of SWIG was not pretty. That's no knock on SWIG -- I'm
> amazed at what it can do. Nevertheless the generated interface has all the
> charm of a Babelfish translation. I imagine lots of autogenerated code looks
> "babelfish-ed": meaning is preserved, albeit crudely, but all the idioms are
> lost and it's eminently clear that it was not created by a native speaker.
>
> Until there's an interface generation tool that can build an interface that
> makes the wrapped library look completely Pythonic, then choosing a tool
> will be a matter of choosing which compromises you want to make. As long as
> that's true, I think there's room for multiple library-wrapping packages,
> just like there's room for more than one programming language in the world.
>
> Cheers
> Philip
>
>
>
>
>
>>
>> On Thu, Jun 4, 2009 at 2:07 PM, Thomas Heller  wrote:
>>
>>  Philip Semanchuk schrieb:
>>>
>>>  Hi Thomas,
 We're weighing options for accessing C++ objects via Python. I know of
 SIWG and Boost; are there others that you think deserve consideration?

>>>
>>> I haven't used any of them myself.  A common suggestion is SIP,
>>> less known are pybindgen and Robin.  But there may be many more,
>>> and others with more experience might have much more to say about them.
>>> Also there is Roman Yokavenko's pygccxml which has a lot of stuff.
>>>
>>>  I've been happy with ctypes in my limited exposure to it and would
 love to find a way to make that work. This thread has been very
 interesting to me.

>>>
>>> Unfortunately there is no solution in sight, with ctypes.
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>>  --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Brian
Just to expound a bit on pygccxml, which really makes boost worth it.
pygccxml enables you to do all of your binding work from within Python. It
calls gccxml, which is an xml backend to gcc that outputs the parse tree in
an xml format. Pygccxml provides a very high level interface between the gcc
xml representation and the pygccxml boost code generator. It's really a
great thing. You just have to learn to read the code that pygccxml outputs
for boost, and then go back to python and use the high level interface to
modify the code that it outputs. It has all sorts of selectors so you can
operate on all code that has certain arbitrary properties all at the same
time.

Play with it before deciding on swig. It's frikkin' cool :)

On Thu, Jun 4, 2009 at 3:01 PM, Brian  wrote:

> Well you'll just have to try Boost.Python. There is a pygccxml gui gets you
> started in about 15 minutes. You'll be able to see how well it groks your
> code and what that generated code is.
>
> Boost is the best. People complain about it because they don't understand
> C++ templates and they don't realize how incredibly well designed it is.
>
> If you don't know the basics it's this: Boost.Python uses meta template
> programming. This allows it to use the C++ preprocessor for what other
> binding systems generally write a custom tool for. Long compile times are
> not a real problem for anyone - it's easy to set up a compile farm and the
> benefit is that your runtime code is blazing fast.
>
> In my opinion Boost is more sophisticated, SWIG, etc.. is more of a hack.
> Of course they all help you get the job done.
>
>
> On Thu, Jun 4, 2009 at 2:54 PM, Philip Semanchuk wrote:
>
>>
>> On Jun 4, 2009, at 4:23 PM, Brian wrote:
>>
>>  What is the goal of this conversation that goes above and beyond what
>>> Boost.Python + pygccxml achieve? Boost has published a variety of
>>> libraries
>>> that will be included into the next c++ standard. It's hard to imagine a
>>> better designed python/c++ interface library than Boost.Python. Further,
>>> pygccxml is amazing with regards to scanning your source code using gcc
>>> itself and then using that xml representation to write out the
>>> Boost.Python
>>> code. What more do people in this conversation want?
>>>
>>
>> Hi Brian,
>> I've only experimented with SWIG (and ctypes for wrapping a C library).
>> We're not yet sold on using SWIG to wrap our C++ libraries and so we're
>> exploring alternatives. Before I started with SWIG, I did some research to
>> see what other folks were using. The arguments I recall reading that swayed
>> me to try SWIG before Boost were --
>> - Boost involves more "magic" than SWIG which means it does a bit more
>> work for you, but when things go wrong (i.e. interface won't compile or
>> doesn't work as expected) it is very difficult to debug.
>> - Boost-ed code requires a Boost runtime library. This isn't a showstopper
>> problem, obviously, but it's a  mark against since it adds yet another
>> prerequisite to our install process.
>> - Boost's generated code can take a long time to compile.
>>
>> I don't know what pygccxml adds to the equation, so perhaps some of those
>> disadvantages disappear with Boost.Python + pygccxml versus just plain
>> Boost.Python. If you'd like to expound on this, feel free. I'd appreciate
>> the education.
>>
>> I don't know about what Boost (or any other tool) generates, but the
>> interface I got out of SWIG was not pretty. That's no knock on SWIG -- I'm
>> amazed at what it can do. Nevertheless the generated interface has all the
>> charm of a Babelfish translation. I imagine lots of autogenerated code looks
>> "babelfish-ed": meaning is preserved, albeit crudely, but all the idioms are
>> lost and it's eminently clear that it was not created by a native speaker.
>>
>> Until there's an interface generation tool that can build an interface
>> that makes the wrapped library look completely Pythonic, then choosing a
>> tool will be a matter of choosing which compromises you want to make. As
>> long as that's true, I think there's room for multiple library-wrapping
>> packages, just like there's room for more than one programming language in
>> the world.
>>
>> Cheers
>> Philip
>>
>>
>>
>>
>>
>>>
>>> On Thu, Jun 4, 2009 at 2:07 PM, Thomas Heller 
>>> wrote:
>>>
>>>  Philip Semanchuk schrieb:

  Hi Thomas,
> We're weighing options for accessing C++ objects via Python. I know of
> SIWG and Boost; are there others that you think deserve consideration?
>

 I haven't used any of them myself.  A common suggestion is SIP,
 less known are pybindgen and Robin.  But there may be many more,
 and others with more experience might have much more to say about them.
 Also there is Roman Yokavenko's pygccxml which has a lot of stuff.

  I've been happy with ctypes in my limited exposure to it and would
> love to find a way to make that work. This thread has been very
> interesting to me.
>

 U

Re: Illegal seek with os.popen

2009-06-04 Thread Aahz
[posted & e-mailed]

In article <5edde6ee-4446-4f53-91ee-ad3aea4b5...@q37g2000vbi.googlegroups.com>,
  wrote:
>
>Python 3.0.1 (r301:69556, Jun  4 2009, 16:07:22) [C] on aix5
>Type "help", "copyright", "credits" or "license" for more information.
 import os
 os.popen('cat','w')
>
>
>So it seems to be something in 3.1 that causes it to fail.
>BTW it is not like I use os.popen a lot. I found this problem while
>trying to use help().

Please file a report on bugs.python.org and send a note to python-dev.

Thanks!
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face.  It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger."  --Roy Smith, c.l.py, 2004.05.23
-- 
http://mail.python.org/mailman/listinfo/python-list


MultiReplace (performance and unordered dicts)

2009-06-04 Thread Joseph Reagle

I have programs that do lots of string-to-string replacements, so I'm trying
to create a speedy implementation (tons of .replace statements has become
unwieldy). My MultiReplace object does as well as the function regexp,
which both do better than the for loop function, any other suggestions?

def multi_repl(text, subs):
for ori, sub in subs:
text = text.replace(ori, sub)
return text


import string
latex_esc_dic = dict(latex_esc)
latex_esc_ori, latex_esc_rep = zip(*latex_esc)
def symbol_replace(match, get=latex_esc_dic.get):
return get(match.group(1), "")
symbol_pattern = re.compile(
"(" + string.join(map(re.escape, latex_esc_ori), "|") + ")"
)

class MultiReplace(object):
"""
Replace multiple instances from a list of ori/rep pairs.
I use an object for performance: compiled regexes persist.
Table is a list of pairs, I have to convert to dict for regex
replace function, don't use a dict natively since they aren't ordered.
"""
def __init__(self, table):
print "initing object"
self.originals, self.replacements = zip(*table)
self.pattern = re.compile(
"(" + string.join(map(re.escape, self.originals), "|") + ")"
)
self.table_dic = dict(table)

def _get_replacement(self, match): # passed match
#print "replacing %s with %s" % (match.group(1),
self.table_dic.get(match.group(1), ""))
return self.table_dic.get(match.group(1), "") # use match to return
replacement

def replace(self, line):
return self.pattern.sub(self._get_replacement, line) # pass
replacement function
mr = MultiReplace(latex_esc)

...

#line = multi_repl(line, latex_esc) # 0.406
#line = symbol_pattern.sub(symbol_replace, line) #0.385
line = mr.replace(line) #0.385


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


Odd closure issue for generators

2009-06-04 Thread Brian Quinlan

This is from Python built from the py3k branch:
>>> c = (lambda : i for i in range(11, 16))
>>> for q in c:
... print(q())
...
11
12
13
14
15
>>> # This is expected
>>> c = (lambda : i for i in range(11, 16))
>>> d = list(c)
>>> for q in d:
... print(q())
...
15
15
15
15
15
>>> # I was very surprised

Looking at the implementation, I see why this happens:
>>> c = (lambda : i for i in range(11, 16))
>>> for q in c:
... print(id(q.__closure__[0]))
...
3847792
3847792
3847792
3847792
3847792
>>> # The same closure is used by every lambda

But it seems very odd to me and it can lead to some problems that are a 
real pain in the ass to debug.


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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Joseph Garvin
On Thu, Jun 4, 2009 at 3:23 PM, Brian  wrote:
> What is the goal of this conversation that goes above and beyond what
> Boost.Python + pygccxml achieve?

I can't speak for others but the reason I was asking is because it's
nice to be able to define bindings from within python. At a minimum,
compiling bindings means a little extra complexity to address with
whatever build tools you're using.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-04 Thread Lawrence D'Oliveiro
In message , Nick Craig-
Wood wrote:

> You quit emacs with Ctrl-X Ctrl-C.

That's "save-buffers-kill-emacs". If you don't want to save buffers, the 
exit sequence is alt-tilde, f, e.

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Joseph Garvin
On Thu, Jun 4, 2009 at 2:35 PM, Thomas Heller  wrote:
> [Please keep the discussion on the list]
>
> All in all, as I said, IMO it is too complicated to figure out the binary
> layout of the C++ objects (without using a C++ compiler), also there are
> quite some Python packages for accessing them.

Requiring that the C++ compiler used to make the dll's/so's to be the
same one Python is compiled with wouldn't be too burdensome would it?
Because then you could have it run a bunch of configure tests to
determine information exposing the layout. I don't know if everything
is testable, but you can for example (I learned this from the object
model book btw) write a C++ program that determines whether the
virtual function table is stored at the beginning or the end of an
object by comparing the address of an object with a virtual function
to the address of its first data member. If they're different, it's
because the vptr is stored there, otherwise it's on the end
(theoretically it could be in the middle but there's no reason for
compiler writers to do that). cpptypes could then use information
generated by tests like this that are run when the interpreter is
compiled.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-04 Thread Lawrence D'Oliveiro
In message , Albert van der Horst wrote:

> Memories of Atari 260/520/1040 that had a keyboard with a key actually
> marked ... HELP.

And the OLPC machines have a key marked "reveal source".

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


Re: Project source code layout?

2009-06-04 Thread Lawrence D'Oliveiro
In message , Allen 
Fowler wrote:

> 1) Do you use virtualpython?

No idea what that is.

> 2) How do you load the modules in your lib directory?

At the beginning of my scripts, I have a sequence like

test_mode = False # True for testing, False for production

if test_mode :
home_dir = "/home/shop-test"
else :
home_dir = "/home/shop"
#end if

sys.path.append(home_dir + "/lib")

import common
[etc]

I have an installation script that looks for that "test_mode = True/False" 
assignment and edits it accordingly. That flag is used to select the top-
level directory (as above) as well as the database name, etc. This allows me 
to run two complete parallel sets of code and data, so I can mess around 
with the testing version without impacting the production system.

> 3) How do you reference your configuration directives from within your
> modules and CGI/daemon scripts?

For my last project using the above system, I used XML as the config file 
format.

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


Re: "where" in python

2009-06-04 Thread Robert Kern

On 2009-06-04 12:53, Chris wrote:

I am a newby in Python and I'm first looking for equivalent to things
I already manage: IDL.
For example, I want to plot a sub-set of points, selected from a
bigger set by applying some filter. In my example, I want to select
only the values>  0.
I succeed to write 5 different ways to do this, which one is the more
efficient, the quicker, the more Python?


You will want to ask numpy questions on the numpy mailing list.

  http://www.scipy.org/Mailing_Lists

The 1st method. The rest just do a lot of extra work or use old APIs.


It seems the 4th method give wrong results, but I don't know why...
Thanks for your tips,
Christophe
--
import pylab as pylab
import numpy as numpy


You don't need to use "as" unless if you are renaming the modules.

--
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: Odd closure issue for generators

2009-06-04 Thread Gabriel Genellina
En Thu, 04 Jun 2009 18:40:07 -0300, Brian Quinlan   
escribió:



This is from Python built from the py3k branch:


It's not new; same thing happens with 2.x

A closure captures (part of) the enclosing namespace, so names are  
resolved in that environment even after the enclosing block has finished  
execution.
As always, the name->value evaluation happens when it is required at  
runtime, not earlier ("late binding"). So, in the generator expression  
(lambda : i for i in range(11, 16)) the 'i' is searched in the enclosing  
namespace when the lambda is evaluated, not when the lambda is created.


Your first example:


[1] >>> c = (lambda : i for i in range(11, 16))
[2] >>> for q in c:
[3] ... print(q())
...
11
12
13
14
15
 >>> # This is expected


This code means:

[1] create a generator expression and name it `c`
[2] ask `c` for an iterator. A generator is its own iterator, so returns  
itself.
Loop begins: Ask the iterator a new item (the first one, in fact). So the  
generator executes one cycle, yields a lambda expression, and freezes.  
Note that the current value of `i` (in that frozen environment) is 11.  
Name the yielded object (the lambda) `q`.
[3] Call the q object. That means, execute the lambda. It returns the  
current value of i, 11. Print it.
Back to [2]: ask the iterator a new item. The generator resumes, executes  
another cycle, yields another lambda expression, and freezes. Now, i is 12  
inside the frozen environment.

[3] execute the lambda -> 12
etc.

Your second example:


[4] >>> c = (lambda : i for i in range(11, 16))
[5] >>> d = list(c)
[6] >>> for q in d:
[7] ... print(q())
...
15
15
15
15
15
 >>> # I was very surprised


[4] creates a generator expression same as above.
[5] ask for an iterator (c itself). Do the iteration NOW until exhaustion,  
and collect each yielded object into a list. Those objects will be  
lambdas. The current (and final) value of i is 15, because the range()  
iteration has finished.

[6] iterate over the list...
[7] ...and execute each lambda. At this time, `i` is always 15.



Looking at the implementation, I see why this happens:
 >>> c = (lambda : i for i in range(11, 16))
 >>> for q in c:
... print(id(q.__closure__[0]))
...
3847792
3847792
3847792
3847792
3847792
 >>> # The same closure is used by every lambda


...because all of them refer to the same `i` name.

But it seems very odd to me and it can lead to some problems that are a  
real pain in the ass to debug.


Yes, at least if one is not aware of the consequences. I think this (or a  
simpler example) should be explained in the FAQ. The question comes in  
this list again and again...


--
Gabriel Genellina

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


Re: Using C++ and ctypes together: a vast conspiracy? ;)

2009-06-04 Thread Scott David Daniels

Joseph Garvin wrote:

On Thu, Jun 4, 2009 at 2:35 PM, Thomas Heller  wrote:

[Please keep the discussion on the list]

All in all, as I said, IMO it is too complicated to figure out the binary
layout of the C++ objects (without using a C++ compiler), also there are
quite some Python packages for accessing them.


Requiring that the C++ compiler used to make the dll's/so's to be the
same one Python is compiled with wouldn't be too burdensome would it?

And what gave you then impression that Python is compiled with a C++
compiler?

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


Re: Odd closure issue for generators

2009-06-04 Thread Carl Banks
On Jun 4, 2:40 pm, Brian Quinlan  wrote:
> This is from Python built from the py3k branch:
>  >>> c = (lambda : i for i in range(11, 16))
>  >>> for q in c:
> ...     print(q())
> ...
> 11
> 12
> 13
> 14
> 15
>  >>> # This is expected
>  >>> c = (lambda : i for i in range(11, 16))
>  >>> d = list(c)
>  >>> for q in d:
> ...     print(q())
> ...
> 15
> 15
> 15
> 15
> 15
>  >>> # I was very surprised
>
> Looking at the implementation, I see why this happens:
>  >>> c = (lambda : i for i in range(11, 16))
>  >>> for q in c:
> ...     print(id(q.__closure__[0]))
> ...
> 3847792
> 3847792
> 3847792
> 3847792
> 3847792
>  >>> # The same closure is used by every lambda
>
> But it seems very odd to me and it can lead to some problems that are a
> real pain in the ass to debug.

It's really the only sane way to handle it, odd though it may seem in
this narrow case.  In Python nested functions have to be able to
reference the current value of a variable because of use cases like
this:

def func():
def printx():
print x
x = 1
printx()
x = 2
printx()

Referencing a nonlocal variable always uses the current (or last)
value of that variable, not the value it had when the nested function
was defined.


The way to handle the issue you are seeing is to create a new scope
with a variable the remains at the value you want to close upon:

create_const_function(value):
def func():
return value
c = (create_const_function(i) for i in range(11, 16))

Or you can do it the slacker way and use a default argument:

c = (lambda i=i: i for i in range(11, 16))


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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Peter Pearson
On Thu, 04 Jun 2009 03:29:42 -0500, Nick Craig-Wood wrote:
[snip]
> Here is a demo with pygame...
[snip]

And just for completeness, here is a demo with PyGUI, written
in similar style.  (I'm a PyGUI newbie, so constructive criticism
would be appreciated.)

from GUI import Window, View, application, Color, Task
from random import randrange

class Brownian( View ):

  def __init__( self, **kwargs ):
View.__init__( self, **kwargs )
width, height = self.size
self.dots = [ (randrange(width), randrange(height))
  for _ in range( 100 ) ]

  def step( self ):
self.dots = [ (dot[0]+randrange(-1,2), dot[1]+randrange(-1,2))
  for dot in self.dots ]
self.invalidate()

  def draw( self, canvas, rectangle ):
canvas.set_backcolor( Color( 0, 0, 0 ) )
canvas.set_forecolor( Color( 0.3, 0.85, 0.25 ) )
radius = 10
canvas.erase_rect( rectangle )
for dot in self.dots:
  canvas.stroke_oval( ( dot[0]-radius, dot[1]-radius,
dot[0]+radius, dot[1]+radius ) )


def main():

  size = 640, 480
  win = Window( size = size, title = "A test of PyGUI" )
  b = Brownian( size = size )
  win.add( b )
  win.show()
  t = Task( b.step, interval = 0.02, repeat = True, start = True )
  application().run()
  
if __name__ == "__main__":
main()


-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Odd closure issue for generators

2009-06-04 Thread Scott David Daniels

Brian Quinlan wrote:

This is from Python built from the py3k branch:
 >>> c = (lambda : i for i in range(11, 16))
 >>> for q in c:
... print(q())
...
11
12
13
14
15
 >>> # This is expected
 >>> c = (lambda : i for i in range(11, 16))
 >>> d = list(c)
 >>> for q in d:
... print(q())
...
15
15
15
15
15
 >>> # I was very surprised


You are entitled to be surprised.  Then figure out what is going on.
Hint: it is the moral equivalent of what is happening here:

>>> c = []
>>> for i in range(11, 16):
c.append(lambda: i)

>>> i = 'Surprise!'
>>> print([f() for f in c])
['Surprise!', 'Surprise!', 'Surprise!', 'Surprise!', 'Surprise!']

>>> i = 0
>>> print([f() for f in c])
[0, 0, 0, 0, 0]

The body of your lambda is an un-evaluated expression with a reference,
not an expression evaluated at the time of loading c.  TO get what you
expected, try this:

>>> c = []
>>> for i in range(11, 16):
c.append(lambda i=i: i)

>>> i = 'Surprise!'
>>> print([f() for f in c])
[11, 12, 13, 14, 15]

When you evaluate a lambda expression, the default args are evaluated,
but the expression inside the lambda body is not.  When you apply that
evaluated lambda expression, the expression inside the lambda body is
is evaluated and returned.

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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Esmail

Nick Craig-Wood wrote:


Here is a demo with pygame...



Thanks Nick, I'll be studying this too :-)

Esmail

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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Esmail

Scott David Daniels wrote:

Esmail wrote:

Scott David Daniels wrote:

Esmail wrote:

... Tk seems a bit more complex .. but I really don't know much about
it and its interface with Python to make any sort of judgments as
to which option would be better.


This should look pretty easy:


Thanks Scott for taking the time to share this code with me, it
will give me something to study. I'm not sure if I'd say it looks
easy (but then again I am not very familiar with Tk :-)


I threw in too much, I think. 


:-)

Tk seems quite capable, perhaps a bit more than I need for my
simple visualization task at the moment (visualizing a PSO), but
I'll probably investigate it when I have more ambitious GUI needs
or am doing more than plotting the movement of particles on a 2D
surface.

Thanks again for sharing your code and explanations with me, a
great way to learn.

Esmail


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


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Esmail

Peter Pearson wrote:

On Thu, 04 Jun 2009 03:29:42 -0500, Nick Craig-Wood wrote:
[snip]

Here is a demo with pygame...

[snip]

And just for completeness, here is a demo with PyGUI, written
in similar style.  


Thanks for this too!

Esmail

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


Re: import _sqlite3 no module named error

2009-06-04 Thread Vincent Davis
On Thu, Jun 4, 2009 at 1:41 PM, Ned Deily  wrote:
> In article
> <77e831100906041151g70868dbre1546cdb01082...@mail.gmail.com>,
>  Vincent Davis  wrote:
>> Yes I am using macports I think sqlite is installed? here is what I
>> get when I run
>> sudo port install py25-sqlite3
>>
>> vincent-daviss-macbook-pro-2:~ vmd$ sudo port install py25-sqlite3
>> Skipping org.macports.activate (py25-sqlite3 ) since this port is already
>> active
>> --->  Cleaning py25-sqlite3
>> vincent-daviss-macbook-pro-2:~ vmd$
>
> Hmm!  This is on 10.5.7, BTW.
>
> $ sudo port version
> Version: 1.710
> $ sudo port info py25-sqlite3
> py25-sqlite3 @2.5.4 (python, databases)
> [...]
> $ /opt/local/bin/python2.5
> Python 2.5.4 (r254:67916, May  4 2009, 01:40:08)
> [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
 from _sqlite3 import *

>
> A quick web search shows that there were apparently some issues with
> MacPort's py25-sqlite3 in the not too distant past.  Perhaps your ports
> need to be upgraded?
>
> $ sudo port selfupdate
> or
> $ sudo port sync
>
> $ sudo port upgrade installed

Also 10.5.7 my self, I have completely removed and reinstall macport
and all the ports. All befor posting this thread...

here is what i get, jhgfjhgfjhg it is working now, I have no clue. I
went on a bike ride and now it works? Thanks for you help.

What do you know about python_select, .bash_profile and .profile?
-- 
http://mail.python.org/mailman/listinfo/python-list


Printing list/tuple elements on separate lines

2009-06-04 Thread Johnny Chang
I have a large list of strings that I am unpacking and splitting, and
I want each one to be on a new line.  Someone showed me how to do it
and I got it working, except it is not printing each on its own
separate line as his did, making it incredibly hard to read.  He did
it without adding a new line for anything.  I can't get in touch with
him right now.

An example:

recs =
'asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf'
[(rec.split('f')) for rec in recs]

output:

[['asd', 'asd', 'asd', 'asd', 'asd', ''], ['asd', 'asd', 'asd', 'asd',
'asd', ''], ['asd', 'asd', 'asd', 'asd', 'asd', '']]

desired output:

[['asd', 'asd', 'asd', 'asd', 'asd', '']
['asd', 'asd', 'asd', 'asd', 'asd', '']
['asd', 'asd', 'asd', 'asd', 'asd', '']]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing list/tuple elements on separate lines

2009-06-04 Thread Stephen Hansen
On Thu, Jun 4, 2009 at 5:37 PM, Johnny Chang  wrote:

> I have a large list of strings that I am unpacking and splitting, and
> I want each one to be on a new line.  Someone showed me how to do it
> and I got it working, except it is not printing each on its own
> separate line as his did, making it incredibly hard to read.  He did
> it without adding a new line for anything.  I can't get in touch with
> him right now.
>

How about--

import pprint
> recs = 'asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf'
> pprint.pprint([(rec.split('f')) for rec in recs])
>

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


unladen swallow: python and llvm

2009-06-04 Thread Luis M . González
I am very excited by this project (as well as by pypy) and I read all
their plan, which looks quite practical and impressive.
But I must confess that I can't understand why LLVM is so great for
python and why it will make a difference.

AFAIK, LLVM is alot of things at the same time (a compiler
infrastructure, a compilation strategy, a virtual instruction set,
etc).
I am also confussed at their use of the term "jit" (is LLVM a jit? Can
it be used to build a jit?).
Is it something like the .NET or JAVA jit? Or it can be used to
implement a custom jit (ala psyco, for example)?

Also, as some pypy folk said, it seems they intend to do "upfront
compilation". How?
Is it something along the lines of the V8 javascript engine (no
interpreter, no intermediate representation)?
Or it will be another interpreter implementation? If so, how will it
be any better...?

Well, these are a lot of questions and they only show my confussion...
I would highly appreciate if someone knowledgeable sheds some light on
this for me...

Thanks in advance!
Luis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-04 Thread Emile van Sebille

On 6/4/2009 3:19 PM Lawrence D'Oliveiro said...

In message , Nick Craig-
Wood wrote:


You quit emacs with Ctrl-X Ctrl-C.


That's "save-buffers-kill-emacs". If you don't want to save buffers, the 
exit sequence is alt-tilde, f, e.



Ha ha ha ha ha!

No -- really?

Emile

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


Yet another unicode WTF

2009-06-04 Thread Ron Garret
Python 2.6.2 on OS X 10.5.7:

[...@mickey:~]$ echo $LANG
en_US.UTF-8
[...@mickey:~]$ cat frob.py 
#!/usr/bin/env python
print u'\u03BB'

[...@mickey:~]$ ./frob.py 
ª
[...@mickey:~]$ ./frob.py > foo
Traceback (most recent call last):
  File "./frob.py", line 2, in 
print u'\u03BB'
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03bb' in 
position 0: ordinal not in range(128)


(That's supposed to be a small greek lambda, but I'm using a 
brain-damaged news reader that won't let me set the character encoding.  
It shows up correctly in my terminal.)

According to what I thought I knew about unix (and I had fancied myself 
a bit of an expert until just now) this is impossible.  Python is 
obviously picking up a different default encoding when its output is 
being piped to a file, but I always thought one of the fundamental 
invariants of unix processes was that there's no way for a process to 
know what's on the other end of its stdout.

Clues appreciated.  Thanks.

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


__file__ access extremely slow

2009-06-04 Thread Zac Burns
The section of code below, which simply gets the __file__ attribute of
the imported modules, takes more than 1/3 of the total startup time.
Given that many modules are complicated and even have dynamic
population this figure seems very high to me. it would seem very high
if one just considered the time it would take to load the pyc files
off the disk vs... whatever happens when module.__file__ happens.

The calculation appears to be cached though, so a subsequent check
does not take very long.

>From once python starts and loads the main module to after all the
imports occur and this section executes takes 1.3sec. This section
takes 0.5sec. Total module count is ~800.

Python version is 2.5.1

Code:

for module in sys.modules:
try:
path = module.__file__
except (AttributeError, ImportError):
return




--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-04 Thread Ben Finney
Emile van Sebille  writes:

> On 6/4/2009 3:19 PM Lawrence D'Oliveiro said...
> > In message , Nick Craig-
> > Wood wrote:
> >
> >> You quit emacs with Ctrl-X Ctrl-C.
> >
> > That's "save-buffers-kill-emacs". If you don't want to save buffers,
> > the exit sequence is alt-tilde, f, e.

This is an invocation of the menu system, driven by the keyboard. (Also,
it's not Alt+tilde (which would be Alt+Shift+`), it's Alt+` i.e. no
Shift.) It's an alternate command, and IMO is just adding confusion to
the discussion.

> Ha ha ha ha ha!
> 
> No -- really?

Not really. If you don't want to save buffers, you just answer “No”
when prompted by ‘save-buffers-kill-emacs’, so Ctrl+X Ctrl+C is all you
need to know to exit Emacs.

-- 
 \“Consider the daffodil. And while you're doing that, I'll be |
  `\  over here, looking through your stuff.” —Jack Handey |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


ctype question

2009-06-04 Thread Amit Gupta
Hi,

I have been using ctype.cdll to load a library, but I am unable to
figure out how to load multiple libraries that depends on each other.
E.g. I have two libraries A.so and B.so. A.so has some undefined
references, and those symbols are defined in B.so.

When I try to load ctypes.cdll.LoadLibrary("A.so"), it gives errors
about the undefined Symbols. Even if I load B.so before loading A.so,
the error remains the same (which is expected). Can someone help me to
find out, how to load A.so by telling it to look for undefined symbols
in B.so as well?

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


Re: __file__ access extremely slow

2009-06-04 Thread Zac Burns
Sorry, there is a typo. The code should read as below to repro the problem:


for module in sys.modules.itervalues():
   try:
   path = module.__file__
   except (AttributeError, ImportError):
   return




--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jun 4, 2009 at 6:24 PM, Zac Burns  wrote:
> The section of code below, which simply gets the __file__ attribute of
> the imported modules, takes more than 1/3 of the total startup time.
> Given that many modules are complicated and even have dynamic
> population this figure seems very high to me. it would seem very high
> if one just considered the time it would take to load the pyc files
> off the disk vs... whatever happens when module.__file__ happens.
>
> The calculation appears to be cached though, so a subsequent check
> does not take very long.
>
> From once python starts and loads the main module to after all the
> imports occur and this section executes takes 1.3sec. This section
> takes 0.5sec. Total module count is ~800.
>
> Python version is 2.5.1
>
> Code:
> 
> for module in sys.modules:
>        try:
>                path = module.__file__
>        except (AttributeError, ImportError):
>                return
> 
>
>
>
> --
> Zachary Burns
> (407)590-4814
> Aim - Zac256FL
> Production Engineer (Digital Overlord)
> Zindagi Games
>
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I continue after the error?

2009-06-04 Thread chad
Let's say I have a list of 5 files. Now lets say that one of the files
reads nude333.txt instead of nude3.txt. When this happens, the program
generates an error and quits. What I want it to do is just skip over
the bad file and continue on with the next one.  Below is the actual
code. It only works on the local bbs that I hang out on. Sorry. I
couldn't think of the simple case that could isolate my problem.

#!/usr/bin/python

#The original code was written by Mr.Pardue, which sounds a lot like
#fuck you

import telnetlib, os
import time
import glob

my_porn_collection = ["nude.txt", "nude2.txt", "nude333.txt",
  "nude4.txt", "nude5.txt"]

path = 'porn/'

#my_porn_collection = []

def get_files():
for infile in glob.glob( os.path.join(path, '*.txt*')):
my_porn_collection.append(infile)

def generate(some_naked_bitches):
try:
f = open(some_naked_bitches, "r")
except:
pass
art = f.read()
f.close()
return art

def get_name():
user = raw_input("\nUsername: ")
password = raw_input("Password: ")
enter_party(user, password)

def enter_party(user, password):
now = telnetlib.Telnet("m-net.arbornet.org")
now.read_until("login: ")
print "\nEntered username.."
now.write(user + "\n")
now.read_until("Password:")
now.write(password + "\n" )
print "Entered pass.."
now.read_until("m-net%")
now.write("party\n")
print "Entered into party.."
scroll_some_porn(now)

def scroll_some_porn(now):
while 1:
for some_titty_porn in my_porn_collection:
now.write(" \n")
bitch_please = generate(some_titty_porn)
now.write(bitch_please)
time.sleep(10)
now.write(" \n")

if __name__ == "__main__":
#get_files()
get_name()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Odd closure issue for generators

2009-06-04 Thread Brian Quinlan

Gabriel Genellina wrote:
En Thu, 04 Jun 2009 18:40:07 -0300, Brian Quinlan  
escribió:



This is from Python built from the py3k branch:


It's not new; same thing happens with 2.x

A closure captures (part of) the enclosing namespace, so names are 
resolved in that environment even after the enclosing block has finished 
execution.
As always, the name->value evaluation happens when it is required at 
runtime, not earlier ("late binding"). So, in the generator expression 
(lambda : i for i in range(11, 16)) the 'i' is searched in the enclosing 
namespace when the lambda is evaluated, not when the lambda is created.


OK, I talked myself into agreeing that it is better for the generator 
comprehension to share a context amongst every enclosed generator 
expression (rather than having one context per generator expression) for 
consistency reasons.


Thanks!

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


Re: What text editor is everyone using for Python

2009-06-04 Thread greg

Albert van der Horst wrote:


Memories of Atari 260/520/1040 that had a keyboard with a key actually
marked ... HELP.


Modern day Mac keyboards have one of those, too.

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


Re: unladen swallow: python and llvm

2009-06-04 Thread Jesse Noller
You can email these questions to the unladen-swallow mailing list.
They're very open to answering questions.

2009/6/4 Luis M. González :
> I am very excited by this project (as well as by pypy) and I read all
> their plan, which looks quite practical and impressive.
> But I must confess that I can't understand why LLVM is so great for
> python and why it will make a difference.
>
> AFAIK, LLVM is alot of things at the same time (a compiler
> infrastructure, a compilation strategy, a virtual instruction set,
> etc).
> I am also confussed at their use of the term "jit" (is LLVM a jit? Can
> it be used to build a jit?).
> Is it something like the .NET or JAVA jit? Or it can be used to
> implement a custom jit (ala psyco, for example)?
>
> Also, as some pypy folk said, it seems they intend to do "upfront
> compilation". How?
> Is it something along the lines of the V8 javascript engine (no
> interpreter, no intermediate representation)?
> Or it will be another interpreter implementation? If so, how will it
> be any better...?
>
> Well, these are a lot of questions and they only show my confussion...
> I would highly appreciate if someone knowledgeable sheds some light on
> this for me...
>
> Thanks in advance!
> Luis
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making the case for repeat

2009-06-04 Thread Gabriel Genellina

En Thu, 04 Jun 2009 10:37:45 -0300, pataphor  escribió:


So here is my proposed suggestion for a once and for all reconciliation
of various functions in itertools that can not stand on their own and
keep a straight face. Because of backwards compatibility issues we
cannot remove them but we can boldly jump forward and include the right
repeat in the builtin namespace, which I think would be the best thing.
Alternatively -- the second best solution -- would be to give this
function its own namespace where it can supersede the old incongruencies
in itertools. Combiniter or combinator?


Ok, you're proposing a "bidimensional" repeat. I prefer to keep things
simple, and I'd implement it in two steps. First, something similar to
your repeat_each function in another post:

py> thing = ['1','2','3','4']
py> chain.from_iterable(repeat(elem, 3) for elem in thing)

py> list(_)
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4']

Note that this doesn't require any additional storage. Second step would
be to build a bidimensional repeat:

py> one = chain.from_iterable(repeat(elem, 3) for elem in thing)
py> two = chain.from_iterable(tee(one, 2))
py> list(two)
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '1', '1',
'1', '2',
  '2', '2', '3', '3', '3', '4', '4', '4']

Short and simple, but this one requires space for one complete run (3*4
items in the example).
Another variant that only requires space for freezing the original
iterable (4 items in the example) is:


thing = ['1','2','3','4']
items = list(thing) # ok, silly in this case, but not for a generic  
iterable
chain.from_iterable(chain.from_iterable(repeat(elem, 3) for elem in  
items) f

or rownumber in range(2))


list(_)

['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '1', '1',
'1', '2',
  '2', '2', '3', '3', '3', '4', '4', '4']

All of them run at full speed, using the optimized itertools machinery
written in C.

--
Gabriel Genellina

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


Re: Yet another unicode WTF

2009-06-04 Thread Lawrence D'Oliveiro
In message , Ron 
Garret wrote:

> Python 2.6.2 on OS X 10.5.7:

Same result, Python 2.6.1-3 on Debian Unstable. My $LANG is en_NZ.UTF-8.

> ... I always thought one of the fundamental
> invariants of unix processes was that there's no way for a process to
> know what's on the other end of its stdout.

Well, there have long been functions like isatty(3). That's probably what's 
involved here.

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


Re: 2d barcode library?

2009-06-04 Thread Trevor
>
> Christian
>
> [1]https://cybernetics.hudora.biz/projects/wiki/huBarcode

Thanks guys! huBarcode will work..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Yet another unicode WTF

2009-06-04 Thread Ben Finney
Ron Garret  writes:

> According to what I thought I knew about unix (and I had fancied myself 
> a bit of an expert until just now) this is impossible.  Python is 
> obviously picking up a different default encoding when its output is 
> being piped to a file, but I always thought one of the fundamental 
> invariants of unix processes was that there's no way for a process to 
> know what's on the other end of its stdout.

It certainly can. If you're using GNU and a terminal that declares
support for colour, examine the difference between these two:

$ ls --color=auto
$ ls --color=auto > foo ; cat foo

> Clues appreciated.  Thanks.

Research ‘man 3 isatty’ for the function most commonly used to determine
whether a file descriptor represents a terminal.

-- 
 \   “If you are unable to leave your room, expose yourself in the |
  `\window.” —instructions in case of fire, hotel, Finland |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I continue after the error?

2009-06-04 Thread Ben Finney
chad  writes:

> Let's say I have a list of 5 files. Now lets say that one of the files
> reads nude333.txt instead of nude3.txt. When this happens, the program
> generates an error and quits. What I want it to do is just skip over
> the bad file and continue on with the next one.

Have you worked thoroughly through the Python tutorial
http://docs.python.org/tutorial/>? You will exercise many
fundamental concepts, including the exception handling system.

> Below is the actual code. It only works on the local bbs that I hang
> out on. Sorry. I couldn't think of the simple case that could isolate
> my problem.

You achieve this by one of two methods:

* start with a program that does extermely little, and add as little as
  possible until you have a very small program which demonstrates the
  behaviour. Once you have this, continue below.

* remove apparently-irrelevant parts from the program until the
  behaviour stops occuring; the most recent removal, then, is at least
  related to the behaviour. Add it back in.

Repeat these until you have aprogram which can't have any part of it
removed without losing the behaviour that's confusing you. Once you have
that, post it here with your question.

-- 
 \“Good morning, Pooh Bear”, said Eeyore gloomily. “If it is a |
  `\   good morning”, he said. “Which I doubt”, said he. —A. A. Milne, |
_o__)_Winnie-the-Pooh_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >