Re: python performance on Solaris

2009-10-14 Thread John Nagle

inaf wrote:

I have been following this group for quite some time and I figured
(after searching enough on google --and on this group-- and not
finding anything useful) I could pose this question here. Can anyone
shed some light on python's performance on Solaris? 


   Note that multithreaded compute-bound Python programs really suck
on multiprocessors.  Adding a second CPU makes the program go slower,
due to a lame mechanism for resolving conflicts over the global interpreter
lock.

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


Re: subprocess hangs on reading stdout

2009-10-14 Thread Minesh Patel
>
> Any ideas? comments on code welcome also.

Here's something that I would probably do, there may be better ways.
This only works on python2.6 for the terminate() method.


import signal
import subprocess

def timeout_handler(signum, frame):
print "About to kill process"
p.terminate()

for machine_name in self.alive:
cmd = ["/bin/remsh", machine_name, 'ps -flu %s' % uid]
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(1)
p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
signal.alarm(0)
if stdout:
   print stdout
elif stderr:
   print stderr



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


subprocess hangs on reading stdout

2009-10-14 Thread Tim Arnold
Hi, I'm querying a list of network servers for processes belonging to a 
specific user. The problem is that when I try to read the stdout from the 
subprocess it sometimes hangs. Not always though.

I thought maybe I needed to set unbufferered to true, so at the beginning of 
the code I set
os.environ['PYTHONUNBUFFERED'] = '1'
But I think it's more likely the subprocess that needs to be unbuffered.

Here's the bit of code:
---
for machine_name in self.alive:# a list of servers that responded to 
ping already.
cmd = ["/bin/remsh", machine_name, 'ps -flu %s' % uid]
finish = time.time() + 4.0
p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
while p.poll() is None:
time.sleep(0.5)
if finish < time.time():
p.kill()
print 'skipping' # this works ok
break

s = ''
if p:
s = p.stdout.read() # trhis will hang occasionally
if not s:
continue
---

Any ideas? comments on code welcome also.
thanks,
--Tim Arnold


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


Re: unicode-to-ascii: replace with space, not "?"

2009-10-14 Thread Gabriel Genellina
En Wed, 14 Oct 2009 23:08:53 -0300, Allen Fowler   
escribió:


I've been using "data.encode('ascii','replace')" to force an ASCII  
string out of Unicode data, with "?" in the place of non-ASCII letters.


However, now I want to use a blank space (or maybe a dash) instead of a  
question mark.


Use a custom encoding handler:

import codecs

def replace_spc_error_handler(error):
# error is an UnicodeEncodeError/UnicodeDecodeError instance
# with these attributes:
#   object = unicode object being encoded
#   start:end = slice of object with error
#   reason = error message
# Must return a tuple (replacement unicode object,
#   index into object to continue encoding)
# or raise the same or another exception
return (u' ' * (error.end-error.start), error.end)

codecs.register_error("replace_spc", replace_spc_error_handler)

print u"¡añá membuí!".encode("ascii", "replace_spc")
print "¡añá membuí!".decode("ascii", "replace_spc")


--
Gabriel Genellina

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


Re: unicode-to-ascii: replace with space, not "?"

2009-10-14 Thread Mark Tolonen


"Allen Fowler"  wrote in message 
news:59796.73163...@web45608.mail.sp1.yahoo.com...

Hello,

I've been using "data.encode('ascii','replace')" to force an ASCII string 
out of Unicode data, with "?" in the place of non-ASCII letters.


However, now I want to use a blank space (or maybe a dash) instead of a 
question mark.


How do I do this?


See codecs.register_error().  Here's a simplistic example:

# coding: utf-8
import codecs

def handler(e):
   return (u'-',e.start + 1)

codecs.register_error('mine',handler)
s = u'My name is 马克.'
print s.encode('ascii','mine')

OUTPUT:
My name is --.


-Mark 



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


Re: id( ) function question

2009-10-14 Thread Erik Max Francis

Tim Chase wrote:
CPython has the option to cache frequently used items, and does so for a 
small range of ints.  It's not guaranteed behavior (or a guaranteed 
range) so you shouldn't rely on it, but it's an efficiency thing.  In my 
current version, it looks like it's ints from -5 to 256. YMMV


In general, if you're using "is" (and not comparing with None) or id(), 
you're doing it wrong unless you already know the peculiarities of 
Python's identity implementations.


Right.  Another way to do look at it is that if you're curious about 
what the value of `id` is or how the `is` operator works, the short 
version is, don't worry about them, as you won't be using them.


I'm really rather surprised at the number of questions about them. 
They're really something one does not need to worry about.


--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
  All bad poetry springs from genuine feeling.
   -- Oscar Wilde
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python XMLRPC question

2009-10-14 Thread Gabriel Genellina
En Wed, 14 Oct 2009 22:08:09 -0300, prasanna   
escribió:



Out of curiosity--one more thing I haven't yet figured out, is there a
xmlrpc command I can send that stops or restarts the server?


If you're using Python 2.6, the easiest way is to register its shutdown()  
method. Note that it *must* be called from a separate thread (just inherit  
from ForkingMixIn)


On earlier versions, overwrite the serve_forever loop (so it reads `while  
not self._quit: ...`) and add a shutdown() method that sets self._quit to  
True. You'll need to call shutdown twice in that case.


=== begin xmlrpcshutdown.py ===
import sys

def server():
from SocketServer import ThreadingMixIn
from SimpleXMLRPCServer import SimpleXMLRPCServer

# ThreadingMixIn must be included when publishing
# the shutdown method
class MyXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
pass

print 'Running XML-RPC server on port 8000'
server = MyXMLRPCServer(("localhost", 8000),
logRequests=False, allow_none=True)
# allow_none=True because of shutdown
server.register_function(lambda x,y: x+y, 'add')
server.register_function(server.shutdown)
server.serve_forever()

def client():
from xmlrpclib import ServerProxy

print 'Connecting to XML-RPC server on port 8000'
server = ServerProxy("http://localhost:8000";)
print "2+3=", server.add(2, 3)
print "asking server to shut down"
server.shutdown()

if sys.argv[1]=="server": server()
elif sys.argv[1]=="client": client()
=== end xmlrpcshutdown.py ===


C:\TEMP>start python xmlrpcshutdown.py server

C:\TEMP>python xmlrpcshutdown.py client
Connecting to XML-RPC server on port 8000
2+3= 5
asking server to shut down

C:\TEMP>

--
Gabriel Genellina

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


Re: What command should be use when the testing of arguments is failed?

2009-10-14 Thread Roger Binns
Peng Yu wrote:
> I actually wanted to ask what return code should be returned in this
> case when the arguments are not right. Thank you1

The BSD world attempted to standardize the codes so you may as well use
their definitions.  You can see them in /usr/include/sysexits.h on your
nearest Linux/Unix box (usually).  There is also an equivalent man page:

  http://www.freebsd.org/cgi/man.cgi?query=sysexits&sektion=3

The posix module also defines these constants if known for the platform.

>>> import os
>>> os.EX_USAGE
64

Doesn't help on non-Unix platforms though :-)

Roger

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


Re: id( ) function question

2009-10-14 Thread Luis Alberto Zarrabeitia Gomez

> It's believable if id({}) does the following:
> 
> 1. Construct an empty dict
> 2. Take the id of the dict
> 3. Reduce the reference-count on the now-unneeded dict.
> 
> It's not too hard for the second empty dict to get allocated in the same 
> memory that the first one (now dereferenced and deallocated) used, so 
> CPython gives it the same id value.

Wow, I never thought about it, but at least in my system, it seems to work like
that:

In [6]: id({1:2}) == id({3:4})
Out[6]: True

Interesting...
(only as a curiosity, though... One shouldn't rely on that)

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie

-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu

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


Re: id( ) function question

2009-10-14 Thread Mel
Chris Rebert wrote:
> Although I have no idea how it is that `id({}) == id({})` as a prior
> posted showed; FWIW, I can't manage to reproduce that outcome.

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> id({})== id({})
True


It's believable if id({}) does the following:

1. Construct an empty dict
2. Take the id of the dict
3. Reduce the reference-count on the now-unneeded dict.

It's not too hard for the second empty dict to get allocated in the same 
memory that the first one (now dereferenced and deallocated) used, so 
CPython gives it the same id value.

When the == comparison happens, all it needs are the two ints returned from 
the id calls.

Mel.


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


unicode-to-ascii: replace with space, not "?"

2009-10-14 Thread Allen Fowler
Hello,

I've been using "data.encode('ascii','replace')" to force an ASCII string out 
of Unicode data, with "?" in the place of non-ASCII letters.

However, now I want to use a blank space (or maybe a dash) instead of a 
question mark.

How do I do this?

Thank you,
:)



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


Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 12:07�pm, Ethan Furman  wrote:
> Mensanator wrote:
> > On Oct 14, 2:19 am, Dennis Lee Bieber  wrote:
>
> >>On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
> >> declaimed the following in
> >>gmane.comp.python.general:
>
> >>>You're not getting away that easy.
>
> >>>What's YOUR opinion of "whilr True"?
>
> >> Uhm... that it isn't valid in any language having English influence
> >>upon it's keywords...
>
> > Duh. You DO know that 'r' is next to 'e' on the
> > keyboard?
>
> Not on mine -- it's next to 'o' and 'u'. �:-) �Go Dvorak!

Does that mean you think "whilr" is a word?

>
> >> If anything -- I'd suggest a proposal to add a plain loop as a
> >>keyword in Python, whose effect is equivalent to a "while True", but a
> >>break must be used to exit said loop (well, we'll ignore raising an
> >>exception )
>
> > And what will that accomplish? The problem isn't
> > using while True, it's the fact that you are
> > escaping the loop. Best Practice is to EXIT the
> > loop properly, not escape from it.
>
> I don't think anyone's arguing the opposite. �

I get the impression many people are, in fact,
arguing the opposite.

> What I *am* seeing argued
> is if it's the only correct way to do it, and that anyone who does it
> any other way is a scoundrel and a knave. �;-)

First of all, _I_ didn't bring up the concept of Best Practice,
nor have I insisted Best Practice means there is only one
correct way to do something. I interpret it as meaning there
may be many correct ways to do something, but of those,
this is the best way, barring special circumstances.

>
> For what it's worth, most of my loops run to completion, with no sign of
> a break anywhere. �Some have a break, and use it. �Some, even, (dare I
> say it?) use break *and* else! �

Breaks can be used properly, but it's easy to use them
improperly. How many of your loops start "while True"?

> And it's awesome! �Go Python! �:-D
>
> ~Ethan~

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


Re: What command should be use when the testing of arguments is failed?

2009-10-14 Thread Cameron Simpson
On 14Oct2009 19:58, Peng Yu  wrote:
| On Wed, Oct 14, 2009 at 7:57 PM, Joe Riopel  wrote:
| > On Wed, Oct 14, 2009 at 8:53 PM, Peng Yu  wrote:
| >> I have the following python code snippet. I'm wondering what command I
| >> should use to terminate the program if the arguments are not right.
| >
| > I usually use sys.exit.
| 
| I actually wanted to ask what return code should be returned in this
| case when the arguments are not right. Thank you1

It should be non-zero (0 means "success"). Generally, having lots of possible
values for failure (anything non-zero) lets programs indicate the reason for
the failure. For example, rsync and sendmail have specific exit codes for
specific problems. Most programs don't have or need that degree of detail.
Unless there are special circumstances my own convention is to use 2 for a
usage/wrong-arguments failure and 1 for "this didn't work".

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Don't have awk? Use this simple sh emulation:
#!/bin/sh
echo 'Awk bailing out!' >&2
exit 2
- Tom Horsley 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What command should be use when the testing of arguments is failed?

2009-10-14 Thread Xavier Ho
On Thu, Oct 15, 2009 at 10:58 AM, Peng Yu  wrote:

> I actually wanted to ask what return code should be returned in this
> case when the arguments are not right. Thank you1
>
>
I think that depends on the design of the program. Is there a return value
that would make sense to be returned by default? If not, None is usually a
good choice. Be sure to put these return values in the doc string.

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


Re: Python XMLRPC question

2009-10-14 Thread prasanna
On Oct 13, 1:22 pm, "Gabriel Genellina" 
wrote:
> En Tue, 13 Oct 2009 16:55:09 -0300, Falcolas  escribió:
>
>
>
>
>
> > On Oct 13, 12:47 pm,prasanna wrote:
> >> In using Python's XMLRPC, there is a statement that gets printed on
> >> stdout of the form:
> >>                  localhost - - [12/Oct/2009 23:36:12] "POST /RPC2 HTTP/
> >> 1.0" 200 -
>
> >> Where does this message originate? Can I turn it off, or at least
> >> redirect it into a logging file? I am planning to run the server code
> >> automatically at start up and wouldn't have a terminal window open to
> >> get this message. I guess I could redirect/pipe it to a file, but it
> >> would be more useful I could send it to the same log file that I have
> >> the server writing other messages to.
>
> >> Thanks for any help.
>
> > Looking back through the SimpleXMLRPCServer code, it appears that this
> > happens if the logRequests parameter in the SimpleXMLRPCServer class
> > initialization is set to True, which it is by default. The underlying
> > implementation of the logging is in BaseHTTPServer.py, which uses
> > sys.stderr.
>
> > Looks like the simplest way to change that would be to inherit from
> > the SimpleXMLRPCRequestHandler class and implement your own
> > log_request method. You could then pass that to the SimpleXMLRPCServer
> > constructor.
>
> I think it's easier to pass logRequests=False when creating the server.
>
> --
> Gabriel Genellina

Thanks. That helped get rid of the message.

Out of curiosity--one more thing I haven't yet figured out, is there a
xmlrpc command I can send that stops or restarts the server?

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


Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 6:08�pm, Steven D'Aprano  wrote:
> On Wed, 14 Oct 2009 09:34:28 -0700, Mensanator wrote:
> > On Oct 14, 2:19 am, Dennis Lee Bieber  wrote:
> >> On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
> >>  declaimed the following in
> >> gmane.comp.python.general:
>
> >> > You're not getting away that easy.
>
> >> > What's YOUR opinion of "whilr True"?
>
> >> � � � �Uhm... that it isn't valid in any language having English
> >> influence upon it's keywords...
>
> > Duh. You DO know that 'r' is next to 'e' on the keyboard?
>
> Only on QWERTY keyboards. Not on Dvorak keyboards.

Does anyone actually use those things?

>
> Do you know how to proof-read your writing before hitting send?

Yeah. And I missed it. Maybe because my laptop has a few
broken columns of pixels.

> If not,
> please learn. A spell checker may help.

Not with Google.

> If you do know how, if you care
> so little for what you write that you can't be bothered, why should
> anyone care enough to read what you write?

Conclusion based on false premise.

> Either way, there's no call
> for you to be snarky when people call you on stupid typos.

I suppose this wasn't snarky:

> >>Uhm... that it isn't valid in any language having English
> >> influence upon it's keywords...

> Your mistake
> could happen to anyone, but it was still *your* mistake.

No, it wasn't. You should learn the difference between
an error and a mistake. There's nothing to be learned from
pointing out a typo. Whereas a mistake, such as using
"their" in place of "there" should be pointed out so
as to prevent future occurences.

>
> [...]
>
> > And what will that accomplish? The problem isn't using while True, it's
> > the fact that you are escaping the loop.
>
> That's not a problem.

It can be.

>
> > Best Practice is to EXIT the loop properly, not escape from it.
>
> A break does exit the loop properly. That's what it is for, it would be a
> pretty stupid language that had break exit the loop improperly.
>
> Nobody is forcing you to use break.

And no one is forcing the OP to stop using "while True"
even if it is not considered Best Practice.

> You can write Pascal in any language you like.

My experience with Pascal is it tended to produce
bullet-proof code. The lessons learned from Pascal
can only make my Python programs better.

> > --
> Steven

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


Re: What command should be use when the testing of arguments is failed?

2009-10-14 Thread Peng Yu
On Wed, Oct 14, 2009 at 7:57 PM, Joe Riopel  wrote:
> On Wed, Oct 14, 2009 at 8:53 PM, Peng Yu  wrote:
>> I have the following python code snippet. I'm wondering what command I
>> should use to terminate the program if the arguments are not right.
>
> I usually use sys.exit.

I actually wanted to ask what return code should be returned in this
case when the arguments are not right. Thank you1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What command should be use when the testing of arguments is failed?

2009-10-14 Thread Joe Riopel
On Wed, Oct 14, 2009 at 8:53 PM, Peng Yu  wrote:
> I have the following python code snippet. I'm wondering what command I
> should use to terminate the program if the arguments are not right.

I usually use sys.exit.
-- 
http://mail.python.org/mailman/listinfo/python-list


What command should be use when the testing of arguments is failed?

2009-10-14 Thread Peng Yu
I have the following python code snippet. I'm wondering what command I
should use to terminate the program if the arguments are not right.

#!/usr/bin/env python

import sys
import os

if len(sys.argv) <= 1:
  print "usage:", os.path.basename(sys.argv[0]), ''
  return ## what command should be used here to terminate the program?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Paul Rubin
Steven D'Aprano  writes:
> Why should Python make that guarantee about this hypothetical "loop 
> forever" construct?

It doesn't make much sense for Python as normally practiced.
Termination proofs (aka proofs of progress) are used in formal
verification systems to make sure that the verification logic is
consistent and that type-level inferences are valid.  They generally
have very little to do with making sure that the program's actual
running time is bounded by anything reasonable.  In fact infinite
loops are permitted in some such systems, but only on infinite data
streams (e.g. the request stream of a web server).

  http://en.wikipedia.org/wiki/Total_functional_programming 

cites a pretty accessible paper by D. Turner that explains the
idea in more detail.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: id( ) function question

2009-10-14 Thread Stephen Hansen
On Wed, Oct 14, 2009 at 4:19 PM, Chris Rebert  wrote:

> Although I have no idea how it is that `id({}) == id({})` as a prior
> posted showed; FWIW, I can't manage to reproduce that outcome.
>

With Python 2.5.1 on MacOS X, I can; it looks like there's an optimization
in there where its 'saving' dictionaries that are created but not used, and
then using those saved dictionaries until they're really used, instead of
making new ones all the time.

At least that's how I interpret this output (comments added after-the-fact):
>>> id({}) # at this point a dictionary appears to be created  (though it
may have happened earlier)
6296752
>>> id({}) # previous dictionary wasn't used, so was saved; and now instead
of creating a new one, it'll just use what's on the waiting list
6296752
>>> id({}) == id({})
True
>>> x = {} # that same dictionary still hasn't been used, but now it is
right here-- so the waiting list is empty.
>>> y = {} # and a new one is made!
>>> id(x)
6296752
>>> id(y)
6299344
>>> id({})
6349136
>>> id({})
6349136
>>> z = {}
>>> id(z)
6349136
>>> id({})
7214832

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


Re: id( ) function question

2009-10-14 Thread Christian Heimes
Chris Rebert wrote:
> The built-ins aren't mutable, and the singletons are each immutable
> and/or unique; so in no case do objects that are both different and
> mutable have the same ID.

Correct, the fact allows you to write code like "type(egg) is str" to
check if an object *is* an instance of str. The isistance() methods also
returns True if the argument is a subclass of str.

> Although I have no idea how it is that `id({}) == id({})` as a prior
> posted showed; FWIW, I can't manage to reproduce that outcome.

The behavior is caused by a free list in the dict implementation. When
the reference count of a object drops to zero, it's usually removed from
memory entirely. However some types keep a list of unused objects around
to increase efficiency and spare some calls to malloc() and free().

For the code "{} is {}" Python has to create two distinct dict objects.
"id({}) == id({})" returns True under most circumstances because the
reference count of the first dict drops to zero as soon as the id()
function returns its memory address. The second call to id() retrieves
the very same template from the dict type's free list thus returning the
same memory address.

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


Re: id( ) function question

2009-10-14 Thread Stephen Hansen
On Wed, Oct 14, 2009 at 1:37 PM, Laszlo Nagy  wrote:
>
>  Andre Engels schrieb:
>>
>>>
>>> None, True, False, NotImplemented are guaranteed to be singletons, all
>> builtin types and exceptions can be considered as singletons, too.
>>
>>
> I thought that different mutable objects always have different ids. If this
> is not true, then what the id() function is used for? What useful thing can
> we do with it?
>

They do.

None, True, False, integers and strings are not mutable. The only time the
id is the "same" between two objects is if they are the identical two
objects.

CPython just (as a performance optimization) re-uses the same objects
sometimes even if people think they're using different objects.

In,
>>> a = 2
>>> b = 2

The id is the same, as they're the same integer object. Its not two separate
integer objects. Their ID will be the same, as they're precisely the same
object.

But,
>>> a = 1e100
>>> b = 1e100

Those are two separate objects, because Python's not re-using an existing
object the second time it sees 1e100.

Christian's point is, I believe, that this is all an implementation detail
to the CPython platform and not a language-defined feature. Other
implementations may do other things, so one should not rely on this
behavior. Basically, don't use "is" unless you really know what you're doing
-- or are testing verses a singleton :) "is" is never the right thing for
numbers. Usually. Ahem.

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


Re: id( ) function question

2009-10-14 Thread Chris Rebert
On Wed, Oct 14, 2009 at 1:37 PM, Laszlo Nagy  wrote:
>> Andre Engels schrieb:
>>> What is going on is that a few objects that are often used, in
>>> particular the small (how small is small depends on the
>>> implementation) integers, are 'preloaded'. When one of these is then
>>> referred to, a new object is not created, but the pre-defined object
>>> is used. 10 is apparently a preloaded constant in your implementation,
>>> 1e10 is not.
>>>
>>> As far as I know, only None is _guaranteed_ to be such a preloaded
>>> object, so one should not rely on it in implementations.
>>>
>>
>> None, True, False, NotImplemented are guaranteed to be singletons, all
>> builtin types and exceptions can be considered as singletons, too.
>>
>
> I thought that different mutable objects always have different ids. If this
> is not true, then what the id() function is used for? What useful thing can
> we do with it?

The built-ins aren't mutable, and the singletons are each immutable
and/or unique; so in no case do objects that are both different and
mutable have the same ID.
Although I have no idea how it is that `id({}) == id({})` as a prior
posted showed; FWIW, I can't manage to reproduce that outcome.

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


Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 20:17:40 +, Jorgen Grahn wrote:

>> But we have exceptions. And I know somebody, in other languages, thinks
>> it's a Best Practice to avoid using exceptions for flow control.
> 
> A lot of C++ programmers think so, and Stroustrup himself says
> "exceptions are for exceptional things" or something to that effect. Is
> that what you're thinking of?
> 
> Thankfully, Stroustrup doesn't use the dreaded phrase "Best Practice",
> which as far as I can tell is designed to shut down rational thought in
> the audience.
> 
>> Thankfully, python programmers are less dogmatic, and use whatever
>> makes sense to use. I hope.
> 
> Calling it "dogmatic" is unfair.  C++ is very different from Python, and
> has a different implementation of exceptions. You also tend to use the
> language to solve a different set of problems.
> 
> That said, I still don't fully understand the rationale behind that
> advice or rule ... so I'm willing to break it, and sometimes I do.

Setting up a try...except block is cheap in Python. According to my 
tests, the overhead is little more than that of a single pass statement.

But actually raising and catching the exception is not cheap. If you use 
a lot of exceptions for flow control, performance will probably suffer.

In C++, exceptions are expensive, whether you catch one or not.

Also, using exceptions this way is a structured form of GOTO -- it's easy 
to abuse and turn it into spaghetti code. Actually, not that easy to 
abuse, because you can't jump back into the try block. It's more like a 
multi-level break outside of a loop than a general GOTO.




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


Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 18:30:06 +0100, Tim Rowe wrote:

> 2009/10/14 Dennis Lee Bieber :
> 
>>        If anything -- I'd suggest a proposal to add a plain    loop
>>           as a
>> keyword in Python, whose effect is equivalent to a "while True", but a
>> break    must be used to exit said loop (well, we'll ignore raising an
>> exception )
> 
> And with enough static analysis to guarantee that the break will be
> reached? I think it would be a bit much to expect Python to solve the
> halting problem!

That's a stupid objection. Python doesn't guarantee that any of the 
following will halt:

for x in iterator:
pass

while flag:
pass

for x in [1, 10, 20, 10**100]:
time.sleep(x)

(Technically, that last one will eventually halt, if you're prepared to 
wait long enough... about a billion trillion trillion trillion trillion 
trillion trillion trillion years.)


Why should Python make that guarantee about this hypothetical "loop 
forever" construct?





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


Re: The rap against "while True:" loops

2009-10-14 Thread Steven D'Aprano
On Wed, 14 Oct 2009 09:34:28 -0700, Mensanator wrote:

> On Oct 14, 2:19�am, Dennis Lee Bieber  wrote:
>> On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
>>  declaimed the following in
>> gmane.comp.python.general:
>>
>> > You're not getting away that easy.
>>
>> > What's YOUR opinion of "whilr True"?
>>
>>Uhm... that it isn't valid in any language having English
>> influence upon it's keywords...
> 
> Duh. You DO know that 'r' is next to 'e' on the keyboard?

Only on QWERTY keyboards. Not on Dvorak keyboards.

Do you know how to proof-read your writing before hitting send? If not, 
please learn. A spell checker may help. If you do know how, if you care 
so little for what you write that you can't be bothered, why should 
anyone care enough to read what you write? Either way, there's no call 
for you to be snarky when people call you on stupid typos. Your mistake 
could happen to anyone, but it was still *your* mistake.


[...]
> And what will that accomplish? The problem isn't using while True, it's
> the fact that you are escaping the loop. 

That's not a problem.


> Best Practice is to EXIT the loop properly, not escape from it.

A break does exit the loop properly. That's what it is for, it would be a 
pretty stupid language that had break exit the loop improperly.

Nobody is forcing you to use break. You can write Pascal in any language 
you like.



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


Re: id( ) function question

2009-10-14 Thread Laszlo Nagy



Andre Engels schrieb:
  

What is going on is that a few objects that are often used, in
particular the small (how small is small depends on the
implementation) integers, are 'preloaded'. When one of these is then
referred to, a new object is not created, but the pre-defined object
is used. 10 is apparently a preloaded constant in your implementation,
1e10 is not.

As far as I know, only None is _guaranteed_ to be such a preloaded
object, so one should not rely on it in implementations.



None, True, False, NotImplemented are guaranteed to be singletons, all
builtin types and exceptions can be considered as singletons, too.
  
I thought that different mutable objects always have different ids. If 
this is not true, then what the id() function is used for? What useful 
thing can we do with it?


Thanks,

L

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


Re: The rap against "while True:" loops

2009-10-14 Thread Rhodri James

On Wed, 14 Oct 2009 02:26:17 +0100, Mensanator  wrote:


On Oct 13, 5:38�pm, "Rhodri James" 
wrote:
On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator   
wrote:

> And I'm not saying John nor the OP should stop
> using what works for them. But there are certainly
> valid reasons for "don't use while True" to be
> on the "Best Practices" list.

Unfortunately, some of them seem to be reasons from
my point of view to put "*do* use while True" on the
"Best Practices" list. �


Really? Which ones?


Some of the constructs you
seem to like ring big alarm bells with me, because
I've found entirely too many bugs hidden by them.


For example?


Well, this one's always popular:

done = False
while not done:
  do_stuff()
  done = worry_about_stuff()
  do_more_stuff_at_great_length()
  done = worry_about_more_stuff()
  and_so_on()

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


Re: efficient running median

2009-10-14 Thread Ethan Furman

Janto Dreijer wrote:

On Oct 13, 7:37 pm, Ethan Furman  wrote:


Janto Dreijer wrote:


I'm looking for code that will calculate the running median of a
sequence, efficiently. (I'm trying to subtract the running median from
a signal to correct for gradual drift).



My naive attempt (taking the median of a sliding window) is
unfortunately too slow as my sliding windows are quite large (~1k) and
so are my sequences (~50k). On my PC it takes about 18 seconds per
sequence. 17 of those seconds is spent in sorting the sliding windows.



I've googled around and it looks like there are some recent journal
articles on it, but no code. Any suggestions?



Thanks
Janto


You might look athttp://pypi.python.org/pypi/blist/0.9.4

~Ethan~



Very nice! I assume you mean I can use it to quickly insert items into
the sliding window?

Thanks
Janto


I'm afraid I can't help any further.  Going from your post, I thought a 
quicker list implementation might be useful, but beyond that I have no 
knowledge to share.


Who said ignorance is bliss?  *hangs head*

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


Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/12 RDrewD :
> I was a bit surprised that nobody in this discussion so far bantered
> around the phrase "loop invariant", but then I looked in
> http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in
> so much formalism that it's sure to put off all but the most dedicated
> of Computer Science fans.

I think in this case the loop variant is more use than the loop variant.

Basically, the loop variant is what is true on every pass of the loop.
If you're being formal, you have to show that it's true on entry to
the loop and remains true on every pass of the loop. That means that
on exit from the loop you can guarantee the loop invariant and the
exit condition.

The loop variant is a finite natural number (positive or zero integer)
that is guaranteed to decrease on every pass of the loop. Because a
finite natural number cannot decrease indefinitely, if you can define
a loop variant then you gurantee that the loop will terminate.

Even if you are not being formal, just considering what the loop
variants and invariants can save no end of trouble with tricky loops.

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


Re: The rap against "while True:" loops

2009-10-14 Thread Jack Norton

kj wrote:


I'm coaching a group of biologists on basic Python scripting.  One
of my charges mentioned that he had come across the advice never
to use loops beginning with "while True".  Of course, that's one
way to start an infinite loop, but this seems hardly a sufficient
reason to avoid the construct altogether, as long as one includes
an exit that is always reached.  (Actually, come to think of it,
there are many situations in which a bona fide infinite loops
(typically within a try: block) is the required construct, e.g.
when implementing an event loop.)

I use "while True"-loops often, and intend to continue doing this
"while True", but I'm curious to know: how widespread is the
injunction against such loops?  Has it reached the status of "best
practice"?

TIA!

kynn
  
This thread has gotten a lot of posts concerning programming practices 
and dogma alike.  I'd like to add a personal use of `while True:` that 
has nothing to do with either best practices or dogma. 
I use python a *lot* to do day-to-day tasks in an engineering lab.  I 
use it to control, log, or otherwise converse with rs232 based gear, as 
well as use it to back up or organize documents, etc... (lo and behold, 
I use this scripting language to write little scripts here and there).  
Don't get me wrong, I also write full blown control/logging apps with 
python, but that is only 10% of my usage. 
Whenever I need to quickly log something (serial output of a device) 
quickly, I find myself writing this in the python REPL:

import serial
comport = serial.Serial('COMx', timeout=1)
while True:
   get = comport.readline()
   f.open("blah", 'a')
   f.write(get)
   f.close()

It is short enough that I don't see the need to write my own module.  
Sometimes I even add a little regexp based filtering -- which adds 2 
lines total.  When I am done logging I just give 'er a CTRL-C and be 
done with it.  It is also a hell of a lot less buggy and error prone 
than hyperterminal, which my boss uses to do the same thing. 
I think this is a perfect example of `while True:` that works damn well, 
and there isn't anything that can replace its simplicity.  Programming 
practices be damned, it is invaluable, and I would recommend doing it in 
my situation to any person, regardless of programming experience. 
Food for thought.


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


Re: Poll on Eval in Python

2009-10-14 Thread TerryP
On Oct 14, 9:48 pm, Kazimir Majorinc  wrote:
> Do you think
> it would be better if I asked that? That result would
> be significantly different?
>

Not really. The eval, exec, and compile builtins are more or less
related and serve similar purposes, but don't seem to be highly used
in Python. There's just not a great need of it in many typical
applications. That being said though, a language without the ability
to eval is like dancing with cement shoes ;).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
On Wed, Oct 14, 2009 at 10:30 AM, Zero Piraeus  wrote:

> '(?:etc)' instead of '(etc)' are non-grouping parentheses (since you
> apparently don't care about that bit).

Ah yes, thanks.

> '[^\]]' instead of '[\w\s]' matches "everything except a closing bracket".

I originally had just '[^\]', and I couldn't figure out why it
wouldn't work.  Maybe I need new glasses.

> The '\s*' before the second set of parentheses takes out the leading
> whitespace that would otherwise be returned as part of the match.

And I want that.  The next line of my code is:

description = m.group(2).strip() + "\n\n"

-- 
Timur Tabi
Linux kernel developer at Freescale
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient running median

2009-10-14 Thread Janto Dreijer
On Oct 14, 4:53 pm, Peter Otten <__pete...@web.de> wrote:
> Some numbers:
>
> 10.197 seconds for running_median_scipy_medfilt
> 25.043 seconds for running_median_python
> 13.040 seconds for running_median_python_msort
> 14.280 seconds for running_median_python_scipy_median
> 4.024 seconds for running_median_numpy
> 0.221 seconds for running_median_insort
>
> What would be an acceptable performance, by the way?
>

That's great!
Well, the faster it works, the better. It means I can process more
data before getting frustrated. So if you have a faster version I'd
like to see it :)

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


Re: Poll on Eval in Python

2009-10-14 Thread Kazimir Majorinc

On 14.10.2009 17:55, TerryP wrote:


And what about exec?


(note: exec in python is more in spirit of eval then C-style exec
functions)


I thought about that, but decided not to ask about it
in poll, because I wanted to compare opinions on eval
specifically, not on all similar features. Do you think
it would be better if I asked that? That result would
be significantly different?


--
Kazimir Majorinc
blog: http://kazimirmajorinc.blogspot.com

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


Re: windows side-by-side configuration woes on windows HPC

2009-10-14 Thread Nick Touran
I've made enough progress to get my setup working! I attempted to apply the
patch and recompile the packages that complained but got in too deep while
compiling matplotlib on windows (I don't have admin privileges and can't
install the prereqs) so I dug deeper.

I found this: http://blog.kalmbachnet.de/?postid=80 which suggests removing
the publicKeyToken attribute from the manifests to force local DLLs. This
would possibly give the same effect as not embedding the manifests in the
first place using the patch. So I went in, using VS2008, and removed this
attribute from the embedded manifests in python26.dll, python.exe, and the
manifest file in C:\python26. I also copied msvcm90.dll, msvcp90.dll, and
msvcr90.dll from the 9.0.21022.8 folder in c:\Windows\WinSxS into
c:\Python26 W.ith that, matplotlib started working, but pymssql still did
not.

I then renamed _mssql.pyd to _mssql.dll so that VS2008 could recognize the
manifest, removed the publicKeyToken attribute, and renamed it back to
_mssql.pyd, but it still complained. Finally, using depends.exe from the
internet, I noticed msvcr71.dll was required on my local machine, so I tried
copying it over to the remote machine, into the site-packages folder. Bam.
It worked. So while I don't really consider this a solution, it definitely
worked for what I needed. Any other 3rd party modules that complain in the
future in my weird xcopy-deployment will undergo manifest-stripping via
VS2008 and other dependency checking via depends.exe. Yay.

-nick

On Mon, Oct 12, 2009 at 2:41 PM, M.-A. Lemburg  wrote:

> Nick Touran wrote:
> > It is indeed a pain. I would really like a work-around. Matplotlib is
> > supposed to be immune to this nowadays but it's not. Nor are some other
> > third-party modules. Did they break with the new release? (2.6.3?)
>
> The main problem appears to be that the the MS VC9 compiler defaults
> to embedding a dependency on the MS VC90 CRT DLL into extension modules:
>
>  
>
>   version="9.0.21022.8"
> processorArchitecture="x86"
> publicKeyToken="1fc8b3b9a1e18e3b">
>
>  
>
> Unless you have installed the CRT runtime DLLs installed system-wide,
> this will require the DLLs to be installed next to the extension
> module DLL or PYD file... and that even though the Python process
> itself will already have loaded the DLL from the Python directory.
>
> A work-around is attached to the ticket as patch.
>
> Even though a fix for distutils is planned in 2.6.4, this type of
> problem will pop up for all kinds of software using VC90-based DLLs
> as plugins, so it's probably better to just install the CRT runtime
> DLLs in the WinSxS directory using the CRT installers:
>
> x86:
>
> http://www.microsoft.com/downloads/details.aspx?familyid=A5C84275-3B97-4AB7-A40D-3802B2AF5FC2&displaylang=en
>
> x86_64:
>
> http://www.microsoft.com/downloads/details.aspx?familyid=BA9257CA-337F-4B40-8C14-157CFDFFEE4E&displaylang=en
>
> > On Thu, Oct 8, 2009 at 1:12 PM, M.-A. Lemburg  wrote:
> >
> >> Nick Touran wrote:
> >>> Copying my local copy of Python 2.6 to a Windows HPC 2008 system is
> >> giving
> >>> dll side-by-side configuration errors for some third-party packages
> >>> (matplotlib, pyMSSQL, in particular). I understand that there is a
> >> tradition
> >>> of Python supporting XCOPY deployment, and would really like to be able
> >> to
> >>> just copy my C:\python26 folder to the network drive and have it run on
> >> the
> >>> server.
> >>>
> >>> I got around a related issue (http://bugs.python.org/issue4566) just
> by
> >>> upgrading to 2.6.3 and was able to import socket and mpi4py and
> >> everything,
> >>> except matplotlib and pyMSSQL, that is.
> >>>
> >>> I also understand that if I were to install the MS Visual Studio 2008
> >>> redistribution package on the server that everything would be fine
> >> because
> >>> the modules just can't find the proper C run-time DLL. The problem with
> >> that
> >>> is two-fold: I don't have admin rights on the machine and there are
> over
> >>> 1000 machines on the cluster and I don't think the admin is going to
> >> install
> >>> that on all of them.
> >>>
> >>> So is there any way to set an environmental variable or something to
> get
> >>> these packages to know where to find the proper msvcr90.dll, akin to
> >> setting
> >>> LD_LIBRARY_PATH in Linux? Is there another solution?
> >>
> >> I assume this is related to this new problem:
> >>
> >>http://bugs.python.org/issue4120
> >>
> >> Manifests were meant to solve some of the DLL mess... apparently they
> >> cause even more grief.
> >>
> >> --
> >> Marc-Andre Lemburg
> >> eGenix.com
> >>
> >> Professional Python Services directly from the Source  (#1, Oct 08 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/
> >> __

Re: python performance on Solaris

2009-10-14 Thread inaf
On Oct 14, 7:15 am, Antoine Pitrou  wrote:
> inaf  gmail.com> writes:
>
>
>
> > Good point. I failed to compare the CPU power on these machines.. 32
> > bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
> > guess that explains :) Thank you for the tip..
>
> You have to compare not only CPU frequencies but the CPU models.
> Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
> "UltraSPARC T2" or Niagara CPUs) which have, by design, very poor
> single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
> difference in single-threaded performance compared to a modern Intel or AMD 
> CPU
> is totally expected.
>
> Regards
>
> Antoine.

Antonie -- yes, you are right. Even the architecture of the two types
make a difference. I was under the impression that RISC based CPUs did
not need to have a very high clock speed and that they can perform
similarly compared to an x86 processor with higher clock speed. That
is why I was a bit surprised. I guess there could be other factors at
play. That's why I was asking if there are specific things to be done
while compiling Python on Solaris. I found some tips online which led
me to compile it with a different threading lib resulting in slightly
better performance after my original post.

In terms of the processors I have, please see below for details:

Status of virtual processor 40 as of: 10/14/2009 17:13:51
  on-line since 07/23/2009 18:48:21.
  The sparcv9 processor operates at 1415 MHz,
and has a sparcv9 floating point processor.

So I guess this is not one of those you are talking about..

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


Looking for programmer experienced in Python/Django/Git/Unix

2009-10-14 Thread TMChris
Hi everyone,

We're looking for an expert in Python, Django, Git and Unix.  We have
multiple projects needed paid on hourly basis.
Do you have a strong confidence in all of these?
If so, please provide a resume and a little bit about yourself in
regards to these.


Thank you,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Paul Rubin
Raymond Hettinger  writes:
> IIRC, the C++ admonition against using exceptions for flow control
> was rooted in performance concerns specific to that language and
> its compilers.  It was not stylistic advice and did not deny that
> flow control exceptions could provide elegant solutions to some
> programming challenges.

I've been wondering about that.  I know that Java exceptions are
ridiculously expensive but I didn't realize it was so bad with C++.

> Python's IndexError and KeyError are all about flow control.
> The notion is deeply embedded in the language and it would
> be a disservice to advise people to not use the language as
> designed.

Well, usually they're wrapped in iterators etc.

> So instead, we have this idiom:
> 
> while True:
> s = f.read(blocksize)
> if not s:
> break
> ...

Well,

   while iter(lambda: f.read(blocksize), ''): 

evolved because of the awkwardness of that idiom...

> "The Little MLer" -- a chunk of this book is devoted to showing
> how exceptions can simplify code that would otherwise be
> somewhat awkward to express (the remainder of the book is devoted
> to thinking about types and how to compose program components).

Interesting--I've been wanting to look at that book.  I wonder whether
its uses of exceptions could mostly be better handled with coroutines.

> "Structured Programming with go to Statements" by Donald Knuth
> has an in-depth comparative analysis of many different looping
> constructs.

Found some pdf's: 

 http://scholar.google.com/scholar?cluster=17368311454828547380

Keep in mind that the article is 35 years old though, and is purely
imperative.  Lots of stuff done with cockamamie looping constructs is
more cleanly done with Python generators, itertools, higher-order
functions, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to handle broken pipes

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Igor Mikushkin wrote:
> Hello all!
>
> Could anyone please say me what is the right way to handle broken
> pipes in Python?
> I can wrap all my print statements with try/except blocks but it looks
> like overkill for me.
>
> I'm using my Python script this way: my_script | less
> The script produces a lot of data.
> So usually when I find what I'm looking for and press 'q' script is
> still writing something.

You mean like this on Unix?

  python -c 'while 1: print "hello, world"'|less

which produces

  Traceback (most recent call last):
File "", line 1, in 
  IOError: [Errno 32] Broken pipe
  
Well, you can catch IOError, examine the errno, and do a sys.exit()
if it's EPIPE. Don't know if it should be sys.exit(0) or sys.exit(1)
though.

Oh, and *now* I see what you wrote at the top:

> I can wrap all my print statements with try/except blocks but it looks
> like overkill for me.

It's overkill if you have to do it for each print. You should always
(IMHO) wrap all your logic inside an object or a function, let's say
foo(). Then you only have to wrap the single call to foo().

There should be an even cleaner way. Mine is kind of ugly (catch,
examine, exit or re-raise) and it also incorrectly catches broken pipes
which aren't related to sys.stdout/stderr.

There is a similar problem with Ctrl-C, by the way -- the user gets a
KeyboardInterrupt exception thrown in his face where other languages
would have exited silently by default.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Raymond Hettinger
> And I know somebody, in other languages, thinks
> it's a Best Practice to avoid using exceptions for flow control.

Ah, now we have two code prions in just one thread.
I hope no newbie or supervisor reads this thread and latches
on to those two counter-productive ideas.

ISTM, both ideas are dangerous contagious because they are
true in some limited contexts but useless (and harmful)
when applied to programming in general.

IIRC, the C++ admonition against using exceptions for flow control
was rooted in performance concerns specific to that language and
its compilers.  It was not stylistic advice and did not deny that
flow control exceptions could provide elegant solutions to some
programming challenges.

Python's IndexError and KeyError are all about flow control.
The notion is deeply embedded in the language and it would
be a disservice to advise people to not use the language as
designed.

Likewise, the use of "while True" tends to be more important
in Python than in other languages because we can't combine
assignment with a conditional as we can in C.  So instead,
we have this idiom:

while True:
s = f.read(blocksize)
if not s:
break
...

Suggested further reading for those who are interested:

"The Little MLer" -- a chunk of this book is devoted to showing
how exceptions can simplify code that would otherwise be
somewhat awkward to express (the remainder of the book is devoted
to thinking about types and how to compose program components).

"Structured Programming with go to Statements" by Donald Knuth
has an in-depth comparative analysis of many different looping
constructs.


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


Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Marco Mariani wrote:
> Dennis Lee Bieber wrote:
>
>>  One thing to note is that "break" ONLY exits the innermost loop --
>> Ada adds the confusion that one could define a label on the loops, and
>> have the innermost use
>>  exit outer_label [when condition]
>> 
>> 
>>  THAT I find scary... Since you have to match the label name to
>> something that occurs somewhere prior to the "exit", and THEN have to
>> find the end of that loop.
>
> But we have exceptions. And I know somebody, in other languages, thinks 
> it's a Best Practice to avoid using exceptions for flow control.

A lot of C++ programmers think so, and Stroustrup himself says
"exceptions are for exceptional things" or something to that effect.
Is that what you're thinking of?

Thankfully, Stroustrup doesn't use the dreaded phrase "Best Practice",
which as far as I can tell is designed to shut down rational thought
in the audience.

> Thankfully, python programmers are less dogmatic, and use whatever makes 
> sense to use. I hope.

Calling it "dogmatic" is unfair.  C++ is very different from Python,
and has a different implementation of exceptions. You also tend to use
the language to solve a different set of problems.

That said, I still don't fully understand the rationale behind that
advice or rule ... so I'm willing to break it, and sometimes I do.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


how to handle broken pipes

2009-10-14 Thread Igor Mikushkin
Hello all!

Could anyone please say me what is the right way to handle broken
pipes in Python?
I can wrap all my print statements with try/except blocks but it looks
like overkill for me.

I'm using my Python script this way: my_script | less
The script produces a lot of data.
So usually when I find what I'm looking for and press 'q' script is
still writing something.

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


where's self.assertMatch (for Django)?

2009-10-14 Thread Phlip

Hypo Nt:

Been a while, now I'm back, and the first helpless question is...

When I google (including codesearch) for assertMatch, I get a mishmash of 
opinions. Am I missing the One True assertMatch(), or isn't there one and I 
gotta write it? or copy and use one of the pretenders?


Distractingly-yrs,

--
  Phlip
  http://zeekland.zeroplayer.com/Pigleg_Too/1
--
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, Grant Edwards wrote:
> On 2009-10-12, Gabriel Genellina  wrote:
>
>>> my_prissy_little_indicator_variable = true
>>> while (my_prissy_little_indicator_variable){
>>> 
>>> }
>>> isn't satisfying because it doesn't guard the  with any
>>> assurance that the loop invariant will be true before you enter into
>>> that block of code.
>>
>> I think you meant the other way; the above is the simplest loop case, with  
>> the test at the start.
>
> Except the test at the start is meaningless when it comes to
> reading the code and troubleshooting.  What counts are
> assignments to my_prissy_little_indicator_variable inside the
> loop.  And those aren't really any easier to spot that "break"
> statements.

It's a red herring.  A good loop tends to *not* have a boolean
variable as the while ... expression.  That smells like flag
programming, and if I cannot come up with anything better that that, I
often prefer a "while 1" with breaks in it.

For a real-life loop, see for example

  http://en.wikipedia.org/wiki/Binary_search#Iterative

(except it confuses me because it's a repeat ... until and it's in
Pascal with that quaint 1-based indexing)

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Jorgen Grahn
On Mon, 2009-10-12, RDrewD wrote:
...
> I was a bit surprised that nobody in this discussion so far bantered
> around the phrase "loop invariant", but then I looked in
> http://en.wikipedia.org/wiki/Loop_invariant and found it was draped in
> so much formalism that it's sure to put off all but the most dedicated
> of Computer Science fans.

Haven't read it. But much of the CS parts of the Wikipedia sucks, and
whoever writes there doesn't own the trademark on loop invariants
anyway.

IME, a loop invariant is a simple and useful tool for thinking about
the correctness of code. Class invariants (or whatever they are called)
are even better.

> I haven't been in college in 35 years, so
> I'll admit to being rusty on this, but as I remember it, any time we
> wrote a loop, we were expected to be able to say what the loop
> invariant is.

Yes, it's as simple as that.

> my_prissy_little_indicator_variable = true
> while (my_prissy_little_indicator_variable){
> 
> }
> isn't satisfying because it doesn't guard the  with any
> assurance that the loop invariant will be true before you enter into
> that block of code.

Why not? To me, it obviously does.

It would also help if you didn't use intentionally meaningless and
annoying variable names in your examples. In reality you would have a
meaningful expression like "not inputqueue.empty()" or
"time() < deadline" or something.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python performance on Solaris

2009-10-14 Thread James Matthews
I use python in almost the same environment. I use it on Joyent and on the
Rackspace cloud. Joyent is faster for a few reasons (cpu bursting and faster
disks) but these aren't real benchmarks until they are on the same machines.


James

On Wed, Oct 14, 2009 at 9:59 AM, Jorgen Grahn

> wrote:

> On Wed, 2009-10-14, Antoine Pitrou wrote:
> > inaf  gmail.com> writes:
> >>
> >> Good point. I failed to compare the CPU power on these machines.. 32
> >> bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
> >> guess that explains :) Thank you for the tip..
> >
> > You have to compare not only CPU frequencies but the CPU models.
>
> Yes, at least that.  Megahertz figures have been useless for decades,
> except in advertising.
>
> > Recently Sun has been selling CPUs optimized for multi-threading (e.g.
> the
> > "UltraSPARC T2" or Niagara CPUs) which have, by design, very poor
> > single-threaded performance. If your Solaris zone uses such a CPU then a
> 6-8x
> > difference in single-threaded performance compared to a modern Intel
> > or AMD CPU
> > is totally expected.
>
> (Had to Google it. A "Solaris Zone" is apparently some kind of
> virtualization thing, with low CPU overhead.)
>
> s/multi-threading/multi-programming/ I suppose. I certainly hope you
> can still get performance while running many separate true processes in
> parallel.
>
> /Jorgen
>
> --
>  // Jorgen Grahn  \X/ snipabacken.se>   O  o   .
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com

http://www.jewelerslounge.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python so sad?

2009-10-14 Thread James Matthews
This posting just made my day!

On Wed, Oct 14, 2009 at 2:59 PM, Dotan Cohen  wrote:

> > ;-)
> >
>
> Be careful, that looks dangerously close to PHP:
> function();
>
>
> --
> Dotan Cohen
>
> http://what-is-what.com
> http://gibberish.co.il
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python so sad?

2009-10-14 Thread Dotan Cohen
> ;-)
>

Be careful, that looks dangerously close to PHP:
function();


-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
-- 
http://mail.python.org/mailman/listinfo/python-list


win32com.client import problem : solved

2009-10-14 Thread Threader Slash
-- Forwarded message --
From: Dave Angel 
To: Threader Slash 
Date: Wed, 14 Oct 2009 07:04:21 -0400
Subject: Re: win32com.client import problem
Threader Slash wrote:

> Hi Everybody,
>
> I have 2 imports:
>
> import pythoncom
> from win32com.client import Dispatch
>
> if I run it on my Python 2.6 Console, it works nicely. However, when I go
> to
> Eclipse IDE, open a project, open a main.py file, and try run, it gives the
> error:
>
> import pythoncom
> ImportError: No module named pythoncom
>
> All other imports are working ok on Eclipse IDE -- e.g. import MySQLdb.
>
> Any suggestion about what is missing?
>
> All comments and suggestion are welcome.
>
> ThreaderSlash
>
>
>
Two things to check, python version (sys.version), and sys.path.   Add
prints for the two of them at the beginning of your script, and try the
script in both environments.  If there are any differences, figure out how
to reconfigure Eclipse to match what you've got at the console.


DaveA


--  --  --  --

-- hope this can help and save time for others too

Here is what did and works:
* copy the file mfc71.dll on windows\system32
* copy the same also on directories
* copy the same to your directories Python26\DLLs and
Python26\lib\site-packages\win32
* go to preferences : pydev : interpreter python : remove all the
interpreter you have there. apply, ok.
then add python 2.6 again, apply, ok.

It will do the trick...|:0), ThreaderSlash

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


Re: The rap against "while True:" loops

2009-10-14 Thread Ethan Furman

Mensanator wrote:

On Oct 14, 2:19�am, Dennis Lee Bieber  wrote:


On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
 declaimed the following in
gmane.comp.python.general:



You're not getting away that easy.



What's YOUR opinion of "whilr True"?


� � � � Uhm... that it isn't valid in any language having English influence
upon it's keywords...



Duh. You DO know that 'r' is next to 'e' on the
keyboard?


Not on mine -- it's next to 'o' and 'u'.  :-)  Go Dvorak!


� � � � If anything -- I'd suggest a proposal to add a plain � �loop � �as a
keyword in Python, whose effect is equivalent to a "while True", but a
break � �must be used to exit said loop (well, we'll ignore raising an
exception )



And what will that accomplish? The problem isn't
using while True, it's the fact that you are
escaping the loop. Best Practice is to EXIT the
loop properly, not escape from it.


I don't think anyone's arguing the opposite.  What I *am* seeing argued 
is if it's the only correct way to do it, and that anyone who does it 
any other way is a scoundrel and a knave.  ;-)


For what it's worth, most of my loops run to completion, with no sign of 
a break anywhere.  Some have a break, and use it.  Some, even, (dare I 
say it?) use break *and* else!  And it's awesome!  Go Python!  :-D


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


Re: Parsing email attachments: get_payload() produces unsaveable data

2009-10-14 Thread dpapathanasiou
On Oct 4, 10:27 am, dpapathanasiou 
wrote:
> I'm using python to access an email account via POP, then for each
> incoming message, save any attachments.
>
> This is the function which scans the message for attachments:
>
> def save_attachments (local_folder, msg_text):
>     """Scan the email message text and save the attachments (if any)
> in the local_folder"""
>     if msg_text:
>         for part in email.message_from_string(msg_text).walk():
>             if part.is_multipart() or part.get_content_maintype() ==
> 'text':
>                 continue
>             filename = part.get_filename(None)
>             if filename:
>                 filedata = part.get_payload(decode=True)
>                 if filedata:
>                     write_file(local_folder, filename, filedata)
>
> All the way up to write_file(), it's working correctly.
>
> The filename variable matches the name of the attached file, and the
> filedata variable contains binary data corresponding to the file's
> contents.
>
> When I try to write the filedata to a file system folder, though, I
> get an AttributeError in the stack trace.
>
> Here is my write_file() function:
>
> def write_file (folder, filename, f, chunk_size=4096):
>     """Write the the file data f to the folder and filename
> combination"""
>     result = False
>     if confirm_folder(folder):
>         try:
>             file_obj = open(os.path.join(folder, file_base_name
> (filename)), 'wb', chunk_size)
>             for file_chunk in read_buffer(f, chunk_size):
>                 file_obj.write(file_chunk)
>             file_obj.close()
>             result = True
>         except (IOError):
>             print "file_utils.write_file: could not write '%s' to
> '%s'" % (file_base_name(filename), folder)
>     return result
>
> I also tried applying this regex:
>
> filedata = re.sub(r'\r(?!=\n)', '\r\n', filedata) # Bare \r becomes \r
> \n
>
> after reading this post (http://stackoverflow.com/questions/787739/
> python-email-getpayload-decode-fails-when-hitting-equal-sign), but it
> hasn't resolved the problem.
>
> Is there any way of correcting the output of get_payload() so I can
> save it to a file?

An update for the record (and in case anyone else also has this
problem):

The regex suggested in the StackOverflow post (i.e., filedata = re.sub
(r'\r(?!=\n)', '\r\n', filedata) # Bare \r becomes \r\n) is necessary
but not sufficient.

It turns out that because get_payload() returns a binary stream, the
right way to save those bytes to a file is to use a function like
this:

def write_binary_file (folder, filename, filedata):
"""Write the binary file data to the folder and filename
combination"""
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name
(filename)), 'wb')
file_obj.write(filedata)
file_obj.close()
result = True
except (IOError):
print "file_utils.write_file: could not write '%s' to
'%s'" % (file_base_name(filename), folder)
return result

I.e., filedata, the output of get_payload(), can be written all at
once, w/o reading and writing in 4k chunks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python along or bash combined with python (for manipulating files)

2009-10-14 Thread Falcolas
On Oct 13, 10:18 pm, TerryP  wrote:
> On Oct 14, 2:13 am, Peng Yu  wrote:
>
> > Bash is easy to use on manipulating files and directories (like change
> > name or create links, etc) and on calling external programs. For
> > simple functions, bash along is enough. However, bash does not support
> > the complex functions. Python has a richer library that could provide
> > support for complex functions (such compute the relative path between
> > two paths).
>
> > I'm wondering for a task that can not be done with bash along whether
> > it would be better to do in pure python or with a mix of both python
> > and bash. What I care is mostly coding speed and a little bit
> > maintainability (but not much). Can somebody provide some experience
> > on when to combine python and bash and when to use pure python?
>
> bash can **not** manipulate files and directories beyond things like
> the '>' and '>>' I/O redirections, and some minor loading/saving of
> state data from/to files (command history, directory stack, etc). Most
> of what you refer to are **separate operating system specific
> programs** and have absolutely nothing to do with the shell.
>
> Very sophisticated scripts are possible using bash and ksh, there is
> even a form of ksh that has tk capabilities! (tksh). The Python and
> Bourne-derived languages are however fundamentally different
> creatures, and use very different data models. You should **not**
> write Python (or Perl) scripts as if they were shell scripts -- doing
> so is very bad practice. When you want a shell script, write a shell
> script. When you write a Python script, write a Python script. It
> really is that simple.
>
> As a rule of thumb, when you have need of data structures beyond what
> scalar strings and very simple word lists can provide -- you should
> use Python. bash and ksh provide support for arrays, and ksh even has
> dictionaries! (Hashes in Perl speak.) That makes programming in bash/
> ksh more robust then pure sh, but also less portable. The best time to
> use bash is when you require bash specific features, other wise don't
> use bash. The same can be said for ksh.
>
> When the words array, dictionary, class, object, and/or using multiple
> source files comes to mind when implementing a program - you probably
> want to use Python, Perl, Ruby, or some other general programming
> language, not a shell scripting language like bash.
>
> You should be cautious to avoid mixing bash and Python code in one
> file.
>
> If maintainability is not a factor in what you are writing, then you
> should probably not be writing code in any language unless it is the
> language of Mathematics (and even then, maintainability is a wise
> consideration).
>
> --
>   TerryP.
> Just Another Programmer.

With all of Terry's admonitions in mind, Python scripts do integrate
very well as a individual tool within a shell toolchain. With the
multiple command line parsers and the ease of reading stdin and
writing to stdout, it's fairly trivial to make a script which
integrates cleanly into a bash script (or oneliner). It's trivial to
implement a script which will either work with files, or work with
stdin/stdout.

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


Re: python along or bash combined with python (for manipulating files)

2009-10-14 Thread edexter
On Oct 14, 3:42 am, Jean-Michel Pichavant 
wrote:
> Peng Yu wrote:
> > Bash is easy to use
>
> +JOTW
>
> :)
>
> JM

why choose..  http://shython.sourceforge.net/
I don't think this is the most recent I would also try the package
index
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/14 Dennis Lee Bieber :

>        If anything -- I'd suggest a proposal to add a plain    loop    as a
> keyword in Python, whose effect is equivalent to a "while True", but a
> break    must be used to exit said loop (well, we'll ignore raising an
> exception )

And with enough static analysis to guarantee that the break will be
reached? I think it would be a bit much to expect Python to solve the
halting problem!

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


Re: RotatingFileHandler issue

2009-10-14 Thread Max Lynch
I never got a response back from this, but I'm noticing even more odd
behavior, see inline:

On Wed, Sep 30, 2009 at 4:38 PM, Max Lynch  wrote:

> Hi.
> I have a RotatingFileHandler for my logging system.  I have it set to
> rotate once the file becomes 5MB in size.  Here is the conf line I have in
> my logging config file:
>
> [handler_fileHandlerDebugNoRequest]
> class=handlers.RotatingFileHandler
> formatter=formatterNoRequest
> args=('/web/logs/gobuzz_debug.log', 'a', 5242880, 8)
>
> However, my logging folder contains these files:
> -rw-r--r-- 1 www-data www-data 566K Sep 30 16:35 gobuzz_debug.log
> -rw-r--r-- 1 www-data www-data 4.2M Sep 30 16:35 gobuzz_debug.log.1
> -rw-r--r-- 1 www-data www-data 572K Sep 30 16:36 gobuzz_debug.log.2
> -rw-r--r-- 1 www-data www-data 558K Sep 30 16:35 gobuzz_debug.log.3
> -rw-r--r-- 1 www-data www-data 3.7K Sep 29 20:52 gobuzz_debug.log.4
> -rw-r--r-- 1 www-data www-data 3.7K Sep 29 20:52 gobuzz_debug.log.5
> -rw-r--r-- 1 www-data www-data 566K Sep 30 16:36 gobuzz_debug.log.6
> -rw-r--r-- 1 www-data www-data 1.6M Sep 30 16:36 gobuzz_debug.log.7
> -rw-r--r-- 1 www-data www-data  45K Sep 29 20:50 gobuzz_debug.log.8
> -rwxrwxrwx 1 www-data www-data 691K Sep 28 09:39 gobuzz_error.log
>
> Clearly, the files are rotating far before they hit 5MB.  The consequence
> of such being that I'm losing a lot of log data.  What gives?  Am I doing
> something wrong?
>

For some reason, my Apache2/mod_wsgi/django system is writing to all of the
separate rotated files at once.  I can't detect a pattern, but some times,
for example, logging data goes into "gobuzz_debug.log.8" and some times they
go into "gobuzz_debug.log.5", rather than only going to gobuzz_debug.log and
rotating after 5MB.

Does anyone have any ideas? Here are my formatter sections if it matters:
[formatter_formatterNoRequest]
format=%(asctime)s - %(mymodule)s:%(mylineno)d - %(levelname)s - %(message)s
datefmt=%a, %d %b %Y %I:%M:%S %p




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


Re: Pyusb

2009-10-14 Thread Chris Withers

Ronn Ross wrote:
Does anyone know where I can download a copy of PyUSB 1.0? I can only 
find 0.x versions on sourceforge. I'm following a tutorial that requires 
1.0. Thanks


Googling "pyusb" gives me loads of hits and the newer versions appear to 
be on about the 3rd link down...


Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
   - http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python so sad?

2009-10-14 Thread Chris Withers

Zac Burns wrote:

There are 10741 occurences of ): or :( in our source code and only 2
occurrences of :) or (:. Not what you would expect from a language
named after a comedian.


def ...(...):
  ...

class ...(...):
  ...

etc

;-)

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
   - http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: XML-RPC(using SimpleXMLRPCServer) slow on the first call

2009-10-14 Thread Mahi Haile
-- Forwarded message --
> From: "Gabriel Genellina" 
> To: python-list@python.org
> Date: Wed, 14 Oct 2009 00:52:13 -0300
> Subject: Re: XML-RPC(using SimpleXMLRPCServer) slow on the first call
> En Mon, 12 Oct 2009 18:58:45 -0300, Mahi Haile 
> escribió:
>
>  Hello all,I have an xml-rpc server running on a machine in the same LAN as
>> the client. Both the server and the client are in Python.
>>
>> When I have a series of xmlrepc calls from the client to the server, the
>> first call usually takes much longer than it should - orders of magnitude.
>> The latency is usually sub-10ms on the other calls, but the first call
>> takes
>> up to 10 seconds or so. This are very simple functions, with almost no
>> computation.
>>
>> Do you have any ideas?
>>
>
> I doubt this is a Python problem. I'd look into the network: DNS
> resolution, IPv6 (Windows XP has some timeout issues with IPv6 enabled).
>
> --
> Gabriel Genellina
>
> That seems to be correct. The machine is behind a NAT, so that is probably
why. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread MRAB

Dennis Lee Bieber wrote:

On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
 declaimed the following in
gmane.comp.python.general:


You're not getting away that easy.

What's YOUR opinion of "whilr True"?


Uhm... that it isn't valid in any language having English influence
upon it's keywords...

If anything -- I'd suggest a proposal to add a plainloopas a
keyword in Python, whose effect is equivalent to a "while True", but a
breakmust be used to exit said loop (well, we'll ignore raising an
exception )


I'd prefer it to be called "repeat".
--
http://mail.python.org/mailman/listinfo/python-list


Why is python so sad?

2009-10-14 Thread Zac Burns
There are 10741 occurences of ): or :( in our source code and only 2
occurrences of :) or (:. Not what you would expect from a language
named after a comedian.

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


Pyusb

2009-10-14 Thread Ronn Ross
Does anyone know where I can download a copy of PyUSB 1.0? I can only find
0.x versions on sourceforge. I'm following a tutorial that requires 1.0.
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Mensanator
On Oct 14, 2:19�am, Dennis Lee Bieber  wrote:
> On Tue, 13 Oct 2009 15:02:09 -0700 (PDT), Mensanator
>  declaimed the following in
> gmane.comp.python.general:
>
> > You're not getting away that easy.
>
> > What's YOUR opinion of "whilr True"?
>
> � � � � Uhm... that it isn't valid in any language having English influence
> upon it's keywords...

Duh. You DO know that 'r' is next to 'e' on the
keyboard?

>
> � � � � If anything -- I'd suggest a proposal to add a plain � �loop � �as a
> keyword in Python, whose effect is equivalent to a "while True", but a
> break � �must be used to exit said loop (well, we'll ignore raising an
> exception )

And what will that accomplish? The problem isn't
using while True, it's the fact that you are
escaping the loop. Best Practice is to EXIT the
loop properly, not escape from it.

> --
> � � � � Wulfraed � � � � Dennis Lee Bieber � � � � � � � KD6MOG
> � � � � wlfr...@ix.netcom.com � � HTTP://wlfraed.home.netcom.com/

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


Re: The rap against "while True:" loops

2009-10-14 Thread TerryP
When all is said and done, is not all looping *basically* equivalent
to something like this?

  begin_loop:
unless TEST
  goto end_loop
;; loop body here
if TEST
  goto begin_loop
  end_loop:

Which could likely be used to implement something like:

  while TEST:
# loop body here

in any highly expressive language; which in of it self displays
something about Computer Science.


or am I just talking out my ass?



I've watched this thread with some interest, but really it sounds to
me like the metrics are getting rather lax and this will probably end
up on par with a for (i=0; i < count; i++) versus for (i=0; i < count;
i--) discussion. By that, I mean:


  Fruitful conversation but there is no one spoon for every bowl.

--
  TerryP.
Just Another Programmer.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are there any modules for IRC, that work with Python 3.1?

2009-10-14 Thread Jorgen Grahn
On Sat, 2009-10-10, TerryP wrote:
> Does anyone know of any modules for dealing with the IRC protocol,
> that will work with Python 3.1? It doens't have to be super great,
> just less time consuming then playing with sockets directly (and obv.
> stable). The only module in my systems package manager is irclib for
> Python 2.6. I can live with writing code for Python 2.4+ easily but,
> ahem, I think it would be wise to write new code around Python 3.1
> instead...

Even though it is not widely used yet, and the module you want to use
doesn't support it?  I assume you have installed Python 3.x manually
too (my Debian 'stable' is only at Python 2.5 at the moment -- it
probably takes lots of work to bring in Python 3 without losing
important packages).

Or you can ask the irclib maintainers if they have something. If not,
you can do the work for them, after you have convinced yourself it's
good enough (by starting to use it with Python 2.x).

I don't have any more substantial advice, sorry.

> # circumstances
>
> Having recently been put into search for a new IRC client, and
> everything I've thrown in the cauldron having become a
> disappointment... let's just say, I've come to a conclusion -- either
> I'm going to install ircII and live with whatever it has to offer(!),
> or hash out something quickly in Python that fits my needs. If I'm
> considering writing an IRC client, it makes sense to check for modules
> implementing the protocol before I have to roll something myself, but
> nothing seems to fit the bill.
>
>
> (For those that don't know it, ircII is a really freaking old Internet
> Rely Chat client ;)

I would have thought (given the number of hackers who use it a lot)
there were lots of good IRC clients, but I don't use it myself, so ...

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Testoob 1.15 released

2009-10-14 Thread Jorgen Grahn
On Thu, 2009-10-08, oripel wrote:
> Testoob is the advanced Python test runner and testing framework that
> spices up any existing unittest test suite.
>
> Home: http://code.google.com/p/testoob

But this sentence on the home page

The documentation is sadly outdated, but may be
a starting point:

made me stop looking.  As far as I can tell, you cannot even find out
what's so advanced about it (or why "advanced" is a good thing)
without starting to use it.  A brief comparison with module unittest
(which I am rather unhappy with) would have been nice, too.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The rap against "while True:" loops

2009-10-14 Thread Tim Rowe
2009/10/14 Marco Mariani :
> Dennis Lee Bieber wrote:
>
>>        One thing to note is that "break" ONLY exits the innermost loop --
>> Ada adds the confusion that one could define a label on the loops, and
>> have the innermost use
>>        exit outer_label [when condition]
>>
>>
>>        THAT I find scary... Since you have to match the label name to
>> something that occurs somewhere prior to the "exit", and THEN have to
>> find the end of that loop.
>
> But we have exceptions.

So has Ada.

> And I know somebody, in other languages, thinks it's
> a Best Practice to avoid using exceptions for flow control.
>
> Thankfully, python programmers are less dogmatic, and use whatever makes
> sense to use. I hope.

Absolutely. And it doesn't make sense to use exceptions for flow control :-)

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


Re: Help with regex and optional substring in search string

2009-10-14 Thread Zero Piraeus
:

2009/10/14 Timur Tabi :
> Never mind ... I figured it out.  The middle block should have been [\w
> \s/]*

This is fragile - you'll have to keep adding extra characters to match
if the input turns out to contain them.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with regex and optional substring in search string

2009-10-14 Thread Zero Piraeus
:

2009/10/14 Timur Tabi :
> I'm having trouble creating a regex pattern that matches a string that
> has an optional substring in it.  What I'm looking for is a pattern
> that matches both of these strings:
>
> Subject: [PATCH 08/18] This is the patch name
> Subject: This is the patch name
>
> What I want is to extract the "This is the patch name".  I tried this:
>
> m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)
>
> Unfortunately, the second group appears to be too greedy, and returns
> this:
>
 print m.group(1)
> None
 print m.group(2)
> [PATCH 08/18] Subject line

It's not that the second group is too greedy. The first group isn't
matching what you want it to, because neither \w nor \s match the "/"
inside your brackets. This works for your example input:

>>> import re
>>> pattern = re.compile("Subject:\s*(?:\[[^\]]*\])?\s*(.*)")
>>> for s in (
... "Subject: [PATCH 08/18] This is the patch name",
... "Subject: This is the patch name",
... ):
... re.search(pattern, s).group(1)
...
'This is the patch name'
'This is the patch name'

Going through the changes from your original regex in order:

'(?:etc)' instead of '(etc)' are non-grouping parentheses (since you
apparently don't care about that bit).

'[^\]]' instead of '[\w\s]' matches "everything except a closing bracket".

The '\s*' before the second set of parentheses takes out the leading
whitespace that would otherwise be returned as part of the match.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and USB

2009-10-14 Thread Gabriel Genellina
En Wed, 14 Oct 2009 09:55:15 -0300, Ronn Ross   
escribió:



I'm new to Python and would like to capture mouse movements. I envision
writing a script that will just write out the mouse movements in the  
term.

Is this possible? Can someone point me in the right direction?


Capture mouse movements in your application, or globally?
All GUI frameworks that I know of (including the one that comes with  
Python, Tkinter, a wrapper around Tk and Tcl) provide some sort of  
notifications when the mouse is over your program window.
If you need to track the mouse everywhere (not only inside your app), that  
depends on the OS and desktop you're using. On Windows I think there's a  
library named pyhook or similar.


--
Gabriel Genellina

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


Re: Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
On Oct 14, 9:51 am, Timur Tabi  wrote:
> I'm having trouble creating a regex pattern that matches a string that
> has an optional substring in it.  What I'm looking for is a pattern
> that matches both of these strings:
>
> Subject: [PATCH 08/18] This is the patch name
> Subject: This is the patch name
>
> What I want is to extract the "This is the patch name".  I tried this:
>
> m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

Never mind ... I figured it out.  The middle block should have been [\w
\s/]*

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


Re: Clear interface for mail class

2009-10-14 Thread Francesco Bochicchio
On Oct 14, 2:39 pm, Benedict Verheyen 
wrote:
> Hi,
>
> I'm trying to come up with a decent interface for my email class.
> Basically, i have one email function but it has many (too many?) variables:
>
>     send_mail(self, send_from, send_to, send_cc, subject, text, 
> separate_emails = False, files=[], inline_files=[], server="localhost",
> charset="iso-8859-1")
>
> I'm thinking on how i could simplify it and make it look nicer and more easy 
> to use.
> I can reduce the number of arguments by adding functions for the less common 
> tasks
> like adding attachement, inline files and so on.
> Then i would end up with functions like this:
>
> def __init__(self):
>     """
>     """
>     self._files=[]
>     self._inline_files=[]
> ...
>
> def add_files(self, files=[]):
>     assert type(files)==list
>     self._files=files
> ...
>
> When sending an email, i could check if files where specified and if so, send 
> them too.
>
> But it doesn't feel right to just have a bunch of small functions to set 
> variables.
> Calling the function would change from:
>  (where m is the mail class)
>  m.send_mail( "from...@work",
>               "t...@work",
>               [],
>               "Test emailmodule ",
>               MSG,
>               separate_emails = True,
>               files=["attached_pic.png"],
>               inline_files=["inline_pic.png"],
>               server="mysmtpserver")
>
> to:
>
> m.add_files(["attached_pic.png"])
> m.add_inline_files(["inline_pic.png"])
> m.smtp_server("mysmtpserver")
> m.send_mail( "from...@work",
>              "t...@work",
>              [],
>              "Test emailmodule "",
>              MSG)
>
> It looks already better and i could set the smtp server as a class variable,
> but i'm not sure this will present me with the most natural interface to use.
>
> Or should i make 1 general function that sets vars according to a type?
> For instance: add_header("To",[list_of_recipients])
>
> This kind of problems seems to happen sometimes: i need to fix something 
> quickly,
> build a script for it, and then i realise that the code i've written is not 
> the best in
> terms of reusability and has some "not so great" functions or classes.
> Speedy development eh.
>
> Any ideas are welcome.
>
> Thanks,
> Benedict

I would add a server class, maybe subclassing something in standard
library, and add to it the 'send' method, so that sending a mail would
be
something like:

myserver = MyMailServer("mysmtpserver", "localhost", ) # this only
needs to be done once, not for each mail

m = MyMail( subject, text, separate_emails = False, files=[],
inline_files=[] ) # mail creation

myserver.send( m, from= "from...@work", # mail sending
   to = "t...@work",
   cc_to= None  )

Note that I put sender and destination senders in the send method, not
as attributes  of the mail object. It makes more sense to me, and yopu
can reuse
the same object if you want to send the same mail to many addresses
( and/or via different servers ).

IN general, un case like yours I use a lot default parameters, as you
did already. Having separate methods to setting specific part of an
object only makes
sens (to me) if you need  first to create an object and later change
some of the attributes. Also, if you are just going to change the
attributes, you do not
need a method: just use the object.attribute = value syntax. If you
are going to need later to do more complex thing, you can always
transform your
attribute in a property.




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


Re: efficient running median

2009-10-14 Thread Peter Otten
Janto Dreijer wrote:

> On Oct 13, 6:12 pm, Peter Otten <__pete...@web.de> wrote:
>> Janto Dreijer wrote:
>> > I'm looking for code that will calculate the running median of a
>> > sequence, efficiently. (I'm trying to subtract the running median from
>> > a signal to correct for gradual drift).
>>
>> > My naive attempt (taking the median of a sliding window) is
>> > unfortunately too slow as my sliding windows are quite large (~1k) and
>> > so are my sequences (~50k). On my PC it takes about 18 seconds per
>> > sequence. 17 of those seconds is spent in sorting the sliding windows.
>>
>> > I've googled around and it looks like there are some recent journal
>> > articles on it, but no code. Any suggestions?
>>
>> If you aren't using numpy, try that. Showing your code might also be a
>> good idea...
>>
>> Peter
> 
> I placed the test code and its output here:
> http://bitbucket.org/janto/snippets/src/tip/running_median.py

That gives me something to tinker ;)
 
> I also have a version that uses numpy. On random data it seems to be
> about twice as fast as the pure python one. It spends half the time
> sorting and the other half converting the windows from python lists to
> numpy arrays.
> If the data is already sorted, the version using python's builtin sort
> outperforms the numpy convert-and-sort by about 5 times. Strangely
> satisfying :)

I was thinking of using as many of numpy's bulk operations as possible:

def running_median_numpy(seq):
data = array(seq, dtype=float)
result = []
for i in xrange(1, window_size):
window = data[:i]
result.append(median(window))
for i in xrange(len(data)-window_size+1):
window = data[i:i+window_size]
result.append(median(window))
return result

But it didn't help as much as I had hoped.
The fastest I came up with tries hard to keep the data sorted:

def running_median_insort(seq):
seq = iter(seq)
d = deque()
s = []
result = []
for item in islice(seq, window_size):
d.append(item)
insort(s, item)
result.append(s[len(d)//2])
m = window_size // 2
for item in seq:
old = d.popleft()
d.append(item)
del s[bisect_left(s, old)]
insort(s, item)
result.append(s[m])
return result

Some numbers:

10.197 seconds for running_median_scipy_medfilt
25.043 seconds for running_median_python
13.040 seconds for running_median_python_msort
14.280 seconds for running_median_python_scipy_median
4.024 seconds for running_median_numpy
0.221 seconds for running_median_insort

What would be an acceptable performance, by the way?

Peter

PS: code not tested for correctness


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


Help with regex and optional substring in search string

2009-10-14 Thread Timur Tabi
I'm having trouble creating a regex pattern that matches a string that
has an optional substring in it.  What I'm looking for is a pattern
that matches both of these strings:

Subject: [PATCH 08/18] This is the patch name
Subject: This is the patch name

What I want is to extract the "This is the patch name".  I tried this:

m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

Unfortunately, the second group appears to be too greedy, and returns
this:

>>> print m.group(1)
None
>>> print m.group(2)
[PATCH 08/18] Subject line
>>>

Can anyone help me?  I'd hate to have to use two regex patterns, one
with the [...] and one without.
-- 
http://mail.python.org/mailman/listinfo/python-list


Pyusb

2009-10-14 Thread Ronn Ross
I'm running Python 2.6 on windows. I'm install Pyusb and I'm having trouble
including the library in my script. Can someone point me in the right
direction to get this working or know of a good tutorial to help me out.
Best regards
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is Islam?-1

2009-10-14 Thread Aahz
In article ,
J   wrote:
>On Tue, Oct 13, 2009 at 20:05, Aahz  wrote:
Mensanator:
>
>There's no point in trying to reason with a Muslim.

 That's not funny, and if you were being serious, that was incredibly
 rude.
>>>
>>>Not as much as posting in comp.lang.python.
>>
>> What exactly are you claiming is rude?
>
>This entire thread is rude and really has no place on a list like this.

You have a point, but posting derogatory remarks about religions is even
less appropriate and I will call people who post them to task.  The
correct response to spam is to just ignore it in the first place (or
report it to the person's ISP).

There are plenty of Muslims in the Python community and they don't
deserve to get treated to insults from Mensanator.

Side note: you're not much of one to talk about rudeness when you remove
attributions from quotes.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it."  --re...@lion.austin.ibm.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MUD Game Programmming - Python Modules in C++

2009-10-14 Thread Gabriel Genellina

En Wed, 14 Oct 2009 05:19:06 -0300, Ulrich Eckhardt
 escribió:


Gabriel Genellina wrote:

#ifdef _DEBUG
#undef _DEBUG
#include 
#define _DEBUG
#else
#include 
#endif

[...to keep Python from linking against non-existant debug libraries.]


No, don't do that. Just compile your application in release mode.


Why not, does it break anything?


You have to ensure the same block is used everywhere  is
included, or remember to always use "mypython.h", but there is still the
risk when adding some other external library.
The object layout is different in a debug build, and there are other
incompatible differences. Mixing code compiled in both modes hopefully
would generate a linker error, but if not, it may crash the application.
The debug flag for Python *should* be decoupled from the debug flag for
the application embedding it, but unfortunately it isn't. There is a
feature request at http://bugs.python.org (the site doesn't work for me
now)

--
Gabriel Genellina

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


Re: Clear interface for mail class

2009-10-14 Thread Joe Riopel
On Wed, Oct 14, 2009 at 8:39 AM, Benedict Verheyen
 wrote:
> This kind of problems seems to happen sometimes: i need to fix something 
> quickly,
> build a script for it, and then i realise that the code i've written is not 
> the best in
> terms of reusability and has some "not so great" functions or classes.
> Speedy development eh.

This happens to me a lot too, especially when working on a "quick
fix". I don't worry about the re-usability of my code until I actually
have another use for it. Once I find another/similar problem I am
trying to solve, I will spend the time on making the original code
more re-usable. Trying to guess at what will be a useful API, sometime
down the line, is hard for me to do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread Diez B. Roggisch
vicky wrote:

> On Oct 14, 5:52 pm, "Diez B. Roggisch"  wrote:
>> vicky wrote:
>> > Hello All,
>>
>> > I am a new Python user and don't know much about it.
>>
>> > I just want to know that, due to any reason if a script exits, is
>> > their some way to release all the resources acquired by the script
>> > during execution ?
>>
>> > Actually In my system I want to execute some piece of code at the time
>> > of script exit (expected or unexpected) to ensure the release of all
>> > the resources. I don't know how to do that :(
>>
>> > Can anyone please help me ?
>>
>> What resources? Usually, the garbage-collection and the OS will do that
>> for you anyway. So are there concrete problems you observe?
>>
>> Also, take a look at the atexit-module.
>>
>> Diez
> 
> According to the documentation of atexit module:
> 
> The atexit module defines a single function to register cleanup
> functions. Functions thus registered are automatically executed upon
> normal interpreter termination.
> 
> Note: the functions registered via this module are not called when the
> program is killed by a signal, when a Python fatal internal error is
> detected, or when os._exit() is called.
> 
> Actually I have Python ported on vx-works. During initialization I am
> initializing some python interpreters, and each interpreter is
> associated with a specific buffer to send or receive the messages
> using underlying protocol (embedded in C library). Using same library
> I am using the subscription functionality. So when I am using Python
> to made subscriptions it register an entry. But if some user forgets
> to clear the subscription or script exited accidently due to some
> error, subscription still remain active. And when next time the same
> interpreter is used to execute some script, the older subscription
> create a trouble for the user. I want some implementation which
> guarantees the clearance of all the subscriptions at script exit
> (expected or unexpected) automatically.

Ok, the embedded part is crucial here - because the atexit is AFAIK
implemented in terms of the "real" atexit - so it will only be called when
the process dies.

In your case, I guess you have two options: 

 - make the interpreter execute scripts that always have try/finally with
some cleanup-code in the finally

 - clean up after the interpreter in C++

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


Re: python performance on Solaris

2009-10-14 Thread Jorgen Grahn
On Wed, 2009-10-14, Antoine Pitrou wrote:
> inaf  gmail.com> writes:
>> 
>> Good point. I failed to compare the CPU power on these machines.. 32
>> bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
>> guess that explains :) Thank you for the tip..
>
> You have to compare not only CPU frequencies but the CPU models.

Yes, at least that.  Megahertz figures have been useless for decades,
except in advertising.

> Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
> "UltraSPARC T2" or Niagara CPUs) which have, by design, very poor
> single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
> difference in single-threaded performance compared to a modern Intel
> or AMD CPU
> is totally expected.

(Had to Google it. A "Solaris Zone" is apparently some kind of
virtualization thing, with low CPU overhead.)

s/multi-threading/multi-programming/ I suppose. I certainly hope you
can still get performance while running many separate true processes in
parallel.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread vicky
On Oct 14, 5:52 pm, "Diez B. Roggisch"  wrote:
> vicky wrote:
> > Hello All,
>
> > I am a new Python user and don't know much about it.
>
> > I just want to know that, due to any reason if a script exits, is
> > their some way to release all the resources acquired by the script
> > during execution ?
>
> > Actually In my system I want to execute some piece of code at the time
> > of script exit (expected or unexpected) to ensure the release of all
> > the resources. I don't know how to do that :(
>
> > Can anyone please help me ?
>
> What resources? Usually, the garbage-collection and the OS will do that for
> you anyway. So are there concrete problems you observe?
>
> Also, take a look at the atexit-module.
>
> Diez

According to the documentation of atexit module:

The atexit module defines a single function to register cleanup
functions. Functions thus registered are automatically executed upon
normal interpreter termination.

Note: the functions registered via this module are not called when the
program is killed by a signal, when a Python fatal internal error is
detected, or when os._exit() is called.

Actually I have Python ported on vx-works. During initialization I am
initializing some python interpreters, and each interpreter is
associated with a specific buffer to send or receive the messages
using underlying protocol (embedded in C library). Using same library
I am using the subscription functionality. So when I am using Python
to made subscriptions it register an entry. But if some user forgets
to clear the subscription or script exited accidently due to some
error, subscription still remain active. And when next time the same
interpreter is used to execute some script, the older subscription
create a trouble for the user. I want some implementation which
guarantees the clearance of all the subscriptions at script exit
(expected or unexpected) automatically.

Thanks for your reply.
/vicky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Clear interface for mail class

2009-10-14 Thread Benedict Verheyen
Marco Mariani wrote:
> Benedict Verheyen wrote:
> 
>> Any ideas are welcome.
> 
> easy_install turbomail
> 
> :)
> 

Looks good but i'm still interested in how one would
make a clean class interface for this type of problem.

Having said that, turbomail looks quite good :)

Thanks

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


Re: cpython compilation parameter

2009-10-14 Thread Jorgen Grahn
On Thu, 2009-10-08, Diez B. Roggisch wrote:
> cEd wrote:
>
>> Hello,
>> 
>> I'm wondering how to compile python to get good performance.
>> Because when I compare this ugly code which find prime number:

...

>>  between :
>>  - the python distributed with my ubuntu
>> #time python prime_number.py > /dev/null
>> real0m12.237s
>> user0m12.129s
>> sys0m0.024s
>> 
>>  - and the one compiled by my self
>> time my_python_compiled prime_number.py > /dev/null
>> real0m42.193s
>> user0m41.891s
>> sys0m0.044s
>> 
>> so which option should I give or which flag ???
>
> I doubt that there is such a flag. There must be a different reason for
> this. Can you give us the python versions for each, and architecture (32/64
> bit)?

He could start by compiling it exactly like Ubuntu does. Just get the
Ubuntu source packet -- it's all in there, Ubuntu doesn't keep it a
secret.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread Mick Krippendorf
vicky schrieb:
> Actually In my system I want to execute some piece of code at the time
> of script exit (expected or unexpected) to ensure the release of all
> the resources. I don't know how to do that :(

You maybe want to use a context manager. Look for 'with statement' and
'contextlib' in your docs.


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


Re: Unexpected exit of Python script

2009-10-14 Thread Donn
On Wednesday 14 October 2009 14:23:11 vicky wrote:
> I just want to know that, due to any reason if a script exits, is
> their some way to release all the resources acquired by the script
> during execution ?
Python cleans-up after itself so I would not worry about that until you are an 
expert and need to split hairs. Just get on with learning and writing code.

Look at exceptions (try, except) for this kind of thing -- but it's a basic 
part of learning Python anyway.

About the only thing I ever found I could not catch was a segfault (under 
Linux) and to get around that I open my main app from a smaller one using the 
subprocess module. When the main app crashes (which it does sometime) that 
process dies suddenly, but it falls-back to the original script which then 
detects the error return code and can continue whatever it needs to do.

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


Python and USB

2009-10-14 Thread Ronn Ross
I'm new to Python and would like to capture mouse movements. I envision
writing a script that will just write out the mouse movements in the term.
Is this possible? Can someone point me in the right direction?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-arrays python

2009-10-14 Thread Piet van Oostrum
> bbarb...@inescporto.pt (b) wrote:

>b> Hi again!
>b> After testing the whole day, I have got my goals from the last email,  but
>b> as always, another issues came up! and now that Ive been able to  save a
>b> list of list (or multi-arrays) as below :

>b> ['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']
>b> ['10001.mp3\n' '10005.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>b> ['10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>b> ['10006.mp3\n' '10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n']
>b> ['10008.mp3\n' '100.mp3\n' '10001.mp3\n' '10005.mp3\n' '10006.mp3\n']

>b> I am not able to manipulate it again! I read it with:
>b> Myfile.read() and all what I get is a str type data, what make my aim  very
>b> difficult to reach!  What I want, is just to read one line(one  specific
>b> line, so  I wouldnt have to read the whole file) and to get  the numbers of
>b> the songs from that line. Maybe I should save the  information in another
>b> way... But I just get those lines as lists, and  write them in a file. Is
>b> there a better way? I am very receptive to  suggestions! Thanks again for
>b> your help!

You are unclear about the syntax of your file. What is a line? Is this a
line?
['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']

If so what do the \n after the filenames mean? Is \n a newline, or is it
a backslash followed by 'n'? In the first case then every filename is on
a separate line. So then you are mixing two concepts of line.

Anyhow you would make your life easier by getting rid of the \n's.

If all your filenames are numeric with the extension mp3 then the
following gives you a list of the filenames from a line:

In [31]: re.findall('[0-9]+\.mp3', line)
Out[31]: ['100.mp3', '10008.mp3', '10005.mp3', '10001.mp3', '10006.mp3']

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exit of Python script

2009-10-14 Thread Diez B. Roggisch
vicky wrote:

> Hello All,
> 
> I am a new Python user and don't know much about it.
> 
> I just want to know that, due to any reason if a script exits, is
> their some way to release all the resources acquired by the script
> during execution ?
> 
> Actually In my system I want to execute some piece of code at the time
> of script exit (expected or unexpected) to ensure the release of all
> the resources. I don't know how to do that :(
> 
> Can anyone please help me ?

What resources? Usually, the garbage-collection and the OS will do that for
you anyway. So are there concrete problems you observe? 

Also, take a look at the atexit-module. 

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


Re: Clear interface for mail class

2009-10-14 Thread Marco Mariani

Benedict Verheyen wrote:


Any ideas are welcome.


easy_install turbomail

:)

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


Clear interface for mail class

2009-10-14 Thread Benedict Verheyen
Hi,


I'm trying to come up with a decent interface for my email class.
Basically, i have one email function but it has many (too many?) variables:

send_mail(self, send_from, send_to, send_cc, subject, text, separate_emails 
= False, files=[], inline_files=[], server="localhost",
charset="iso-8859-1")

I'm thinking on how i could simplify it and make it look nicer and more easy to 
use.
I can reduce the number of arguments by adding functions for the less common 
tasks
like adding attachement, inline files and so on.
Then i would end up with functions like this:

def __init__(self):
"""
"""
self._files=[]
self._inline_files=[]
...

def add_files(self, files=[]):
assert type(files)==list
self._files=files
...

When sending an email, i could check if files where specified and if so, send 
them too.

But it doesn't feel right to just have a bunch of small functions to set 
variables.
Calling the function would change from:
 (where m is the mail class)
 m.send_mail( "from...@work",
  "t...@work",
  [],
  "Test emailmodule ",
  MSG,
  separate_emails = True,
  files=["attached_pic.png"],
  inline_files=["inline_pic.png"],
  server="mysmtpserver")

to:

m.add_files(["attached_pic.png"])
m.add_inline_files(["inline_pic.png"])
m.smtp_server("mysmtpserver")
m.send_mail( "from...@work",
 "t...@work",
 [],
 "Test emailmodule "",
 MSG)

It looks already better and i could set the smtp server as a class variable,
but i'm not sure this will present me with the most natural interface to use.

Or should i make 1 general function that sets vars according to a type?
For instance: add_header("To",[list_of_recipients])

This kind of problems seems to happen sometimes: i need to fix something 
quickly,
build a script for it, and then i realise that the code i've written is not the 
best in
terms of reusability and has some "not so great" functions or classes.
Speedy development eh.

Any ideas are welcome.

Thanks,
Benedict

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


Unexpected exit of Python script

2009-10-14 Thread vicky
Hello All,

I am a new Python user and don't know much about it.

I just want to know that, due to any reason if a script exits, is
their some way to release all the resources acquired by the script
during execution ?

Actually In my system I want to execute some piece of code at the time
of script exit (expected or unexpected) to ensure the release of all
the resources. I don't know how to do that :(

Can anyone please help me ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: id( ) function question

2009-10-14 Thread Christian Heimes
Andre Engels schrieb:
> What is going on is that a few objects that are often used, in
> particular the small (how small is small depends on the
> implementation) integers, are 'preloaded'. When one of these is then
> referred to, a new object is not created, but the pre-defined object
> is used. 10 is apparently a preloaded constant in your implementation,
> 1e10 is not.
> 
> As far as I know, only None is _guaranteed_ to be such a preloaded
> object, so one should not rely on it in implementations.

None, True, False, NotImplemented are guaranteed to be singletons, all
builtin types and exceptions can be considered as singletons, too.

Christian

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


Re: id( ) function question

2009-10-14 Thread Tim Chase

But if I chose as a value another number (a big one, let say 1e10) I
get what I will expect also in the case of the chose of the integer 10
showed above:

a=1e10
d=1e10
d is a

False

id(a)

11388984

id(d)

11388920


CPython has the option to cache frequently used items, and does 
so for a small range of ints.  It's not guaranteed behavior (or a 
guaranteed range) so you shouldn't rely on it, but it's an 
efficiency thing.  In my current version, it looks like it's ints 
from -5 to 256. YMMV


In general, if you're using "is" (and not comparing with None) or 
id(), you're doing it wrong unless you already know the 
peculiarities of Python's identity implementations.


-tkc


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


Re: id( ) function question

2009-10-14 Thread Andre Engels
What is going on is that a few objects that are often used, in
particular the small (how small is small depends on the
implementation) integers, are 'preloaded'. When one of these is then
referred to, a new object is not created, but the pre-defined object
is used. 10 is apparently a preloaded constant in your implementation,
1e10 is not.

As far as I know, only None is _guaranteed_ to be such a preloaded
object, so one should not rely on it in implementations.

On Wed, Oct 14, 2009 at 12:46 PM, raffaele ponzini
 wrote:
> Dear all,
> I have a question concerning the output of the id() function.
> In particular since is should:
> ""
> Return the identity of an object.  This is guaranteed to be unique among
> simultaneously existing objects.  (Hint: it's the object's memory address.)
> ""
> i expect that for two differnt objects it returns two differnt adress in 
> memory.
>
> Let's seee a correct case:
 a=10
 b=20
 a is b
> False
 id(a)
> 9986060
 id(b)
> 9985940
 c=a
 c is a
> True
 id(c)
> 9986060
 id(a)
> 9986060
>
> And now a strange (for me) output:
 d=10 #here i'm assingning a integer value to a fresh new variable d 
 without any kind of  link to the variable a
 d is a
> True
 d==a
> True
 id(a)
> 9986060
 id(b)
> 9985940
 id(d)
> 9986060
 a=1e10
 d=1e10
 d is a
> False
 id(a)
> 11388984
 id(d)
> 11388920

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




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


Re: id( ) function question

2009-10-14 Thread Christian Heimes
raffaele ponzini schrieb:
> Dear all,
> I have a question concerning the output of the id() function.
> In particular since is should:
> ""
> Return the identity of an object.  This is guaranteed to be unique among
> simultaneously existing objects.  (Hint: it's the object's memory address.)
> ""
> i expect that for two differnt objects it returns two differnt adress in 
> memory.

[snip]

Some Python implementations may choose to cache certain objects or use
an internal free list to cache objects. For example CPython caches small
integers, strings with one character and some other objects. It uses a
free list for tuples, lists and dicts. This leads to effects like:

>>> {} is {}
False
>>> id({}) == id({})
True

Christian

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


Re: python performance on Solaris

2009-10-14 Thread Antoine Pitrou
inaf  gmail.com> writes:
> 
> Good point. I failed to compare the CPU power on these machines.. 32
> bit linux box I have is 2666 Mhz vs the Solaris zone is 1415 Mhz.. I
> guess that explains :) Thank you for the tip..

You have to compare not only CPU frequencies but the CPU models.
Recently Sun has been selling CPUs optimized for multi-threading (e.g. the
"UltraSPARC T2" or Niagara CPUs) which have, by design, very poor
single-threaded performance. If your Solaris zone uses such a CPU then a 6-8x
difference in single-threaded performance compared to a modern Intel or AMD CPU
is totally expected.

Regards

Antoine.


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


Re: win32com.client import problem

2009-10-14 Thread Dave Angel

Threader Slash wrote:

Hi Everybody,

I have 2 imports:

import pythoncom
from win32com.client import Dispatch

if I run it on my Python 2.6 Console, it works nicely. However, when I go to
Eclipse IDE, open a project, open a main.py file, and try run, it gives the
error:

import pythoncom
ImportError: No module named pythoncom

All other imports are working ok on Eclipse IDE -- e.g. import MySQLdb.

Any suggestion about what is missing?

All comments and suggestion are welcome.

ThreaderSlash

  
Two things to check, python version (sys.version), and sys.path.   Add 
prints for the two of them at the beginning of your script, and try the 
script in both environments.  If there are any differences, figure out 
how to reconfigure Eclipse to match what you've got at the console.



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


Re: the usage of 'yield' keyword

2009-10-14 Thread Dave Angel

Peng Yu wrote:

http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt

The explanation of yield is not clear to me, as I don't know what a
generator is. I see the following example using 'yield'. Could
somebody explain how 'yield' works in this example? Thank you!

def brange(limit):
  i = 0
  while i < limit:
  yield i
  i += 1

  
Informally, a generator is a convenient syntax for writing an iterator.  
What the above does is match xrange() behavior for single argument, but 
without any limitation on the size or type of the limit parameter.


A little too much detail:  When a function has the keyword "yield" 
within it, the function works much differently than normal functions.  
When it's called as a function, it returns a special object called an 
iterator.  That iterator has a method called next(), which when called 
executes a piece of this function.  It executes the function until it 
encounters a 'yield' statement.  Then the yield value is returned from 
the next() function, but the function is still out-there, suspended.  
All local variables still exist, and it's just waiting for a chance to 
run some more.  Next time next() method is called, the function resumes 
right after the yield, and runs until it gets to another yield (in this 
case the same yield, but with a new value).  That new value is returned 
from the next().  Eventually, the function may 'return' instead of 
'yield', as this one does when the limit value is reached.  At that 
point, it generates a "stop iteration" exception (or some name like 
that).  Now this sounds way too complex.


But, if you study the definition of the for loop, you'll find a 
complementary description.  When you write

   for  item in  iterable:

The iterable is called (I'm blurring the details of when you need 
parens, and when you don't), and next() is called repeatedly, with the 
results of next() being assigned to 'item' until an exception occurs.  
If the exception is 'stop iteration' then the loop terminates normally.


Bottom line is it is easy to write very complicated generators which are 
easy to use in simple for loops.  And although you should also try to 
create an iterator object manually, for the experience, most of the time 
the generator function is much easier/quicker to write, and much easier 
to maintain and debug.




Note that yield can do even more.  I'm trying to describe the common 
usage, which is very convenient.  And it can frequently  be used to take 
a complicated generator and wrap it to make an easier-to-use generator 
that's more specific to a particular purpose.  For example, if os.walk 
is a pain to use, and all you need is a sequence of all the files in a 
directory tree,


def find(root):
  for pdf in os.walk(root, topdown=False):
  for file in pdf[2]:
  yield os.path.join(pdf[0],file)



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


id( ) function question

2009-10-14 Thread raffaele ponzini
Dear all,
I have a question concerning the output of the id() function.
In particular since is should:
""
Return the identity of an object.  This is guaranteed to be unique among
simultaneously existing objects.  (Hint: it's the object's memory address.)
""
i expect that for two differnt objects it returns two differnt adress in memory.

Let's seee a correct case:
>>> a=10
>>> b=20
>>> a is b
False
>>> id(a)
9986060
>>> id(b)
9985940
>>> c=a
>>> c is a
True
>>> id(c)
9986060
>>> id(a)
9986060

And now a strange (for me) output:
>>> d=10
#here i'm assingning a integer value to a fresh new variable d without
any kind of
#link to the variable a

>>> d is a
True
>>> d==a
True
>>> id(a)
9986060
>>> id(d)
9986060

But if I chose as a value another number (a big one, let say 1e10) I
get what I will expect also in the case of the chose of the integer 10
showed above:
>>> a=1e10
>>> d=1e10
>>> d is a
False
>>> id(a)
11388984
>>> id(d)
11388920
>>>

can you please explain me the reasion of this strange behaviour.
Thanks,
--
-- 
lele
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >