Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Jack Diederich
On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote:
> Hi,
>
> I'm just wondering why <, <=, >=, and > are not supported by
> collections.OrderedDict:
>
>    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3)))
>    >>> d2 = d1.copy()
>    >>> d2["z"] = 4
>    >>> d1 == d2
>    False
>    >>> d1 < d2
>    Traceback (most recent call last):
>    File "", line 1, in 
>        d1 < d2
>    TypeError: unorderable types: OrderedDict() < OrderedDict()
>
> It just seems to me that since the items in ordered dictionaries are
> ordered, it would make sense to do an item by item comparison from
> first to last item in exactly the same way that Python compares lists
> or tuples?

>>> import this
In the face of ambiguity, refuse the temptation to guess.

It isn't an OrderedDict thing, it is a comparison thing.  Two regular
dicts also raise an error if you try to LT them.  What does it mean
for a dict to be greater than or less than its peer?  Nothing, so we
refuse to guess.

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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Mark
On 16 July, 08:12, Jack Diederich  wrote:
> On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote:
> > Hi,
>
> > I'm just wondering why <, <=, >=, and > are not supported by
> > collections.OrderedDict:
>
> >    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3)))
> >    >>> d2 = d1.copy()
> >    >>> d2["z"] = 4
> >    >>> d1 == d2
> >    False
> >    >>> d1 < d2
> >    Traceback (most recent call last):
> >    File "", line 1, in 
> >        d1 < d2
> >    TypeError: unorderable types: OrderedDict() < OrderedDict()
>
> > It just seems to me that since the items in ordered dictionaries are
> > ordered, it would make sense to do an item by item comparison from
> > first to last item in exactly the same way that Python compares lists
> > or tuples?
> >>> import this
>
> In the face of ambiguity, refuse the temptation to guess.
>
> It isn't an OrderedDict thing, it is a comparison thing.  Two regular
> dicts also raise an error if you try to LT them.  What does it mean
> for a dict to be greater than or less than its peer?  Nothing, so we
> refuse to guess.
>
> -Jack

You are right that it doesn't make sense to compare two dicts.

But OrderedDicts can be viewed logically as lists of (key,value)
tuples so they are much more like lists or tuples when it comes to
comparisons.

For example:

>>> l = [("a", 1), ("z", 2), ("k", 3)]
>>> l1 = l[:]
>>> l1[1] = ("z", 5)
>>> l < l1
True

But if you do:

>>> d = collections.OrderedDict(l)
>>> d1 = collections.OrderedDict(l1)

You can't use <, <=, =>, or >, even though ordered dictionaries
preserve the order and their items are just as comparable as those in
a list or tuple of tuples.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread John Nagle

akhil1988 wrote:

Sorry, it is sgmllib.py and not sgmmlib.py


   Oh, that bug again.  See

http://bugs.python.org/issue1651995

It's a bug in SGMLParser.  When Python 2.5 restricted ASCII to 0..127,
SGMLParser needed to be modified, but wasn't.

I reported that bug in February 2007.  It was fixed in
Python 2.6 and 3.0 on March 31, 2009.

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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Chris Rebert
On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield wrote:
> Hi,
>
> I'm just wondering why <, <=, >=, and > are not supported by
> collections.OrderedDict:
>
>    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3)))
>    >>> d2 = d1.copy()
>    >>> d2["z"] = 4
>    >>> d1 == d2
>    False
>    >>> d1 < d2
>    Traceback (most recent call last):
>    File "", line 1, in 
>        d1 < d2
>    TypeError: unorderable types: OrderedDict() < OrderedDict()
>
> It just seems to me that since the items in ordered dictionaries are
> ordered, it would make sense to do an item by item comparison from
> first to last item in exactly the same way that Python compares lists
> or tuples?

Use case? I'm curious.

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Hendrik van Rooyen
"Hrvoje Niksic"  wrote:


> Note that in Python A or B is in fact not equivalent to not(not A and
> not B).

De Morgan would turn in his grave.

- Hendrik


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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Steven D'Aprano
On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote:

> On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield
> wrote:
>> Hi,
>>
>> I'm just wondering why <, <=, >=, and > are not supported by
>> collections.OrderedDict:
>>
>>    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 =
>>    >>> d1.copy()
>>    >>> d2["z"] = 4
>>    >>> d1 == d2
>>    False
>>    >>> d1 < d2
>>    Traceback (most recent call last):
>>    File "", line 1, in 
>>        d1 < d2
>>    TypeError: unorderable types: OrderedDict() < OrderedDict()
>>
>> It just seems to me that since the items in ordered dictionaries are
>> ordered, it would make sense to do an item by item comparison from
>> first to last item in exactly the same way that Python compares lists
>> or tuples?
> 
> Use case? I'm curious.


Surely it would be the same use case as for comparing two lists? There 
doesn't need to be a special "OrderedDict use case" beyond "an 
OrderedDict is just like a list of (key,value) tuples, but searches are 
faster".

Or maybe not. If OrderedDicts are sequences as well as mappings, then we 
should be able to sort them. And that seems a bit much even for me.



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


Re: allowing output of code that is unittested?

2009-07-16 Thread Ulrich Eckhardt
per wrote:
> i am using the standard unittest module to unit test my code. my code
> contains several print statements which i noticed are repressed when i
> call my unit tests using:
> 
> if __name__ == '__main__':
> suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode)
> unittest.TextTestRunner(verbosity=2).run(suite)

I have here

   if __name__ == '__main__':
   unittest.main()

preceded by classes which derive from unittest.TestCase. Running this allows
me to write to stdout using print as usual, even though it's ugly because
it clutters the output of the testcases.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Steven D'Aprano
On Thu, 16 Jul 2009 09:43:53 +0200, Hendrik van Rooyen wrote:

> "Hrvoje Niksic"  wrote:
> 
> 
>> Note that in Python A or B is in fact not equivalent to not(not A and
>> not B).
> 
> De Morgan would turn in his grave.

No he wouldn't. Python isn't Boolean algebra, and there is no requirement 
to limit Python's operators to the semantics of Boolean algebra.



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


EuroPython 2009: Making 50 Mio. EUR per year using Python

2009-07-16 Thread eGenix Team: M.-A. Lemburg
Now available as video...

http://www.egenix.com/company/news/EuroPython-2009-Lightning-Talk.html

Enjoy,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jul 16 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/


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


Memory leak involving traceback objects

2009-07-16 Thread Rotem
Hi,

I'm debugging a nasty memory leak in a framework written in Python
(v2.6.2).
After much digging around I found that the entire object group that is
leaking is held by a frame object which is subsequently held by a
traceback object.

Traversing the get_referrers() of each traceback frame leads
eventually to a root traceback frame which has no referrers
(gc.get_referrers returns an empty list).

However, this traceback object seems not to be picked by the garbage
collector, and is still there even after many iterations and calls to
gc.collect(). The code location to which the traceback frame points
doesn't do anything special - it just catches an exception, without
saving the exception itself and/or traceback anywhere.

Before I dive into the Python code itself - does anyone have any idea
what can cause this?

Thanks,

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


Re: How to Force exiting from program/script

2009-07-16 Thread Piet van Oostrum
> Alex  (A) a écrit:

>A> hi at all,
>A>  I have made a script with a while loop and I want that after 30
>A> seconds the program stop and exit . But the code like this doesn't
>A> run:
>A> In the Console I can see work so that function is correctly called...

>A> #Function to exit
>A> def exit():
>A> print "work"
>A> raise SystemExit()
>A> t = threading.Timer(30.0, exit)
>A> t.start()

>A> # Loop
>A> while True:
>A> ...many lines

This code gives you a bit more control as it doesn't just force a system
exit, but allows you to continue some other work after aborting the
loop:

import signal, os
from threading import Timer

signalcode = signal.SIGALRM
class AlarmError(Exception):
pass

def handler(signum, frame):
raise AlarmError, "command lasts too long"

signal.signal(signalcode, handler)

def interrupt():
os.kill(os.getpid(), signalcode)

def execute(function, timeout):
Timer(timeout, interrupt).start()
try:
function()
except AlarmError, e:
print e
print 'Execution aborted'

def long_function():
while True:
pass

print "The everlasting command"
execute(long_function, 10)
print "The End"

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Lino Mastrodomenico
2009/7/16 Hendrik van Rooyen :
> "Hrvoje Niksic"  wrote:
>
>
>> Note that in Python A or B is in fact not equivalent to not(not A and
>> not B).
>
> De Morgan would turn in his grave.

If this can make him happier, in Python (not (not a and not b)) *is*
equivalent to bool(a or b). (Modulo crazy things like redefining
"bool" or having a __bool__ with side effects.)

In the first expression you implicitly request a bool because you use
"not", in the second one you do this with an explicit "bool".

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant

Nobody wrote:

On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote:

  

So if I resume:
- not 'foo' => False
- 'foo' or 'foo' => 'foo'

I may be missing something, but honestly, Guido must have smoked some 
heavy stuff to write such logic, has he ?



Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or"
returns the first element which is considered true while "and" returns the
last element provided that all preceding elements are considered true.
  
[snip]
  


Ok then, why "or" does not return True, if the first element is 
considered True ? Why returning the element itself. Any reason for that 
? Because it's confusing, maybe people used to that logic find it 
obvious, but I really don't.


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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread Piet van Oostrum
> akhil1988  (a) wrote:

>a> Chris,

>a> Using 

>a> print (u'line: %s' % line).encode('utf-8')

>a> the 'line' gets printed, but actually this print statement I was using just
>a> for testing, actually my code operates on 'line', on which I use line =
>a> line.decode('utf-8') as 'line' is read as bytes from a stream.

>a> And if I use line = line.encode('utf-8'), 

>a> I start getting other error like
>a> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4561:
>a> ordinal not in range(128)
>a> at line = line.replace('<<', u'«').replace('>>', u'»')

You do a Unicode replace here, so line should be a unicode string.
Therefore you have to do this before the line.encode('utf-8'), but after
the decode('utf-8'). 

It might be better to use different variables for Unicode strings and
byte code strings to prevent confusion, like:

'line' is read as bytes from a stream
uline = line.decode('utf-8')
uline = uline.replace('<<', u'«').replace('>>', u'»')
line = uline.encode('utf-8')
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


How to search this newsgroup by a python script.

2009-07-16 Thread Helmut Jarausch

Hi,

I haven't found anything with Google's group search, so let me
ask it (again?).

How can I search this newsgroup from within a Python script.
(Perhaps by searching Google Groups or Gmane by some Python code.)

Many thanks for a hint,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Piet van Oostrum
> Mark  (M) wrote:

>M> You are right that it doesn't make sense to compare two dicts.

>M> But OrderedDicts can be viewed logically as lists of (key,value)
>M> tuples so they are much more like lists or tuples when it comes to
>M> comparisons.

>M> For example:

> l = [("a", 1), ("z", 2), ("k", 3)]
> l1 = l[:]
> l1[1] = ("z", 5)
> l < l1
>M> True

>M> But if you do:

> d = collections.OrderedDict(l)
> d1 = collections.OrderedDict(l1)

>M> You can't use <, <=, =>, or >, even though ordered dictionaries
>M> preserve the order and their items are just as comparable as those in
>M> a list or tuple of tuples.

But why should the order be as if the OrderedDict was a list of tuples.
A dict can be considered as a mapping and then you might want to treat
either the key or the value as contravariant (the key I guess). So there
is ambiguity. Why would the view as a list of tuples for the ordering be
the `natural' view? 

Maybe you may expect some kind of monotonicity such that d1
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Bug By Any Other Name ...

2009-07-16 Thread rwwh
On Jul 7, 2:00 pm, Steven D'Aprano  wrote:
> On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote:
> >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op.
> [...]
> > Well, yes, but when would you apply it twice in a row?
>
> My point was that unary + isn't a no-op, and therefore neither is ++. For
> Decimal, I can't think why you'd want to apply ++x, but for other
> objects, who knows?
>
> Here's a toy example:
>
> >>> class Spam(str):
>
> ...     def __pos__(self):
> ...         return self.__class__("spam " + self)
> ...>>> s = Spam("")
> >>> s
>
> 'spam spam spam spam '

Here's another toy example:

class Toy(int):
def __init__(self, value):
self._incrd = False
int.__init__(self, value)

def incrHalf(self):
self._incrd = True

def __pos__(self):
if self._incrd:
return self.__class__(self+1)
else:
p = self.__class__(self)
p.incrHalf()
return p

def __add__(self, other):
return self.__class__(int(self)+other)

nows122[126]~% python -i toy.py
>>> i=Toy(5)
>>> +i
5
>>> ++i
6
>>> +++i
6
>>> +i++i
10
>>> +(+i++i)
10
>>> (++i)++i
11
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to search this newsgroup by a python script.

2009-07-16 Thread Chris Rebert
On Thu, Jul 16, 2009 at 2:12 AM, Helmut
Jarausch wrote:
> Hi,
>
> I haven't found anything with Google's group search, so let me
> ask it (again?).
>
> How can I search this newsgroup from within a Python script.
> (Perhaps by searching Google Groups or Gmane by some Python code.)

1. Generate URL of the form:
http://search.gmane.org/?query=foo&group=gmane.comp.python.general
where "foo" is the search terms, with proper URL escaping applied.
2. Fetch URL using urllib - http://docs.python.org/library/urllib.html
3. Parse resulting HTML page (e.g. using BeautifulSoup)
4. Extract desired information from search results using the parse tree.
5. ???
6. Profit!

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


Re: How to search this newsgroup by a python script.

2009-07-16 Thread Tim Golden

Chris Rebert wrote:

On Thu, Jul 16, 2009 at 2:12 AM, Helmut
Jarausch wrote:

Hi,

I haven't found anything with Google's group search, so let me
ask it (again?).

How can I search this newsgroup from within a Python script.
(Perhaps by searching Google Groups or Gmane by some Python code.)


1. Generate URL of the form:
http://search.gmane.org/?query=foo&group=gmane.comp.python.general
where "foo" is the search terms, with proper URL escaping applied.
2. Fetch URL using urllib - http://docs.python.org/library/urllib.html
3. Parse resulting HTML page (e.g. using BeautifulSoup)
4. Extract desired information from search results using the parse tree.
5. ???
6. Profit!


Alternatively, you could do something with the mailing list archive:

 http://mail.python.org/pipermail/python-list/

The .gz files are gzipped mbox format so can be dropped into, eg,
Thunderbird for offline browsing, or searched with the email
package from the stdlib.

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


using timers to force an execution time

2009-07-16 Thread superpollo

hello.

based upon previuos suggestions, i tried to write a program in which i 
would like to have a certain code fragment to execute only for a 
specified amount of time (say 3 seconds), then bail out.


but i get the following:

$ cat tmr004.py
#!/usr/bin/python -u

import threading , time

e = threading.Event()
t = threading.Timer(3.0, e.set)
t.start()
print time.asctime(time.localtime(time.time()))
while not e.isSet():
for repeat in range(10):
print time.time()
time.sleep(0.66)
print time.asctime(time.localtime(time.time()))
$ ./tmr004.py
Thu Jul 16 12:31:27 2009
1247740287.44
1247740288.1
1247740288.76
1247740289.42
1247740290.08
1247740290.74
1247740291.4
1247740292.06
1247740292.72
1247740293.38
Thu Jul 16 12:31:34 2009
$

see? the while body ran for about 7 seconds... i bet it has to do with 
the fact that the timer does not control inner loops... any suggestion?


$ python -V
Python 2.3.4
$ uname -a
Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux
$

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


Re: using timers to force an execution time

2009-07-16 Thread Xavier Ho
On Thu, Jul 16, 2009 at 6:34 PM, superpollo  wrote:

> hello.
>
> based upon previuos suggestions, i tried to write a program in which i
> would like to have a certain code fragment to execute only for a specified
> amount of time (say 3 seconds), then bail out.
>
> 


> see? the while body ran for about 7 seconds... i bet it has to do with the
> fact that the timer does not control inner loops... any suggestion?
>
>
I'm guessing it has to do with a sleep of 0.66 seconds x 10 times = sleep
for 6.6 seconds, and then it checks for e.isSet().

Best regards,

Ching-Yun "Xavier" Ho, Technical Artist

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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Mark
On 16 July, 10:21, Piet van Oostrum  wrote:
> > Mark  (M) wrote:
> >M> You are right that it doesn't make sense to compare two dicts.
> >M> But OrderedDicts can be viewed logically as lists of (key,value)
> >M> tuples so they are much more like lists or tuples when it comes to
> >M> comparisons.
> >M> For example:
> > l = [("a", 1), ("z", 2), ("k", 3)]
> > l1 = l[:]
> > l1[1] = ("z", 5)
> > l < l1
> >M> True
> >M> But if you do:
> > d = collections.OrderedDict(l)
> > d1 = collections.OrderedDict(l1)
> >M> You can't use <, <=, =>, or >, even though ordered dictionaries
> >M> preserve the order and their items are just as comparable as those in
> >M> a list or tuple of tuples.
>
> But why should the order be as if the OrderedDict was a list of tuples.
> A dict can be considered as a mapping and then you might want to treat
> either the key or the value as contravariant (the key I guess). So there
> is ambiguity. Why would the view as a list of tuples for the ordering be
> the `natural' view?
>
> Maybe you may expect some kind of monotonicity such that d1 d1[x] 2:5}. So maybe there is only a partial ordering?

OK, that seems to me to be a convincing argument against supporting
ordering.
-- 
http://mail.python.org/mailman/listinfo/python-list


turtle dump

2009-07-16 Thread superpollo

hi there.

is there a way to dump the content of a turtle window to a file or a 
file object?


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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Mark
On 16 July, 08:51, Steven D'Aprano
 wrote:
> On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote:
> > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield
> > wrote:
> >> Hi,
>
> >> I'm just wondering why <, <=, >=, and > are not supported by
> >> collections.OrderedDict:
>
> >>    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 =
> >>    >>> d1.copy()
> >>    >>> d2["z"] = 4
> >>    >>> d1 == d2
> >>    False
> >>    >>> d1 < d2
> >>    Traceback (most recent call last):
> >>    File "", line 1, in 
> >>        d1 < d2
> >>    TypeError: unorderable types: OrderedDict() < OrderedDict()
>
> >> It just seems to me that since the items in ordered dictionaries are
> >> ordered, it would make sense to do an item by item comparison from
> >> first to last item in exactly the same way that Python compares lists
> >> or tuples?
>
> > Use case? I'm curious.
>
> Surely it would be the same use case as for comparing two lists? There
> doesn't need to be a special "OrderedDict use case" beyond "an
> OrderedDict is just like a list of (key,value) tuples, but searches are
> faster".
>
> Or maybe not. If OrderedDicts are sequences as well as mappings, then we
> should be able to sort them. And that seems a bit much even for me.
>
> --
> Steven

One thing that I've just noticed is that you can use <, <=, >=, and >
with sets:

>>> s1 = {1,2,3}
>>> s2 = {1,2,4}
>>> s1 == s2
False
>>> s1 < s2
False
>>> s1 <= s2
False
>>> s2 < s1
False
>>> s2 <= s1
False
>>> s1 != s2
True

It seems a bit inconsistent that with sets you always get False when
using an ordering operator but with an ordered dict you get an
exception?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using timers to force an execution time

2009-07-16 Thread Diez B. Roggisch
superpollo wrote:

> hello.
> 
> based upon previuos suggestions, i tried to write a program in which i
> would like to have a certain code fragment to execute only for a
> specified amount of time (say 3 seconds), then bail out.
> 
> but i get the following:
> 
> $ cat tmr004.py
> #!/usr/bin/python -u
> 
> import threading , time
> 
> e = threading.Event()
> t = threading.Timer(3.0, e.set)
> t.start()
> print time.asctime(time.localtime(time.time()))
> while not e.isSet():
>  for repeat in range(10):
>  print time.time()
>  time.sleep(0.66)
> print time.asctime(time.localtime(time.time()))
> $ ./tmr004.py
> Thu Jul 16 12:31:27 2009
> 1247740287.44
> 1247740288.1
> 1247740288.76
> 1247740289.42
> 1247740290.08
> 1247740290.74
> 1247740291.4
> 1247740292.06
> 1247740292.72
> 1247740293.38
> Thu Jul 16 12:31:34 2009
> $
> 
> see? the while body ran for about 7 seconds... i bet it has to do with
> the fact that the timer does not control inner loops... any suggestion?

Of course the inner loop isn't affected by the set event - how should it be,
if you don't check it.

if you rewrite it as this:

while True:
for repeat in range(10):
if e.isSet():
   break
print time.time()
time.sleep(.66)

it should terminate earlier.

What you can't achieve in python without major black magic hackery that is
very dangerous is to terminate a thread while it is executing. Termination
has to be co-operative.

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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Mark
On 16 July, 11:58, Mark  wrote:
> On 16 July, 08:51, Steven D'Aprano
>
>
>
>  wrote:
> > On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote:
> > > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield
> > > wrote:
> > >> Hi,
>
> > >> I'm just wondering why <, <=, >=, and > are not supported by
> > >> collections.OrderedDict:
>
> > >>    >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 =
> > >>    >>> d1.copy()
> > >>    >>> d2["z"] = 4
> > >>    >>> d1 == d2
> > >>    False
> > >>    >>> d1 < d2
> > >>    Traceback (most recent call last):
> > >>    File "", line 1, in 
> > >>        d1 < d2
> > >>    TypeError: unorderable types: OrderedDict() < OrderedDict()
>
> > >> It just seems to me that since the items in ordered dictionaries are
> > >> ordered, it would make sense to do an item by item comparison from
> > >> first to last item in exactly the same way that Python compares lists
> > >> or tuples?
>
> > > Use case? I'm curious.
>
> > Surely it would be the same use case as for comparing two lists? There
> > doesn't need to be a special "OrderedDict use case" beyond "an
> > OrderedDict is just like a list of (key,value) tuples, but searches are
> > faster".
>
> > Or maybe not. If OrderedDicts are sequences as well as mappings, then we
> > should be able to sort them. And that seems a bit much even for me.
>
> > --
> > Steven
>
> One thing that I've just noticed is that you can use <, <=, >=, and >
> with sets:
>
> >>> s1 = {1,2,3}
> >>> s2 = {1,2,4}
> >>> s1 == s2
> False
> >>> s1 < s2
> False
> >>> s1 <= s2
> False
> >>> s2 < s1
> False
> >>> s2 <= s1
> False
> >>> s1 != s2
>
> True
>
> It seems a bit inconsistent that with sets you always get False when
> using an ordering operator but with an ordered dict you get an
> exception?

Ooops---disregard the above---I forgot that these do subset and
superset comparisions!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread akhil1988

I have switched to python 3.1 , but now I am getting some syntax errors in
the code:

File "./customWikiExtractor.py", line 81
__char_entities =  {' '   :u'\u00A0', '¡' :u'\u00A1',
'¢':u'\u00A2',
^
SyntaxError: invalid syntax

line 81 is:
__char_entities =  {' '   :u'\u00A0', '¡' :u'\u00A1', '¢'   
:u'\u00A2',
'£'  :u'\u00A3', '¤':u'\u00A4', '¥'
:u'\u00A5',
'¦' :u'\u00A6', '§'  :u'\u00A7', '¨'
:u'\u00A8',
'©'   :u'\u00A9', 'ª'  :u'\u00AA',
'«'   :u'\u00AB',
'¬':u'\u00AC', '­'   :u'\u00AD', '®'
:u'\u00AE',
'¯'   :u'\u00AF', '°'   :u'\u00B0',
'±'  :u'\u00B1',
'²'   :u'\u00B2', '³'  :u'\u00B3',
'´'   :u'\u00B4',
'µ'  :u'\u00B5', '¶'  :u'\u00B6',
'·'  :u'\u00B7',
'¸'  :u'\u00B8', '¹'  :u'\u00B9',
'º':u'\u00BA',}

--Akhil


John Nagle-2 wrote:
> 
> akhil1988 wrote:
>> Sorry, it is sgmllib.py and not sgmmlib.py
> 
> Oh, that bug again.  See
> 
>   http://bugs.python.org/issue1651995
> 
> It's a bug in SGMLParser.  When Python 2.5 restricted ASCII to 0..127,
> SGMLParser needed to be modified, but wasn't.
> 
> I reported that bug in February 2007.  It was fixed in
> Python 2.6 and 3.0 on March 31, 2009.
> 
>   John Nagle
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514309.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread akhil1988

Please click reply on the post and then read this reply in the editor.
Actually, some sequences have been replaced to their graphical form when
this post is published. So the python code is being displayed, what actually
it is not.

--Akhil



akhil1988 wrote:
> 
> I have switched to python 3.1 , but now I am getting some syntax errors in
> the code:
> 
> File "./customWikiExtractor.py", line 81
> __char_entities =  {' '   :u'\u00A0', '¡' :u'\u00A1',
> '¢':u'\u00A2',
> ^
> SyntaxError: invalid syntax
> 
> line 81 is:
> __char_entities =  {' '   :u'\u00A0', '¡' :u'\u00A1', '¢'   
> :u'\u00A2',
> '£'  :u'\u00A3', '¤':u'\u00A4',
> '¥' :u'\u00A5',
> '¦' :u'\u00A6', '§'  :u'\u00A7',
> '¨' :u'\u00A8',
> '©'   :u'\u00A9', 'ª'  :u'\u00AA',
> '«'   :u'\u00AB',
> '¬':u'\u00AC', '­'   :u'\u00AD',
> '®' :u'\u00AE',
> '¯'   :u'\u00AF', '°'   :u'\u00B0',
> '±'  :u'\u00B1',
> '²'   :u'\u00B2', '³'  :u'\u00B3',
> '´'   :u'\u00B4',
> 'µ'  :u'\u00B5', '¶'  :u'\u00B6',
> '·'  :u'\u00B7',
> '¸'  :u'\u00B8', '¹'  :u'\u00B9',
> 'º':u'\u00BA',}
> 
> --Akhil
> 
> 
> John Nagle-2 wrote:
>> 
>> akhil1988 wrote:
>>> Sorry, it is sgmllib.py and not sgmmlib.py
>> 
>> Oh, that bug again.  See
>> 
>>  http://bugs.python.org/issue1651995
>> 
>> It's a bug in SGMLParser.  When Python 2.5 restricted ASCII to 0..127,
>> SGMLParser needed to be modified, but wasn't.
>> 
>> I reported that bug in February 2007.  It was fixed in
>> Python 2.6 and 3.0 on March 31, 2009.
>> 
>>  John Nagle
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514367.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: using timers to force an execution time

2009-07-16 Thread superpollo

Diez B. Roggisch wrote:

What you can't achieve in python without major black magic hackery that is
very dangerous is to terminate a thread while it is executing. Termination
has to be co-operative.


i see. thanks a lot.

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


Re: using timers to force an execution time

2009-07-16 Thread Diez B. Roggisch
Diez B. Roggisch wrote:

> superpollo wrote:
> 
>> hello.
>> 
>> based upon previuos suggestions, i tried to write a program in which i
>> would like to have a certain code fragment to execute only for a
>> specified amount of time (say 3 seconds), then bail out.
>> 
>> but i get the following:
>> 
>> $ cat tmr004.py
>> #!/usr/bin/python -u
>> 
>> import threading , time
>> 
>> e = threading.Event()
>> t = threading.Timer(3.0, e.set)
>> t.start()
>> print time.asctime(time.localtime(time.time()))
>> while not e.isSet():
>>  for repeat in range(10):
>>  print time.time()
>>  time.sleep(0.66)
>> print time.asctime(time.localtime(time.time()))
>> $ ./tmr004.py
>> Thu Jul 16 12:31:27 2009
>> 1247740287.44
>> 1247740288.1
>> 1247740288.76
>> 1247740289.42
>> 1247740290.08
>> 1247740290.74
>> 1247740291.4
>> 1247740292.06
>> 1247740292.72
>> 1247740293.38
>> Thu Jul 16 12:31:34 2009
>> $
>> 
>> see? the while body ran for about 7 seconds... i bet it has to do with
>> the fact that the timer does not control inner loops... any suggestion?
> 
> Of course the inner loop isn't affected by the set event - how should it
> be, if you don't check it.
> 
> if you rewrite it as this:
> 
> while True:
> for repeat in range(10):
> if e.isSet():
>break
> print time.time()
> time.sleep(.66)
> 
> it should terminate earlier.

This is of course wrong, remove the outer "while"

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


Re: using timers to force an execution time

2009-07-16 Thread superpollo

Diez B. Roggisch wrote:

superpollo wrote:

see? the while body ran for about 7 seconds... i bet it has to do with
the fact that the timer does not control inner loops... any suggestion?



Of course the inner loop isn't affected by the set event - how should it be,
if you don't check it.

if you rewrite it as this:

while True:
for repeat in range(10):
if e.isSet():
   break
print time.time()
time.sleep(.66)

it should terminate earlier.


so it seems almost impossible to allocate a certain amount of time to a 
code fragment *without* somehow rewriting its logic?


am i wrong?

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


Re: using timers to force an execution time

2009-07-16 Thread Diez B. Roggisch
superpollo wrote:

> Diez B. Roggisch wrote:
>> superpollo wrote:
>>>see? the while body ran for about 7 seconds... i bet it has to do with
>>>the fact that the timer does not control inner loops... any suggestion?
>> 
>> 
>> Of course the inner loop isn't affected by the set event - how should it
>> be, if you don't check it.
>> 
>> if you rewrite it as this:
>> 
>> while True:
>> for repeat in range(10):
>> if e.isSet():
>>break
>> print time.time()
>> time.sleep(.66)
>> 
>> it should terminate earlier.
> 
> so it seems almost impossible to allocate a certain amount of time to a
> code fragment *without* somehow rewriting its logic?
> 
> am i wrong?

No, you are right, for threads that is. You can try & trick around with the
trace-functionality of python, and some ctypes-based
system-thread-module-tricks that are, as mentioned before, black-magic & a
spinning gatling gun pointed to your very own lower extremities.

The only reliable way of terminating an asynchronous computation is to use
processes, since python2.6 that's rather convenient with the
multiprocessing-module. However, that imposes some limits to what you can
do.

Diez

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


Re: using timers to force an execution time

2009-07-16 Thread superpollo

Diez B. Roggisch wrote:

superpollo wrote:

am i wrong?



No, you are right, for threads that is. You can try & trick around with the
trace-functionality of python, and some ctypes-based
system-thread-module-tricks that are, as mentioned before, black-magic & a
spinning gatling gun pointed to your very own lower extremities.



OUCH.


The only reliable way of terminating an asynchronous computation is to use
processes, since python2.6 that's rather convenient with the
multiprocessing-module. However, that imposes some limits to what you can
do.


fair enough.

very helpful indeed.

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


Re: turtle dump

2009-07-16 Thread Diez B. Roggisch
superpollo wrote:

> hi there.
> 
> is there a way to dump the content of a turtle window to a file or a
> file object?

Why should I want to dump a turtle? They are very benign creatures, dumping
them is going to hurt them needlessly.

Without a cheek-in-tongue: how are we supposed to know what a"turtle window"
is, how you create it, what features it uses? You need to give more
details, such as the module you are using, on what system that runs and so
forth.

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


Re: turtle dump

2009-07-16 Thread superpollo

Diez B. Roggisch wrote:

superpollo wrote:



hi there.

is there a way to dump the content of a turtle window to a file or a
file object?



Why should I want to dump a turtle? They are very benign creatures, dumping
them is going to hurt them needlessly.



lol. ;-) the title was indeed supposed to stir a bit of curiosity upon 
the reader...



Without a cheek-in-tongue: how are we supposed to know what a"turtle window"
is, how you create it, what features it uses? You need to give more
details, such as the module you are using, on what system that runs and so
forth.


in fact i was looking for a *platform independent* way to draw into a 
graphics file (say, a postscript or a png) using the turtle module. so i 
understand that "dumping a window" is not a good expression... maybe 
"redirecting graphics commands to a file instead of a window"?


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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread alex23
On Jul 16, 9:00 pm, akhil1988  wrote:
> I have switched to python 3.1 , but now I am getting some syntax errors in
> the code:

Python 3.x was a major release that endeavoured to clean up a number
of lingering issues with the language, the upshot being that it isn't
entirely backwards compatible with past versions. Unicode became the
default string type, which is what is causing the error here: the u-
prefix is no longer required (or even allowed).

However, Py3.x _does_ come with a handy tool for automatically
converting Python 2.x code to 3.x, called 2to3. One of the things it
should do is convert Py2.x unicode values into their correct
representation in 3.x.

With any luck, it should be able to convert the code you're using
entirely. Let us know how it goes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turtle dump

2009-07-16 Thread alex23
On Jul 16, 9:18 pm, superpollo  wrote:
> lol. ;-) the title was indeed supposed to stir a bit of curiosity upon
> the reader...

Which isn't really useful when trying to obtain assistance... you want
certainty, not curiosity.

> in fact i was looking for a *platform independent* way to draw into a
> graphics file (say, a postscript or a png) using the turtle module. so i
> understand that "dumping a window" is not a good expression... maybe
> "redirecting graphics commands to a file instead of a window"?

You didn't actually answer Diez question. The turtle module is only a
recent (2.6/3.0) addition to Python and is probably obscure enough not
to have tracked across everyone's radar.

So _if_ you're talking about the standard lib turtle module, you're
not looking to 'dump a window', you're looking to write the contents
of turtle.Canvas to file. Canvas only supports writing to postscript,
so you'll have to do something like this (untested):

import turtle
screen = turtle.Screen()
# go go gadget turtle...
screen._canvas.postscript(file='turtle.ps')

Or it may be open('turtle.ps','w').write(screen._canvas.postscript
())... I couldn't find a definitive answer. Try looking through the Tk
docs, it should be covered there.

Note that this is a postscript _text_ file, so you'll also need to
find something to render it with.

Hope this helps point you in the right direction.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turtle dump

2009-07-16 Thread superpollo

alex23 wrote:

On Jul 16, 9:18 pm, superpollo  wrote:


lol. ;-) the title was indeed supposed to stir a bit of curiosity upon
the reader...



Which isn't really useful when trying to obtain assistance... you want
certainty, not curiosity.



ok. my bad.




in fact i was looking for a *platform independent* way to draw into a
graphics file (say, a postscript or a png) using the turtle module. so i
understand that "dumping a window" is not a good expression... maybe
"redirecting graphics commands to a file instead of a window"?



You didn't actually answer Diez question. The turtle module is only a
recent (2.6/3.0) addition to Python and is probably obscure enough not
to have tracked across everyone's radar.



actually i am still using 2.3.4, which means that...


screen = turtle.Screen()


... is not possible



Hope this helps point you in the right direction.


it certainly did, tahnks.

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


Re: turtle dump

2009-07-16 Thread Bearophile
Superchicken:
> is there a way to dump the content of a turtle window to a file or a file 
> object?

A possible low-tech solution is to append to a list the sequence of
your plotting commands (using a decorator too, even, something like
the logging decorator), and then save all of them at the end into a
text file :-)

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


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread Piet van Oostrum
> mheavner  (m) wrote:

>m> I'm using multiprocessing to spawn several subprocesses, each of which
>m> uses a very large data structure (making it impractical to pass it via
>m> pipes / pickling). I need to allocate this structure once when the
>m> process is created and have it remain in memory for the duration of
>m> the process. The way the multiprocessing module is set up, only the
>m> 'run' method runs within the subprocess - so creating a wrapper class
>m> with a constructor that allocates the structure in __init__ will not
>m> work, as far as I know, as this will still be within the parent
>m> process.

>m> If I were working in C/C++, I would declare the variable "static"
>m> within the function body - is there any way with the multiprocessing
>m> module to have persistent data members within subprocesses?

>m> Any ideas??

Your post is not entirely clear. Is `the process' the same as `the
subprocess'? 

Assuming it is, what is the problem? You can create the datastructure
first thing in the run method can't you?

Like this:

from multiprocessing import Process
from time import sleep
from random import random

class MyProcess(Process):

def __init__(self, number):
self.number = number
Process.__init__(self)

def run(self):
print "Process %s started" % self.number
self.data = range(self.number * 10, (self.number + 1) * 10)
self.doit()

def doit(self):
for i in range(5):
sleep(3 * random())
self.data[i] += i
print self.data[i]

processes = []
for k in range(10):
p = MyProcess(k)
p.start()
processes.append(p)


for p in processes:
p.join()

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


Re: turtle dump

2009-07-16 Thread Peter Otten
superpollo wrote:

> alex23 wrote:
>> On Jul 16, 9:18 pm, superpollo  wrote:
>> 
>>>lol. ;-) the title was indeed supposed to stir a bit of curiosity upon
>>>the reader...
>> 
>> 
>> Which isn't really useful when trying to obtain assistance... you want
>> certainty, not curiosity.
>> 
> 
> ok. my bad.
> 
>> 
>>>in fact i was looking for a *platform independent* way to draw into a
>>>graphics file (say, a postscript or a png) using the turtle module. so i
>>>understand that "dumping a window" is not a good expression... maybe
>>>"redirecting graphics commands to a file instead of a window"?
>> 
>> 
>> You didn't actually answer Diez question. The turtle module is only a
>> recent (2.6/3.0) addition to Python and is probably obscure enough not
>> to have tracked across everyone's radar.
>> 
> 
> actually i am still using 2.3.4, which means that...
> 
>> screen = turtle.Screen()
> 
> ... is not possible

Tested on 2.4:

>>> import turtle
>>> turtle.reset()
>>> for i in range(4):
... turtle.forward(50)
... turtle.right(90)
...
>>> turtle._canvas.postscript(file="tmp.ps")
''

I think the big rewrite has happened in 2.6, so the above should also work 
in 2.3.

Peter

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


Re: turtle dump

2009-07-16 Thread superpollo

Peter Otten wrote:

Tested on 2.4:



import turtle
turtle.reset()
for i in range(4):


... turtle.forward(50)
... turtle.right(90)
...


turtle._canvas.postscript(file="tmp.ps")


''

I think the big rewrite has happened in 2.6, so the above should also work 
in 2.3.


Peter



mr otten, you are great.

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


Re: Passing python list from C to python

2009-07-16 Thread hartley
/* the first telling */
(...)
/* the third telling */
(...)
/* the third telling */


-
If you had loosened up on the sarcasm I would probably have read what
you wrote more thoroughly instead of just skimming through it. Thanks
for the help, but you should seriously consider doing something with
your patronizing attitude.

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


Re: Passing python list from C to python

2009-07-16 Thread hartley
/* the first telling */
(...)
/* the second telling */
(...)
/* the third telling */

-
If you had loosened up on the sarcasm I would probably have read what
you wrote more thoroughly instead of just skimming through it. Thanks
for the help, but you should seriously consider doing something with
your patronizing attitude.

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


Re: turtle dump

2009-07-16 Thread Michiel Overtoom


I got success with the following code (python 2.6.2):

import turtle
turtle.reset()
for i in range(4):
   turtle.forward(50)
   turtle.right(90)
can=turtle.getscreen().getcanvas()
can.postscript(file="tmp.ps")


--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Valloppillil
http://www.catb.org/~esr/halloween/halloween4.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if any item from a list of strings is in a big string?

2009-07-16 Thread inkhorn
Hi all,

This was more a question of programming aesthetics for me than one of
great practical significance.  I was looking to perform a certain
function on files in a directory so long as those files weren't found
in certain standard directories.  In other words, I was using os.walk
() to get multiple root directory strings, and the lists of files in
each directory.  The function was to be performed on those files, so
long as certain terms weren't in the root directory string.

In actuality, I could have stuck with the helper function I created,
but I'm always curious to see how well multiple lines of code can turn
into fewer lines of code in python and retain the same functional
value :)

Matt

On Jul 15, 6:46 am, denis  wrote:
> Sure, Aho-Corasick is fast for fixedstrings; but without real
> numbers / a concrete goal
>
> > > Matt, how many words are you looking for, in how long a string ?
>
> a simple solution is good enough, satisficing.  Matt asked "how to
> make that function look nicer?"
> but "nice" has many dimensions -- bicycles are nice for some tasks,
> Ferraris for others.
>
> Bythewayhttp://en.wikipedia.org/wiki/Aho-Corasick_algorithmhas a
> link to a Python implementation,
> alsohttp://en.wikipedia.org/wiki/Worse_is_Betteris fun.

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


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread mheavner
On Jul 16, 8:39 am, Piet van Oostrum  wrote:
> > mheavner  (m) wrote:
> >m> I'm using multiprocessing to spawn several subprocesses, each of which
> >m> uses a very large data structure (making it impractical to pass it via
> >m> pipes / pickling). I need to allocate this structure once when the
> >m> process is created and have it remain in memory for the duration of
> >m> the process. The way the multiprocessing module is set up, only the
> >m> 'run' method runs within the subprocess - so creating a wrapper class
> >m> with a constructor that allocates the structure in __init__ will not
> >m> work, as far as I know, as this will still be within the parent
> >m> process.
> >m> If I were working in C/C++, I would declare the variable "static"
> >m> within the function body - is there any way with the multiprocessing
> >m> module to have persistent data members within subprocesses?
> >m> Any ideas??
>
> Your post is not entirely clear. Is `the process' the same as `the
> subprocess'?
>
> Assuming it is, what is the problem? You can create the datastructure
> first thing in the run method can't you?
>
> Like this:
>
> from multiprocessing import Process
> from time import sleep
> from random import random
>
> class MyProcess(Process):
>
>     def __init__(self, number):
>         self.number = number
>         Process.__init__(self)
>
>     def run(self):
>         print "Process %s started" % self.number
>         self.data = range(self.number * 10, (self.number + 1) * 10)
>         self.doit()
>
>     def doit(self):
>         for i in range(5):
>             sleep(3 * random())
>             self.data[i] += i
>             print self.data[i]
>
> processes = []
> for k in range(10):
>     p = MyProcess(k)
>     p.start()
>     processes.append(p)
>
> for p in processes:
>     p.join()
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected]

'The process' refers to the subprocess. I could do as you say, load
the data structure each time, but the problem is that takes a
considerable amount of time compared to the the actual computation
with the data it contains. I'm using these processes within a loop as
follows:

 # Don't recreate processes or Queues
 pop1 = Queue()
 pop2 = Queue()
 pop_out = Queue()
 p1 = CudaProcess(0, args=(costf,pop1,pop_out))
 p2 = CudaProcess(1, args=(costf,pop2,pop_out))

 # Main loop
 for i in range(maxiter):
 print 'ITERATION: '+str(i)
 if log != None:
 l = open(log,'a')
 l.write('Iteration: '+str(i)+'\n')
 l.close()

 # Split population in two
 pop1.putmany(pop[0:len(pop)/2])
 pop2.putmany(pop[len(pop)/2:len(pop)])

 # Start two processes
 if not p1.isAlive():
 p1.start()
 print 'started %s'%str(p1.getPid())
 else:
 p1.run()
 if not p2.isAlive():
 p2.start()
 print 'started %s'%str(p2.getPid())
 else:
 p2.run()
 .
 .
 .

So I'd like to load that data into memory once and keep there as long
as the process is alive (ideally when the subprocess is created,
storing some sort of pointer to it), rather than loading it each time
run is called for a process within the loop. Could be my CudaProcess
class - I'll check out what Diez suggested and post back.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: missing 'xor' Boolean operator

2009-07-16 Thread Anthony Tolle
On Jul 15, 8:32 pm, Paul Rubin  wrote:
> Among other things, that uses quadratic time!  Why do you want to keep
> popping items from that list instead of iterating through it anyway?
>
> Anyway, I think you wrote something close to this:
> ...

Very true!  I didn't think about the problems with pop().  I was using
it as a shortcut for pulling off the first operand.  I forgot that if
you start with an initial operand of "False", the result will be the
same (0 xor X = X)

While I'm not sure how useful it would be, here's a version of the
first function that returns one of the operands (ala AND and OR),
except in the case where there is an even number of True elements,
where it returns False:

def xor(*operands):
r, rprime = False, False
for x in operands:
xprime = bool(x)
if rprime:
if xprime:
r, rprime = False, False
else:
r, rprime = x, xprime
return r

>>> xor(0, 0)
0
>>> xor(0, 1)
1
>>> xor(1, 0)
1
>>> xor(1, 1)
False
>>> xor(0, 1, 2)
False
>>> xor(0, 1, 2, 3)
3
>>> xor(None, [])
[]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: missing 'xor' Boolean operator

2009-07-16 Thread Emile van Sebille

On 7/16/2009 2:06 AM Jean-Michel Pichavant said...
Ok then, why "or" does not return True, if the first element is 
considered True ? Why returning the element itself. Any reason for that 
? Because it's confusing, maybe people used to that logic find it 
obvious, but I really don't.


For example, I sometimes use it to set defaults:

daysInAdvance = int(inputVar) or 25

Emile

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


Re: using timers to force an execution time

2009-07-16 Thread Nick Craig-Wood
superpollo  wrote:
>  Diez B. Roggisch wrote:
> > superpollo wrote:
> >>am i wrong?
> > 
> > 
> > No, you are right, for threads that is. You can try & trick around with the
> > trace-functionality of python, and some ctypes-based
> > system-thread-module-tricks that are, as mentioned before, black-magic & a
> > spinning gatling gun pointed to your very own lower extremities.
> > 
> 
>  OUCH.
> 
> > The only reliable way of terminating an asynchronous computation is to use
> > processes, since python2.6 that's rather convenient with the
> > multiprocessing-module. However, that imposes some limits to what you can
> > do.
> 
>  fair enough.

Or if you are on unix you can use signals...

It is probably just about acceptable to raise an exception in a signal
handler like this code does.

import signal, os, time

class TimeOut(Exception):
"""Thrown on alarm"""
pass

def sig_alarm(signum, frame):
raise TimeOut()

def time_out(t, fn, *args, **kwargs):
"""Calls fn with the args and kwargs returning its result or raising a 
TimeOut exception if it doesn't complete within t seconds"""

# Turn alarm off and read old value
old_alarm = signal.alarm(0)
# Install new handler remembering old
old_handler = signal.signal(signal.SIGALRM, sig_alarm)
# Set the timer going
signal.alarm(t)

try:
rc = fn(*args, **kwargs)
finally:
# Restore the old handler
signal.signal(signal.SIGALRM, old_handler)
signal.alarm(0)  

def test():
for repeat in range(10):
print time.time()
time.sleep(0.66)

if __name__ == "__main__":
try:
time_out(3, test)
except TimeOut:
print "timed out"

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


Re: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Inky 788
On Jul 10, 7:35 pm, Ben Finney  wrote:
> walterbyrd  writes:
> > I believe Guido himself has said that all indentions should be four
> > spaces - no tabs.
>
> Yes. That's a “should” and not a “must”, even though PEP 8 says it
> with a simple imperative::
>
>     Use 4 spaces per indentation level.

That actually sounds pretty weird. "Don't do {foo}. Really, I mean
*really* don't do {foo}. Oh, also, the interpreter allows you to do
{foo}. But don't do it! I mean it!".

Very ... perlish, if you ask me.

I realize that a small portion of the community likes the tabs.
They're sold on the tabs. They like the tabs. But tabs are an archaic
holdover from an era when typewriters had physical tabstops on them.
Actually, after *that* they're a holdover from a program called `make`
whose designers unfortunately decided to use tab "characters" to
prefix commands. The tab is vestigial. Your text editor can treat 4
consecutive spaces as a unit if you wish. Let tabs go away. Stop using
them. If your editor keeps irrationally inserting them, make it stop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread mheavner
On Jul 16, 9:18 am, mheavner  wrote:
> On Jul 16, 8:39 am, Piet van Oostrum  wrote:
>
>
>
> > > mheavner  (m) wrote:
> > >m> I'm using multiprocessing to spawn several subprocesses, each of which
> > >m> uses a very large data structure (making it impractical to pass it via
> > >m> pipes / pickling). I need to allocate this structure once when the
> > >m> process is created and have it remain in memory for the duration of
> > >m> the process. The way the multiprocessing module is set up, only the
> > >m> 'run' method runs within the subprocess - so creating a wrapper class
> > >m> with a constructor that allocates the structure in __init__ will not
> > >m> work, as far as I know, as this will still be within the parent
> > >m> process.
> > >m> If I were working in C/C++, I would declare the variable "static"
> > >m> within the function body - is there any way with the multiprocessing
> > >m> module to have persistent data members within subprocesses?
> > >m> Any ideas??
>
> > Your post is not entirely clear. Is `the process' the same as `the
> > subprocess'?
>
> > Assuming it is, what is the problem? You can create the datastructure
> > first thing in the run method can't you?
>
> > Like this:
>
> > from multiprocessing import Process
> > from time import sleep
> > from random import random
>
> > class MyProcess(Process):
>
> >     def __init__(self, number):
> >         self.number = number
> >         Process.__init__(self)
>
> >     def run(self):
> >         print "Process %s started" % self.number
> >         self.data = range(self.number * 10, (self.number + 1) * 10)
> >         self.doit()
>
> >     def doit(self):
> >         for i in range(5):
> >             sleep(3 * random())
> >             self.data[i] += i
> >             print self.data[i]
>
> > processes = []
> > for k in range(10):
> >     p = MyProcess(k)
> >     p.start()
> >     processes.append(p)
>
> > for p in processes:
> >     p.join()
>
> > --
> > Piet van Oostrum 
> > URL:http://pietvanoostrum.com[PGP8DAE142BE17999C4]
> > Private email: [email protected]
>
> 'The process' refers to the subprocess. I could do as you say, load
> the data structure each time, but the problem is that takes a
> considerable amount of time compared to the the actual computation
> with the data it contains. I'm using these processes within a loop as
> follows:
>
>          # Don't recreate processes or Queues
>          pop1 = Queue()
>          pop2 = Queue()
>          pop_out = Queue()
>          p1 = CudaProcess(0, args=(costf,pop1,pop_out))
>          p2 = CudaProcess(1, args=(costf,pop2,pop_out))
>
>          # Main loop
>          for i in range(maxiter):
>                  print 'ITERATION: '+str(i)
>                  if log != None:
>                          l = open(log,'a')
>                  l.write('Iteration: '+str(i)+'\n')
>                  l.close()
>
>                  # Split population in two
>                  pop1.putmany(pop[0:len(pop)/2])
>                  pop2.putmany(pop[len(pop)/2:len(pop)])
>
>                  # Start two processes
>                  if not p1.isAlive():
>                          p1.start()
>                          print 'started %s'%str(p1.getPid())
>                  else:
>                          p1.run()
>                  if not p2.isAlive():
>                          p2.start()
>                          print 'started %s'%str(p2.getPid())
>                  else:
>                          p2.run()
>                  .
>                  .
>                  .
>
> So I'd like to load that data into memory once and keep there as long
> as the process is alive (ideally when the subprocess is created,
> storing some sort of pointer to it), rather than loading it each time
> run is called for a process within the loop. Could be my CudaProcess
> class - I'll check out what Diez suggested and post back.

Essentially, I'd like to "sneak" that allocation in somewhere after
the fork is done (in start()) in the context of the subprocess,
holding a pointer to that structure, but before all of the run() calls
are done
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turtle dump

2009-07-16 Thread alex23
On Jul 16, 10:11 pm, superpollo  wrote:
> actually i am still using 2.3.4, which means that...
>
> > screen = turtle.Screen()
>
> ... is not possible

Ah, sorry about that. My belief that turtle was a new module was based
on a line from 
http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-PyCon2007.pdf

   Since Python 2.6/3.0, Python has had a new turtle module.

At which point I stopped reading and missed the following line:

   Its development was based entirely on the previous one.

In my defence, I _had_ been drinking.

Thankfully Peter stepped up with a more appropriate solution, and
Michiel pointed out the more suitable API calls over dealing directly
with the underlying implementation :) Good work guys!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using timers to force an execution time

2009-07-16 Thread Paul Moore
2009/7/16 superpollo :
> hello.
>
> based upon previuos suggestions, i tried to write a program in which i would
> like to have a certain code fragment to execute only for a specified amount
> of time (say 3 seconds), then bail out.
>
> but i get the following:
>
> $ cat tmr004.py
> #!/usr/bin/python -u
>
> import threading , time
>
> e = threading.Event()
> t = threading.Timer(3.0, e.set)
> t.start()
> print time.asctime(time.localtime(time.time()))
> while not e.isSet():
>    for repeat in range(10):
>        print time.time()
>        time.sleep(0.66)
> print time.asctime(time.localtime(time.time()))
> $ ./tmr004.py
> Thu Jul 16 12:31:27 2009
> 1247740287.44
> 1247740288.1
> 1247740288.76
> 1247740289.42
> 1247740290.08
> 1247740290.74
> 1247740291.4
> 1247740292.06
> 1247740292.72
> 1247740293.38
> Thu Jul 16 12:31:34 2009
> $
>
> see? the while body ran for about 7 seconds... i bet it has to do with the
> fact that the timer does not control inner loops... any suggestion?

Clearly, this isn't what you are actually trying to do. For this case,
your event is getting set when you expect, but your main thread does
not check the event often enough to let it stop in a timely manner.

To fix this, remove the inner "for repeat in range(10)" loop. But I
assume that your "real" code isn't something that you can fix this
easily. If you describe your real requirement, it may be possible to
give you a better answer. (But that answer will almost certainly not
be a way of interrupting one thread from another - that's not really
possible with Python - but rather a way of achieving your goal without
*needing* to interrupt one thread from another).

Paul.

PS If you really must interrupt one thread from another, you can use C
code (or ctypes) to call the PyThreadState_SetAsyncExc API. But I'm
not going to tell you how, as it's almost certainly *not* what you
want to do in practice :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using timers to force an execution time

2009-07-16 Thread superpollo

Nick Craig-Wood wrote:

superpollo  wrote:

> ...

Or if you are on unix you can use signals...

It is probably just about acceptable to raise an exception in a signal
handler like this code does.


> ...

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant

Emile van Sebille wrote:

On 7/16/2009 2:06 AM Jean-Michel Pichavant said...
Ok then, why "or" does not return True, if the first element is 
considered True ? Why returning the element itself. Any reason for 
that ? Because it's confusing, maybe people used to that logic find 
it obvious, but I really don't.


For example, I sometimes use it to set defaults:

daysInAdvance = int(inputVar) or 25

Emile

Sure this looks like an elegant way to set default values and I will use 
this form , but I'm not sure this justifies by itself the trickery. 
Python has extended the algebra definition of "or" and "and" top any 
type, but it is so unintuitive (I'm no LISP programmer). I think than 
using the short-circuiting mechanism of bool operators along with the 
python tricks is just error prone and may result in bug difficult to 
spot, unless you are very aware of all python boolean mechanisms.


JM

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


Re: Passing handlers between bound c++ libs

2009-07-16 Thread bobicanprogram
On Jul 14, 7:07 pm, Freyr  wrote:
> I have a python bound physics library that uses handler to process
> events. The passing of handlers between c++ and python causes a huge
> overhead slowing down the process.  Can I implement a handler in my
> custom python bound c++ lib B and pass it to blackbox python bound c++
> lib A so that A would call B directly without passing through the
> python layer?  Or to phrase the question differently:  Can I pass a
> handler from B through python to A so that A will invoke B with out
> calling into python code?


Depending on your exact configuration you may be able to wrap that C++
library using the SIMPL toolkit and get at your data with a
message.There is some "hello world" demo code here:

http://www.icanprogram.com/06py/main.html

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


Re: using timers to force an execution time

2009-07-16 Thread superpollo

Nick Craig-Wood wrote:
> ...

import signal, os, time
...

importing os is useless of course...
--
http://mail.python.org/mailman/listinfo/python-list


Re: missing 'xor' Boolean operator

2009-07-16 Thread Grant Edwards
On 2009-07-16, Emile van Sebille  wrote:
> On 7/16/2009 2:06 AM Jean-Michel Pichavant said...
>> Ok then, why "or" does not return True, if the first element is 
>> considered True ? Why returning the element itself. Any reason for that 
>> ? Because it's confusing, maybe people used to that logic find it 
>> obvious, but I really don't.
>
> For example, I sometimes use it to set defaults:
>
> daysInAdvance = int(inputVar) or 25

I don't get it.  That doesn't work right when inputVar == "0".

-- 
Grant Edwards   grante Yow! What I want to find
  at   out is -- do parrots know
   visi.commuch about Astro-Turf?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to search this newsgroup by a python script.

2009-07-16 Thread Grant Edwards
On 2009-07-16, Chris Rebert  wrote:
> On Thu, Jul 16, 2009 at 2:12 AM, Helmut
> Jarausch wrote:
>> Hi,
>>
>> I haven't found anything with Google's group search, so let me
>> ask it (again?).
>>
>> How can I search this newsgroup from within a Python script.
>> (Perhaps by searching Google Groups or Gmane by some Python code.)
>
> 1. Generate URL of the form:
> http://search.gmane.org/?query=foo&group=gmane.comp.python.general
> where "foo" is the search terms, with proper URL escaping applied.
> 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html
> 3. Parse resulting HTML page (e.g. using BeautifulSoup)
> 4. Extract desired information from search results using the parse tree.
> 5. ???
> 6. Profit!

The underpants gnomes would be proud.

-- 
Grant Edwards   grante Yow! I'm encased in the
  at   lining of a pure pork
   visi.comsausage!!
-- 
http://mail.python.org/mailman/listinfo/python-list


This is a mess...

2009-07-16 Thread Nick
I've been coding python for about a week now, and i'm trying to make
an object oriented version of a program i just wrote.  in this post is
the original program.  the next post will include the new programs and
the traceback.  i'm sure there are many underlying problems, but have
been stuck on this for 2 days now...

email me for input files since they are big and i don't see a way to
upload a file here(~5 mB)
this program works, and inputing the same file twice you should get a
"Q" values of ~1 (.99...)

##CODE#

#9July09
#Compute the covariance overlap of two results
#Read in _U.asc files for both ensembles to find e-vectors
#Read in _ .asc files for both ensembles to find sqrt e-values
#strip out matrix info
#compute corvariance


import sys
import math
from math import sqrt


if len(sys.argv) != 3:
print " "
print "The corrent usage is 'python covariance.py file1 file2'"
print "where the _U.asc and _s.asc will be appended when needed"
print " "
exit(1)


Input the first prefix1_U.asc file:
this is the first eigen vector matrix
prefix1 = sys.argv[1] + "_U.asc"
fileholder = open(prefix1)
text = fileholder.readlines()
fields = text[1].split()
num_rows = int(fields[1])
num_cols = int(fields[2])

U1_matrix = []
for line in text[2: num_rows+2]:
fields = line.split()
for i in range(len(fields)):
fields[i] = float(fields[i])
U1_matrix.append(fields)

Input the second prefix2_U.asc file:
this is the 2nd eigen vector matrix
prefix2 = sys.argv[2] + "_U.asc"
fileholder = open(prefix2)
text = fileholder.readlines()
fields = text[1].split()
num_rows = int(fields[1])
num_cols = int(fields[2])
#print "look here nick:", fields

U2_matrix = []
for line in text[2: num_rows+2]:
fields = line.split()
for i in range(len(fields)):
fields[i] = float(fields[i])
U2_matrix.append(fields)

Then Read in the first eigen values
1st 2 lines are header and need to be stripped away
prefix3 = sys.argv[1] + "_s.asc"
fileholder = open(prefix3)
text = fileholder.readlines()
fields = text[1].split()
num_vals1 = int(fields[1]) #add check to see if correct # of values
added
print "square if", len(U1_matrix), "=", len(U2_matrix), "=", len
(U1_matrix[1]), "=", len(U2_matrix[1])
#the list of sqrt e vals
sqrt_s1 = [float(line) for line in text[2: num_vals1+2]]
s1 = [float(line) * float(line) for line in text[2: num_vals1+2]]

Finally, read in the 2nd set of eigen vals
prefix4 = sys.argv[2] + "_s.asc"
fileholder = open(prefix4)
text = fileholder.readlines()
fields = text[1].split()
num_vals2 = int(fields[1]) #add check to see if correct # of values
added
#the list of sqrt e vals
sqrt_s2 = [float(line) for line in text[2: num_vals1+2]]
s2 = [float(line) * float(line) for line in text[2: num_vals1+2]]


#=
double summation (the 2nd term)
doublesum = 0.0
for i in range(len(U1_matrix[1])):
   #print "i = ", i
v1 = U1_matrix[i]
sum = 0
for j in range(len(U2_matrix)):
 v2 = U2_matrix[j]
 dot = 0
 for k in range(len(U1_matrix)):
 dot += v1[k] * v2[k]
 root = sqrt_s1[i] * sqrt_s2[j]
 sum += root * dot * dot
doublesum += sum


print "double sum: ", doublesum

single summation (the 1st term and denominator)
singsum = 0.0
for k in range(len(s1)):
singsum += s1[k] + s2[k]
print "sing sum:", singsum


Put it all together
Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum))
print "your Q:"
print Q

# end of code covariance.py

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


Re: Python Equivalent for dd & fold

2009-07-16 Thread seldan24
On Jul 15, 1:48 pm, Emile van Sebille  wrote:
> On 7/15/2009 10:23 AM MRAB said...
>
> >> On Jul 15, 12:47 pm, Michiel Overtoom  wrote:
> >>> seldan24 wrote:
>  what can I use as the equivalent for the Unix 'fold' command?
> >>> def fold(s,len):
> >>>      while s:
> >>>          print s[:len]
> >>>          s=s[len:]
>
> 
> > You might still need to tweak the above code as regards how line endings
> > are handled.
>
> You might also want to tweak it if the strings are _really_ long to
> simply slice out the substrings as opposed to reassigning the balance to
> a newly created s on each iteration.
>
> Emile

Thanks for all of the help.  I'm almost there.  I have it working now,
but the 'fold' piece is very slow.  When I use the 'fold' command in
shell it is almost instantaneous.  I was able to do the EBCDIC->ASCII
conversion usng the decode method in the built-in str type.  I didn't
have to import the codecs module.  I just decoded the data to cp037
which works fine.

So now, I'm left with a large file, consisting of one extremely long
line of ASCII data that needs to be sliced up into 35 character
lines.  I did the following, which works but takes a very long time:

f = open(ascii_file, 'w')
while ascii_data:
f.write(ascii_data[:len])
ascii_data = ascii_data[len:]
f.close()

I know that Emile suggested that I can slice out the substrings rather
than do the gradual trimming of the string variable as is being done
by moving around the length.  So, I'm going to give that a try... I'm
a bit confused by what that means, am guessing that slice can break up
a string based on characters; will research.  Thanks for the help thus
far.  I'll post again when all is working fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ImportError: No module named _functools

2009-07-16 Thread Aahz
In article <45228cf4-0b37-4c52-bf6f-d7bd124b0...@l32g2000vbp.googlegroups.com>,
Tony  Lay   wrote:
>
>Traceback (most recent call last):
>  File "/usr/local/bin/meld", line 35, in 
>import gettext
>  File "/usr/local/lib/python2.6/gettext.py", line 49, in 
>import locale, copy, os, re, struct, sys
>  File "/usr/local/lib/python2.6/locale.py", line 15, in 
>import functools
>  File "/usr/local/lib/python2.6/functools.py", line 10, in 
>from _functools import partial, reduce
>ImportError: No module named _functools

Where is _functools.so?
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Tim Chase

I realize that a small portion of the community likes the tabs.
They're sold on the tabs. They like the tabs. But tabs are an archaic
holdover from an era when typewriters had physical tabstops on them.


However, they are a single logical level of indentation -- I come 
down fairly solidly on the "tabs" side of the "tabs vs. spaces" 
argument.  I can set my editor (vim in this case) to show tabs as 
as many spaces as I want.  I usually have this set to 4, but 
sometimes 1 or 2.  In Vim, using tabs I can just switch up my 
tab-stop setting (":set ts=2") and the presentation of my code 
reindents the way I expect.  If I wanted to switch from 
4-real-spaces to 2-real-spaces, I'd have to concoct a perlish 
regexp to handle the munging.


The same "separation of content and presentation" that is all the 
rage with N-tier programmers and web developers, my content is 
"logical levels of indentation indicated by a tab character in 
the source-code" and my presentation is "N spaces as presented by 
my editor".


Yes, the dictatorial "a tab always equals 8 spaces" is a 
vestigial holdover for which I hold no love.


I'll code on other people's projects with spaces if that's the 
project convention (as Vim lets me switch around fairly easily). 
 But as for my own code, it's tabs all the way.


-tkc



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


Re: This is a mess...

2009-07-16 Thread Nick
this is the new oop version, its pretty messy currently, and i do
understand it is a simple routine, but i'm using it as an exercise to
learn oop python...

first the (current) traceback:
 [:~/python]$  python oop_covariance.py b2ar_all_test b2ar_all_test


Traceback (most recent call last):
  File "oop_covariance.py", line 24, in 
cov = set1.covariance(set2, Eigen_vect.dot)
  File "/home/nleioatts/python/Eigen.py", line 66, in covariance
print self.vectors[i][i]
AttributeError: Eigen_vect instance has no attribute '__getitem__'

and a quick explaination:  the file structures aare a 2d list of lists
of numbers corresponding to eginvectors and a list of numbers
corresponding to eigenvalues

#33##Here is the main body
#!/usr/bin/env python
import sys
import Eigen
from Eigen import *

if len(sys.argv) != 3:
print " "
print "The corrent usage is 'python covariance.py file1 file2'"
print "where the _U.asc and _s.asc will be appended when needed"
print " "
exit(1)
file1 =  sys.argv[1]
file2 =  sys.argv[2]
set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc")
set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc")
cov = set1.covariance(set2, Eigen_vect.dot)
print cov


###and here are the classes:

#!/usr/bin/env python
import sys
import math
from math import sqrt

class Eigen_vect:
def __init__(self, e_val, e_vect):
self.e_val  = e_val
self.e_vect = e_vect
def length(self):
return len(self.e_vect)

def dot(self, other):
d = 0.0
if other.length() != self.length():
raise ValueError, "Eigen Vectors not same Length"
for k in range(self.length()):
   # print "HI NICK", self.e_vect[k], other.e_vect[k]
d += float(self.e_vect[k]) * float(other.e_vect[k])
return d


class Eigen_set:
def __init__(self,  vec_filename, val_filename):
self.vec_filename = vec_filename
self.val_filename = val_filename
# open two files
# loop through them, skipping lines that begin with #
# for each row, extract eigen vector and eigen values
fileholder = open(self.vec_filename)
text = fileholder.readlines()
fields = text[2].split()
#print "len of fields", len(fields)
self.vectors = []
for line in text[2: len(fields)]:
fields = line.split()
#print "len of fields", len(fields)
for i in range(len(fields)):
fields[i] = float(fields[i])
e_vect = fields
fileholder = open(self.val_filename)
text = fileholder.readlines()
e_val = [float(line) for line in text[2: self.length()]]
self.vectors.append(Eigen_vect(e_val, e_vect))
#print "this is self.vectors"
#print self.vectors(e_val)

def length(self):
return len(self.vectors)

def covariance(self, other, dot):
newdot = 0.0
# do a length check to make sure we're consistent
if other.length() != self.length():
raise ValueError, "Eigen Vectors not same Length"
#double loop over all my vectors and all of other's vectors
doublesum = 0.0
for i in range(self.length()):
sum = 0.0
v1 = self.vectors[i]
for j in range(self.length()):
newdot += v1.dot(self.vectors[j])
   # root = self.e_val[i] * other.e_val[j]
print self.vectors[i]
print self.vectors[j]
print self.vectors[i][i]
#<

Re: Passing python list from C to python

2009-07-16 Thread John Machin
On Jul 16, 11:13 pm, hartley  wrote:
> /* the first telling */
> (...)
> /* the second telling */
> (...)
> /* the third telling */
>
> -
> If you had loosened up on the sarcasm

That wasn't sarcasm, it was an attempt at humourous watering-down the
expression of exasperation at continued ignoring of advice and wonder
that I was continuing to bother ...

> HTH,

It didn't.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory leak involving traceback objects

2009-07-16 Thread Aahz
In article ,
Rotem   wrote:
>
>I'm debugging a nasty memory leak in a framework written in Python
>(v2.6.2).
>After much digging around I found that the entire object group that is
>leaking is held by a frame object which is subsequently held by a
>traceback object.
>
>Traversing the get_referrers() of each traceback frame leads
>eventually to a root traceback frame which has no referrers
>(gc.get_referrers returns an empty list).
>
>However, this traceback object seems not to be picked by the garbage
>collector, and is still there even after many iterations and calls to
>gc.collect(). The code location to which the traceback frame points
>doesn't do anything special - it just catches an exception, without
>saving the exception itself and/or traceback anywhere.

What *does* it do?  Does it re-raise?  This sounds like you're still in
block scope of an exception.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


IFL 2009: Third Call for Papers

2009-07-16 Thread IFL 2009
Call for Papers IFL 2009Seton Hall UniversitySOUTH ORANGE, NJ, USAhttp://tltc.shu.edu/blogs/projects/IFL2009/* NEW *Registration is now opened! Register at: http://tltc.shu.edu/blogs/projects/IFL2009/registration.htmlInvited Speaker:    Benjamin C. Pierce    University of Pennsylvania    Talk Title: How To Build Your Own Bidirectional Programming LanguageRegistration Fee:      Students: US$325.00    Non-students: US$375.00*The 21st International Symposium on Implementation and Application of Functional Languages, IFL 2009, will be held for the first time in the USA. The hosting institution is Seton Hall University in South Orange, NJ, USA and the symposium dates are September 23-25, 2009. It is our goal to make IFL a regular event held in the USA and in Europe. The goal of the IFL symposia is to bring together researchers actively engaged in the implementation and application of functional and function-based programming languages. IFL 2009 will be a venue for researchers to present and discuss new ideas and concepts, work in progress, and publication-ripe results related to the implementation and application of functional languages and function-based programming.Following the IFL tradition, IFL 2009 will use a post-symposium review process to produce a formal proceedings which will be published by Springer in the Lecture Notes in Computer Science series. All participants in IFL 2009 are invited to submit either a draft paper or an extended abstract describing work to be presented at the symposium. These submissions will be screened by the program committee chair to make sure they are within the scope of IFL and will appear in the draft proceedings distributed at the symposium. Submissions appearing in the draft proceedings are not peer-reviewed publications. After the symposium, authors will be given the opportunity to incorporate the feedback from discussions at the symposium and will be invited to submit a revised full arcticle for the formal review process. These revised submissions will be reviewed by the program committee using prevailing academic standards to select the best articles that will appear in the formal proceedings.TOPICSIFL welcomes submissions describing practical and theoretical work as well as submissions describing applications and tools. If you are not sure if your work is appropriate for IFL 2009, please contact the PC chair at [email protected]. Topics of interest include, but are not limited to: language concepts  type checking  contracts compilation techniques  staged compilation runtime function specialization runtime code generation  partial evaluation   (abstract) interpretation  generic programming techniques  automatic program generation  array processing  concurrent/parallel programming  concurrent/parallel program execution  functional programming and embedded systems  functional programming and web applications  functional programming and security  novel memory management techniques  runtime profiling and performance measurements  debugging and tracing  virtual/abstract machine architectures  validation and verification of functional programs    tools and programming techniques  FP in EducationPAPER SUBMISSIONSProspective authors are encouraged to submit papers or extended abstracts to be published in the draft proceedings and to present them at the symposium. All contributions must be written in English, conform to the Springer-Verlag LNCS series format and not exceed 16 pages. The draft proceedings will appear as a technical report of the Department of Mathematics and Computer Science of Seton Hall University.IMPORTANT DATESRegistration deadline   August 15, 2009Presentation submission deadline  August 15, 2009IFL 2009 Symposium    September 23-25, 2009Submission for review process deadline  November 1, 2009Notification Accept/Reject December 22, 2009Camera ready version   February 1, 2010PROGRAM COMMITTEEPeter Achten    University of Nijmegen, The NetherlandsJost Berthold    Philipps-Universität Marburg, GermanyAndrew Butterfield University of Dublin, IrelandRobby Findler    Northwestern University, USAKathleen Fisher AT&T Research, USACormac Flanagan   University of California at Santa Cruz, USAMatthew Flatt   University of Utah, USAMatthew Fluet   Rochester Institute of Technology, USADaniel Friedman    Indiana University, USAAndy Gill  University of Kansas, USAClemens Grelck University of Amsterdam/Hertfordshire, The Netherlands/UKJurriaan Hage    Utrecht University, The NetherlandsRalf Hinze Oxford University, UKPaul Hudak    Yale University, USAJohn Hughes  Chalmers University of Technology, SwedenPatricia Johann   University of Strathclyde, UKYuki

Re: interactive fiction in Python?

2009-07-16 Thread J Kenneth King
George Oliver  writes:

> hi, I'm just curious who might be working on interactive fiction
> modules in the style of Inform or TADS for Python. I've seen a few
> threads on this list [1] (among many that mention IF tangentially),
> and there are old projects like PUB and PAWS. There are some newer
> potential projects such as Curveship as well. In any case I'd like to
> see what's out there.

I'd be interested in checking out such a project, but I don't really see
the point in starting one.  There are already several great IF tools out
there that are already freely available!  The only reason I would see to
start one is either for pure intellectual curiosity or because you have
some feature ideas that no one else has implemented (and you'd rather
cash in on the potential glory than submit a patch!).

Best of luck finding what you're looking for... but maybe you should
start writing one yourself and post here about it! :)

Cheers

>
>
> thanks, George
>
>
> [1]:
>
> http://bit.ly/Python_Text_Adventure_Authoring_System
>
> http://bit.ly/simple_interactive_fiction_engine
>
> http://bit.ly/adventure_engines_in_Python
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ann: Google releases Python-based open-source NX server

2009-07-16 Thread J Kenneth King
jkn  writes:

> Google quietly releases open-source NX server ...written in Python,
> apparently
>
>  Google_quietly_releases_open_source_NX_server?taxonomyId=88>
>
> Neatx can be downloaded from Google's code repository:
>
> 
>
> Regards
> J^n

It's pretty cool, but not PEP8! Probably because they just bought the
source off of another smaller proprietary project.  Makes me sad seeing
Google, proud supporter of all things Python, release non-PEP8 code.

Either way, I did check out the project and it looks pretty exciting.
Hopefully I'll get some time to contribute a few patches.
-- 
http://mail.python.org/mailman/listinfo/python-list


no return value for threading.Condition.wait(timeout)?

2009-07-16 Thread Gabriel Rossetti

Hello everyone,

I am using threading.Condition.wait(timeout) and was surprised to see 
that there is no return value nor an exception when wait() is used w/ a 
timeout. How am I supposed to know if it was notified or if it timed out?


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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread Max Erickson
akhil1988  wrote:

> 
> akhil1988 wrote:
>> 
>> I have switched to python 3.1 , but now I am getting some syntax
>> errors in the code:
>> 
>> File "./customWikiExtractor.py", line 81
>> __char_entities =  {' '   :u'\u00A0', '¡'
>> :u'\u00A1', 
>> '¢':u'\u00A2',
>> ^

You may want to try 2.6. Python 3.1 is not syntax compatible with 2.5 
(so the u'' stuff won't work in 3.1):

http://docs.python.org/dev/py3k/whatsnew/3.0.html#removed-syntax



max

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


Re: Python Equivalent for dd & fold

2009-07-16 Thread Michiel Overtoom

seldan24 wrote:


I know that Emile suggested that I can slice out the substrings rather
than do the gradual trimming of the string variable as is being done
by moving around the length. 


An excellent idea.

def fold(s,chunklength):
offset=0
while offsethttp://www.catb.org/~esr/halloween/halloween4.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread John Machin
On Jul 16, 9:04 pm, akhil1988  wrote:
> Please click reply on the post and then read this reply in the editor.
> Actually, some sequences have been replaced to their graphical form when
> this post is published. So the python code is being displayed, what actually
> it is not.

What editor? I guess you mean that somebody's browser may be
interpreting the & blah ; thingies  but this is not relevant; your
syntax error problem is that 2.x unicode literals have a u in front
but 3.x str literals don't IOW you need to lose the first u in
u'\u00A0'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Equivalent for dd & fold

2009-07-16 Thread Casey Webster
On Jul 16, 10:12 am, seldan24  wrote:
> On Jul 15, 1:48 pm, Emile van Sebille  wrote:
>
>
>
> > On 7/15/2009 10:23 AM MRAB said...
>
> > >> On Jul 15, 12:47 pm, Michiel Overtoom  wrote:
> > >>> seldan24 wrote:
> >  what can I use as the equivalent for the Unix 'fold' command?
> > >>> def fold(s,len):
> > >>>      while s:
> > >>>          print s[:len]
> > >>>          s=s[len:]
>
> > 
> > > You might still need to tweak the above code as regards how line endings
> > > are handled.
>
> > You might also want to tweak it if the strings are _really_ long to
> > simply slice out the substrings as opposed to reassigning the balance to
> > a newly created s on each iteration.
>
> > Emile
>
> Thanks for all of the help.  I'm almost there.  I have it working now,
> but the 'fold' piece is very slow.  When I use the 'fold' command in
> shell it is almost instantaneous.  I was able to do the EBCDIC->ASCII
> conversion usng the decode method in the built-in str type.  I didn't
> have to import the codecs module.  I just decoded the data to cp037
> which works fine.
>
> So now, I'm left with a large file, consisting of one extremely long
> line of ASCII data that needs to be sliced up into 35 character
> lines.  I did the following, which works but takes a very long time:
>
> f = open(ascii_file, 'w')
> while ascii_data:
>     f.write(ascii_data[:len])
>     ascii_data = ascii_data[len:]
> f.close()
>
> I know that Emile suggested that I can slice out the substrings rather
> than do the gradual trimming of the string variable as is being done
> by moving around the length.  So, I'm going to give that a try... I'm
> a bit confused by what that means, am guessing that slice can break up
> a string based on characters; will research.  Thanks for the help thus
> far.  I'll post again when all is working fine.

The problem is that it creates a new string every time you iterate
through the "ascii_data = ascii_data[len:]".  I believe Emile was
suggesting that you just keep moving the starting index through the
same string, something like (warning - untested code!):

>>> i = 0
>>> str_len = len(ascii_data)
>>> while i < str_len:
>>> j = min(i + length, str_len)
>>> print ascii_data[i:j]
>>> i = j
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if any item from a list of strings is in a big string?

2009-07-16 Thread Pablo Torres N.
On Thu, Jul 9, 2009 at 22:07, Steven
D'Aprano wrote:
> On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote:
>
>> def list_items_in_string(list_items, string):
>>     for item in list_items:
>>         if item in string:
>>             return True
>>     return False
> ...
>> Any ideas how to make that function look nicer? :)
>
> Change the names. Reverse the order of the arguments. Add a docstring.
>

Why reverse the order of the arguments? Is there a design principle there?

I always make a mess out of the order of my arguments...

-- 
Pablo Torres N.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Equivalent for dd & fold

2009-07-16 Thread pdpi
On Jul 16, 3:12 pm, seldan24  wrote:
> On Jul 15, 1:48 pm, Emile van Sebille  wrote:
>
>
>
>
>
> > On 7/15/2009 10:23 AM MRAB said...
>
> > >> On Jul 15, 12:47 pm, Michiel Overtoom  wrote:
> > >>> seldan24 wrote:
> >  what can I use as the equivalent for the Unix 'fold' command?
> > >>> def fold(s,len):
> > >>>      while s:
> > >>>          print s[:len]
> > >>>          s=s[len:]
>
> > 
> > > You might still need to tweak the above code as regards how line endings
> > > are handled.
>
> > You might also want to tweak it if the strings are _really_ long to
> > simply slice out the substrings as opposed to reassigning the balance to
> > a newly created s on each iteration.
>
> > Emile
>
> Thanks for all of the help.  I'm almost there.  I have it working now,
> but the 'fold' piece is very slow.  When I use the 'fold' command in
> shell it is almost instantaneous.  I was able to do the EBCDIC->ASCII
> conversion usng the decode method in the built-in str type.  I didn't
> have to import the codecs module.  I just decoded the data to cp037
> which works fine.
>
> So now, I'm left with a large file, consisting of one extremely long
> line of ASCII data that needs to be sliced up into 35 character
> lines.  I did the following, which works but takes a very long time:
>
> f = open(ascii_file, 'w')
> while ascii_data:
>     f.write(ascii_data[:len])
>     ascii_data = ascii_data[len:]
> f.close()
>
> I know that Emile suggested that I can slice out the substrings rather
> than do the gradual trimming of the string variable as is being done
> by moving around the length.  So, I'm going to give that a try... I'm
> a bit confused by what that means, am guessing that slice can break up
> a string based on characters; will research.  Thanks for the help thus
> far.  I'll post again when all is working fine.

Assuming your rather large text file is 1 meg long, you have 1 million
characters in there. 100/35 = ~29k lines. The size remaining
string decreases linearly, so the average size is (100 + 0) / 2 or
500k. All said and done, you're allocating and copying a 500K string
-- not once, but 29 thousand times. That's where your slowdown resides.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list of all possible values

2009-07-16 Thread David Gibb
> Certainly possible with list comprehensions.
>
 a = "abc"
 [(x, y) for x in a for y in a]
> [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'),
> ('c', 'a'), ('c', 'b'), ('c', 'c')]
>
> But I like bearophile's version better.
>

Andreas,

Thanks, but I think you were missing my point. I should have explained better.

The advantage that bearophile's version is generic with respect to the
number of elements in each combination. To go from 2 element pairs
(e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e'))
requires only a change in a parameter passed to itertools.

I don't see how you would do that with list comprehensions. You're
example works nicely with 2 element pairs, but it seems to me like
you'd need to recode it if you wanted to change it to 5 element pairs.

Am I wrong about that? Can you think of a way to write a function
that, using list comprehensions, takes a list of values and the size
of each combination, and returns the len(list)**(combination size)
possible combinations using those values?

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


Re: missing 'xor' Boolean operator

2009-07-16 Thread Emile van Sebille

On 7/16/2009 7:04 AM Unknown said...

On 2009-07-16, Emile van Sebille  wrote:

daysInAdvance = int(inputVar) or 25


I don't get it.  That doesn't work right when inputVar == "0".

Aah, but you didn't get to define right.  :)  For that particular 
example 0 is not a valid response.


Emile

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


Re: Python Equivalent for dd & fold

2009-07-16 Thread ryles
On Jul 15, 1:14 pm, seldan24  wrote:
> On Jul 15, 12:47 pm, Michiel Overtoom  wrote:
>
>
>
> > seldan24 wrote:
> > > what can I use as the equivalent for the Unix 'fold' command?
>
> > def fold(s,len):
> >      while s:
> >          print s[:len]
> >          s=s[len:]
>
> > s="A very long string indeed. Really that long? Indeed."
> > fold(s,10)
>
> > Output:
>
> > A very lon
> > g string i
> > ndeed. Rea
> > lly that l
> > ong? Indee
> > d.
>
> > Greetings,
>
> > --
> > "The ability of the OSS process to collect and harness
> > the collective IQ of thousands of individuals across
> > the Internet is simply amazing." - Vinod 
> > Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html
>
> Wow, I feel like a dork.  I should have done more research prior to
> posting.  Anyway, thanks for the advice.  The trouble with Python is
> that things make 'too much' sense.  Loving this language.

You might also find the textwrap module useful:

http://docs.python.org/library/textwrap.html
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: list of all possible values

2009-07-16 Thread Andreas Tawn
> > Certainly possible with list comprehensions.
> >
>  a = "abc"
>  [(x, y) for x in a for y in a]
> > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
'c'),
> > ('c', 'a'), ('c', 'b'), ('c', 'c')]
> >
> > But I like bearophile's version better.
> >
> 
> Andreas,
> 
> Thanks, but I think you were missing my point. I should have explained
better.
> 
> The advantage that bearophile's version is generic with respect to the
> number of elements in each combination. To go from 2 element pairs
> (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e'))
> requires only a change in a parameter passed to itertools.
> 
> I don't see how you would do that with list comprehensions. You're
> example works nicely with 2 element pairs, but it seems to me like
> you'd need to recode it if you wanted to change it to 5 element pairs.
> 
> Am I wrong about that? Can you think of a way to write a function
> that, using list comprehensions, takes a list of values and the size
> of each combination, and returns the len(list)**(combination size)
> possible combinations using those values?
> 
> Thanks again,
> David

David,

I think my post got caught in the nebulous email eddys and seems to have
taken 16 hours to arrive on the list. It was meant to be a reply to your
first post, not your second.

Having said that, I think I missed the point of that post too ;o)

Maybe someone smarter than me can come up with a way to dynamically nest
the fors in a list comprehension, but I certainly can't do it.

Sorry for the confusion.

Cheers,

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


Re: Python Equivalent for dd & fold

2009-07-16 Thread MRAB

seldan24 wrote:

On Jul 15, 1:48 pm, Emile van Sebille  wrote:

On 7/15/2009 10:23 AM MRAB said...


On Jul 15, 12:47 pm, Michiel Overtoom  wrote:

seldan24 wrote:

what can I use as the equivalent for the Unix 'fold' command?

def fold(s,len):
 while s:
 print s[:len]
 s=s[len:]



You might still need to tweak the above code as regards how line endings
are handled.

You might also want to tweak it if the strings are _really_ long to
simply slice out the substrings as opposed to reassigning the balance to
a newly created s on each iteration.

Emile


Thanks for all of the help.  I'm almost there.  I have it working now,
but the 'fold' piece is very slow.  When I use the 'fold' command in
shell it is almost instantaneous.  I was able to do the EBCDIC->ASCII
conversion usng the decode method in the built-in str type.  I didn't
have to import the codecs module.  I just decoded the data to cp037
which works fine.

So now, I'm left with a large file, consisting of one extremely long
line of ASCII data that needs to be sliced up into 35 character
lines.  I did the following, which works but takes a very long time:

f = open(ascii_file, 'w')
while ascii_data:
f.write(ascii_data[:len])
ascii_data = ascii_data[len:]
f.close()


The 'write' method doesn't append any line ending, so that code gives
the same output as f.write(ascii_data).


I know that Emile suggested that I can slice out the substrings rather
than do the gradual trimming of the string variable as is being done
by moving around the length.  So, I'm going to give that a try... I'm
a bit confused by what that means, am guessing that slice can break up
a string based on characters; will research.  Thanks for the help thus
far.  I'll post again when all is working fine.


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


Re: Python Equivalent for dd & fold

2009-07-16 Thread MRAB

Michiel Overtoom wrote:

seldan24 wrote:


I know that Emile suggested that I can slice out the substrings rather
than do the gradual trimming of the string variable as is being done
by moving around the length. 


An excellent idea.

def fold(s,chunklength):
offset=0
while offset
More Pythonic:

for offset in range(0, len(s), chunklength):
print s[offset : offset + chunklength]


s="A very long string indeed. Really that long? Indeed."
fold(s,10)



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


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread Piet van Oostrum
> mheavner  (m) wrote:

>m> 'The process' refers to the subprocess. I could do as you say, load
>m> the data structure each time, but the problem is that takes a
>m> considerable amount of time compared to the the actual computation
>m> with the data it contains. I'm using these processes within a loop as
>m> follows:

>m>  # Don't recreate processes or Queues
>m>  pop1 = Queue()
>m>  pop2 = Queue()
>m>  pop_out = Queue()
>m>  p1 = CudaProcess(0, args=(costf,pop1,pop_out))
>m>  p2 = CudaProcess(1, args=(costf,pop2,pop_out))

>m>  # Main loop
>m>  for i in range(maxiter):
>m>  print 'ITERATION: '+str(i)
>m>  if log != None:
>m>  l = open(log,'a')
>m>  l.write('Iteration: '+str(i)+'\n')
>m>  l.close()

>m>  # Split population in two
>m>  pop1.putmany(pop[0:len(pop)/2])
>m>  pop2.putmany(pop[len(pop)/2:len(pop)])

>m>  # Start two processes
>m>  if not p1.isAlive():
>m>  p1.start()
>m>  print 'started %s'%str(p1.getPid())
>m>  else:
>m>  p1.run()

That won't work. p1.run() will execute the run method in the Master
process, not in the subprocess. And if it would your could would have a
race condition: between the p1.isAlive() (which must be is_alive btw), and
the p1.run() the process can have stopped.

The proper way to do is to put the work in a Queue and let the processes
get work out of the Queue. The datastructure will remain in the process
then. 

>m>  if not p2.isAlive():
>m>  p2.start()
>m>  print 'started %s'%str(p2.getPid())
>m>  else:
>m>  p2.run()
>m>  .
>m>  .
>m>  .

>m> So I'd like to load that data into memory once and keep there as long
>m> as the process is alive (ideally when the subprocess is created,
>m> storing some sort of pointer to it), rather than loading it each time
>m> run is called for a process within the loop. Could be my CudaProcess
>m> class - I'll check out what Diez suggested and post back.

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


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread mheavner
I realize that the Queue would be the best way of doing this, however
that involves transferring the huge amount of data for each call - my
hope was to transfer it once and have it remain in memory for the
subprocess across run() calls.

On Jul 16, 1:18 pm, Piet van Oostrum  wrote:
> > mheavner  (m) wrote:
> >m> 'The process' refers to the subprocess. I could do as you say, load
> >m> the data structure each time, but the problem is that takes a
> >m> considerable amount of time compared to the the actual computation
> >m> with the data it contains. I'm using these processes within a loop as
> >m> follows:
> >m>          # Don't recreate processes or Queues
> >m>          pop1 = Queue()
> >m>          pop2 = Queue()
> >m>          pop_out = Queue()
> >m>          p1 = CudaProcess(0, args=(costf,pop1,pop_out))
> >m>          p2 = CudaProcess(1, args=(costf,pop2,pop_out))
> >m>          # Main loop
> >m>          for i in range(maxiter):
> >m>                  print 'ITERATION: '+str(i)
> >m>                  if log != None:
> >m>                          l = open(log,'a')
> >m>                  l.write('Iteration: '+str(i)+'\n')
> >m>                  l.close()
> >m>                  # Split population in two
> >m>                  pop1.putmany(pop[0:len(pop)/2])
> >m>                  pop2.putmany(pop[len(pop)/2:len(pop)])
> >m>                  # Start two processes
> >m>                  if not p1.isAlive():
> >m>                          p1.start()
> >m>                          print 'started %s'%str(p1.getPid())
> >m>                  else:
> >m>                          p1.run()
>
> That won't work. p1.run() will execute the run method in the Master
> process, not in the subprocess. And if it would your could would have a
> race condition: between the p1.isAlive() (which must be is_alive btw), and
> the p1.run() the process can have stopped.
>
> The proper way to do is to put the work in a Queue and let the processes
> get work out of the Queue. The datastructure will remain in the process
> then.
>
> >m>                  if not p2.isAlive():
> >m>                          p2.start()
> >m>                          print 'started %s'%str(p2.getPid())
> >m>                  else:
> >m>                          p2.run()
> >m>                  .
> >m>                  .
> >m>                  .
> >m> So I'd like to load that data into memory once and keep there as long
> >m> as the process is alive (ideally when the subprocess is created,
> >m> storing some sort of pointer to it), rather than loading it each time
> >m> run is called for a process within the loop. Could be my CudaProcess
> >m> class - I'll check out what Diez suggested and post back.
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected]

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


Re: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Inky 788
On Jul 16, 10:18 am, Tim Chase  wrote:
> > I realize that a small portion of the community likes the tabs.
> > They're sold on the tabs. They like the tabs. But tabs are an archaic
> > holdover from an era when typewriters had physical tabstops on them.
>
> However, they are a single logical level of indentation -- I come
> down fairly solidly on the "tabs" side of the "tabs vs. spaces"
> argument.

My bet is that the problem is this: some people like to format their
code in ways that don't work well when you're using tabs. For example,
they might want to call a function like this (note spaces):

some_func(foo=1,
  bar=2,
  baz=3)

instead of:

some_func(
foo=1,
bar=2,
baz=3)

The first one requires 10 spaces in front of bar and baz. If you're
using tabs, that means one or two tabs plus some space characters. So,
people who do that would probably never use tabs. The 2nd example is
fine for tabs or spaces. I'm sure there are a bunch of similar
examples for things besides function calls. Don't you ever find cases
where you'd like to add in an extra space or two to make things line
up nicely?

> I can set my editor (vim in this case) to show tabs as
> as many spaces as I want.  I usually have this set to 4, but
> sometimes 1 or 2.

Just curious: why would you want to do that? In my experience, once my
eyes got used to 4-space indents, everything else looks either too
little or too much. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant

Emile van Sebille wrote:

On 7/16/2009 7:04 AM Unknown said...

On 2009-07-16, Emile van Sebille  wrote:

daysInAdvance = int(inputVar) or 25


I don't get it.  That doesn't work right when inputVar == "0".

Aah, but you didn't get to define right.  :)  For that particular 
example 0 is not a valid response.


Emile

When I was talking about such error prone form of boolean operations, I 
didn't expect to be right so quickly :p
Steven explained the truth notion with the Something/Nothing. "0" is 
Something, 0 would be Nothing. I'm not sure it makes sens anyway. I 
mean, I could easily argue that the number 0 is something. In the end I 
wonder if I shouldn't just acknowledge the python mechanism without 
trying to find any intuitive/natural/obvious logic in it, knowing that 
sometimes the Truth lies far away from the Evidence.


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


Re: Ann: Google releases Python-based open-source NX server

2009-07-16 Thread jkn
On Jul 16, 3:51 pm, J Kenneth King  wrote:

> It's pretty cool, but not PEP8! Probably because they just bought the
> source off of another smaller proprietary project.  Makes me sad seeing
> Google, proud supporter of all things Python, release non-PEP8 code.

Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding
conventions for the Python code **comprising the standard library in
the main Python distribution**' (my emphasis). My coding conventions
are similar to, but not exactly the same as, PEP-8. It would be
ridiculous IMO (and PEP-8 acknowledges this) for such a permissive
language as Python to put a 'coding style' straitjacket around itself.

YMMV, of course.

J^n

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


Re: Ann: Google releases Python-based open-source NX server

2009-07-16 Thread Emile van Sebille

On 7/16/2009 10:59 AM jkn said...

Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding
conventions for the Python code **comprising the standard library in
the main Python distribution**' (my emphasis). My coding conventions
are similar to, but not exactly the same as, PEP-8. 


Mine too, and my objection to PEP-8 is that the coding style doesn't 
allow for copy-n-paste into the interpreter, and I like to be able to 
test that way.  Perhaps if I configured my editor not to strip trailing 
whitespace from lines it would work, but then I don't like trailing 
whitespace.


Of course, I _like_ leading whitespace.  :)

Emile

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


Re: Ann: Google releases Python-based open-source NX server

2009-07-16 Thread Robert Kern

On 2009-07-16 09:51, J Kenneth King wrote:

jkn  writes:


Google quietly releases open-source NX server ...written in Python,
apparently



Neatx can be downloaded from Google's code repository:



 Regards
 J^n


It's pretty cool, but not PEP8! Probably because they just bought the
source off of another smaller proprietary project.


No, it' just that Google does not use PEP8 for its internal style standard.

--
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: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Tim Chase

However, they are a single logical level of indentation -- I come
down fairly solidly on the "tabs" side of the "tabs vs. spaces"
argument.


My bet is that the problem is this: some people like to format their
code in ways that don't work well when you're using tabs. For example,
they might want to call a function like this (note spaces):

some_func(foo=1,
  bar=2,
  baz=3)

instead of:

some_func(
foo=1,
bar=2,
baz=3)


For continued indenting statements such as this, I tend to use 
the coding convention used at my first job out of college 
(Computer Sciences Corp...for better or worse) which just indents 
two levels:


  def some_func(foo=1,
  bar=2,
  baz=3):
  do_something(foo)
  do_other_stuff(bar)


examples for things besides function calls. Don't you ever find cases
where you'd like to add in an extra space or two to make things line
up nicely?


I have occasionally (okay, "very rarely") use the "mixed 
tab+space" method of indentation for continued lines if I want 
them lined up nicely:


  if (foo == bar and
  baz > frob and
  fred != barney):
  do_something()
  do_more()

This scheme insures that the visual alignment for the 
continued-line matches up, even if the tab-stop is changed.  The 
positioning of the indented block (the do_* bit) floats 
inconveniently with relation to the continued text, with pessimal 
cases being indistinguishable from the continued lines (which is 
why I generally opt not to use this unless it has great benefits 
in code clarity).  By regularly indenting continued lines for 
containing blocks (if, while, etc) by two tabs, the continuation 
stands out from the contained code regardless of my tab stops.



 I can set my editor (vim in this case) to show tabs as
as many spaces as I want.  I usually have this set to 4, but
sometimes 1 or 2.


Just curious: why would you want to do that? In my experience, once my
eyes got used to 4-space indents, everything else looks either too
little or too much. :)


It totally depends on the project -- I like the condensed nature 
of 2sp/tab for code sharing on mailing lists (and tend to copy 
out of vim with tabs expanded to 2 spaces for pasting into 
emails) and for my own visual preference.  If it's code that I'd 
expect anybody else to view, I tend to use 4sp/tab to keep my 
lines below 80 chars per line with the tabs taken into consideration.


I guess I change up my indent enough that sometimes 2 seems just 
right or too small, and sometimes 4 seems just right or too large.


-tkc




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


Re: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Carl Banks
On Jul 16, 6:32 am, Inky 788  wrote:
> On Jul 10, 7:35 pm, Ben Finney  wrote:
>
> > walterbyrd  writes:
> > > I believe Guido himself has said that all indentions should be four
> > > spaces - no tabs.
>
> > Yes. That's a “should” and not a “must”, even though PEP 8 says it
> > with a simple imperative::
>
> >     Use 4 spaces per indentation level.
>
> That actually sounds pretty weird. "Don't do {foo}. Really, I mean
> *really* don't do {foo}. Oh, also, the interpreter allows you to do
> {foo}. But don't do it! I mean it!".
>
> Very ... perlish, if you ask me.

Not Perlish at all.

(Perl would never want you not to use something.)


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


Re: Why aren't OrderedDicts comparable with < etc?

2009-07-16 Thread Terry Reedy

Mark wrote:

On 16 July, 10:21, Piet van Oostrum  wrote:



But why should the order be as if the OrderedDict was a list of tuples.
A dict can be considered as a mapping and then you might want to treat
either the key or the value as contravariant (the key I guess). So there
is ambiguity. Why would the view as a list of tuples for the ordering be
the `natural' view?

Maybe you may expect some kind of monotonicity such that d1

OK, that seems to me to be a convincing argument against supporting
ordering.


To put the above in a slightly different way. OrderedDicts are a 
recently added niche class that Raymond added to what is mostly his 
collections module because there are enough few use cases. There was 
pydev discussion which included the idea, I believe, that they should 
fundamentally be dicts, not lists. Regardless, the justifying use cases 
did not include a need to compare OrderedDicts. The small fraction of 
the few use cases for OrderedDicts that do require comparision can be 
met by extracting the needed sequences and comparing *them* in the 
appropriate manner. The 'appropriate manner' is not likely to always be 
the same. This point may have come up in the discussion, but I would let 
you check for sure if curious.


'Consistency' is a Python design principle, but it is balanced with 
others, so that it is not sufficient reason to add nearly useless 
features. There is a cost to every addition.


Terry Jan Reedy

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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread akhil1988

Hi,

Thanks all for the replies.

I am working on a cluster of 15 nodes and I have now installed python 3.1 on
all of them. I tried installing python2.6 but there was some make error. So,
I do not want to give more time in installing 2.4  and rather use 3.1 but
for that I need to convert my 2.4 code to 3.1. 

I used 2to3 tool, and it did make many changes in the 2.4 code, but still
there are some indentation errors that I am unable to resolve being new to
python. I have attached my python code, can anyone please fix the
indentation error in the code. I am using vi editor.

--Thanks a lot,
Akhil http://www.nabble.com/file/p24522412/temp.py temp.py 


alex23 wrote:
> 
> On Jul 16, 9:00 pm, akhil1988  wrote:
>> I have switched to python 3.1 , but now I am getting some syntax errors
>> in
>> the code:
> 
> Python 3.x was a major release that endeavoured to clean up a number
> of lingering issues with the language, the upshot being that it isn't
> entirely backwards compatible with past versions. Unicode became the
> default string type, which is what is causing the error here: the u-
> prefix is no longer required (or even allowed).
> 
> However, Py3.x _does_ come with a handy tool for automatically
> converting Python 2.x code to 3.x, called 2to3. One of the things it
> should do is convert Py2.x unicode values into their correct
> representation in 3.x.
> 
> With any luck, it should be able to convert the code you're using
> entirely. Let us know how it goes.
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24522412.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


URLError errno and strerror not set

2009-07-16 Thread 1x7y2z9

python 2.5.2

errno, strerror and message do not appear to be set in the following
two cases (at least).
Is this to be expected?  (as an aside, arg[0] is set)

# case 1
> print exception, exception.errno, exception.strerror, exception.message == ''
 None None True

# case 2
> print exception, exception.errno, exception.strerror, exception.message == ''
 None None True

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


Re: Persistent variable in subprocess using multiprocessing?

2009-07-16 Thread Piet van Oostrum
> mheavner  (m) wrote:

>m> I realize that the Queue would be the best way of doing this, however
>m> that involves transferring the huge amount of data for each call - my
>m> hope was to transfer it once and have it remain in memory for the
>m> subprocess across run() calls.

Which huge amount of data? The datastructure you talked about can remain
in the process. You only have to transfer the input for your calculation
in the queue but you have to do that anyway. And there is only one run
call per process. When run has terminated the process exits, so you
would have a loop in the run(0 method getting work from the queue.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing python list from C to python

2009-07-16 Thread Aahz
In article <0afc5c4d-1af5-4d0e-9442-26b51b12e...@m11g2000yqh.googlegroups.com>,
hartley   wrote:
>
>If you had loosened up on the sarcasm I would probably have read what
>you wrote more thoroughly instead of just skimming through it. Thanks
>for the help, but you should seriously consider doing something with
>your patronizing attitude.

http://www.mikeash.com/getting_answers.html
http://www.catb.org/~esr/faqs/smart-questions.html
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ann: Google releases Python-based open-source NX server

2009-07-16 Thread J Kenneth King
Robert Kern  writes:

> On 2009-07-16 09:51, J Kenneth King wrote:
>> jkn  writes:
>>
>>> Google quietly releases open-source NX server ...written in Python,
>>> apparently
>>>
>>> >> Google_quietly_releases_open_source_NX_server?taxonomyId=88>
>>>
>>> Neatx can be downloaded from Google's code repository:
>>>
>>> 
>>>
>>>  Regards
>>>  J^n
>>
>> It's pretty cool, but not PEP8! Probably because they just bought the
>> source off of another smaller proprietary project.
>
> No, it' just that Google does not use PEP8 for its internal style standard.

Yeah probably.

I'm just a little OCD sometimes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

2009-07-16 Thread akhil1988

ok!
I got the indentation errors fixed. Bu I get another error:

Traceback (most recent call last):
  File "./temp.py", line 484, in 
main()
  File "./temp.py", line 476, in main
line.decode('utf-8').strip()
AttributeError: 'str' object has no attribute 'decode'

I am using Python3.1

Thanks
Akhil




akhil1988 wrote:
> 
> Hi,
> 
> Thanks all for the replies.
> 
> I am working on a cluster of 15 nodes and I have now installed python 3.1
> on all of them. I tried installing python2.6 but there was some make
> error. So, I do not want to give more time in installing 2.4  and rather
> use 3.1 but for that I need to convert my 2.4 code to 3.1. 
> 
> I used 2to3 tool, and it did make many changes in the 2.4 code, but still
> there are some indentation errors that I am unable to resolve being new to
> python. I have attached my python code, can anyone please fix the
> indentation error in the code. I am using vi editor.
> 
> --Thanks a lot,
> Akhil http://www.nabble.com/file/p24522412/temp.py temp.py 
> 
> 
> alex23 wrote:
>> 
>> On Jul 16, 9:00 pm, akhil1988  wrote:
>>> I have switched to python 3.1 , but now I am getting some syntax errors
>>> in
>>> the code:
>> 
>> Python 3.x was a major release that endeavoured to clean up a number
>> of lingering issues with the language, the upshot being that it isn't
>> entirely backwards compatible with past versions. Unicode became the
>> default string type, which is what is causing the error here: the u-
>> prefix is no longer required (or even allowed).
>> 
>> However, Py3.x _does_ come with a handy tool for automatically
>> converting Python 2.x code to 3.x, called 2to3. One of the things it
>> should do is convert Py2.x unicode values into their correct
>> representation in 3.x.
>> 
>> With any luck, it should be able to convert the code you're using
>> entirely. Let us know how it goes.
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24523113.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: ImportError: No module named _functools

2009-07-16 Thread Tony Lay
On Jul 16, 10:14 am, [email protected] (Aahz) wrote:
> In article 
> <45228cf4-0b37-4c52-bf6f-d7bd124b0...@l32g2000vbp.googlegroups.com>,
> Tony  Lay   wrote:
>
>
>
> >Traceback (most recent call last):
> >  File "/usr/local/bin/meld", line 35, in 
> >    import gettext
> >  File "/usr/local/lib/python2.6/gettext.py", line 49, in 
> >    import locale, copy, os, re, struct, sys
> >  File "/usr/local/lib/python2.6/locale.py", line 15, in 
> >    import functools
> >  File "/usr/local/lib/python2.6/functools.py", line 10, in 
> >    from _functools import partial, reduce
> >ImportError: No module named _functools
>
> Where is _functools.so?
> --
> Aahz ([email protected])           <*>        http://www.pythoncraft.com/
>
> "If you think it's expensive to hire a professional to do the job, wait
> until you hire an amateur."  --Red Adair

/usr/local/lib/python2.6/lib-dynload/

It's in the path...
export PYTHONHOME=/usr/local/lib/python2.6
export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/
site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/
pango
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >