Re: os.system question

2008-08-07 Thread SamG
On Aug 7, 6:07 am, Kevin Walzer <[EMAIL PROTECTED]> wrote:
>  >>> import os
>  >>> foo = os.system('whoami')
> kevin
>  >>> print foo
> 0
>  >>>
>
> The standard output of the system command 'whoami' is my login name. Yet
> the value of the 'foo' object is '0,' not 'kevin.' How can I get the
> value of 'kevin' associated with foo?
>
> --
> Kevin Walzer
> Code by Kevinhttp://www.codebykevin.com

Why dont you try commands module instead

In [28]: import commands

In [29]: dir(commands)
Out[29]:
['__all__',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 'getoutput',
 'getstatus',
 'getstatusoutput',
 'mk2arg',
 'mkarg']

In [30]: (a,b) = commands.getstatusoutput('whoami')

In [31]: print a
0

In [32]: print b
luma35

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


Re: benchmark

2008-08-07 Thread jlist
I think what makes more sense is to compare the code one most
typically writes. In my case, I always use range() and never use psyco.
But I guess for most of my work with Python performance hasn't been
a issue. I haven't got to write any large systems with Python yet, where
performance starts to matter.

> That Python code is bad, it contains range() instead of xrange, the
> big loop is in the main code instead of inside a function, uses ==
> None, etc. That person can try this (with Psyco), I have changed very
> little, the code is essentially the same:


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


Re: Calculate sha1 hash of a binary file

2008-08-07 Thread Nikolaus Rath
LaundroMat <[EMAIL PROTECTED]> writes:
> Hi -
>
> I'm trying to calculate unique hash values for binary files,
> independent of their location and filename, and I was wondering
> whether I'm going in the right direction.
>
> Basically, the hash values are calculated thusly:
>
> f = open('binaryfile.bin')
> import hashlib
> h = hashlib.sha1()
> h.update(f.read())
> hash = h.hexdigest()
> f.close()
>
> A quick try-out shows that effectively, after renaming a file, its
> hash remains the same as it was before.
>
> I have my doubts however as to the usefulness of this. As f.read()
> does not seem to read until the end of the file (for a 3.3MB file only
> a string of 639 bytes is being returned, perhaps a 00-byte counts as
> EOF?), is there a high danger for collusion?
>
> Are there better ways of calculating hash values of binary files?


Apart from opening the file in binary mode, I would consider to read
and update the hash in chunks of e.g. 512 KB. The above code is
probably going to perform horribly for sufficiently large files, since
you try read the entire file into memory.


Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
 -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list

.cpp to .pyd

2008-08-07 Thread vedrandekovic
Hello,

I want to build my C++ (.cpp) script to (.pyd) like this:

http://en.wikibooks.org/wiki/Python_Programming/Extending_with_C%2B%2B

I have installed "Microsoft Visual studio .NET 2003" and "Boost
Python" and then after I run my setup script:

python setup.py build


I get this error:


running build
running build_ext
building 'hello' extension
D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c /
nologo /Ox
 /MD /W3 /GX /DNDEBUG -IC:\Panda3D-1.5.2\python\include -IC:
\Panda3D-1.5.2\pytho
n\PC /Tphellomodule.cpp /Fobuild\temp.win32-2.5\Release
\hellomodule.obj
hellomodule.cpp
hellomodule.cpp(9) : fatal error C1083: Cannot open include file:
'boost/python/
module.hpp': No such file or directory
error: command '"D:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\bin\cl.e
xe"' failed with exit status 2

I think that my MS visual studio  cannot find "boost python", if
that's the problem then can you tell me how can I solve it.
This is very begginer question,but I can't find answer nowhere, and I
don't have any expirience with Microsoft products.


Sorry for my bad english!
Regards,
Veki
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread alex23
Steven D'Aprano wrote:
> In other words, about 20% of the time he measures is the time taken to
> print junk to the screen.

Which makes his claim that "all the console outputs have been removed
so that the benchmarking activity is not interfered with by the IO
overheads" somewhat confusing...he didn't notice the output? Wrote it
off as a weird Python side-effect?

I find his reluctance to entertain more idiomatic implementations
particularly telling. It's seems he's less interested in actual
performance comparisons and more into showing that writing static lang
style code in dynamic langs is bad, which isn't really anything new to
anyone anywhere, I would've thought.

All up, this reminds me of last years benchmarking fiasco that
demonstrated Storm's ORM handling being -incredibly-faster than
SQLAlchemy's SQL expression handling, something that just didn't seem
to be born out by user experience. Eventually, Mike Beyer reverse
engineered the benchmarks to discover that, surprise surprise, the
tests -weren't equal-; in one test SQLA was issuing a commit per
insert, while Storm was performing -no commits whatsoever-.

Benchmarks, IMO, are like statistics. You can tweak them to prove
pretty much any position you already take.
--
http://mail.python.org/mailman/listinfo/python-list


Re: .cpp to .pyd

2008-08-07 Thread Matthieu Brucher
> I think that my MS visual studio  cannot find "boost python", if
> that's the problem then can you tell me how can I solve it.
> This is very begginer question,but I can't find answer nowhere, and I
> don't have any expirience with Microsoft products.

Hi,

Put Boost in your include path (options of Visual Studio).

Matthieu
-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using an DTD not specified in XML file for validation

2008-08-07 Thread Enrico
"Brian Quinlan" <[EMAIL PROTECTED]> ha scritto nel messaggio
news:[EMAIL PROTECTED]
> Hey,
>
> I'm trying to figure out how I can validate an XML file using a DTD that
> isn't specified in the XML file.

I did it once using lxml. You can read from:
http://codespeak.net/lxml/validation.html

With this package is quite simple (code not tested):

from lxml import etree
dtd = etree.DTD('mydtd.dtd')
f = file('mydoc.xml')
xml = etree.XML(f.read())
dtd.validate(xml)

Enrico


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


Re: Best practise implementation for equal by value objects

2008-08-07 Thread Duncan Booth
Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> I believe the recommendation for eval(repr(obj)) to give obj again is 
> meant as a convenience for the interactive interpreter, and even there 
> only for simple types like int or list.


and even with types as simple as list and int the round tripping can easily 
fail:

>>> lst = [1, 2, 3]
>>> lst[1] = lst
>>> eval(repr(lst))

Traceback (most recent call last):
  File "", line 1, in 
eval(repr(lst))
  File "", line 1
[1, [...], 3]
 ^
SyntaxError: invalid syntax

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing of a file

2008-08-07 Thread Bruno Desthuilliers

Tommy Grav a écrit :

I have a file with the format

Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames 5 
Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames 5 
Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames 5 
Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames 5 
Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames 5 
Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames 5 
Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames 5 
Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames 5 
Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames 5 
Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames 5 
Set 10


I would like to parse this file by extracting the field id, ra, dec and 
mjd for each line. It is
not, however, certain that the width of each value of the field id, ra, 
dec or mjd is the same
in each line. Is there a way to do this such that even if there was a 
line where Ra=** and

MJD= was swapped it would be parsed correctly?


Q&D :

src = open('/path/to/yourfile.ext')
parsed = []
for line in src:
line = line.strip()
if not line:
continue
head, rest = line.split(':', 1)
field_id = head.split()[1]
data = dict(field_id=field_id)
parts = rest.split()
for part in parts:
try:
key, val = part.split('=')
except ValueError:
continue
data[key] = val
parsed.append(data)
src.close()
--
http://mail.python.org/mailman/listinfo/python-list


Horses

2008-08-07 Thread weiguo6
Where have all the horses gone?

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


Re: .cpp to .pyd

2008-08-07 Thread vedrandekovic
On 7 kol, 09:48, "Matthieu Brucher" <[EMAIL PROTECTED]>
wrote:
> > I think that my MS visual studio  cannot find "boost python", if
> > that's the problem then can you tell me how can I solve it.
> > This is very begginer question,but I can't find answer nowhere, and I
> > don't have any expirience with Microsoft products.
>
> Hi,
>
> Put Boost in your include path (options of Visual Studio).
>
> Matthieu
> --
> French PhD student
> Website :http://matthieu-brucher.developpez.com/
> Blogs :http://matt.eifelle.comandhttp://blog.developpez.com/?blog=92
> LinkedIn :http://www.linkedin.com/in/matthieubrucher

Hello again,

Thanks for your quick reply!

> Put Boost in your include path (options of Visual Studio).

Look at my image:
http://img225.imageshack.us/my.php?image=zaimghostfy1.png

I still get the same error.

Regards,
Veki
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread Steven D'Aprano
On Thu, 07 Aug 2008 00:44:14 -0700, alex23 wrote:

> Steven D'Aprano wrote:
>> In other words, about 20% of the time he measures is the time taken to
>> print junk to the screen.
> 
> Which makes his claim that "all the console outputs have been removed so
> that the benchmarking activity is not interfered with by the IO
> overheads" somewhat confusing...he didn't notice the output? Wrote it
> off as a weird Python side-effect?

Wait... I've just remembered, and a quick test confirms... Python only 
prints bare objects if you are running in a interactive shell. Otherwise 
output of bare objects is suppressed unless you explicitly call print.

Okay, I guess he is forgiven. False alarm, my bad.


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


Re: random numbers according to user defined distribution ??

2008-08-07 Thread Raymond Hettinger
On Aug 6, 3:02 pm, Alex <[EMAIL PROTECTED]> wrote:
> I wonder if it is possible in python to produce random numbers
> according to a user defined distribution?
> Unfortunately the random module does not contain the distribution I
> need :-(

Sure there's a way but it won't be very efficient.  Starting with an
arbitrary probability density function over some range, you can run it
through a quadrature routine to create a cumulative density function
over that range.  Use random.random() to create a uniform variate x.
Then use a bisecting search to find x in the cumulative density
function over the given range.

from __future__ import division
from random import random

def integrate(f, lo, hi, steps=1000):
dx = (hi - lo) / steps
lo += dx / 2
return sum(f(i*dx + lo) * dx for i in range(steps))

def make_cdf(f, lo, hi, steps=1000):
total_area = integrate(f, lo, hi, steps)
def cdf(x):
assert lo <= x <= hi
return integrate(f, lo, x, steps) / total_area
return cdf

def bisect(target, f, lo, hi, n=20):
'Find x between lo and hi where f(x)=target'
for i in range(n):
mid = (hi + lo) / 2.0
if target < f(mid):
hi = mid
else:
lo = mid
return (hi + lo) / 2.0

def make_user_distribution(f, lo, hi, steps=1000, n=20):
cdf = make_cdf(f, lo, hi, steps)
return lambda: bisect(random(), cdf, lo, hi, n)

if __name__ == '__main__':
def linear(x):
return 3 * x - 6
lo, hi = 2, 10
r = make_user_distribution(linear, lo, hi)
for i in range(20):
print r()

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


Re: Calculate sha1 hash of a binary file

2008-08-07 Thread LaundroMat
Thanks all!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Locking around

2008-08-07 Thread [EMAIL PROTECTED]
On Aug 6, 8:33 pm, Nikolaus Rath <[EMAIL PROTECTED]> wrote:
> Tobiah <[EMAIL PROTECTED]> writes:
> > On Mon, 04 Aug 2008 15:30:51 +0200, Nikolaus Rath wrote:

> > Do you think you could use an SQL database on the network to
> > handle the locking?
>
> Yeah, I could. It wouldn't even have to be over the network (I'm
> synchronizing access from within the same program). But I think that
> is even more resource-wasteful than my original idea.

You could use Amazon SQS to queue the requests, as you are talking
about files kept in S3 anyway, that way it would be fully distributed,

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


Re: benchmark

2008-08-07 Thread Angel Gutierrez
Steven D'Aprano wrote:

> On Thu, 07 Aug 2008 00:44:14 -0700, alex23 wrote:
> 
>> Steven D'Aprano wrote:
>>> In other words, about 20% of the time he measures is the time taken to
>>> print junk to the screen.
>> 
>> Which makes his claim that "all the console outputs have been removed so
>> that the benchmarking activity is not interfered with by the IO
>> overheads" somewhat confusing...he didn't notice the output? Wrote it
>> off as a weird Python side-effect?
> 
> Wait... I've just remembered, and a quick test confirms... Python only
> prints bare objects if you are running in a interactive shell. Otherwise
> output of bare objects is suppressed unless you explicitly call print.
> 
> Okay, I guess he is forgiven. False alarm, my bad.
> 
> 
Well.. there must be somthing because this is what I got in a normal script
execution:

[EMAIL PROTECTED] test]$ python iter.py
Time per iteration = 357.467989922 microseconds
[EMAIL PROTECTED] test]$ vim iter.py
[EMAIL PROTECTED] test]$ python iter2.py
Time per iteration = 320.306909084 microseconds
[EMAIL PROTECTED] test]$ vim iter2.py
[EMAIL PROTECTED] test]$ python iter2.py
Time per iteration = 312.917997837 microseconds

iter.py - Original script
iter2.py - xrange instead of range
iter2.py (2nd) - 't=' added

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


WinCvs Macros

2008-08-07 Thread Ren ChunYi

Hi, 

Does anyone here have some doc like WinCVS API? I'm reading WinCvs integrated 
Macros, but someplace make me puzzle.

And, is there Macro for listing all items with Tag name?

Thanks and best regards.



Ren Chunyi
Control Room and Resource Systems Department
BT FRONTLINE (Dalian) Co., Ltd.
Tel : 86-411-84760069 ext 215
Fax : 86-411-84760075
MSN : [EMAIL PROTECTED]
SJPhone : 2409 
Our values : Innovation, Customer Focus, People Focus, Teamwork & 
Communication, Product & Service Quality



This email and any files transmitted with it are confidential and intended 
solely for the use of the individual or entity to whom they are addressed.If 
you are not the named addressee you should not disseminate, distribute or copy 
this e-mail.
--
http://mail.python.org/mailman/listinfo/python-list

Re: .cpp to .pyd

2008-08-07 Thread Ulrich Eckhardt
[EMAIL PROTECTED] wrote:
> hellomodule.cpp(9) : fatal error C1083: Cannot open include file:
> 'boost/python/module.hpp': No such file or directory

You need to tell it where to find the Boost includes. I suggest you first
try to get some header-only Boost library going as example, if you have
that right, you also have the include paths right.

In your second posting, you gave a link to a picture which had the exact
right dialog for setting this as system setup. You can also set the path on
a per-project base, then it's in the C++ settings.

Note that for compiled libraries like Boost.Python you will first have to
compile those (see Boost docs), put the DLLs into a place where they can be
found (e.g. the windows dir or anything else on the path) and also have to
set a path to the .LIBS in the linker settings.

As an alternative to compiling libraries separately, you can also include
the Boost sources into your project, just add BOOST_PYTHON_STATIC_LIB to
the preprocessor defines and include the following files from one(!)
translation unit:

















Note: this is with Boost 1.33.1, newer versions likely differ.


Uli

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

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

Re: benchmark

2008-08-07 Thread M8R-n7vorv
On Aug 7, 6:38 am, [EMAIL PROTECTED] wrote:
> On Aug 7, 2:05 am, "Jack" <[EMAIL PROTECTED]> wrote:
>
> > I know one benchmark doesn't mean much but it's still disappointing to see
> > Python as one of the slowest languages in the test:
>
> >http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-p...
>
> That Python code is bad, it contains range() instead of xrange, the
> big loop is in the main code instead of inside a function, uses ==
> None, etc. That person can try this (with Psyco), I have changed very
> little, the code is essentially the same:
>

Yes, this was pointed out in the comments. I had updated the code to
use
xrange and is and is not instead of range, == and !=, which is how
the
benchmark got updated to 192 microseconds. Moving the main loop into
a main function resulted in no discernible difference.

Testing with psyco resulted in a time of 33 microseconds per
iteration.

> On my PC the Java version takes 1.17 us, while the C++ version (with
> MinGW 4.2.1) takes 9.8 us.
> A raw D translation needs 14.34 us, while a cleaned up (that uses
> structs, no getters/setters) needs 4.67 us.
> I don't know why my C++ is so much slow (doing the same things to the C
> ++ version doesn't change its running time much).
>
> Bye,
> bearophile

Wonder what optimisation level you are using. I to the best of my
recollection used -O3

Cheers,
Dhananjay
http://blog.dhananjaynene.com

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


Re: Function from C/PHP to Python

2008-08-07 Thread Grom
On 7 Sie, 06:01, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Wed, 06 Aug 2008 20:15:11 -0700, Grom wrote:
> > Hello everyone :)
> > I have one problem with that function in C
> ...
> > Can someone help me to rewrite it to python?
>
> > There is the same function, in PHP:
> ...
> > Please... its very important to me
>
> I don't know C or PHP. But since it's important, and because you said
> please, let me try:
>
> def calc_passcode(password):
>     magic1 = 0x50305735
>     magic2 = 0x12345671
>     sum = 7
>     for i in range(len(password)):
>         z = ord(password[i])
>         if z == 32 or z == 9:
>             continue
>         magic1 = magic1 ^ magic1 & 0x3f) + sum) * z) + (magic1 << 8))
>         magic2 = magic2 + ((magic2 << 8) ^ magic1)
>         sum += z
>         magic1 = magic1 & 0x7fff
>         magic2 = magic2 & 0x7fff
>     return '%08x%08x' % (magic1, magic2)
>
> --
> Steven

It works, thanks :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread Bruno Desthuilliers

Steven D'Aprano a écrit :

On Thu, 07 Aug 2008 07:49:45 +0200, Stefan Behnel wrote:


Jack wrote:

I know one benchmark doesn't mean much but it's still disappointing to
see Python as one of the slowest languages in the test:

http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-

python-ruby-jython-jruby-groovy/

Just ignore that. If the code had been designed for Python from the
start, it would have performed a lot better.



I recommend folks copy and paste his code into an interactive session, 
and watch the thousands of <__main__.Person object at 0xb7f18e2c> that 
flash onto the screen. You want to know why it's so slow? That's part of 
the reason.


This only happens when run in an interactive session.

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

Re: benchmark

2008-08-07 Thread M8R-n7vorv
On Aug 7, 12:44 pm, alex23 <[EMAIL PROTECTED]> wrote:
> Steven D'Aprano wrote:
> > In other words, about 20% of the time he measures is the time taken to
> > print junk to the screen.
>
> Which makes his claim that "all the console outputs have been removed
> so that the benchmarking activity is not interfered with by the IO
> overheads" somewhat confusing...he didn't notice the output? Wrote it
> off as a weird Python side-effect?

Gee, I really hope I am a little more capable than writing it off. And
to
answer your question bluntly no I did not notice the output because
there
wasn't any. Run a python program as "python filename.py" instead
of using the interactive console, and you will not get any output
except
exceptions or anything that your code explicitly spews out.
>
> I find his reluctance to entertain more idiomatic implementations
> particularly telling. It's seems he's less interested in actual
> performance comparisons and more into showing that writing static lang
> style code in dynamic langs is bad, which isn't really anything new to
> anyone anywhere, I would've thought.

I am reluctant to entertain more idiomatic implementations was in the
context of what to me made sense as a part of the exercise. I have
fully
and in great detail explained the rationale in the post itself.


>
> Benchmarks, IMO, are like statistics. You can tweak them to prove
> pretty much any position you already take.

How's this position of mine for starters :
http://blog.dhananjaynene.com/2008/06/whyhow-i-ended-up-selecting-python-for-my-latest-project/
? And if you are not sure, you could browse this as well :
http://blog.dhananjaynene.com/2008/07/presentation-contrasting-java-and-dynamic-languages/

Really how silly can it be when you suggest someone is taking a
position and tweaking the benchmarks to prove a point, when I am
actually quite enthusiastic about python, really like coding using it,
and it was disappointing to me just like to jack who started off this
thread that python did not do so well. In fact I would argue that it
wasn't entirely easy to actually publish the findings given the fact
that these would not have been the findings I would've been wished
for.

Cheers,
Dhananjay
http://blog.dhananjaynene.com
--
http://mail.python.org/mailman/listinfo/python-list


Howto: single line webserver

2008-08-07 Thread Martin Bless
If I have the option  I choose Python to get things done or gain real
understanding. The high quality and stability over time of both the
language and the libraries is just amazing. 

OTH I sometimes really miss little example snippets integrated into
the docs right away. I didn't really know what I could do with
CGIHTTPServer.py until I recently found out.

If you're interested you can find a little howto here:
http://mbless.blogspot.com/2008/08/one-line-webserver.html

Enjoy!

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


Re: benchmark

2008-08-07 Thread bearophileHUGS
jlist:
> I think what makes more sense is to compare the code one most
> typically writes. In my case, I always use range() and never use psyco.

If you don't use Python 3 and your cycles can be long, then I suggest
you to start using xrange a lot :-) (If you use Psyco you don't need
xrange).


M8R-n7v...:
> Wonder what optimisation level you are using. I to the best of my
> recollection used -O3

For this program I have used the best (simple) combination of flags I
have found by try & errors:
-O3 -s -fomit-frame-pointer

I'll try adding a freelist, to see (and probably show you) the
results. The HotSpot speeds up this code first of all because its GC
is much better than the current one used by D, because it optimizes
away the getters/setters that D is unable to do, and probably manages
the methods two classes as static ones (while D manages all of them as
virtual).

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


Re: benchmark

2008-08-07 Thread Bruno Desthuilliers

Stefan Behnel a écrit :

Jack wrote:
I know one benchmark doesn't mean much but it's still disappointing to see 
Python as one of the slowest languages in the test:


http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/ 


Just ignore that. If the code had been designed for Python from the start, it
would have performed a lot better.

Currently it looks like syntax-adapted Java code to me.


Yeps, sure. I don't have time for this now, but I think I would have 
choose an itertool based solution here instead of that hand-made linked 
list.



Stefan

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


cache-like structure

2008-08-07 Thread konstantin
Hi,

I've write a class that actually is data structure with items that
automatically removed from collection when timeout expires. Interface
is pretty simple. First public method is to make a record. And second
(__contains__) is to define if record is actually in table. I prefer
async way to clean out expired records. Thus I have to create timeout
thread every cycle to wait for the next clean-up.

So my questions are:
- is the implementation thread-safe (or I've missed something)
- is there way to avoid creating new timer every cycle
- are there better ways to do this (or any ready recipes)

---
import threading
import time
import collections

class CacheTable(object):
def __init__(self, ttl):
self.records = collections.deque()
self.ttl = ttl
self.timer = None
self.mutex = threading.Lock()

def record(self, item):
self.mutex.acquire()
try:
self.records.append((time.time(), item))
if not self.timer:
self.__settimer(self.ttl)
finally:
self.mutex.release()

def __cleanup(self):
self.mutex.acquire()
try:
current = time.time()
self.timer = None
while self.records:
timestamp, item = self.records.popleft()
if timestamp + self.ttl > current:
self.records.appendleft((timestamp, item))
time_to_wait = timestamp + self.ttl - current
self.__settimer(time_to_wait)
break
finally:
self.mutex.release()

def __settimer(self, timeout):
self.timer = threading.Timer(timeout, self.__cleanup)
self.timer.start()

def __contains__(self, value):
self.mutex.acquire()
try:
items = [item for ts, item in self.records]
return items.__contains__(value)
finally:
self.mutex.release()
---

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


Re: benchmark

2008-08-07 Thread M8R-n7vorv
On Aug 7, 5:05 am, "Jack" <[EMAIL PROTECTED]> wrote:
> I know one benchmark doesn't mean much but it's still disappointing to see
> Python as one of the slowest languages in the test:
>
> http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-p...

I was actually disappointed myself with the results from a python
perspective. One thing I did notice was that both ruby and groovy have
substantially improved performance in their new versions. There is a
likelihood that maybe this particular code is not best suited for
pythonic idioms, but the same would've been the case I guess for ruby,
jruby and groovy, yet they performed really well. While I am a
relative newcomer to the python world, from what I have seen, ruby /
jruby and groovy are all making substantial improvements in their
performance (If this post did not include the newer versions of these
languages, python would've been on top of all of them) but I haven't
seen any evidence of the same in upcoming versions of python.

Having said that the same code with psyco works really fast (and beats
all the other dynamic languages v. handsomely). But I did not include
it in the comparisons because psyco is really not a part of core
feature set of python and I was unclear of the implications thereof.

Is there any reason why the psyco is not a part of the core python
feature set ? Is there a particular reason it is better to be kept as
a separate extension ? Are there any implications of using psyco ?

Cheers,
Dhananjay
http://blog.dhananjaynene.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread M8R-n7vorv
On Aug 7, 2:52 pm, [EMAIL PROTECTED] wrote:
> On Aug 7, 6:38 am, [EMAIL PROTECTED] wrote:
>
> > On Aug 7, 2:05 am, "Jack" <[EMAIL PROTECTED]> wrote:
>
> > > I know one benchmark doesn't mean much but it's still disappointing to see
> > > Python as one of the slowest languages in the test:
>
> > >http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-p...
>
> > That Python code is bad, it contains range() instead of xrange, the
> > big loop is in the main code instead of inside a function, uses ==
> > None, etc. That person can try this (with Psyco), I have changed very
> > little, the code is essentially the same:
>
> Yes, this was pointed out in the comments. I had updated the code to
> use
> xrange and is and is not instead of range, == and !=, which is how
> the
> benchmark got updated to 192 microseconds. Moving the main loop into
> a main function resulted in no discernible difference.
>
> Testing with psyco resulted in a time of 33 microseconds per
> iteration.
>

I have since updated the post to reflect the python with psyco timings
as well.
--
http://mail.python.org/mailman/listinfo/python-list


Re: .cpp to .pyd

2008-08-07 Thread Kay Schluehr
It doesn't look like a VS issue since you've started the setup.py
script that calls the command line compiler. So I'd expect it behaves
not different from other command line tools and you might have to add
the boost path directly to the command-line options.
--
http://mail.python.org/mailman/listinfo/python-list


Re: random numbers according to user defined distribution ??

2008-08-07 Thread Alex
Thanks for the many answers.

So basically I have to get the inverse of the CDF and use this to
transform my uniformly distributed random numbers. If my desired
distribution is simple I can get an analytical solution for the
inverse, otherwise I have to use numerical methods.

Okay, things are now much clearer.

Many thanks,

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


Re: Calculate sha1 hash of a binary file

2008-08-07 Thread LaundroMat
I did some testing, and calculating the hash value of a 1Gb file does
take some time using this method.
Would it be wise to calculate the hash value based on say for instance
the first Mb? Is there a much larger chance of collusion this way (I
suppose not). If it's helpful, the files would primarily be media
(video) files.

Thanks,

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


Re: Calculate sha1 hash of a binary file

2008-08-07 Thread Paul Rubin
LaundroMat <[EMAIL PROTECTED]> writes:
> Would it be wise to calculate the hash value based on say for instance
> the first Mb? Is there a much larger chance of collusion this way (I
> suppose not). If it's helpful, the files would primarily be media
> (video) files.

The usual purpose of using this type of hash is to detect corruption
and/or tampering.  So you want to hash the whole file, not just part
of it.  If you're not worried about intentional tampering, md5 should
be somewhat faster than sha, but there are some attacks against it
and you shouldn't use it for high security applications where you
want security against forgery.  It should still have almost no chance
of accidental collisions.
--
http://mail.python.org/mailman/listinfo/python-list


Re: More Datastore Examples Please

2008-08-07 Thread v4vijayakumar
On Aug 7, 11:38 am, Tim Roberts <[EMAIL PROTECTED]> wrote:
> v4vijayakumar <[EMAIL PROTECTED]> wrote:
>
> >Google appengine datastore is not very clear, and I couldn't get much
> >from API documents. It would be very helpful if there are some more
> >detailed documents with examples.
>
> I would gently suggest that you are not trying hard enough.  I had never
> encountered the Google appengine datastore, but the first hit in Googling
> that phrase took me to the Google documentation, which seems to be quite
> thorough.

Google documentation is not clear for me. Documentation provided there
is not helping much with modeling entities. This requires some
practice, so I thought that If there were some ways to access this
APIs through Idle. This is not as simple as adding some directories to
PYTHONPATH. There are some more things to be done before working
directly on datastore through Idle. This is not documented, but
luckily got some help from the group, google-appengine.

> If you have never worked with a database before, then you might want to go
> find a good book on databases first.

No. That won't help, because this datastore is not a relational
database, but Google's proprietary format called BigTable.

> What are you trying to DO with the datastore?

I am just experimenting with google-appengine, and building an
application on it.
--
http://mail.python.org/mailman/listinfo/python-list


Starting seperate programs

2008-08-07 Thread David Dean
[cid:[email protected]]

Is there a way for a python program start another python program, and then 
continue, without waiting for the second program to finish?
os.startfile() seems to be what I want on windows, but I need it to work on 
linux and mac as well.
Is there a function that does essentially the same as startfile, but without 
needing an associtated program (i.e. asking for one)?

Thanks in advance...
Dave
<>--
http://mail.python.org/mailman/listinfo/python-list

Re: benchmark

2008-08-07 Thread alex23
On Aug 7, 8:08 pm, [EMAIL PROTECTED] wrote:
> Really how silly can it be when you suggest someone is taking a
> position and tweaking the benchmarks to prove a point [...]

I certainly didn't intend to suggest that you had tweaked -anything-
to prove your point.

I do, however, think there is little value in slavishly implementing
the same algorithm in different languages. To constrain a dynamic
language by what can be achieved in a static language seemed like such
an -amazingly- artificial constraint to me. That you're a fan of
Python makes such a decision even more confusing.

It's great that you saw value in Python enough to choose it for actual
project work. It's a shame you didn't endeavour to understand it well
enough before including it in your benchmark.

As for it being "disappointing", the real question is: has it been
disappointing for you in actual real-world code?

Honestly, performance benchmarks seem to be the dick size comparison
of programming languages.
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyprocessing/multiprocessing for x64?

2008-08-07 Thread Kris Kennaway

Benjamin Kaplan wrote:

The only problem I can see is that 32-bit programs can't access 64-bit 
dlls, so the OP might have to install the 32-bit version of Python for 
it to work.


Anyway, all of this is beside the point, because the multiprocessing 
module works fine on amd64 systems.


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


Re: benchmark

2008-08-07 Thread cokofreedom
>
> Honestly, performance benchmarks seem to be the dick size comparison
> of programming languages.
>

But in the honour of dick size:

http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all
http://shootout.alioth.debian.org/debian/benchmark.php?test=all&lang=all
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.system question

2008-08-07 Thread Kevin Walzer

SamG wrote:



Why dont you try commands module instead


Thank you! That was just what I needed.



--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


python-mode is missing the class browser

2008-08-07 Thread Michele Simionato
I have noticed that the python-mode for Emacs that comes with the
latest Ubuntu is missing the class browser. Moreover if works
differently from the python-mode I was used to (for instance CTRL-c-c
works as CTRL-c-! whereas CTRL-c-! is missing, etc). How can I go back
to the old python-mode
or at least how do I get back the class browser?

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


Re: benchmark

2008-08-07 Thread Chris Mellon
On Thu, Aug 7, 2008 at 8:12 AM, alex23 <[EMAIL PROTECTED]> wrote:
> On Aug 7, 8:08 pm, [EMAIL PROTECTED] wrote:
>> Really how silly can it be when you suggest someone is taking a
>> position and tweaking the benchmarks to prove a point [...]
>
> I certainly didn't intend to suggest that you had tweaked -anything-
> to prove your point.
>
> I do, however, think there is little value in slavishly implementing
> the same algorithm in different languages. To constrain a dynamic
> language by what can be achieved in a static language seemed like such
> an -amazingly- artificial constraint to me. That you're a fan of
> Python makes such a decision even more confusing.
>
> It's great that you saw value in Python enough to choose it for actual
> project work. It's a shame you didn't endeavour to understand it well
> enough before including it in your benchmark.
>
> As for it being "disappointing", the real question is: has it been
> disappointing for you in actual real-world code?
>
> Honestly, performance benchmarks seem to be the dick size comparison
> of programming languages.
> -

I actually think that modelling this problem the way he chose to, with
a Person class and by manually popping stuff out of a linked list
instead of more simply representing the alive/dead state of the
soldiers is a poor solution in general. Whenever you talk about
performance, you need to have a context to evaluate it in and you need
an idea of what you're trying to measure and why it's important for
your purposes. A solution which models the soldiers as bits in a
bitfield is going to run much, much, much faster in C/C++/D than the
current OO/linked list one (not to mention in much less space), and
the JIT in Java/C# and probably python with psyco can improve that as
well.
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread Steven D'Aprano
On Thu, 07 Aug 2008 06:12:04 -0700, alex23 wrote:

> On Aug 7, 8:08 pm, [EMAIL PROTECTED] wrote:
>> Really how silly can it be when you suggest someone is taking a
>> position and tweaking the benchmarks to prove a point [...]
> 
> I certainly didn't intend to suggest that you had tweaked -anything- to
> prove your point.
> 
> I do, however, think there is little value in slavishly implementing the
> same algorithm in different languages. To constrain a dynamic language
> by what can be achieved in a static language seemed like such an
> -amazingly- artificial constraint to me.

I don't know about that... it can be very useful to (say) demonstrate 
that Lisp-style lists are fast in Lisp, and slow in Python. Or that 
try...except is fast in Python, and slow in Java.

And if your aim is to compare languages, then it's only fair to keep the 
algorithm constant. Imagine how we would holler and shout if the 
benchmark compared Ruby using Quicksort and Python using Bubblesort.

I guess what some of us are complaining about is that the algorithm 
chosen doesn't suit Python's execution model very well, and hence Python 
is slow. If the algorithm chosen had suited Python, and hence Python came 
up looking really fast, we'd be ecstatic. How about that, hey? *wink*

...
> Honestly, performance benchmarks seem to be the dick size comparison of
> programming languages.

I can't disagree with that one bit.


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


Re: Adding Microsoft objects in Boa Constuctor Palette

2008-08-07 Thread TheSeeker
On Aug 6, 5:59 pm, Sid K <[EMAIL PROTECTED]> wrote:
> This is what I wanted to do:
>
> Add Microsoft Active objects like Excel sheets and Word files to the
> Palette in Boa Constructor. There is a User tab in the GUI builder
> menu, but I'm not sure how to use/enable it.
>
> 1. Does anyone know how to do this?
> 2. Is anyone aware of any work that is currently going on to support
> adding Microsoft objects in Boa.
> 3. Does any other Python GUI creator/editor exist out there that can
> do this?
> 4. How would one add an excel sheet to a wx.Panel in general w/ or w/o
> Boa?
>
> Looking forward to replies...
>
> -SK

Hi,

You might get a response by trying the wxPython list: wxPython-
[EMAIL PROTECTED] or the Boa list: boa-constructor-
[EMAIL PROTECTED]

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


Re: benchmark

2008-08-07 Thread Chris Mellon
On Thu, Aug 7, 2008 at 9:09 AM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Thu, 07 Aug 2008 06:12:04 -0700, alex23 wrote:
>
>> On Aug 7, 8:08 pm, [EMAIL PROTECTED] wrote:
>>> Really how silly can it be when you suggest someone is taking a
>>> position and tweaking the benchmarks to prove a point [...]
>>
>> I certainly didn't intend to suggest that you had tweaked -anything- to
>> prove your point.
>>
>> I do, however, think there is little value in slavishly implementing the
>> same algorithm in different languages. To constrain a dynamic language
>> by what can be achieved in a static language seemed like such an
>> -amazingly- artificial constraint to me.
>
> I don't know about that... it can be very useful to (say) demonstrate
> that Lisp-style lists are fast in Lisp, and slow in Python. Or that
> try...except is fast in Python, and slow in Java.
>

That's true, but note that the original post doesn't attempt to draw
any conclusions about what's fast or slow from the benchmark, which is
one reason why it's a poor example of benchmarking.

> And if your aim is to compare languages, then it's only fair to keep the
> algorithm constant. Imagine how we would holler and shout if the
> benchmark compared Ruby using Quicksort and Python using Bubblesort.
>

That's definitely true, and (for example) the Alioth benchmarks are
intended to benchmark specific algorithms for comparisons sake.

> I guess what some of us are complaining about is that the algorithm
> chosen doesn't suit Python's execution model very well, and hence Python
> is slow. If the algorithm chosen had suited Python, and hence Python came
> up looking really fast, we'd be ecstatic. How about that, hey? *wink*
>

The "best" way to implement this problem, as bitfield manipulation,
would actually show python in even worse light. I suspect the main
thing that this benchmark is actually testing is loop overhead, and
secondarily object allocation speed. Python is pretty slow in the
former and reasonable in the latter, so I don't find the results very
surprising at all.

> ...
>> Honestly, performance benchmarks seem to be the dick size comparison of
>> programming languages.
>
> I can't disagree with that one bit.
>

As with genitals, the important thing about benchmark comparison is
what you're going to do with the results.

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


Re: benchmark

2008-08-07 Thread Dhananjay
On Aug 7, 6:12 pm, alex23 <[EMAIL PROTECTED]> wrote:
> On Aug 7, 8:08 pm, [EMAIL PROTECTED] wrote:
>
> > Really how silly can it be when you suggest someone is taking a
> > position and tweaking the benchmarks to prove a point [...]
>
> I certainly didn't intend to suggest that you had tweaked -anything-
> to prove your point.

While that was not how I read it first, I assume that was a misjudged
reading.

> I do, however, think there is little value in slavishly implementing
> the same algorithm in different languages. To constrain a dynamic
> language by what can be achieved in a static language seemed like such
> an -amazingly- artificial constraint to me. That you're a fan of
> Python makes such a decision even more confusing.

It is a sufficiently well understood maxim, that any comparison
between two factors should attempt to keep other factors as equal as
possible (Ceteris Paribus - Everything else being equal), slavishly if
you will. It is my perception that had I changed the algorithms, I
would've been a much higher level of criticism a lot more for
comparing apples and oranges.

I simply could not understand your point with regards to dynamic vs.
static languages. If you are by any chance referring to make the code
a little less OO, I believe the entire exercise could be redone using
a procedural algorithm, and all the languages will run much much
faster than they currently do. But that would be essentially moving
from an OO based design to a procedural design. Is that what you are
referring to (I suspect not .. I suspect it is something else) ? If
not, would certainly appreciate you spending 5 mins describing that.

I am a fan of Python on its own merits. There is little relationship
between that and this exercise.


> It's great that you saw value in Python enough to choose it for actual
> project work. It's a shame you didn't endeavour to understand it well
> enough before including it in your benchmark.

I have endeavoured hard, and maybe there's a shortcoming in the
results of that endeavour. But I haven't quite understood what it is I
haven't understood (hope that makes sense :) )

> As for it being "disappointing", the real question is: has it been
> disappointing for you in actual real-world code?

I am extremely happy with it. But there definitely are some projects I
worked on earlier I would simply not choose any dynamic language for
(not ruby / not python / not ruby / not groovy). These languages
simply cannot be upto the performance demands required of some
projects.

> Honestly, performance benchmarks seem to be the dick size comparison
> of programming languages.

Not sure if there is a real life equivalent use case if I was to use
this analogy further. But there are some days (mind you not most days)
one needs a really big dick. Always helpful to know the size.

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


Re: cross-compilation

2008-08-07 Thread Martin v. Löwis
> What about to start to resolve issues step by step ?

The first step surely should be to get the autoconf issue resolved.

I don't think distutils cross-compilation is going to work, instead,
people wishing to cross-compile should edit Modules/Setup.

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: iterating "by twos"

2008-08-07 Thread shahms . king
On Jul 29, 10:36 am, kj <[EMAIL PROTECTED]> wrote:
> Is there a special pythonic idiom for iterating over a list (or
> tuple) two elements at a time?
>
> I mean, other than
>
> for i in range(0, len(a), 2):
>     frobnicate(a[i], a[i+1])
>
> ?
>
> I think I once saw something like
>
> for (x, y) in forgotten_expression_using(a):
>     frobnicate(x, y)
>
> Or maybe I just dreamt it!  :)
>
> Thanks!
> --
> NOTE: In my address everything before the first period is backwards;
> and the last period, and everything after it, should be discarded.

This avoids unnecessary copies of the slice approaches, it does,
however, not include the tail values if the length of iterable isn't
evenly divisible by 'size'.

def groups(iterable, size=2):
  return itertools.izip(*[iter(iterable)]*size)

print list(groups(xrange(5)))

[(0, 1), (1, 2), (3, 4)]

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


problem calling parent's __init__ method

2008-08-07 Thread Ryan Krauss
I am trying to call a parent's __init__ method from the child's:

class ArbitraryBlock(InnerBlock):
def __init__(self, codelist, noout=False, **kwargs):
InnerBlock.__init__(self, codelist, noout=noout, **kwargs)


I get this error:

: unbound method __init__() must be
called with InnerBlock instance as first argument (got ArbitraryBlock
instance instead)


I found a thread that talked about the parent and child being
different types, so I tried setting up the parent class 3 different
ways:

class InnerBlock:

class InnerBlock(object):

class InnerBlock(empty_class):

where

class empty_class(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs

I still get the same error.  Why doesn't this work?

Thanks,

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


Re: Decimals not equalling themselves (e.g. 0.2 = 0.2000000001)

2008-08-07 Thread Tim Rowe
2008/8/3 CNiall <[EMAIL PROTECTED]>:

> However, with some, but not all, decimals, they do not seem to 'equal
> themselves'.

The golden rule is that working with decimals (in pretty much any
language) is like working with a pile of sand. Almost anything you do
leaves you with less sand and more dirt.

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


Re: Calculate sha1 hash of a binary file

2008-08-07 Thread LaundroMat
On Aug 7, 2:22 pm, Paul Rubin  wrote:
> LaundroMat <[EMAIL PROTECTED]> writes:
> > Would it be wise to calculate the hash value based on say for instance
> > the first Mb? Is there a much larger chance of collusion this way (I
> > suppose not). If it's helpful, the files would primarily be media
> > (video) files.
>
> The usual purpose of using this type of hash is to detect corruption
> and/or tampering.  So you want to hash the whole file, not just part
> of it.  If you're not worried about intentional tampering, md5 should
> be somewhat faster than sha, but there are some attacks against it
> and you shouldn't use it for high security applications where you
> want security against forgery.  It should still have almost no chance
> of accidental collisions.

Well, what I really intend to do is store the file hashes, in order to
be able to recognise the files later on when they are stored on
another location, and under another filename. It's not so much
tampering I'm concerned with.



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


Re: problem calling parent's __init__ method

2008-08-07 Thread Jerry Hill
On Thu, Aug 7, 2008 at 11:11 AM, Ryan Krauss <[EMAIL PROTECTED]> wrote:
> I am trying to call a parent's __init__ method from the child's:

This works for me just fine:

class InnerBlock(object):
def __init__(self, codelist, noout=False, **kwargs):
self.codelist = codelist
self.noout = noout

class ArbitraryBlock(InnerBlock):
   def __init__(self, codelist, noout=False, **kwargs):
   InnerBlock.__init__(self, codelist, noout=noout, **kwargs)

ab = ArbitraryBlock('a, b, c')


That's using python 2.5.2 on Windows XP.  What version of python are
you using?  On what platform?  Can you show us more of your code?  Or
a complete self-contained example that shows the problem you are
seeing?

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


Re: python-mode is missing the class browser

2008-08-07 Thread Bruno Desthuilliers

Michele Simionato a écrit :

I have noticed that the python-mode for Emacs that comes with the
latest Ubuntu is missing the class browser. Moreover if works
differently from the python-mode I was used to (for instance CTRL-c-c
works as CTRL-c-! whereas CTRL-c-! is missing, etc). How can I go back
to the old python-mode
or at least how do I get back the class browser?


I'm not really up2date wrt/ my ubuntu station here, but I remember 
having seen this somewhat different (emacs-provided IIRC) python-mode 
which I didn't like. I don't quite remember what I did, but I can at 
least tell you what I have actually have:


- in /usr/share/emacs/site-lisp/python-mode/python-mode.el : the 'true' 
python-mode which header goes like this:

"""
;;; python-mode.el --- Major mode for editing Python programs

;; Copyright (C) 1992,1993,1994  Tim Peters

;; Author: 2003-2006 http://sf.net/projects/python-mode
;; 1995-2002 Barry A. Warsaw
;; 1992-1994 Tim Peters
;; Maintainer: [EMAIL PROTECTED]
;; Created:Feb 1992
;; Keywords:   python languages oop

(defconst py-version "$Revision: 4.78 $"
  "`python-mode' version number.")
"""

- in /usr/share/emacs/22.0.91/site-lisp/python-mode/ : the corresponding 
byte-compiled python-mode.elc


- in /etc/emacs/site-start.d/ : a 50python-mode.el file that reads:
"""
;-*-emacs-lisp-*-

(autoload 'python-mode "python-mode" "Python editing mode." t)
(autoload 'py-shell "python-mode" "Interactive Python interpreter." t)
(autoload 'doctest-mode "doctest-mode" "Python doctest editing mode." t)

(setq load-path
  (append
   (list
(concat "/usr/share/"
(symbol-name debian-emacs-flavor)
"/site-lisp/python-mode")
(concat "/usr/share/"
(symbol-name debian-emacs-flavor)
"/site-lisp/pymacs")
)
   load-path))

(setq auto-mode-alist
  (append
   (list
'("\\.py$" . python-mode)
'("SConstruct$" . python-mode)
'("SConscript$" . python-mode)
)
   auto-mode-alist))

(setq interpreter-mode-alist
  (append
   (list
'("python" . python-mode)
'("python2.1" . python-mode)
'("python2.2" . python-mode)
'("python2.3" . python-mode)
'("python2.4" . python-mode)
'("python2.5" . python-mode)
)
   interpreter-mode-alist))
"""

emacs-version : GNU Emacs 22.0.91.1 (i486-pc-linux-gnu, GTK+ Version 
2.10.6) of 2006-12-20 on rothera, modified by Debian


ubuntu Feisty Fawn (7.04)

FWIW and talking about class-browser, there's the IMHO very excellent 
emacs-code-browser too.


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


Re: problem calling parent's __init__ method

2008-08-07 Thread Peter Otten
Ryan Krauss wrote:

> I am trying to call a parent's __init__ method from the child's:
> 
> class ArbitraryBlock(InnerBlock):
> def __init__(self, codelist, noout=False, **kwargs):
> InnerBlock.__init__(self, codelist, noout=noout, **kwargs)
> 
> 
> I get this error:
> 
> : unbound method __init__() must be
> called with InnerBlock instance as first argument (got ArbitraryBlock
> instance instead)
> 
> 
> I found a thread that talked about the parent and child being
> different types, so I tried setting up the parent class 3 different
> ways:
> 
> class InnerBlock:
> 
> class InnerBlock(object):
> 
> class InnerBlock(empty_class):
> 
> where
> 
> class empty_class(object):
> def __init__(self,_d={},**kwargs):
> kwargs.update(_d)
> self.__dict__=kwargs
> 
> I still get the same error.  Why doesn't this work?

For some reason you have two InnerBlock classes in your code. A
demonstration:

>>> class A(object):
... def __init__(self): pass
...
>>> class B(A):
... def __init__(self):
... A.__init__(self)
...
>>> B()
<__main__.B object at 0x2b67aae02c90>
>>> class A(object):
... def __init__(self): pass
...
>>> B()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in __init__
TypeError: unbound method __init__() must be called with A instance as first
argument (got B instance instead)

Are you perhaps importing your main script into your main script?

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


Testing for the first few letters of a string

2008-08-07 Thread Alexnb

Okay, I have a fix for this problem, but it is messy and I think there might
be a better way. Heres an example:

Lets say I have a string: "My name is alex"

and I have another string "My name is alex, and I like pie". 

I want to test to see if just the "My name is alex" part is there. I don't
care about the pie part. 
My first instinct was to just create a for loop and test for the string like
this:

n = 0

for x in string1:
  if string1[n] == string2[n]
n = n +0
  else:
break
and then later testing to see what n was = to and figuring out if it got
through the whole loop. I feel like there should be an easier way to do
this, and probably is. So Does anyone have a suggestion?
-- 
View this message in context: 
http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-tp18873375p18873375.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Testing for the first few letters of a string

2008-08-07 Thread Shawn Milochik
Check out the built-in string.startswith() method.
--
http://mail.python.org/mailman/listinfo/python-list


RE: Testing for the first few letters of a string

2008-08-07 Thread Edwin . Madari
use re module

import re
template = '^My name is alex'
astring = 'My name is alex, and I like pie'
if re.match(template, astring):
print 'Found it'
else: print '%s does not begin with %s' % (astring, template)

good luck.
Edwin

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
On Behalf Of Alexnb
Sent: Thursday, August 07, 2008 11:40 AM
To: [email protected]
Subject: Testing for the first few letters of a string



Okay, I have a fix for this problem, but it is messy and I think there might
be a better way. Heres an example:

Lets say I have a string: "My name is alex"

and I have another string "My name is alex, and I like pie". 

I want to test to see if just the "My name is alex" part is there. I don't
care about the pie part. 
My first instinct was to just create a for loop and test for the string like
this:

n = 0

for x in string1:
  if string1[n] == string2[n]
n = n +0
  else:
break
and then later testing to see what n was = to and figuring out if it got
through the whole loop. I feel like there should be an easier way to do
this, and probably is. So Does anyone have a suggestion?
-- 
View this message in context: 
http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-tp18873375p18873375.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.


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


Re: Testing for the first few letters of a string

2008-08-07 Thread Sean DiZazzo
try

string1 = "My name is alex"
string2 = "My name is alex, and I like pie"

if string2.startswith(string1):
process()

or

if you want to match a set number of characters you can use a slice:

if string2[:15] == string1[:15]:
process()

or

if you dont care where the characters appear in the string, beginning,
middle, end, etc:

if string2 in string1:
process()

Theres lots of other ways as well.  
http://docs.python.org/lib/string-methods.html

~Sean

On Aug 7, 8:40 am, Alexnb <[EMAIL PROTECTED]> wrote:
> Okay, I have a fix for this problem, but it is messy and I think there might
> be a better way. Heres an example:
>
> Lets say I have a string: "My name is alex"
>
> and I have another string "My name is alex, and I like pie".
>
> I want to test to see if just the "My name is alex" part is there. I don't
> care about the pie part.
> My first instinct was to just create a for loop and test for the string like
> this:
>
> n = 0
>
> for x in string1:
>       if string1[n] == string2[n]
>             n = n +0
>       else:
>             break
> and then later testing to see what n was = to and figuring out if it got
> through the whole loop. I feel like there should be an easier way to do
> this, and probably is. So Does anyone have a suggestion?
> --
> View this message in 
> context:http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-t...
> Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: problem calling parent's __init__ method

2008-08-07 Thread Ryan Krauss
It is in fact an import problem.  My module works fine by itself with
a test case in if __name__ == '__main__', but I have a problem when
using it within another module when both are underdevelopment.
Changing the reload order solves the problem.

This is bad:
import texpy, os, pytex
reload(texpy)
reload(pytex)

This is fine:
import texpy, os, pytex
reload(pytex)
reload(texpy)

The module we have been talking about is pytex.  It gets imported into
texpy.  The reload statements are the problem (I am fairly certain),
and I am only using them because things are under development.

Thanks to Peter and Jerry for their help.

Ryan


On Thu, Aug 7, 2008 at 10:36 AM, Peter Otten <[EMAIL PROTECTED]> wrote:
> Ryan Krauss wrote:
>
>> I am trying to call a parent's __init__ method from the child's:
>>
>> class ArbitraryBlock(InnerBlock):
>> def __init__(self, codelist, noout=False, **kwargs):
>> InnerBlock.__init__(self, codelist, noout=noout, **kwargs)
>>
>>
>> I get this error:
>>
>> : unbound method __init__() must be
>> called with InnerBlock instance as first argument (got ArbitraryBlock
>> instance instead)
>>
>>
>> I found a thread that talked about the parent and child being
>> different types, so I tried setting up the parent class 3 different
>> ways:
>>
>> class InnerBlock:
>>
>> class InnerBlock(object):
>>
>> class InnerBlock(empty_class):
>>
>> where
>>
>> class empty_class(object):
>> def __init__(self,_d={},**kwargs):
>> kwargs.update(_d)
>> self.__dict__=kwargs
>>
>> I still get the same error.  Why doesn't this work?
>
> For some reason you have two InnerBlock classes in your code. A
> demonstration:
>
 class A(object):
> ... def __init__(self): pass
> ...
 class B(A):
> ... def __init__(self):
> ... A.__init__(self)
> ...
 B()
> <__main__.B object at 0x2b67aae02c90>
 class A(object):
> ... def __init__(self): pass
> ...
 B()
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "", line 3, in __init__
> TypeError: unbound method __init__() must be called with A instance as first
> argument (got B instance instead)
>
> Are you perhaps importing your main script into your main script?
>
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: python-mode is missing the class browser

2008-08-07 Thread Alexander Schmolck
Michele Simionato <[EMAIL PROTECTED]> writes:

> I have noticed that the python-mode for Emacs that comes with the
> latest Ubuntu is missing the class browser. Moreover if works
> differently from the python-mode I was used to (for instance CTRL-c-c
> works as CTRL-c-! whereas CTRL-c-! is missing, etc). How can I go back
> to the old python-mode
> or at least how do I get back the class browser?

There are two different and independently developedpython-modes. The
politically correct one that comes with emacs (IIRC python.el) that had pretty
limited functionality last time I looked, and the original but not FSF blessed
python-mode.el (http://sourceforge.net/projects/python-mode/), which should
come with install instructions. If you use ipython you might additionally
want to install ipython.el, which comes with the ipython tar ball.

cheers

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


Re: python-mode is missing the class browser

2008-08-07 Thread Diez B. Roggisch
Bruno Desthuilliers wrote:

> Michele Simionato a écrit :
>> I have noticed that the python-mode for Emacs that comes with the
>> latest Ubuntu is missing the class browser. Moreover if works
>> differently from the python-mode I was used to (for instance CTRL-c-c
>> works as CTRL-c-! whereas CTRL-c-! is missing, etc). How can I go back
>> to the old python-mode
>> or at least how do I get back the class browser?
> 
> I'm not really up2date wrt/ my ubuntu station here, but I remember
> having seen this somewhat different (emacs-provided IIRC) python-mode
> which I didn't like. I don't quite remember what I did, but I can at
> least tell you what I have actually have:



I've got the same, and a IM-Python-menu that contains the class-browser.


> FWIW and talking about class-browser, there's the IMHO very excellent
> emacs-code-browser too.

Yes, it sure is cool.

And I have no idea how I ever worked without flymake & pylint :)

Emacs just rocks...


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

Re: Psycho question

2008-08-07 Thread David C. Ullrich
In article <[EMAIL PROTECTED]>,
 Erik Max Francis <[EMAIL PROTECTED]> wrote:

> David C. Ullrich wrote:
> 
> > Thanks. I would have guessed that I'd want low-level style code;
> > that's the sort of thing I have in mind. In fact the only thing
> > that seems likely to come up right now is looping through an
> > array of bytes, modifying them. The plan is to use the array
> > module first to convert a string or a list to an array, outside
> > the accelerated part, then maybe do something like
> > 
> > for j in range(len(bytes)/3):
> >   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
> >   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
> 
> If len(bytes) is large, you might want to use `xrange`, too.  `range` 
> creates a list which is not really what you need.

I didn't follow the explanation, but I read in the docs
that xrange can actually be slower under Psyco.

This morning I learned that my guess that array.array was
a good idea was correct: When I pass a list of ints to the
routine above it gets accelerated by a factor of between
10 and 15, while if I pass an array it's closer to 50.

This is so cool. Maybe I already said that.

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Access individual fields in csv using python

2008-08-07 Thread Krishna
How do I access individual fields in csv using python? Please let me
know ASAP as its real urgent for me.

Thanks for your help

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


Large production environments using ZODB/ZOE?

2008-08-07 Thread Phillip B Oldham
I've been reading a lot recently on ZODB/ZOE, but I've not seen any
reference to its use in large-scale production envrironments.

Are there any real-world examples of ZODB/ZOE in use for a large
system? By large, I'm thinking in terms of both horizontally-scaled
systems and in terms of data storage size.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psycho question

2008-08-07 Thread MRAB
On Aug 6, 8:52 pm, "David C. Ullrich" <[EMAIL PROTECTED]> wrote:
> In article
> <[EMAIL PROTECTED]>,
>
> [EMAIL PROTECTED] wrote:
> > David C. Ullrich:
> > > Thanks. If I can get it installed and it works as advertised
> > > this means I can finally (eventually) finish the process of
> > > dumping MS Windows: the only reason I need it right now is for
> > > the small number of Delphi programs I have for which straight
> > > Python is really not adequate. Been not looking forward to
> > > learning some C or Objective C (or whatever that Mac thing
> > > is) - if I can just "accelerate" a few Python routines that'll
> > > be great.
>
> > To have better performance with Psyco you need low-level style code,
> > generally not lazy, etc, and adopt some programming conventions, so
> > you may have to rewrite your routines for max speed.
>
> Thanks. I would have guessed that I'd want low-level style code;
> that's the sort of thing I have in mind. In fact the only thing
> that seems likely to come up right now is looping through an
> array of bytes, modifying them. The plan is to use the array
> module first to convert a string or a list to an array, outside
> the accelerated part, then maybe do something like
>
> for j in range(len(bytes)/3):
>   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
>   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
>
> then convert back to a list or string or whatever outside
> the accelerated function.
>
[snip]
A couple of points:

1. '/' with ints in Python 2.x returns an int, but from Python 3.x
it'll return a float. You're recommended to use '//' for int division.

2. 'range' can accept a step value, so you can rewrite that as:

for j in range(0, len(bytes), 3):
g = (bytes[j] + bytes[j+1] + bytes[j+2])//3 # I think you also
want // here
bytes[j] = bytes[j+1] = bytes[j+2] = g
--
http://mail.python.org/mailman/listinfo/python-list


Re: Access individual fields in csv using python

2008-08-07 Thread Timothy Grant
On Thu, Aug 7, 2008 at 9:23 AM, Krishna <[EMAIL PROTECTED]> wrote:
> How do I access individual fields in csv using python? Please let me
> know ASAP as its real urgent for me.
>
> Thanks for your help
>
> Krishna
> --
> http://mail.python.org/mailman/listinfo/python-list
>
I'd likely use the csv module.

http://docs.python.org/lib/module-csv.html


-- 
Stand Fast,
tjg.  [Timothy Grant]
--
http://mail.python.org/mailman/listinfo/python-list


Re: .cpp to .pyd

2008-08-07 Thread vedrandekovic
On 7 kol, 11:37, Ulrich Eckhardt <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > hellomodule.cpp(9) : fatal error C1083: Cannot open include file:
> > 'boost/python/module.hpp': No such file or directory
>
> You need to tell it where to find the Boost includes. I suggest you first
> try to get some header-only Boost library going as example, if you have
> that right, you also have the include paths right.
>
> In your second posting, you gave a link to a picture which had the exact
> right dialog for setting this as system setup. You can also set the path on
> a per-project base, then it's in the C++ settings.
>
> Note that for compiled libraries like Boost.Python you will first have to
> compile those (see Boost docs), put the DLLs into a place where they can be
> found (e.g. the windows dir or anything else on the path) and also have to
> set a path to the .LIBS in the linker settings.
>
> As an alternative to compiling libraries separately, you can also include
> the Boost sources into your project, just add BOOST_PYTHON_STATIC_LIB to
> the preprocessor defines and include the following files from one(!)
> translation unit:
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> Note: this is with Boost 1.33.1, newer versions likely differ.
>
> Uli
>
> --
> Sator Laser GmbH
> Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Hello again,

This answer is probably exactly what I need, but as I said, I don't
have any expirience with MS products! Is there any other easier
way except "boost python" to make .pyd from .cpp?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing of a file

2008-08-07 Thread Mike Driscoll
On Aug 6, 4:06 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On Aug 7, 6:02 am, Mike Driscoll <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Aug 6, 1:55 pm, Tommy Grav <[EMAIL PROTECTED]> wrote:
>
> > > I have a file with the format
>
> > > Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
> > > 5 Set 1
> > > Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
> > > 5 Set 2
> > > Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
> > > 5 Set 3
> > > Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
> > > 5 Set 4
> > > Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
> > > 5 Set 5
> > > Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
> > > 5 Set 6
> > > Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
> > > 5 Set 7
> > > Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
> > > 5 Set 8
> > > Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
> > > 5 Set 9
> > > Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
> > > 5 Set 10
>
> > > I would like to parse this file by extracting the field id, ra, dec
> > > and mjd for each line. It is
> > > not, however, certain that the width of each value of the field id,
> > > ra, dec or mjd is the same
> > > in each line. Is there a way to do this such that even if there was a
> > > line where Ra=** and
> > > MJD= was swapped it would be parsed correctly?
>
> > > Cheers
> > >    Tommy
>
> > I'm sure Python can handle this. Try the PyParsing module or learn
> > Python regular expression syntax.
>
> >http://pyparsing.wikispaces.com/
>
> > You could probably do it very crudely by just iterating over each line
> > and then using the string's find() method.
>
> Perhaps you and the OP could spend some time becoming familiar with
> built-in functions and str methods. In particular, str.split is your
> friend:
>

I'm well aware of the split() method and built-ins, however since this
appeared to be a homework-type question and I was at work, I didn't
spend any time on the issue. The only reason I mentioned McGuire's
PyParsing module was because I had just finished reading his article
on the subject in Python Magazine and it sounded like something the OP
might find interesting.

Here's my own implementation based on what's already been done here.
I'm sure one get have some fun doing it with itertools or list
comprehensions if you wanted to get really fancy.



raw_data = """
Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
5 Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
5 Set 10
""".splitlines()

myList = []
for line in raw_data:
items = line.split()
myDict = {}
for item in items:
if '=' in item:
key, value = item.split('=')
myDict[key] = value
elif item[:1].lower() == 'f' and item[-1:] == ':':
myDict['id'] = item[1:-1]
myList.append(myDict)

print myList



This doesn't have any type checking or error handling, but it works
with the data provided.

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


Re: cache-like structure

2008-08-07 Thread maxime


konstantin wrote:
> Hi,
Hi
>...
> - are there better ways to do this (or any ready recipes)


Did you consider using the shed module (not threaded)?

Cheers,

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


Re: cache-like structure

2008-08-07 Thread maxime
On Aug 7, 7:00 pm, maxime <[EMAIL PROTECTED]> wrote:
> konstantin wrote:
> > Hi,
> Hi
> >...
> > - are there better ways to do this (or any ready recipes)
>
> Did you consider using the shed module (not threaded)?
>
> Cheers,
>
> Maxime

If you need threading, what i would do is implementing a _remove
method like:
def _remove(self, rec): #try not to use __form, almost always an error
self.records.remove(rec)

the Timer would become
Timer(ttl, self._remove, [(t, item)]) # t is the time that you got
before

And if you change the deque for a Queue.Queue, you don't even need a
Lock...

Cheers,

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


Re: os.system question

2008-08-07 Thread Eric Wertman
In your case you could also use the os.environ dictionary:

import os
print os.environ['USER']
--
http://mail.python.org/mailman/listinfo/python-list


Create 2D character matrix

2008-08-07 Thread Simon Parker
Hello.
 
I want to be able to create a 2D character matrix, ThisMatrix, like :
 
 
a A
b B
c C
d D
 
and to be able to pick out elements, or rows or columns.
 
I have become used to programming in R where I can easily refer to a row as :
 
ThisMatrix [1,]
 
and a column as 
 
ThisMatrix[,1].
 
etc..
 
Can this be done easily in Python ?
 
 
Thanks.


  __
Not happy with your email address?.
Get the one you really want - millions of new email addresses available now at 
Yahoo! http://uk.docs.yahoo.com/ymail/new.html--
http://mail.python.org/mailman/listinfo/python-list

Re: Adding Microsoft objects in Boa Constuctor Palette

2008-08-07 Thread Sid K
On Aug 7, 10:13 am, TheSeeker <[EMAIL PROTECTED]> wrote:
> On Aug 6, 5:59 pm, Sid K <[EMAIL PROTECTED]> wrote:
>
>
>
> > This is what I wanted to do:
>
> > Add Microsoft Active objects like Excel sheets and Word files to the
> > Palette in Boa Constructor. There is a User tab in the GUI builder
> > menu, but I'm not sure how to use/enable it.
>
> > 1. Does anyone know how to do this?
> > 2. Is anyone aware of any work that is currently going on to support
> > adding Microsoft objects in Boa.
> > 3. Does any other Python GUI creator/editor exist out there that can
> > do this?
> > 4. How would one add an excel sheet to a wx.Panel in general w/ or w/o
> > Boa?
>
> > Looking forward to replies...
>
> > -SK
>
> Hi,
>
> You might get a response by trying the wxPython list: wxPython-
> [EMAIL PROTECTED] or the Boa list: boa-constructor-
> [EMAIL PROTECTED]
>
> Duane

Hey Duane,

Thanks for the tip.

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


very newbie question

2008-08-07 Thread garywood
stuck on python for absolute beginners 
chapter 6 
i actually done what i was supposed to do use the function ask_number for guess 
a number 
but for some reason it does not count correctly the number of tries 

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values



def ask_number():
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1




while (guess != the_number):


if (guess > the_number):
print "Lower..."

else:
print "Higher..."
tries += 1


guess = int(raw_input("Take a guess: "))
tries += 1




ask_number()




print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"
  
raw_input("\n\nPress the enter key to exit.")
--
http://mail.python.org/mailman/listinfo/python-list

Re: iterating "by twos"

2008-08-07 Thread castironpi
On Jul 29, 3:38 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> kj wrote:
> > Is there a special pythonic idiom for iterating over a list (or
> > tuple) two elements at a time?
>
> > I mean, other than
>
> > for i in range(0, len(a), 2):
> >     frobnicate(a[i], a[i+1])
>
> There have been requests to add a grouper function to itertools, but its
> author has resisted because there are at least three things one might do
> with the short remainder group left over when the group size does not
> evenly divide the sequence size: drop it, return it, or fill it to the
> requested groupsize with a dummy value and then return it.

We note that three distinct methods of the mapping type protocol
support default, -or this if not found-, argument passing.

a.get(k[, x]) a[k] if k in a, else x (4)
a.setdefault(k[, x]) a[k] if k in a, else x (also setting it) (5)
a.pop(k[, x]) a[k] if k in a, else x (and remove k) (8)

You can knock off two of them with a default value:

itertools.igroup( iterable, groupsize[, dummy] )

Observe this overlap from the string type.

   find( sub[, start[, end]])
  Return the lowest index in the string where substring sub is
found, ...
  -1 if sub is not found.
   index( sub[, start[, end]])
  Like find(), but raise ValueError when the substring is not
found.

Include two grouper functions, one that drops (or fills if 'dummy'
provided), one that returns (or fills if 'dummy' provided).

Or, include two grouper functions, one that drops (or fills), one that
raises ValueError exception, including the partial group as an
attribute of the exception.

If filling with 'None' is your concern, just use a special flag:

_special= object( )
iterb= itertools.grouper( itera, 2, _special )
--
http://mail.python.org/mailman/listinfo/python-list


Re: very newbie question

2008-08-07 Thread Ethan Furman

garywood wrote:

stuck on python for absolute beginners
chapter 6
i actually done what i was supposed to do use the function ask_number 
for guess a number

but for some reason it does not count correctly the number of tries
 
# Guess My Number

#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
 
import random 
 
print "\tWelcome to 'Guess My Number'!"

print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
 
# set the initial values
 
 
 
def ask_number():

the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
   
   
   
   
while (guess != the_number):
   
   
if (guess > the_number):

print "Lower..."
   
else:

print "Higher..."
tries += 1

   ^--  *not needed*
   
   
guess = int(raw_input("Take a guess: "))

tries += 1
   
   
   
   
ask_number()
 
 
 


print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"
 
raw_input("\n\nPress the enter key to exit.")






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


RE: very newbie question

2008-08-07 Thread Edwin . Madari
delete the extra 'tries += 1'  after 
else:
print "Higher..."
tries += 1  #delete this 

while at it, and add this line as the first line in function ask_number()
global the_number, tries
 
good luck.
Edwin

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of garywood
Sent: Thursday, August 07, 2008 1:56 PM
To: [email protected]
Subject: very newbie question 


stuck on python for absolute beginners 
chapter 6 
i actually done what i was supposed to do use the function ask_number for guess 
a number 
but for some reason it does not count correctly the number of tries 
 
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
 
import random  
 
print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"
 
# set the initial values
 
 
 
def ask_number():
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1




while (guess != the_number):


if (guess > the_number):
print "Lower..."

else:
print "Higher..."
tries += 1


guess = int(raw_input("Take a guess: "))
tries += 1




ask_number()
 
 
 

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"
  
raw_input("\n\nPress the enter key to exit.")




The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.

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

Re: Books to begin learning Python

2008-08-07 Thread Beliavsky
On Aug 6, 4:08 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote:
> On Aug 6, 2:56 pm, Edward Cormier <[EMAIL PROTECTED]> wrote:
>
> > Which computer books are the best to begin learning Python 2.5 with?
> > I've heard that Learning Python 3rd Edition is a good choice - can
> > anyone give any more advice on this?
>
> > Thanks.
>
> There's lots of good books to read, including a few online ones. A lot
> of people like "Dive Into Python" (http://diveintopython.org/). If you
> want LOTS of information and some good code examples, Lutz's
> "Programming Python 3rd Ed" is great.

I have the 2nd edition. Has the 3rd edition been rewritten so that all
of its code will be valid in Python 3? I'd prefer not to buy Python
books that will become obsolete.

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


HTTP basic authentication with form-based authentication

2008-08-07 Thread Max
Following the tutorial at http://personalpages.tds.net/~kent37/kk/00010.html,
I understand how to access HTTP basic authenticated pages or form-
based authenticated pages. How would I access a page protected by both
form-based authentication (using cookies) *and* HTTP basic
authentication?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psycho question

2008-08-07 Thread David C. Ullrich
In article 
<[EMAIL PROTECTED]>,
 MRAB <[EMAIL PROTECTED]> wrote:

> On Aug 6, 8:52 pm, "David C. Ullrich" <[EMAIL PROTECTED]> wrote:
> > In article
> > <[EMAIL PROTECTED]>,
> >
> > [EMAIL PROTECTED] wrote:
> > > David C. Ullrich:
> > > > Thanks. If I can get it installed and it works as advertised
> > > > this means I can finally (eventually) finish the process of
> > > > dumping MS Windows: the only reason I need it right now is for
> > > > the small number of Delphi programs I have for which straight
> > > > Python is really not adequate. Been not looking forward to
> > > > learning some C or Objective C (or whatever that Mac thing
> > > > is) - if I can just "accelerate" a few Python routines that'll
> > > > be great.
> >
> > > To have better performance with Psyco you need low-level style code,
> > > generally not lazy, etc, and adopt some programming conventions, so
> > > you may have to rewrite your routines for max speed.
> >
> > Thanks. I would have guessed that I'd want low-level style code;
> > that's the sort of thing I have in mind. In fact the only thing
> > that seems likely to come up right now is looping through an
> > array of bytes, modifying them. The plan is to use the array
> > module first to convert a string or a list to an array, outside
> > the accelerated part, then maybe do something like
> >
> > for j in range(len(bytes)/3):
> >   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
> >   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
> >
> > then convert back to a list or string or whatever outside
> > the accelerated function.
> >
> [snip]
> A couple of points:
> 
> 1. '/' with ints in Python 2.x returns an int, but from Python 3.x
> it'll return a float. You're recommended to use '//' for int division.
> 
> 2. 'range' can accept a step value, so you can rewrite that as:
> 
> for j in range(0, len(bytes), 3):
> g = (bytes[j] + bytes[j+1] + bytes[j+2])//3 # I think you also
> want // here
> bytes[j] = bytes[j+1] = bytes[j+2] = g

Not the issues I expected to be worrying about here, but thanks.

Of course the range(0, len(bytes), 3) is more elegant, and
it's probably faster in Python, but curiously it's much
slower under Psyco! Otoh xrange(0, len(bytes), 3) becomes
pretty fast again. So I conjecture that Psyco compiles
"for j in range(l)" just as a loop but actually constructs
an array for range(0, l, step).

Also very curiously, // inside the loop is much slower than
/ here (under Psyco). This one I'm not going to guess why...

Honest:

"""Ah - psyco does work with exec if the import psyco,
etc is inside the code being executed (right now it's
in its own namespace, hence a fresh import each time 
- check whether this works with exec in default
namespaces).

Ie, this script works fine in DUShell:"""

from psyco import proxy, bind

def f(b):
  for j in range(len(b)/3):
i = 3*j
g = (b[i] + b[i+1] + b[i+2])/3
b[i] = b[i+1] = b[i+2] = g

g = proxy(f)

def h(b):
  for j in range(0,len(b),3):
  #for i in range(len(b)/3):
#j = 3*i
g = (b[j] + b[j+1] + b[j+2])//3
b[j] = b[j+1] = b[j+2] = g
bind(h)


from time import time

fs = {'f':f, 'g':g, 'h':h}

def t(f,b):
  F = fs[f]
  st = time()
  F(b)
  et = time()
  print "%s: %s" % (f, et-st)

b = range(3)

from array import array
c = array('i',b)
t('f',c)
t('g',c)
t('h',c)
t('f',c)
t('g',c)
t('h',c)

outputs

f: 0.0158488750458
g: 0.000610113143921
h: 0.00200295448303
f: 0.0184948444366
g: 0.000257015228271
h: 0.00116610527039

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list

wxPython pop--up ("could not retrieve information...")

2008-08-07 Thread mistersulu
Hi all:

I'm using a wx.ListView object with a multi-threaded wxPython app.
The list is dynamically generated and accessed across two or more
threads.  In spite of the fact that I have checks to see if an item at
a given index is in the list and the entire set of logic is contained
within a try: except: block I periodically get pop-up windows stating
"Could not retrieve information for list control item X" when I try to
change or access the list items.

I searched the web and groups for similar posts but can't find any
solutions.

My question is:

Is there a setting somewhere in wxPython to either disable these pop-
up messages or a semaphore or event locking method to enable cross-
thread access to a listview object?

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


Fastest way to store ints and floats on disk

2008-08-07 Thread Laszlo Nagy


 Hi,

I'm working on a pivot table. I would like to write it in Python. I 
know, I should be doing that in C, but I would like to create a cross 
platform version which can deal with smaller databases (not more than a 
million facts).


The data is first imported from a csv file: the user selects which 
columns contain dimension and measure data (and which columns to 
ignore). In the next step I would like to build up a database that is 
efficient enough to be used for making pivot tables. Here is my idea for 
the database:


Original CSV file with column header and values:

"Color","Year","Make","Price","VMax"
Yellow,2000,Ferrari,10,254
Blue,2003,Volvo,5,210

Using the GUI, it is converted to this:

dimensions = [
   { 'name':'Color', 'colindex:0, 'values':[ 'Red', 'Blue', 'Green', 
'Yellow' ], },
   { 'name':'Year', colindex:1, 'values':[ 
1995,1999,2000,2001,2002,2003,2007 ], },
   { 'name':'Make', colindex:2, 'value':[ 'Ferrari', 'Volvo', 'Ford', 
'Lamborgini' ], },

]
measures = [
   { 'name', 'Price', 'colindex':3 },
   { 'name', 'Vmax', 'colindex':4 },
]
facts = [
   ( (3,2,0),(10.0,254.0)  ), # ( dimension_value_indexes, 
measure_values )

   ( (1,5,1),(5.0,210.0) ),
   # Some million rows or less
]


The core of the idea is that, when using a relatively small number of 
possible values for each dimension, the facts table becomes 
significantly smaller and easier to process. (Processing the facts would 
be: iterate over facts, filter out some of them, create statistical 
values of the measures, grouped by dimensions.)


The facts table cannot be kept in memory because it is too big. I need 
to store it on disk, be able to read incrementally, and make statistics. 
In most cases, the "statistic" will be simple sum of the measures, and 
counting the number of facts affected. To be effective, reading the 
facts from disk should not involve complex conversions. For this reason, 
storing in CSV or XML or any textual format would be bad. I'm thinking 
about a binary format, but how can I interface that with Python?


I already looked at:

- xdrlib, which throws me DeprecationWarning when I store some integers
- struct which uses format string for each read operation, I'm concerned 
about its speed


What else can I use?

Thanks,

  Laszlo



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


mktime overflow in March 2008?

2008-08-07 Thread Robert Latest
Here's what happens on my Windows machine (Win XP / Cygwin) at work.
I've googled a bit about this problem but only found references to
instances where people referred to dates before the Epoch.

Of course at home on my Linux box everything works.

I know that everything has its limits somewhere, but I've never
heard of March 2008 being a problem.

Tomorrow I'm going to write a test loop that shows me the exact last
second that mktime is going to be able to handle. Sort of like the way
Calvin's father explains the weight limit specification of bridges.

I wonder if the datetime module is any better, considering that under
the hood it probably uses the same C library functions.

I like to stick with the "seconds since..." approach because I need to
store millions of dates/times efficiently in a sqlite database.

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) 
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> t = time.strptime("Mar 30, 2007 2:43:32 am", "%b %d, %Y %I:%M:%S
>>> %p")
>>> time.mktime(t)
1175215412.0
>>> t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S
>>> %p")
>>> time.mktime(t)
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: mktime argument out of range
>>> 

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


Re: benchmark

2008-08-07 Thread Terry Reedy



[EMAIL PROTECTED] wrote:


Is there any reason why the psyco is not a part of the core python
feature set ?


Psyco was a PhD project.  I do not believe the author ever offered it. 
Last I knew, it was almost but not completely compatible.



Is there a particular reason it is better to be kept as
a separate extension ?


If he did, he would have to commit to updating it to work with new 
version of Python (2.6/3.0) which I don't believe he wants to do.  Last 
I know, he was working with the PyPy project instead and its JIT 
technology.  On the otherhand, extensions are also restricted by 
Python's release schedule, including no new features in bug-fix 
(dot-dot) releases.  So library extensions need to be rather stable but 
maintained.


> Are there any implications of using psyco ?

It compiles statements to machine code for each set of types used in the 
statement or code block over the history of the run.  So code used 
polymorphically with several combinations of types can end up with 
several compiled versions (same as with C++ templates).  (But a few 
extra megabytes in the running image is less of an issue than it was 
even 5 or so years ago.)  And time spent compiling for a combination 
used just once gains little.  So it works best with numeric code used 
just for ints or floats.


Terry J. Reedy

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


Create a short way to save typing script over and over....

2008-08-07 Thread frankrentef

I have a statement "url = 'http://xyzserver/'"  so in my code every
time I need to use xyzserver I state "url +"

What I' m now trying to do is create a call to a login process.  The
script for the login process is below.  I'd like to call "adminlogin"
and not have to keep entering the same several lines of script each
and everytime (I'm testing various login functions and rule changes.)

I'm a newbie, can someone walk me through this?

THNX



#Login to ISeDeposit Admin

ie.navigate (url + 'isweb/admin/default.aspx')
ie.textBoxSet ('AdminLogin1:Username','Admin')
ie.textBoxSet ('AdminLogin1:inputPassword','Password')
ie.buttonClick ('AdminLogin1:btnLogin')
--
http://mail.python.org/mailman/listinfo/python-list


Re: Limits of Metaprogramming

2008-08-07 Thread Wilson
On Aug 6, 6:04 pm, castironpi <[EMAIL PROTECTED]> wrote:
> On Aug 6, 7:24 am, Wilson <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Aug 4, 9:23 pm, castironpi <[EMAIL PROTECTED]> wrote:
>
> > > On Aug 4, 1:57 pm, Wilson <[EMAIL PROTECTED]> wrote:
>
> > > > On Aug 4, 6:49 pm, castironpi <[EMAIL PROTECTED]> wrote:
>
> > > > > Two, if all your methods will have uniform signatures and closures,
> > > > > you can store class methods as only their co_code objects:
>
> > > > > >>> C.g.im_func.func_code.co_code
>
> > > > > 'd\x00\x00S'
>
> > > > > And fabricate them dynamically into full live types as needed.
>
> > > > Thanks for your comments and advice. This second option intrigues me;
> > > > could you elaborate further, I don't follow you...
>
> > > > Thanks Paul
>
> > > Depending on the complexity of the functions, a code string could be
> > > all you need to store to determine (redetermine) a function's
> > > behavior.  For something moderately simple,
>
> > > def trans1( self, prev, trans ):
> > >         if prev== 0 and trans== 'a':
> > >                 return 1
> > >         if prev== 1 and trans== 'b':
> > >                 return 0
> > >         return prev
>
> > > I found you need to store code.co_nlocals, code.co_code, and
> > > code.co_consts, to distinguish from a blank stub.  With extra
> > > variables, I needed code.co_names and code.co_varnames too.  To
> > > recreate a code object completely, you need 12 variables (14 to
> > > include closures), some of which are composite objects and would need
> > > to be pickled to be stored.
>
> > Still don't really understand this so I'm going to admit defeat.
> > Thanks all for your advice... Very much appreciated!
>
> I was describing an alternative to storing functions in a way that
> wasn't in serial in plain text.  It was off-topic from state-machine
> transitions.
>
> Can you start with this?
>
> # state, input, next state
> transitions= [
>   ( 0, 'a', 1 ),
>   ( 1, 'a', 2 ),
>   ( 2, 'a', 0 ),
>   ( 0, 'b', 0 ),
>   ( 1, 'b', 0 ),
>   ( 2, 'b', 2 )
> ]
>
> What further?

My problem relates to the states really. I'd like to import a file
containing the state classes into a "state editor" that is just a
class editor really. Then, at *runtime* I'd like to add and remove
methods from this class and change its attributes (__dict__). I'd also
like to be able to add and remove classes from the module. When done,
I was hoping to do introspection upon which classes were associated
with this module and write back the modified class definitions to the
same module. Next time the module is imported, the changes would
remain intact. Unfortunately, inspect.getsource() reads the original
file and does not magically return source from the runtime objects.

Hope that makes sense!
Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython pop--up ("could not retrieve information...")

2008-08-07 Thread Timothy Grant
On Thu, Aug 7, 2008 at 11:36 AM, mistersulu <[EMAIL PROTECTED]> wrote:
> Hi all:
>
> I'm using a wx.ListView object with a multi-threaded wxPython app.
> The list is dynamically generated and accessed across two or more
> threads.  In spite of the fact that I have checks to see if an item at
> a given index is in the list and the entire set of logic is contained
> within a try: except: block I periodically get pop-up windows stating
> "Could not retrieve information for list control item X" when I try to
> change or access the list items.
>
> I searched the web and groups for similar posts but can't find any
> solutions.
>
> My question is:
>
> Is there a setting somewhere in wxPython to either disable these pop-
> up messages or a semaphore or event locking method to enable cross-
> thread access to a listview object?
>
> Thanks in advance,
> sulu

I haven't done any threaded apps, but my understanding is that the you
should not manipulate the GUI outside of the main application thread.


-- 
Stand Fast,
tjg.  [Timothy Grant]
--
http://mail.python.org/mailman/listinfo/python-list


Re: A question about string and float number

2008-08-07 Thread Wei Guo
Hi Thanks for Tyler and Edwin's help.

For my questions, I need to import some xml file and there are floating
number and strings in it. I need to process string and number differently.
This is reason that I am asking question here. Is this background
information we need for this quesions.

Btw, which way is better? type or with exception ValueError?

Thanks,

Wei


On 8/6/08, Tyler Breisacher <[EMAIL PROTECTED]> wrote:
>
> It's generally a bad idea to use "except" without naming a specific
> exception. The exception you might expect in this case is ValueError. Any
> other exception *should* be uncaught if it happens. By the way, this method
> will return true for integers as well as floats. For example, isFloat('3')
> will return 3.0. So make sure this is what you want, since it wasn't 100%
> clear from the original message.
>
>
> Wei Guo wrote:
>
>> #this is a better way of testing a string for float
>> def isFloat(s):
>>try:
>>s = float(s)
>>except:
>>return False
>>return True
>>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: Best practise implementation for equal by value objects

2008-08-07 Thread Terry Reedy



Slaunger wrote:

On 6 Aug., 21:36, Terry Reedy <[EMAIL PROTECTED]> wrote:



OK, the situation is more complicated than that then. In the case here
though,
the attributes would always be sinmple bulit-in types, where
eval(repr(x))==x
or, where the attribute is a user-defined equal-by-value class, that I
have
control over.


I think most would agree that a more accurate and informative 
representation is better than a general representation like Pythons 
default.  For instance,

>>> a=range(2,10,2) # 3.0
>>> a
range(2, 10, 2)

is nicer than .

So when the initializers for instances are all 'nice' (as for range), go 
for it (as in 'Age(10)').  And test it as you are by eval'ing the rep. 
Just accept that the eval will only work in contexts with the class name 
 bound to the class.  For built-in like range, it always is, by default 
-- unless masked by another assignment!


Terry Jan Reedy


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


Re: mktime overflow in March 2008?

2008-08-07 Thread M.-A. Lemburg

On 2008-08-07 20:40, Robert Latest wrote:

Here's what happens on my Windows machine (Win XP / Cygwin) at work.
I've googled a bit about this problem but only found references to
instances where people referred to dates before the Epoch.

Of course at home on my Linux box everything works.

I know that everything has its limits somewhere, but I've never
heard of March 2008 being a problem.

Tomorrow I'm going to write a test loop that shows me the exact last
second that mktime is going to be able to handle. Sort of like the way
Calvin's father explains the weight limit specification of bridges.

I wonder if the datetime module is any better, considering that under
the hood it probably uses the same C library functions.

I like to stick with the "seconds since..." approach because I need to
store millions of dates/times efficiently in a sqlite database.

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) 
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin

Type "help", "copyright", "credits" or "license" for more information.

import time
t = time.strptime("Mar 30, 2007 2:43:32 am", "%b %d, %Y %I:%M:%S
%p")
time.mktime(t)

1175215412.0

t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S
%p")
time.mktime(t)

Traceback (most recent call last):
  File "", line 1, in 
OverflowError: mktime argument out of range


Well, you could use the parser in mxDateTime:

>>> from mx.DateTime import DateTimeFrom
>>> DateTimeFrom("Mar 30, 2007 2:43:32 am")

>>> DateTimeFrom("Mar 30, 2008 2:43:32 am")

>>> DateTimeFrom("Mar 30, 2007 2:43:32 am").ticks()
1175215412.0
>>> DateTimeFrom("Mar 30, 2008 2:43:32 am").ticks()
1206841412.0

http://www.egenix.com/products/python/mxBase/mxDateTime/

--
Marc-Andre Lemburg
eGenix.com

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


 Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
--
http://mail.python.org/mailman/listinfo/python-list


Re: mktime overflow in March 2008?

2008-08-07 Thread Wojtek Walczak
Dnia 7 Aug 2008 18:40:10 GMT, Robert Latest napisa�(a):
 t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S
 %p")
 time.mktime(t)
> Traceback (most recent call last):
>   File "", line 1, in 
> OverflowError: mktime argument out of range
 

time module is written in C. time.mktime() function is actually
only a wrapper for mktime(3) and it also has its limits. Better
use datetime module instead.

-- 
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list

Re: Fastest way to store ints and floats on disk

2008-08-07 Thread M.-A. Lemburg

On 2008-08-07 20:41, Laszlo Nagy wrote:


 Hi,

I'm working on a pivot table. I would like to write it in Python. I 
know, I should be doing that in C, but I would like to create a cross 
platform version which can deal with smaller databases (not more than a 
million facts).


The data is first imported from a csv file: the user selects which 
columns contain dimension and measure data (and which columns to 
ignore). In the next step I would like to build up a database that is 
efficient enough to be used for making pivot tables. Here is my idea for 
the database:


Original CSV file with column header and values:

"Color","Year","Make","Price","VMax"
Yellow,2000,Ferrari,10,254
Blue,2003,Volvo,5,210

Using the GUI, it is converted to this:

dimensions = [
   { 'name':'Color', 'colindex:0, 'values':[ 'Red', 'Blue', 'Green', 
'Yellow' ], },
   { 'name':'Year', colindex:1, 'values':[ 
1995,1999,2000,2001,2002,2003,2007 ], },
   { 'name':'Make', colindex:2, 'value':[ 'Ferrari', 'Volvo', 'Ford', 
'Lamborgini' ], },

]
measures = [
   { 'name', 'Price', 'colindex':3 },
   { 'name', 'Vmax', 'colindex':4 },
]
facts = [
   ( (3,2,0),(10.0,254.0)  ), # ( dimension_value_indexes, 
measure_values )

   ( (1,5,1),(5.0,210.0) ),
   # Some million rows or less
]


The core of the idea is that, when using a relatively small number of 
possible values for each dimension, the facts table becomes 
significantly smaller and easier to process. (Processing the facts would 
be: iterate over facts, filter out some of them, create statistical 
values of the measures, grouped by dimensions.)


The facts table cannot be kept in memory because it is too big. I need 
to store it on disk, be able to read incrementally, and make statistics. 
In most cases, the "statistic" will be simple sum of the measures, and 
counting the number of facts affected. To be effective, reading the 
facts from disk should not involve complex conversions. For this reason, 
storing in CSV or XML or any textual format would be bad. I'm thinking 
about a binary format, but how can I interface that with Python?


I already looked at:

- xdrlib, which throws me DeprecationWarning when I store some integers
- struct which uses format string for each read operation, I'm concerned 
about its speed


What else can I use?


>>> import marshal
>>> marshal.dump(1, open('test.db', 'wb'))
>>> marshal.load(open('test.db', 'rb'))
1

It also very fast at dumping/loading lists, tuples, dictionaries,
floats, etc.

--
Marc-Andre Lemburg
eGenix.com

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


 Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best practise implementation for equal by value objects

2008-08-07 Thread Paul Rubin
Terry Reedy <[EMAIL PROTECTED]> writes:
> So when the initializers for instances are all 'nice' (as for range),
> go for it (as in 'Age(10)').  And test it as you are by eval'ing the
> rep. Just accept that the eval will only work in contexts with the
> class name bound to the class.  For built-in like range, it always is,
> by default -- unless masked by another assignment!

Eval is extremely dangerous.  Think of data from untrusted sources,
then ask yourself how well you really know where ALL your data came
from.  It's preferable to avoid using it that way.  There have been a
few "safe eval" recipes posted here and at ASPN.  It would be good if
one of them made it into the standard library.  Note that pickle
(which would otherwise be an obious choice for this) has the same
problems, though not as severely as flat-out evalling something.
--
http://mail.python.org/mailman/listinfo/python-list


Re: benchmark

2008-08-07 Thread bearophileHUGS
alex23:
> Honestly, performance benchmarks seem to be the dick size comparison
> of programming languages.

I don't agree:
- benchmarks can show you what language use for your purpose (because
there are many languages, and a scientist has to choose the right tool
for the job);
- it can show where a language implementation needs improvements (for
example the Haskell community has improved one of their compilers
several times thank to the Shootout, the D community has not yet done
the same because the language is in a too much fast evolving phase
still, so performance tunings is premature still);
- making some code faster for a benchmark can teach you how to make
the code faster in general, how CPUs work, or even a some bits of
computer science;
- if the benchmarks are well chosen and well used, they can show you
what are the faster languages (you may say 'the faster
implementations', and that's partially true, but some languages have a
semantic that allows better or much better optimizations). A computer
is a machine useful for many purposes, programming languages allow
some users to make the machine act as they want. So computers and
languages give some power, they allow you to do something that you
can't do without a computer. A language can give you power because it
gives you the ability to write less bug-prone code, or it can give you
more pre-built modules that allow you to do more things in less time,
or it can give you the power to perform computations in less time, to
find a specific solution faster. So Python and C give you different
kinds of power, and they are both useful. Other languages like D/Java
try to become a compromise, they try to give you as much as possible
of both "powers" (and they sometimes succeed, a D/Ocaml program may be
almost as fast as C, while being on the whole much simpler/safer to
write than C code).

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


Re: Problem obtaining an object reference...

2008-08-07 Thread Terry Reedy



SamG wrote:

I have two windowing classes A and B.

Inside A's constructor i created an instance B to display its Modal
window. Only on clicking OK/ Closing this modal window do i proceed to
display the A's window.

All that is fine. Now the problem is i would like to write a python
script to test the this GUI app simulating all the events that make
A's window work through my script. No the problem is to get past my
B's window. Which i'm unable to do since the A's instance would any be
created completely if i click OK/Close button on the B's window. Im
unable to simulate that event since i have little idea about how to
get the object reference to B's window? Here is a sample of the code.


How about


class Awindow:

def __init__(self):


self.showBwindow()
def showBwindow(self):
dialog = Bwindow()

if __debug__: self.dialog = dialog

dialog.ShowModal()

if __debug__: del self.dialog

dialog.Destroy()


From the assert statement doc (3.0, but unchanged):

These equivalences assume that __debug__ and AssertionError refer to the 
built-in variables with those names. In the current implementation, the 
built-in variable __debug__ is True under normal circumstances, False 
when optimization is requested (command line option -O). The current 
code generator emits no code for an assert statement when optimization 
is requested at compile time.


I believe the last applies to all 'if __debug__: ' statements, so 
you can have them not compiled if you wish.  Or use your own 'debug' 
variable to skip them without fussing with the '-O' startup flags and 
.pyo files (See Using Python/Command line arguments).


Terry Jan Reedy

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


Re: cache-like structure

2008-08-07 Thread konstantin
> And if you change the deque for a Queue.Queue, you don't even need a
> Lock...
>
> Cheers,
>
> Maxime

Maxime,

thanks, sched module could be a nice solution. I've never used it
before
but it seems to be the right way.
You guessed right. I want to used it in Queue. But this implementation
does
not guarantee that __cleanup__ won't be invoked while __contains__
processing.

> def _remove(self, rec): #try not to use __form, almost always an error
>self.records.remove(rec)
>
> the Timer would become
> Timer(ttl, self._remove, [(t, item)]) # t is the time that you got
> before

This solution has one shortcoming. When record() called many times
during
short period (almost simultaneously) the _remove(item) calls do not
manage to
remove records on time. So I choosed batch cleanup.

But thanks for suggestion! I'll try sched.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Books to begin learning Python

2008-08-07 Thread Samir
> > There's lots of good books to read, including a few online ones. A lot
> > of people like "Dive Into Python" (http://diveintopython.org/). If you
> > want LOTS of information and some good code examples, Lutz's
> > "Programming Python 3rd Ed" is great.
>
> I have the 2nd edition. Has the 3rd edition been rewritten so that all
> of its code will be valid in Python 3? I'd prefer not to buy Python
> books that will become obsolete.

As a relative newcomer to Python, I found that "Dive Into Python" was
initially out of my league.  It's written assuming that you have a
good understanding of basic Python concepts.  Since I didn't have this
initial mastery of the language, I didn't find it useful.

Now that I've been working with the language for awhile, however, I do
come back and refer to it from time to time.

Some good online tutorials that I found really helpful include:
(1) Python Tutorial
http://docs.python.org/tut/tut.html

(2) A Byte of Python
http://www.ibiblio.org/swaroopch/byteofpython/read/

(3) How to Think Like a Computer Scientist
http://www.greenteapress.com/thinkpython/thinkCSpy/html/index.html

Incidentally, you can find documentatio for Python v3.0 at the
official Python site here:
http://docs.python.org/dev/3.0/

I hope that helps.  Good luck!

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


Re: mktime overflow in March 2008?

2008-08-07 Thread John Machin
On Aug 8, 4:40 am, Robert Latest <[EMAIL PROTECTED]> wrote:
> Here's what happens on my Windows machine (Win XP / Cygwin) at work.
> I've googled a bit about this problem but only found references to
> instances where people referred to dates before the Epoch.
>
> Of course at home on my Linux box everything works.

And of course using the official versions of Python for Windows
everything works:

C:\junk>\python25\python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S %p")
>>> time.mktime(t)
1206805412.0
>>> t
(2008, 3, 30, 2, 43, 32, 6, 90, -1)
>>> ^Z

C:\junk>\python24\python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S %p")
>>> time.mktime(t)
1206805412.0
>>> t
(2008, 3, 30, 2, 43, 32, 6, 90, -1)
>>>

>
> I know that everything has its limits somewhere, but I've never
> heard of March 2008 being a problem.
>
> Tomorrow I'm going to write a test loop that shows me the exact last
> second that mktime is going to be able to handle. Sort of like the way
> Calvin's father explains the weight limit specification of bridges.
>
> I wonder if the datetime module is any better, considering that under
> the hood it probably uses the same C library functions.

Don't wonder; try it out; look at the source.

>
> I like to stick with the "seconds since..." approach because I need to
> store millions of dates/times efficiently in a sqlite database.
>
> $ python
> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin

There's your problem.

> Type "help", "copyright", "credits" or "license" for more information.>>> 
> import time
> >>> t = time.strptime("Mar 30, 2007 2:43:32 am", "%b %d, %Y %I:%M:%S
> >>> %p")
> >>> time.mktime(t)
> 1175215412.0
> >>> t = time.strptime("Mar 30, 2008 2:43:32 am", "%b %d, %Y %I:%M:%S
> >>> %p")
> >>> time.mktime(t)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> OverflowError: mktime argument out of range
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: .cpp to .pyd

2008-08-07 Thread Carl Banks
On Aug 7, 3:25 am, [EMAIL PROTECTED] wrote:
> Hello,
>
> I want to build my C++ (.cpp) script to (.pyd) like this:
>
> http://en.wikibooks.org/wiki/Python_Programming/Extending_with_C%2B%2B
>
> I have installed "Microsoft Visual studio .NET 2003" and "Boost
> Python" and then after I run my setup script:
>
> python setup.py build
>
> I get this error:
>
> running build
> running build_ext
> building 'hello' extension
> D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c /
> nologo /Ox
>  /MD /W3 /GX /DNDEBUG -IC:\Panda3D-1.5.2\python\include -IC:
> \Panda3D-1.5.2\pytho
> n\PC /Tphellomodule.cpp /Fobuild\temp.win32-2.5\Release
> \hellomodule.obj
> hellomodule.cpp
> hellomodule.cpp(9) : fatal error C1083: Cannot open include file:
> 'boost/python/
> module.hpp': No such file or directory
> error: command '"D:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\bin\cl.e
> xe"' failed with exit status 2
>
> I think that my MS visual studio  cannot find "boost python", if
> that's the problem then can you tell me how can I solve it.
> This is very begginer question,but I can't find answer nowhere, and I
> don't have any expirience with Microsoft products.
>
> Sorry for my bad english!
> Regards,
> Veki


First, locate the boost header files.  Suppose you find the file
module.hpp in this location:

C:\boost-whatever-version\include\boost\python\module.hpp


The part that comes before boost\python\module.hpp is the required
include directory.  You can tell setup to use this directory by adding
the following argument to the Extension call:

include_dirs = ['C:\\boost-whatever-version\\include']

Notice the doubling of backslashes.  Remember to add the directory
where the boost header files lie on your system; don't add this line
exactly.

You should end up with a setup call that looks like this:

setup(name="blah",
ext_modules=[
Extension("hello", ["hellomodule.cpp"],
libraries = ["boost_python"],
include_dirs = ['C:\\boost-whatever-version\\include'])
])



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


Re: wxPython pop--up ("could not retrieve information...")

2008-08-07 Thread Frank Niessink
Hi,

2008/8/7 mistersulu <[EMAIL PROTECTED]>:
> Is there a setting somewhere in wxPython to either disable these pop-
> up messages or a semaphore or event locking method to enable cross-
> thread access to a listview object?

Maybe this helps: http://wiki.wxpython.org/LongRunningTasks ?

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


Re: HTTP basic authentication with form-based authentication

2008-08-07 Thread Wojtek Walczak
Dnia Thu, 7 Aug 2008 11:14:05 -0700 (PDT), Max napisa�(a):
> Following the tutorial at http://personalpages.tds.net/~kent37/kk/00010.html,
> I understand how to access HTTP basic authenticated pages or form-
> based authenticated pages. How would I access a page protected by both
> form-based authentication (using cookies) *and* HTTP basic
> authentication?

Use ClientCookie or even better - mechanize:
http://pypi.python.org/pypi/mechanize/
The docs aren't perfect, but you should easily
find what you are searching for.

-- 
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list

kill thread

2008-08-07 Thread Mathieu Prevot
Hi,

I have a threading.Thread class with a "for i in range(1,50)" loop
within. When it runs and I do ^C, I have the error [1] as many as
loops. I would like to catch this exception (and if possible do some
cleanup like in C pthreads) so the program finishes cleanly. Where and
how can I do this ? in __run__ ? __init__ ? a try/except stuff ?

Thanks,
Mathieu

[1]:
^CException in thread Thread-1:
Traceback (most recent call last):
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",
line 486, in __bootstrap_inner
self.run()
  File "./youfetch.py", line 148, in run
self.getids()
  File "./youfetch.py", line 145, in getids
self.ids.append(self.getidsatpage(i))
  File "./youfetch.py", line 138, in getidsatpage
self.child = subprocess.Popen(cmd.split(),stdout=subprocess.PIPE)
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/subprocess.py",
line 594, in __init__
errread, errwrite)
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/subprocess.py",
line 1011, in _execute_child
self.pid = os.fork()
KeyboardInterrupt
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >