Tuesday, February 9, 2010

comp.lang.python - 25 new messages in 14 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* errno 107 socket.recv issue - 6 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/ff1ce95292f1a5ec?hl=en
* Programing family - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/9c369186368028ed?hl=en
* PostgreSQL driver for Python applications that supports bytea correctly? - 1
messages, 1 author
http://groups.google.com/group/comp.lang.python/t/70a061b40f4c8cc7?hl=en
* Executing Commands From Windows Service - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/9f541d309af57595?hl=en
* CLICK WEB AND ERAN MONEY - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/f8dc2e4d3a06cc6b?hl=en
* Modifying Class Object - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/fd36962c4970ac48?hl=en
* mac install - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/fa0d8c6943641d32?hl=en
* pyclutter anyone? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/88aaddf9983d514c?hl=en
* ANN: obfuscate - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6374e775e474ee1a?hl=en
* Incorrect 'Host' header when using urllib2 to access a server by its IPv6
link-local address - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6fe6af758d4fca42?hl=en
* To (monkey)patch or not to (monkey)patch, that is the question - 1 messages,
1 author
http://groups.google.com/group/comp.lang.python/t/49c9d1124ead9150?hl=en
* Pycrypto RSA Issue - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/9c5587fe42575deb?hl=en
* use strings to call functions - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/87c34eb48790eda2?hl=en
* How to measure elapsed time under Windows? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/340c476d0357a7bd?hl=en

==============================================================================
TOPIC: errno 107 socket.recv issue
http://groups.google.com/group/comp.lang.python/t/ff1ce95292f1a5ec?hl=en
==============================================================================

== 1 of 6 ==
Date: Tues, Feb 9 2010 8:36 am
From: Jordan Apgar

> http://docs.python.org/library/socketserver.html
>
> JM

each time a handler is spawned is it client specific? in other words
when two clients send something to the server do handlers spawn for
each of them or does everything just go into a single handler?


== 2 of 6 ==
Date: Tues, Feb 9 2010 9:49 am
From: Jean-Michel Pichavant


Jordan Apgar wrote:
>> http://docs.python.org/library/socketserver.html
>>
>> JM
>>
>
> each time a handler is spawned is it client specific? in other words
> when two clients send something to the server do handlers spawn for
> each of them or does everything just go into a single handler?
>
docstring of the example:

"""
The RequestHandler class for our server.

It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""

If you want data persistant over connections, store them in your handler
class.

class MyTCPHandler(SocketServer.BaseRequestHandler):
cnxNumber = 0
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
# just send back the same data, but upper-cased
self.request.send(self.data.upper())
cnxNumber +=1
print "handling connection N°%s" % cnxNumber

JM


== 3 of 6 ==
Date: Tues, Feb 9 2010 10:03 am
From: Jordan Apgar


thanks JM,

at this point i switched over to this scheme and now I'm getting an
error durring instantiation of the server:
Server.py:
from Crypto.PublicKey import RSA
from ServerNegotiator import ServerNegotiator
from sharedComs import *

f = open("hostid")
tup = stringToTuple(f.readline()[0:-1])
HostID = f.readline()[0:-1]
f.close()

key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
long(tup[3]),
long(tup[4]),long(tup[5])))
host = "localhost"
port = 8005

servernegotiator = ServerNegotiator(host,HostID, port, key)
servernegotiator.start()


ServerNegotiatior.py lines 185 - end
class ServerNegotiator:
def __init__(self, host, port, hostid, rsa_key, buf = 512):
negotiator = Negotiator(host, hostid, rsa_key,buf)
self.server = SocketServer.TCPServer((host, port), negotiator)

def start(self):
self.server.serve_forever()

Traceback (most recent call last):
File "Server.py", line 16, in <module>
servernegotiator = ServerNegotiator(host,HostID, port, key)
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 188, in __init__
self.server = SocketServer.TCPServer((host, port), negotiator)
File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
TypeError: an integer is required

== 4 of 6 ==
Date: Tues, Feb 9 2010 10:41 am
From: George Trojan


Argument mismatch?

Jordan Apgar wrote:
>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
>
> class ServerNegotiator:
> def __init__(self, host, port, hostid, rsa_key, buf = 512):
>


== 5 of 6 ==
Date: Tues, Feb 9 2010 10:51 am
From: Jean-Michel Pichavant


Jordan Apgar wrote:
> thanks JM,
>
> at this point i switched over to this scheme and now I'm getting an
> error durring instantiation of the server:
> Server.py:
> from Crypto.PublicKey import RSA
> from ServerNegotiator import ServerNegotiator
> from sharedComs import *
>
> f = open("hostid")
> tup = stringToTuple(f.readline()[0:-1])
> HostID = f.readline()[0:-1]
> f.close()
>
> key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
> long(tup[3]),
> long(tup[4]),long(tup[5])))
> host = "localhost"
> port = 8005
>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
> servernegotiator.start()
>
>
> ServerNegotiatior.py lines 185 - end
> class ServerNegotiator:
> def __init__(self, host, port, hostid, rsa_key, buf = 512):
> negotiator = Negotiator(host, hostid, rsa_key,buf)
> self.server = SocketServer.TCPServer((host, port), negotiator)
>
> def start(self):
> self.server.serve_forever()
>
>
>
>
>
> Traceback (most recent call last):
> File "Server.py", line 16, in <module>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
> File "/home/twistedphrame/Desktop/communication/
> ServerNegotiator.py", line 188, in __init__
> self.server = SocketServer.TCPServer((host, port), negotiator)
> File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
> self.server_bind()
> File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
> self.socket.bind(self.server_address)
> File "<string>", line 1, in bind
> TypeError: an integer is required
>
>

servernegotiator = ServerNegotiator(host,HostID, port, key)
class ServerNegotiator:
def __init__(self, host, port, hostid, rsa_key, buf = 512):


you swapped port & hostID in your call

JM

== 6 of 6 ==
Date: Tues, Feb 9 2010 11:13 am
From: Jordan Apgar


On Feb 9, 1:51 pm, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> Jordan Apgar wrote:
> > thanks JM,
>
> > at this point i switched over to this scheme and now I'm getting an
> > error durring instantiation of the server:
> > Server.py:
> > from Crypto.PublicKey import RSA
> > from ServerNegotiator import ServerNegotiator
> > from sharedComs import *
>
> > f = open("hostid")
> > tup = stringToTuple(f.readline()[0:-1])
> > HostID = f.readline()[0:-1]
> > f.close()
>
> > key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
> > long(tup[3]),
> >                      long(tup[4]),long(tup[5])))
> > host = "localhost"
> > port = 8005
>
> > servernegotiator = ServerNegotiator(host,HostID, port, key)
> > servernegotiator.start()
>
> > ServerNegotiatior.py lines 185 - end
> > class ServerNegotiator:
> >     def __init__(self, host, port, hostid, rsa_key, buf = 512):
> >         negotiator = Negotiator(host, hostid, rsa_key,buf)
> >         self.server = SocketServer.TCPServer((host, port), negotiator)
>
> >     def start(self):
> >         self.server.serve_forever()
>
> > Traceback (most recent call last):
> >   File "Server.py", line 16, in <module>
> >     servernegotiator = ServerNegotiator(host,HostID, port, key)
> >   File "/home/twistedphrame/Desktop/communication/
> > ServerNegotiator.py", line 188, in __init__
> >     self.server = SocketServer.TCPServer((host, port), negotiator)
> >   File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
> >     self.server_bind()
> >   File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
> >     self.socket.bind(self.server_address)
> >   File "<string>", line 1, in bind
> > TypeError: an integer is required
>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
> class ServerNegotiator:
>     def __init__(self, host, port, hostid, rsa_key, buf = 512):
>
> you swapped port & hostID in your call
>
> JM

tThanks guys it's working now... feel a little stupid though.

==============================================================================
TOPIC: Programing family
http://groups.google.com/group/comp.lang.python/t/9c369186368028ed?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Feb 9 2010 8:49 am
From: Bruno Desthuilliers


On Feb 8, 2010, at 8:39 PM, AON LAZIO <aonlazio@gmail.com> wrote:
>
>> I have thought funny things
>> If we think all languages are like a family

Then it would be a very incestuous family fore sure.

>> I could draft them like this (Python base)
>>
>> C is Python's Mom
>> C++ : Dad

Not that much C++ in Python, IMHO. If that's for the OO part, then the
closer to Python's object model I can think of is javascript.

Historically, Python comes from ABC, which itself comes from SETL.

>> Pascal/Assembly : Grandparents

Assembly ? What about binary machine code then ?-)

>> C# : Uncle
>> Java : Ant

Interesting typo here !-)

Hmmm... Python predates both C# and Java. Ok, technically speaking
nothing prevents an uncle or aunt from being younger than their nephews,
and I even saw the case a couple time - but that's far from being the
common case.

>> Ruby: Cousin
>> Perl : Girlfriend

Then it's kind of a very passionate love-hate relationship - bordering
on pathological FWIW !-)

Now you forgot the whole Lisp / ML heritage - most FP stuff -, and of
course Simula and Smalltalk.

== 2 of 3 ==
Date: Tues, Feb 9 2010 8:56 am
From: Paul Rubin


Bruno Desthuilliers <bruno.42.desthuilliers@websiteburo.invalid> writes:
> Now you forgot the whole Lisp / ML heritage - most FP stuff -, and of
> course Simula and Smalltalk.

http://i.imgur.com/1gF1j.jpg


== 3 of 3 ==
Date: Tues, Feb 9 2010 10:58 am
From: Gnarlodious


On Feb 9, 9:56 am, Paul Rubin wrote:

> http://i.imgur.com/1gF1j.jpg

Very funny, except where is Python and Forth?

-- Gnarlie

==============================================================================
TOPIC: PostgreSQL driver for Python applications that supports bytea correctly?

http://groups.google.com/group/comp.lang.python/t/70a061b40f4c8cc7?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 8:51 am
From: "D'Arcy J.M. Cain"


On Tue, 9 Feb 2010 07:27:39 -0800 (PST)
CyclingGuy <ericthecyclist@gmail.com> wrote:
> Can anyone recommend a PostgreSQL driver for Python that supports
> selecting and inserting bytea types?
> I'm not looking to write server functions in Python, just client
> applications.

http://www.PyGreSQL.org/

--
D'Arcy J.M. Cain <darcy@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

==============================================================================
TOPIC: Executing Commands From Windows Service
http://groups.google.com/group/comp.lang.python/t/9f541d309af57595?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 9:09 am
From: Sean DiZazzo


On Feb 9, 6:52 am, T <misceveryth...@gmail.com> wrote:
> On Feb 8, 2:25 pm, David Bolen <db3l....@gmail.com> wrote:
>
>
>
> > T <misceveryth...@gmail.com> writes:
> > > I have a script, which runs as a Windows service under the LocalSystem
> > > account, that I wish to have execute some commands.  Specifically, the
> > > program will call plink.exe to create a reverse SSH tunnel.  Right now
> > > I'm using subprocess.Popen to do so.  When I run it interactively via
> > > an admin account, all is well.  However, when I'm running it via
> > > service, no luck.  I'm assuming this is to do with the fact that it's
> > > trying to run under the LocalSystem account, which is failing.  What
> > > would be the best way around this?  Thanks!
>
> > The LocalSystem account is not, if I recall correctly, permitted to
> > access the network.
>
> > You'll have to install the service to run under some other account that
> > has appropriate access to the network.
>
> > -- David
>
> The more testing I do, I think you may be right..I was able to get it
> to work under a local admin account, and it worked under debug mode
> (which would also have been running as this user).  I'm a bit
> surprised though - I was under the assumption that LocalSystem had
> rights to access the network?

You really need a way to see the error you are getting. If you can't
get it to show you the error in the shell, set up some logging to a
file, and find the error that way. I think the user can access the
network just fine, but that maybe plink.exe is not in his path or some
such thing.

Find the error!

==============================================================================
TOPIC: CLICK WEB AND ERAN MONEY
http://groups.google.com/group/comp.lang.python/t/f8dc2e4d3a06cc6b?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 9:20 am
From: soniyaa 1111


HI FRIENDS,,,,
Please see this site 259http://www.shoppingreps.com?SourceId=1259 and
try to join if anybody interested.

==============================================================================
TOPIC: Modifying Class Object
http://groups.google.com/group/comp.lang.python/t/fd36962c4970ac48?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 9:25 am
From: Terry Reedy


On 2/9/2010 12:12 AM, Alf P. Steinbach wrote:

> As far as the language spec is concerned the argument passing mechanism
> seems to me to be identical to assignments, not just "very much like".

Except for the cross-namespace nature of the assignment, yes. Glad you
agree.

> Your phrase "or objects derived therefrom" seems to imply that immutable
> objects can be copied

No. Given

def f(*args, **kwds): print(args, kwds)

the objects bound to args and kwds are a tuple and dict *derived* from
(that collect together) the objects passed.

Terry Jan Reedy


==============================================================================
TOPIC: mac install
http://groups.google.com/group/comp.lang.python/t/fa0d8c6943641d32?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Feb 9 2010 9:39 am
From: Thomas Nelson


I downloaded the 3.1.1 dmg from http://www.python.org/download/releases/3.1.1/
but when I run it I get the error "The folowing install step failed:
run postflight script for python documentation." The bugs list has
this bug at http://bugs.python.org/issue6934 but it's described as
fixed. Is it only fixed for the source version? Where should I go to
install?

Thanks,
Tom


== 2 of 2 ==
Date: Tues, Feb 9 2010 10:51 am
From: Gnarlodious


Hello. The answer is, I don't know. But I originally replaced my
existing Python with 3.1.1. I then had to backtrack because some stuff
broke. Nothing in the OS, but adapting every Python script on my HD
was not what I wanted to do.

So my Py3 executable is at:
/Library/Frameworks/Python.framework/Versions/3.1/bin/python3.1
then symlink to that in folder:
/usr/local/bin

using command:
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.1/bin/
python3.1 /usr/local/bin/python3.1

so that saying:
ls /usr/local/bin

shows the link to the Py3 folder as:
python3.1 -> /Library/Frameworks/Python.framework/Versions/3.1/bin/
python3.1

Now your existing scripts still work on Py 2.6. New scripts invoke Py3
like this:
#!/usr/local/bin/python3.1

For Terminal sessions, an alias in your bash_profile puts you in Py3
by default without nasty surprises:
alias python=/usr/local/bin/python3.1

You can also create a file at:
/Library/Python/3.1/site-packages/sitecustomize.py

with a configuration that runs at loadtime, although some things seem
to fail. But all in all this system has worked well for me (including
CGI) and should be easy to go mainstream once Apple goes to Py3.

-- Gnarlie


==============================================================================
TOPIC: pyclutter anyone?
http://groups.google.com/group/comp.lang.python/t/88aaddf9983d514c?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 9:40 am
From: aahz@pythoncraft.com (Aahz)


In article <mailman.1985.1265390331.28905.python-list@python.org>,
donn <donn.ingle@gmail.com> wrote:
>
>No one uses pyClutter?

Never heard of it before.

>I have some code, it does not work, but maybe this will start to help
>solve the problem:

You need to provide some more detail than "does not work". Perhaps this
will help:

http://www.catb.org/~esr/faqs/smart-questions.html
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

import antigravity

==============================================================================
TOPIC: ANN: obfuscate
http://groups.google.com/group/comp.lang.python/t/6374e775e474ee1a?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 9:47 am
From: aahz@pythoncraft.com (Aahz)


In article <mailman.2238.1265733013.28905.python-list@python.org>,
Robert Kern <robert.kern@gmail.com> wrote:
>On 2010-02-09 09:37 AM, Daniel Fetchinson wrote:
>>>
>>> obfuscate is a pure-Python module providing classical encryption
>>> algorithms suitable for obfuscating and unobfuscating text.
>>>
>>> DISCLAIMER: obfuscate is not cryptographically strong, and should not be
>>> used where high security is required. (The ciphers provided in obfuscate
>>> may have been state of the art centuries ago, but should not be used
>>> where strong encryption is required.
>>
>> Great, these packages are badly needed!
>>
>> If the code base stabilizes in a production version after losing the
>> alphas and betas they would be a great addition to the stdlib, I
>> think.
>
>Why?

You missed the white-on-white smiley, I think.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

import antigravity

==============================================================================
TOPIC: Incorrect 'Host' header when using urllib2 to access a server by its
IPv6 link-local address
http://groups.google.com/group/comp.lang.python/t/6fe6af758d4fca42?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 10:02 am
From: slmnhq


I have a snippet of Python code that makes an HTTP GET request to an
Apache web server (v 2.2.3) using urllib2. The server responds with an
HTTP 400 error presumably because of a malformed 'Host' header.

The snippet is quite simple: it creates a url based on IPv6 string
literal syntax, then creates a Request object and calls urlopen.

def via_urllib2 (addr="fe80::207:b8ff:fedc:636b", scope_id="eth4"):
host = "[%s%%%s]:80" % (addr, scope_id) # "[fe80::207:b8ff:fedc:
636b%eth4]"
url = "http://"+host+"/system/status/"
req = urllib2.Request(url)
f = urllib2.urlopen(req)
print f.read()

The urlopen() throws: HTTPError: HTTP Error 400: Bad Request

The Apache error_log reports "Client sent malformed Host header".

Googling for "apache Client sent malformed Host header ipv6" I came
across the following bug:

https://issues.apache.org/bugzilla/show_bug.cgi?id=35122

The problem is that Apache does not handle the scope id in the host
header field very well and reports a 400 error. So I tried to override
that field by creating my own header in the above snippet:

...
req.add_header('Host', "["+urllib.quote(addr)+"]")
...

Now the header is simply 'Host: [fe80::207:b8ff:fedc:636b]" (notice
the lack of "%eth4"). However, this still results in the same error.

I know this is not a problem with urllib2 per say, but I'm posting
here in the hope that some Python coder may be knowledgeable enough
about HTTP and IPv6 that they might be able to provide an answer.

Thank you,

Salman

==============================================================================
TOPIC: To (monkey)patch or not to (monkey)patch, that is the question
http://groups.google.com/group/comp.lang.python/t/49c9d1124ead9150?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 10:03 am
From: Raymond Hettinger


On Feb 9, 12:54 am, George Sakkis <george.sak...@gmail.com> wrote:
> So I'm wondering if there is a consensus on when it's better to (hard)
> patch, monkey patch or just try to work around a third party package
> that doesn't do exactly what one would like. Does it have mainly to do
> with the reason for the patch (e.g. fixing a bug, modifying behavior,
> adding missing feature), the given package (size, complexity,
> maturity, developer responsiveness), something else or there are no
> general rules and one should decide on a case-by-case basis ?

I agree that practically beats purity here. Python is a consenting
adults language that makes monkey patching possible. It provides
a direct way to compensate for someone failing to put hooks in
their API.

The risk of monkey patching is that if other modules use the patched
module, they have no way of knowing that the behavior is changed.
IOW, monkey patching is a fragile technique for a large project.


Raymond


==============================================================================
TOPIC: Pycrypto RSA Issue
http://groups.google.com/group/comp.lang.python/t/9c5587fe42575deb?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Feb 9 2010 10:09 am
From: Jordan Apgar


I am trying to encrypt public data along with another tuple and then
decrypt it after sending. RSA is needed for negotiation of keys for
AES. But I just get garbage after I decrypt it. This is what I'm
attempting to do:

from Crypto.PublicKey import RSA
from Crypto import Random

gkey = RSA.generate(384, Random.new().read)
string = str((gkey.publickey().__getstate__(),
(333,444)))
print string
print
data = gkey.encrypt(string,"")
print "data:",data
print
print "decrypt"
print "decrypt,", gkey.decrypt(data)
print "done"

All I get are junk values when printing the decrypted value.


== 2 of 3 ==
Date: Tues, Feb 9 2010 10:27 am
From: Legrandin

> gkey = RSA.generate(384, Random.new().read)
> string = str((gkey.publickey().__getstate__(),(333,444)))

You are encrypting with RSA a piece of data which is way
larger than the key size (48 bytes).

== 3 of 3 ==
Date: Tues, Feb 9 2010 11:14 am
From: Jordan Apgar


On Feb 9, 1:27 pm, Legrandin <pheenso...@farifluset.mailexpire.com>
wrote:
> > gkey = RSA.generate(384, Random.new().read)
> > string = str((gkey.publickey().__getstate__(),(333,444)))
>
> You are encrypting with RSA a piece of data which is way
> larger than the key size (48 bytes).

ah thanks Legrandin

==============================================================================
TOPIC: use strings to call functions
http://groups.google.com/group/comp.lang.python/t/87c34eb48790eda2?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Feb 9 2010 10:11 am
From: OdarR


On 9 fév, 11:01, Stefan Behnel <stefan...@behnel.de> wrote:
> Klaus Neuner, 09.02.2010 10:04:
>
> > my program is supposed to parse files that I have created myself and that
> > are on my laptop. It is not supposed to interact with anybody else
> > than me.
>
> Famous last words.
>
> Stefan

I knew it.

Olivier

==============================================================================
TOPIC: How to measure elapsed time under Windows?
http://groups.google.com/group/comp.lang.python/t/340c476d0357a7bd?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Feb 9 2010 10:23 am
From: Jean-Michel Pichavant


Grant Edwards wrote:
> What's the correct way to measure small periods of elapsed
> time. I've always used time.clock() in the past:
>
> start = time.clock()
> [stuff being timed]
> stop = time.clock()
>
> delta = stop-start
>
>
> However on multi-processor machines that doesn't work.
> Sometimes I get negative values for delta. According to
> google, this is due to a bug in Windows that causes the value
> of time.clock() to be different depending on which core in a
> multi-core CPU you happen to be on. [insert appropriate
> MS-bashing here]
>
> Is there another way to measure small periods of elapsed time
> (say in the 1-10ms range)?
>
> Is there a way to lock the python process to a single core so
> that time.clock() works right?
>
>

Did you try with the datetime module ?

import datetime
t0 = datetime.datetime.now()
t1 = t0 - datetime.datetime.now()
t1.microseconds
Out[4]: 644114

JM

== 2 of 2 ==
Date: Tues, Feb 9 2010 11:04 am
From: Grant Edwards


On 2010-02-09, Jean-Michel Pichavant <jeanmichel@sequans.com> wrote:
> Grant Edwards wrote:
>> What's the correct way to measure small periods of elapsed
>> time. I've always used time.clock() in the past:
>>
>> start = time.clock()
>> [stuff being timed]
>> stop = time.clock()
>>
>> delta = stop-start
>>
>> However on multi-processor machines that doesn't work.
>> Sometimes I get negative values for delta. According to
>> google, this is due to a bug in Windows that causes the value
>> of time.clock() to be different depending on which core in a
>> multi-core CPU you happen to be on. [insert appropriate
>> MS-bashing here]
>>
>> Is there another way to measure small periods of elapsed time
>> (say in the 1-10ms range)?
>>
>> Is there a way to lock the python process to a single core so
>> that time.clock() works right?
>>
>>
>
> Did you try with the datetime module ?

No. What mechanism does it use to get the current date/time?

> import datetime
> t0 = datetime.datetime.now()
> t1 = t0 - datetime.datetime.now()
> t1.microseconds
> Out[4]: 644114

That looks very broken to me. I need to measure stuff in the
1-20ms range, and the smallest value you can measure using the
method above appears to be 640ms. Thats almost 2/3 of a second.

--
Grant Edwards grante Yow! If our behavior is
at strict, we do not need fun!
visi.com


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate