Re: How to format a string from an array?

2007-06-14 Thread Gerard Flanagan
On Jun 13, 11:11 am, Allen <[EMAIL PROTECTED]> wrote:
> a = range(256)
> I want to output the formated string to be:
> 00 01 02 03 04 05 06 07   08 09 0a 0b 0c 0d 0e 0f
> 10 11 12 13 14 15 16 17   18 19 1a 1b 1c 1d 1e 1f
> 
> f0 f1 f2 f3 f4 f5 6 f7   f8 f9 fa fb fc fd fe ff
>
> How to do it?

a = range(256)

import itertools as it

for k, g in it.groupby(a, lambda x: x//16):
print ' '.join('%02x' % n for n in g)

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


Re: Windows XP timezone language issue

2007-06-14 Thread Paul Sijben
MRAB wrote:
> On Jun 13, 7:31 am, Paul Sijben <[EMAIL PROTECTED]> wrote:
>> I ran into an internationalization issue. I need a consistent idea about
>> the timezone my application is running on. However when I run the following:
>>  >>> import time
>>  >>> time.tzname
>>
>> I get back ('West-Europa (standaardtijd)', 'West-Europa (zomertijd)')
>> which is in dutch (the language of the host machine) and verbose.
>> I wanted to get ('CEST','CET') or something international so I can work
>> with itin the same way on all platforms.
>>
>> That is the right way to find out the timezone in a consistent way
>> across platforms (windows/linux/mac) and languages?
>>
> Well, time.timezone will return the timezone as an integer.
> 
true, I am wondering if that is enough for my app. (with some of the odd 
timezones in the world and DTS issues and all

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


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Gabriel Genellina wrote:

> En Thu, 14 Jun 2007 01:39:29 -0300, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> escribió:
> 
>> Gabriel Genellina wrote:
>>> In addition, += is rather inefficient for strings; the usual idiom is
>>> using ''.join(items)
>>
>> Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
>> strings pretty well.
>>
>> a=""
>> for i in xrange(10):
>> a+="a"
>>
>> and:
>>
>> a=[]
>> for i in xrange(10):
>> a.append("a")
>> a="".join(a)
>>
>> take virtually the same amount of time on my machine (2.5), and the
>> non-join version is clearer, IMO.  I'd still use join in case I wind
>> up running under an older Python, but it's probably not a big issue
>> here.
> 
> Yes, for concatenating a lot of a's, sure... Try again using strings
> around the size of your expected lines - and make sure they are all
> different too.
> 
> py> import timeit
> py>
> py> def f1():
> ...   a=""
> ...   for i in xrange(10):
> ...   a+=str(i)*20
> ...
> py> def f2():
> ...   a=[]
> ...   for i in xrange(10):
> ...   a.append(str(i)*20)
> ...   a="".join(a)
> ...
> py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
> 
> ...after a few minutes I aborted the process...

I can't confirm this.

$ cat join.py
def f1():
a = ""
for i in xrange(10):
a += str(i)*20

def f2():
a = []
for i in xrange(10):
a.append(str(i)*20)
a = "".join(a)

def f3():
a = []
append = a.append
for i in xrange(10):
append(str(i)*20)
a = "".join(a)

$ python2.5 -m timeit -s 'from join import f1' 'f1()'
10 loops, best of 3: 212 msec per loop
$ python2.5 -m timeit -s 'from join import f2' 'f2()'
10 loops, best of 3: 259 msec per loop
$ python2.5 -m timeit -s 'from join import f3' 'f3()'
10 loops, best of 3: 236 msec per loop

Peter

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


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Leo Kislov wrote:

> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>> Hi all,
>>
>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>> is substantially slower when in a method than in a function.
>>
>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 10.214 CPU seconds
> 
>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 0.041 CPU seconds
>>
> 
> I suspect open files are cached so the second reader
> picks up where the first one left: at the of the file.
> The second call doesn't do any text processing at all.
> 
>   -- Leo

Indeed, the effect of attribute access is much smaller than what the OP is
seeing:

$ cat iadd.py
class A(object):
def add_attr(self):
self.x = 0
for i in xrange(10):
self.x += 1
def add_local(self):
x = 0
for i in xrange(10):
x += 1

add_local = A().add_local
add_attr = A().add_attr
$ python2.5 -m timeit -s 'from iadd import add_local' 'add_local()'
10 loops, best of 3: 21.6 msec per loop
$ python2.5 -m timeit -s 'from iadd import add_attr' 'add_attr()'
10 loops, best of 3: 52.2 msec per loop

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


Re: Method much slower than function?

2007-06-14 Thread Francesco Guerrieri
On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
> Gabriel Genellina wrote:
> > ...
> > py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> > [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> > py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
> >
> > ...after a few minutes I aborted the process...
>
> I can't confirm this.

[...]

> $ python2.5 -m timeit -s 'from join import f1' 'f1()'
> 10 loops, best of 3: 212 msec per loop
> $ python2.5 -m timeit -s 'from join import f2' 'f2()'
> 10 loops, best of 3: 259 msec per loop
> $ python2.5 -m timeit -s 'from join import f3' 'f3()'
> 10 loops, best of 3: 236 msec per loop

On my machine (using python 2.5 under win xp) the results are:
>>> print timeit.Timer("f2()", "from __main__ import f2").repeat(number = 1)
[0.19726834822823575, 0.19324697456408974, 0.19474492594212861]
>>> print timeit.Timer("f1()", "from __main__ import f1").repeat(number = 1)
[21.982707133304167, 21.905312587963252, 22.843430035622767]

so it seems that there is a rather sensible difference.
what's the reason of the apparent inconsistency with Peter's test?

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


for web application development

2007-06-14 Thread james_027
hi everyone,

I am very new to python, I am almost done learning the python language
enough that I can start learning developing web app in python. I have
gone thru many research and I still say that I will want to develop
web app in python. Although some says php should be better since the
language is made for the web compare to python. In the point of view
of rails, they say that their framework is really easy and best for
web app.

My problem is looking for a web framework for python, that could
provide a natural way of developing web app. I am avoiding to learn
template language as much as possible.

Any advice will be greatly appreciated.

THanks
james

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


Re: mapping subintervals

2007-06-14 Thread Lee Sander
Dear Matteo and Nis,
Thankyou very much for your help. I wasn't aware of the bisect
library but it's really useful.
thank you both once again
Lee

On 13 Jun, 23:21, Nis Jørgensen <[EMAIL PROTECTED]> wrote:
> Matteo skrev:
>
> > OK - I'm going to assume your intervals are inclusive (i.e. 34-51
> > contains both 34 and 51).
>
> > If your intervals are all really all non-overlapping, one thing you
> > can try is to put all the endpoints in a single list, and sort it.
> > Then, you can use the bisect module to search for intervals, which
> > will give you a logarithmic time algorithm.
>
> > Here, I'm going to assume you just need the index of the containing
> > interval. If you really need a name (i.e. 'a1' or 'a2'), you can use a
> > list of names, and index into that.
>
> > I hope those assumptions are valid! if so, the following should work:
>
> I have taken the liberty of simplifying your code, using the fact that
> tuples are sorted lexicographically. Note that this requires all
> intervals to be tuples and not lists (since list(a) < tuple(b) is always
> True).
>
> from bisect import bisect
>
> def test_interval(ivl,intervals):
>
>   # Find where ivl would lie in the list
>   # i.e. the index of the first interval sorting as larger than ivl
>   idx=bisect(intervals,ivl)
>   # Left endpoints equal is a special case - a matching interval will be
>   # to the right of the insertion point
>   if idx < len(intervals) and intervals[idx][0] == ivl[0]:
> if intervals[idx][1] >= ivl[1]:
> return idx
> else:
> return None
>   # Otherwise, we need to check to the left of the insertion point
>   if idx > 0 and intervals[idx-1][1] >= ivl[1]:
> return idx-1
>   else:
> return None
>
> >>> intervals =[(10, 21), (34, 51), (77, 101)]
> >>> print test_interval((34,35),intervals)
> 1
> >>> print test_interval((34,53),intervals)
> None
> >>> print test_interval((77,53),intervals)
> 2
> >>> print test_interval((77,83),intervals)
> 2
> >>> print test_interval((77,102),intervals)
> None
> >>> print test_interval((77,101),intervals)
>
> 2
>
> u"Nis J\xf8rgensen"


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

Re: for web application development

2007-06-14 Thread Giuseppe Di Martino
Il Thu, 14 Jun 2007 09:16:24 +, james_027 ha scritto:

> 
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.

Currently, i'm playing with Pylons (http://pylonshq.com) and until now i'm
happy !

Giuseppe


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


Re: for web application development

2007-06-14 Thread Benedict Verheyen
james_027 schreef:
> hi everyone,
> 
> I am very new to python, I am almost done learning the python language
> enough that I can start learning developing web app in python. I have
> gone thru many research and I still say that I will want to develop
> web app in python. Although some says php should be better since the
> language is made for the web compare to python. In the point of view
> of rails, they say that their framework is really easy and best for
> web app.
> 
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.
> 
> Any advice will be greatly appreciated.
> 
> THanks
> james


I use Django and it's easy to use.
http://www.djangoproject.com/

Regards,
Benedict

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


Re: Goto

2007-06-14 Thread Hendrik van Rooyen
 "HMS Surprise" <[EMAIL PROTECTED]> wrote:


> 
> How does one effect a goto in python? I only want to use it for debug.
> I dasn't slap an "if" clause around the portion to dummy out, the
> indentation police will nab me.

I use a global boolean called trace:

if trace:
  do debug stuff

But to try to answer your question:

There are two gotos in standard Python.
They are spelled:

"continue" and "break"

The first jumps to the start of a loop, and the last to after the end of one.

Anything more fancy is "Verboten" - except, that, if you ask nicely,
John Machin might explain his comefrom construct.

And maybe I will understand it this time around...

- Hendrik


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


Re: SimplePrograms challenge

2007-06-14 Thread Steve Howell

--- Steven Bethard <[EMAIL PROTECTED]> wrote:
unit.text)
> 
> I posted a slight variant of this, trimmed down a
> bit to 21 lines.
> 

Thanks, I think this will be a very useful example.


   

Pinpoint customers who are looking for what you sell. 
http://searchmarketing.yahoo.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method much slower than function?

2007-06-14 Thread Christof Winter
Gabriel Genellina wrote:
[...]
> py> import timeit
> py>
> py> def f1():
> ...   a=""
> ...   for i in xrange(10):
> ...   a+=str(i)*20
> ...
> py> def f2():
> ...   a=[]
> ...   for i in xrange(10):
> ...   a.append(str(i)*20)
> ...   a="".join(a)
> ...
> py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
> 
> ...after a few minutes I aborted the process...
> 

Using

Python 2.4.4 (#2, Jan 13 2007, 17:50:26)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2

f1() and f2() also virtually take the same amount of time, although I must 
admit 
that this is quite different from what I expected.

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


Re: SimplePrograms challenge

2007-06-14 Thread rzed
Steven Bethard <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]: 

> Steve Howell wrote:
>> --- George Sakkis <[EMAIL PROTECTED]> wrote:
>>> from itertools import count, ifilter
>>> def sieve():
>>> seq = count(2)
>>> while True:
>>> p = seq.next()
>>> seq = ifilter(p.__rmod__, seq)
>>> yield p
> [snip]
>> Is there a way to broaden the problem somehow, so that
>> it can be a longer solution and further down on the
>> page, and so that I can continue to enforce my
>> somewhat arbitrary rule of ordering examples by how
>> long they are?
> 
> How about we just comment it better?
> 
> import itertools
> 
> def iter_primes():
>  # an iterator of all numbers between 2 and +infinity
>  numbers = itertools.count(2)
> 
>  # generate primes forever
>  while True
> 
>  # generate the first number from the iterator,
>  # which should always be a prime
>  prime = numbers.next()
>  yield prime
> 
>  # lazily remove all numbers from the iterator that
>  # are divisible by prime we just selected
>  numbers = itertools.ifilter(prime.__rmod__, numbers)
> 
> I think that's 17-ish, though you could shrink it down by
> removing some of the spaces.
> 
> STeVe

How about including a driver? Generators are frustrating for 
newbies (including oldies new to generators) because they don't 
actually do anything unless you know how to use them. Given the 
above, what's a newbie going to try first? Something like:

>>> iter_primes()

Hmmm. Doesn't do anything.

How about 
>>> for ix in range(10):
... print iter_primes()

Not what you might expect.

Later:

>>> for ix in range(10):
... print iter_primes().next()

Hm

... and so on.

In much of Python's documentation, and in this case, an occasional 
working example of use would go FAR in aiding understanding of the 
underlying concept.

-- 
rzed

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


Re: for web application development

2007-06-14 Thread Thomas Wittek
james_027:
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app.

The three bigger Python web frameworks seem to be:

- Django: http://www.djangoproject.com/
- TurboGears: http://www.turbogears.org/
- Pylons: http://pylonshq.com/

I only tried them briefly.

Django has its own componentes for almost everything (templating, ORM,
routing) and feels thus very integrated and solid.

TurboGears tries to use independent components like Kid, SQLObject and
CherryPy, which you can easily change to you own favorites. So you have
more flexibility, but also a bit more complexity.

Pylons is similar to TurboGears as it also uses independent components,
that are by some people consideres more "modern". By default it uses
SQLAlchemy for ORM and Routes for routing/dispatching.
It also incorporates the Python WSGI standard for web apps.
For me it felt least integrated of those frameworks but it may have the
biggest future potentials.

You may take a look at those comparisons:
http://jesusphreak.infogami.com/blog/vrp1
http://www.ibm.com/developerworks/linux/library/l-django/
http://www.ibm.com/developerworks/linux/library/l-turbogears/

> I am avoiding to learn
> template language as much as possible.

You will need a templating language to put out your HTML.
Of course that will not be a full programming language like PHP.

-- 
Thomas Wittek
http://gedankenkonstrukt.de/
Jabber: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I capture all exceptions especially when os.system() fail? Thanks

2007-06-14 Thread Michael Hoffman
Gabriel Genellina wrote:
> En Wed, 13 Jun 2007 21:47:16 -0300, mike <[EMAIL PROTECTED]> escribió:
> 
>> Following piece of code can capture IOError when the file doesn't
>> exist, also, other unknown exceptions can be captured when I press
>> Ctrl-C while the program is sleeping(time.sleep). Now the question is:
>> when I run the non-exist command, the exception cannot be captured.
> 
>> So far so good, then I changed the code to run a non-exist command
>> "wrong_command_test"(commented the open and sleep lines), then the
>> script printed:
>> sh: wrong_command_test: command not found
>> well Done
> 
> That's because it is not an exception, it is an error message coming 
> from your shell, not from Python.

Of course if you use subprocess.check_call() instead of os.system(), it 
will become an exception (CalledProcessError).
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Professional Grant Proposal Writing Workshop (September 2007: Simon Fraser University)

2007-06-14 Thread Anthony Jones


The Grant Institute's Grants 101: Professional Grant Proposal Writing Workshop will be held at Simon Fraser University at Harbour Centre, September 12 - 14
, 2007. Interested development professionals, researchers, faculty, and graduate students should register as soon as possible, as demand means that seats will fill up quickly. Please forward, post, and distribute this e-mail to your colleagues and listservs. 
 
All participants will receive certification in professional grant writing from the Institute. For more information call (213) 817 - 5308 or visit The Grant Institute at www.thegrantinstitute.com.
 
Please find the program description below:
 
The Grant Institute
Grants 101: Professional Grant Proposal Writing Workshop
will be held at
Simon Fraser University at Harbour Centre
Vancouver,  British Columbia
September 12 - 14, 2007
8:00 AM - 5:00 PM
 

The Grant Institute's Grants 101 course is an intensive and detailed introduction to the process, structure, and skill of professional proposal writing. This course is characterized by its ability to act as a thorough overview, introduction, and refresher at the same time. In this course, participants will learn the entire proposal writing process and complete the course with a solid understanding of not only the ideal proposal structure, but a holistic understanding of the essential factors, 
which determine whether or not a program gets funded. Through the completion of interactive exercises and activities, participants will complement expert lectures by putting proven techniques into practice. This course is designed for both the beginner looking for a thorough introduction and the intermediate looking for a refresher course that will strengthen their grant acquisition skills. This class, simply put, is designed to get results by creating professional grant proposal writers. 

 
Participants will become competent program planning and proposal writing professionals after successful completion of the Grants 101 course. In three active and informative days, students will be exposed to the art of successful grant writing practices, and led on a journey that ends with a masterful grant proposal. 
 
Grants 101 consists of three (3) courses that will be completed during the three-day workshop. 
 
(1) Fundamentals of Program Planning
 
This course is centered on the belief that "it's all about the program." This intensive course will teach professional program development essentials and program evaluation. While most grant writing "workshops" treat program development and evaluation as separate from the writing of a proposal, this class will teach students the relationship between overall program planning and grant writing. 
 
(2) Professional Grant Writing
 

Designed for both the novice and experienced grant writer, this course will make each student an overall proposal writing specialist. In addition to teaching the basic components of a grant proposal, successful approaches, and the do's and don'ts of grant writing, this course is infused with expert principles that will lead to a mastery of the process. Strategy resides at the forefront of this course's intent to illustrate grant writing as an integrated, multidimensional, and dynamic endeavor. 
Each student will learn to stop writing the grant and to start writing the story. Ultimately, this class will illustrate how each component of the grant proposal represents an opportunity to use proven techniques for generating support.
 
(3) Grant Research
 

At its foundation, this course will address the basics of foundation, corporation, and government grant research. However, this course will teach a strategic funding research approach that encourages students to see research not as something they do before they write a proposal, but as an integrated part of the grant seeking process. Students will be exposed to online and database research tools, as well as publications and directories that contain information about foundation, corporation, and 
government grant opportunities. Focusing on funding sources and basic social science research, this course teaches students how to use research as part of a strategic grant acquisition effort.
 
Registration
$597.00 USD tuition includes all materials and certificates.
 
Each student will receive:
*The Grant Institute Certificate in Professional Grant Writing
*The Grant Institute's Guide to Successful Grant Writing
*The Grant Institute Grant Writer's Workbook with sample proposals, forms, and outlines
 
Registration Methods
 
1) On-Line - Complete the online registration form at www.thegrantinstitute.com under Register Now. We'll send your confirmation by e-mail. 
 
2) By Phone - Call (213) 817 - 5308 to register by phone. Our friendly Program Coordinators will be happy to assist you and answer your questions. 
 
3) By E-mail - Send an e-mail with your name, organization, and basic contact information to [EMAIL PROTECTED] and we will reserve your slot and send you

IndentationError: unexpected indent

2007-06-14 Thread desktop
I have this class:

class case(blop.case):
   def __init__(self, n, a, b):
 blop.case.__init__(self)
 print 'Monty Python's Flying Circus has a ' within it...'
 ...
 ...

But I get an error when I run the .py script from shell saying:

 print 'Monty Python's Flying Circus has a ' within it...'
 ^
IndentationError: unexpected indent

I have tried to indent the print statement 8 blanks but that does not 
help. Any hints?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IndentationError: unexpected indent

2007-06-14 Thread Wim Vogelaar
You have possibly unvisible tab characters in your file.
Just copy your lines to the simple MS notepad and try again.

Wim Vogelaar, http://home.wanadoo.nl/w.h.vogelaar/


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


Re: IndentationError: unexpected indent

2007-06-14 Thread Laurent Pointal
desktop a écrit :
> I have this class:
> 
> class case(blop.case):
>   def __init__(self, n, a, b):
> blop.case.__init__(self)
> print 'Monty Python's Flying Circus has a ' within it...'
> ...
> ...
> 
> But I get an error when I run the .py script from shell saying:
> 
> print 'Monty Python's Flying Circus has a ' within it...'
> ^
> IndentationError: unexpected indent
> 
> I have tried to indent the print statement 8 blanks but that does not 
> help. Any hints?

You have also another error:

 >>> print 'Monty Python's Flying Circus has a ' within it...'
   File "", line 1
 print 'Monty Python's Flying Circus has a ' within it...'
 ^
SyntaxError: invalid syntax

Better:
 >>> print "Monty Python's Flying Circus has a ' within it..."
Monty Python's Flying Circus has a ' within it...



Note: for your indentation problem, try to use an editor allowing to 
display tab and spaces and then identify the problem location, or 
replace all tabs by 4 spaces and configure the editor to only use spaces.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for web application development

2007-06-14 Thread Laurent Pointal
james_027 a écrit :
> hi everyone,
> 
> I am very new to python, I am almost done learning the python language
> enough that I can start learning developing web app in python. I have
> gone thru many research and I still say that I will want to develop
> web app in python. Although some says php should be better since the
> language is made for the web compare to python. In the point of view
> of rails, they say that their framework is really easy and best for
> web app.
> 
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.

Near the big ones (Django, TurboGears, Pylons), you may take a look at 
Karrigell:
http://karrigell.sourceforge.net/en/front.htm

(se examples)

> 
> Any advice will be greatly appreciated.
> 
> THanks
> james
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cretins.

2007-06-14 Thread Laurent Pointal
Cousin Stanley a écrit :
>> On Thu, 14 Jun 2007 09:32:10 +1000, Ben Finney wrote:
>>
>>> "Dr. Pastor" <[EMAIL PROTECTED]> writes:
>>>
 Please do not do business with those cretins
 who without authorization attaching [spam footers]
>>> Indeed. The cost of Usenet access should not be translated 
>>> to spam on messages.
>> Out of curiosity, who are these cretins ? 
>   
>   NewsFeeds.Com  
> 
>   I  don't  do business with them either  
> 
>   But my ISP does, apparently farming out 
>   their news service  
> 
>   I complained about the addition of spammed footers
>   by NewsFeeds.Com to my ISP several years back
>   but it didn't help at all  
> 
>   My news client is configured to use my ISP's news server
>   but it gets spun off to NewsFeeds.Com 
> 
>   Anything below  Phoenix, Arizona  in my signature
>   was  not  added by me but by NewsFeeds.Com 

As they wrote: Unlimited-Unrestricted-Secure => spammer's paradise.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with PAM and ctypes

2007-06-14 Thread Chris AtLee
On Jun 11, 6:01 pm, Lenard Lindstrom <[EMAIL PROTECTED]> wrote:
[snip snip snip]
> > if __name__ == "__main__":
> > import getpass, os, sys
> > @conv_func
> > def my_conv(nMessages, messages, pResponse, appData):
> > # Create an array of nMessages response objects
> > # Does r get GC'ed after we're all done?
> > r = (pam_response * nMessages)()
>
> The memory allocated to r is garbage collected immediately after my_conv
> returns. You need to allocate it explicitly using C's calloc or such.
> This assumes pam_start will free the memory for you.
>
> addr = calloc(sizeof(pam_response), nMessages)
>
> addr is the memory address as a Python integer.
>
> > pResponse.contents = cast(r, POINTER(pam_response))
>
> pResponse.contents changes the actual value of pResponse, a value on the
> stack. You want to change the value of the pointer pResponse points to:
>
> pResponse[0] = cast(addr, POINTER(pam_response))
>
> The cast creates a POINTER(pam_reponse) instance with value addr.

Ahhh, thank you!  I never understood how ctypes' pointer.contents
related
to C pointers.  So, the following are equivalent?

int  v = 42;v = 42
int *p = 0; p = cast(0, POINTER(c_int))
p = &v; p.contents = v
*p = 123;   p[0] = 123

Using "pResponse[0] = cast(...)" got me part of the way to fixing my
problem.  PAM started either crashing or saying authentication had
failed.
The crash was in free(), and some digging around revealed that PAM
tries to
free the response sent by the application, which makes sense.

My initial attempt to fix this involved wrapping strdup to allocate a
new
copy of a string to send back to PAM.  Here's how I wrapped it:

strdup = libc.strdup
strdup.argstypes = [c_char_p]
strdup.restype = c_char_p

This still crashed in free().  I took a look at some of the ctypes
regression tests and something made me try this:

strdup = libc.strdup
strdup.argstypes = [c_char_p]
strdup.restype = POINTER(c_char) # NOT c_char_p 

This works like a charm.  Not sure why though...Does ctypes do
something
special with c_char_p return types?  It seems as if Python was freeing
the
result of strdup() when the string was GC'ed, and then PAM would fail
when
trying to free the same memory.

My working code is pasted below.

Thanks,
Chris

from ctypes import *

libpam = CDLL("libpam.so")
libc = CDLL("libc.so.6")

calloc = libc.calloc
calloc.restype = c_void_p
calloc.argtypes = [c_uint, c_uint]

strdup = libc.strdup
strdup.argstypes = [c_char_p]
strdup.restype = POINTER(c_char) # NOT c_char_p 

# Various constants
PAM_PROMPT_ECHO_OFF = 1
PAM_PROMPT_ECHO_ON = 2
PAM_ERROR_MSG = 3
PAM_TEXT_INFO = 4

class pam_handle(Structure):
_fields_ = [
("handle", c_void_p)
]

def __init__(self):
self.handle = 0

class pam_message(Structure):
_fields_ = [
("msg_style", c_int),
("msg", c_char_p),
]

def __repr__(self):
return "" % (self.msg_style, self.msg)

class pam_response(Structure):
_fields_ = [
("resp", c_char_p),
("resp_retcode", c_int),
]

def __repr__(self):
return "" % (self.resp_retcode,
self.resp)

conv_func = CFUNCTYPE(c_int,
c_int, POINTER(POINTER(pam_message)),
   POINTER(POINTER(pam_response)), c_void_p)

class pam_conv(Structure):
_fields_ = [
("conv", conv_func),
("appdata_ptr", c_void_p)
]

pam_start = libpam.pam_start
pam_start.restype = c_int
pam_start.argtypes = [c_char_p, c_char_p, POINTER(pam_conv),
POINTER(pam_handle)]

pam_authenticate = libpam.pam_authenticate
pam_authenticate.restype = c_int
pam_authenticate.argtypes = [pam_handle, c_int]

if __name__ == "__main__":
import getpass, os, sys
@conv_func
def my_conv(nMessages, messages, pResponse, appData):
# Create an array of nMessages response objects
addr = calloc(nMessages, sizeof(pam_response))
pResponse[0] = cast(addr, POINTER(pam_response))
for i in range(nMessages):
if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:
p = strdup(getpass.getpass(messages[i].contents.msg))
pResponse.contents[i].resp = cast(p, c_char_p)
pResponse.contents[i].resp_retcode = 0
else:
print "Unknown message type"
return 0

handle = pam_handle()
c = pam_conv(my_conv, 0)
retval = pam_start("login", getpass.getuser(), pointer(c),
pointer(handle))

if retval != 0:
print "Couldn't start pam session"
sys.exit(-1)

retval = pam_authenticate(handle, 0)
if retval == 21:
print "Authentication information cannot be recovered"
sys.exit(-1)
elif retval == 7:
print "Authentication failure"
elif retval == 0:
print "Ok!"
else:
print retval

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


Re: Method much slower than function?

2007-06-14 Thread Neil Cerutti
On 2007-06-14, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Neil Cerutti a écrit :
> (snip)
>> class bar:
>> def readgenome(self, filehandle):
>> self.s = ''.join(line.strip() for line in filehandle)
>
>=>
>self.s = ''.join(line.strip() for line in filehandle if not 
> '>' in line)

Thanks for that correction.

-- 
Neil Cerutti
I don't know what to expect right now, but we as players have to do what we've
got to do to make sure that the pot is spread equally. --Jim Jackson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert String to Int and Arithmetic

2007-06-14 Thread Facundo Batista
tereglow wrote:


> cpuSpeed = 'Speed: 10'
>
> What I would like to do is extract the '10' from the string,
> and divide that by 1000 twice to get the speed of a processor in MHz.

>>> cpuSpeed = 'Speed: 10'
>>> p = cpuSpeed.split(":")
>>> p
['Speed', ' 10']
>>> p[1]
' 10'
>>> v = int(p[1])
>>> v
10
>>> v / 100
1000
>>> 

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


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


Re: xmlrpclib hangs execution

2007-06-14 Thread itkovian
Hi,

> 2. The Python implementation ofxmlrpcis not very robust. It just waits for
> the connection to close. A well-written client (like your Java client)
> would detect the presence of a Content-Length header and use that.

I'm facing a similar ordeal here. I think the right thing to do would
be to adjust the xmlrpc library to parse the header and check for the
Content-Length. Right now, the socket seems to read 1024 bytes, so
naturally, when the connection closes, the socket throws an error,
because it thinks that more bytes are coming. Which sounds rather
weird to me, given the fact that in rare cases the reply will be a
multiple of 1024 bytes :-)

-- Andy Georges

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


Re: xmlrpclib hangs execution

2007-06-14 Thread itkovian
On Jun 14, 3:10 pm, [EMAIL PROTECTED] wrote:
> Hi,
>
> > 2. The Python implementation ofxmlrpcis not very robust. It just waits for
> > the connection to close. A well-written client (like your Java client)
> > would detect the presence of a Content-Length header and use that.
>
> I'm facing a similar ordeal here. I think the right thing to do would
> be to adjust the xmlrpc library to parse the header and check for the
> Content-Length. Right now, the socket seems to read 1024 bytes, so
> naturally, when the connection closes, the socket throws an error,
> because it thinks that more bytes are coming. Which sounds rather
> weird to me, given the fact that in rare cases the reply will be a
> multiple of 1024 bytes :-)
>
> -- Andy Georges

For now, these changes helped me out. It probably (read: certainly)
needs some cleanup, and I'm probably not taking everything into
account as I should:

--- xmlrpclib.py2007-06-14 15:42:36.0 +0200
+++ /sw/lib/python2.5/xmlrpclib.py  2006-11-29 02:46:38.0
+0100
@@ -1184,11 +1184,6 @@

 errcode, errmsg, headers = h.getreply()

-expected_payload_length = 1024
-if headers.has_key('content-length'):
-  expected_payload_length = int(headers['content-length'])
-
-
 if errcode != 200:
 raise ProtocolError(
 host + handler,
@@ -1203,7 +1198,7 @@
 except AttributeError:
 sock = None

-return self._parse_response(h.getfile(), sock,
expected_payload_length)
+return self._parse_response(h.getfile(), sock)

 ##
 # Create parser.
@@ -1323,23 +1318,21 @@
 #could not be accessed).
 # @return Response tuple and target method.

-def _parse_response(self, file, sock, size=1024):
+def _parse_response(self, file, sock):
 # read response from input file/socket, and parse it

 p, u = self.getparser()

 while 1:
 if sock:
-response = sock.recv(size)
+response = sock.recv(1024)
 else:
-response = file.read(size)
+response = file.read(1024)
 if not response:
 break
 if self.verbose:
 print "body:", repr(response)
 p.feed(response)
-if len(response) == size:
-break

 file.close()
 p.close()


-- Andy Georges

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


Re: How to save python codes in files?

2007-06-14 Thread BartlebyScrivener
On Jun 13, 12:04 am, why? <[EMAIL PROTECTED]> wrote:
> Im working with Python 2.2 on my red hat linux system. Is there any
> way to write python codes in separate files and save them so that i
> can view/edit them in the future? Actually I've just started with
> python and would be grateful for a response. Thanx!

In addition to the help you've already received, you need:

http://wiki.python.org/moin/BeginnersGuide

It doesn't seem to be loading at the moment, but it will soon, I
suspect.

Otherwise, go to

http://tinyurl.com/w7wgp

Skip the installation instructions (for Windows) and go to the four
references and links under the image of the PythonWin interpreter.

HTH

rd

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


Re: Build EXE on Mac OsX 10.4

2007-06-14 Thread Paul McNett
Sherm Pendley wrote:
> "Gabriel Genellina" <[EMAIL PROTECTED]> writes:
> 
>> En Wed, 13 Jun 2007 17:35:19 -0300, Paul McNett <[EMAIL PROTECTED]> escribió:
>>
>>> Tempo wrote:
 Has anyone sucesfully built a *.exe file on a mac operating system
 before from a *.py file? I have been trying to do this with
 pyinstaller, but I keep getting errors and I don't know how to
 install  [...]
>>> You need to build Mac Apps on Mac, Windows EXE's on Windows, and Linux
>>> ELF's on Linux. You can't build a windows.exe from Mac, just as you
>>> can't build a mac.app from Windows.
>> That's not entirely true. gcc on linux can generate a Windows EXE, and
>> using: python setup.py bdist_wininst, you can generate a complete
>> binary  installer for Windows. I'm not sure if this can be done on a
>> Mac too.
> 
> In principle, certainly - there's even a MacPort package for a complete
> cygwin installation. I've built a number of packages with it - SDL and
> several related libraries, for instance. There are also ELF cross-compiler
> MacPort packages, presumably for building Linux binaries.
> 
> On the other hand, I *haven't* tried any of those compilers with setup.py,
> and I have no idea if it can support those targets in practice. :-(

There's the rub. In practice, it is hard enough getting my setup.py 
correct for building a nice windows.exe from Windows. Plus, the first 
thing I want to do after building an exe is *test* it on the target 
platform...

-- 
pkm ~ http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Method much slower than function?

2007-06-14 Thread Grant Edwards
On 2007-06-14, Leo Kislov <[EMAIL PROTECTED]> wrote:
> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>> Hi all,
>>
>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>> is substantially slower when in a method than in a function.
>>
>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 10.214 CPU seconds
>
>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>
>>  20004 function calls in 0.041 CPU seconds
>>
>
> I suspect open files are cached

They shouldn't be.

> so the second reader picks up where the first one left: at the
> of the file.

That sounds like a bug.  Opening a file a second time should
produce a "new" file object with the file-pointer at the
beginning of the file.

> The second call doesn't do any text processing at all.

-- 
Grant Edwards   grante Yow! I'm using my X-RAY
  at   VISION to obtain a rare
   visi.comglimpse of the INNER
   WORKINGS of this POTATO!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: one-time initialization of class members

2007-06-14 Thread James Turk
On Jun 13, 11:42 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> James Turk wrote:
> > It actually occured to me that I could use a @classmethod to do the
> > loading and take that out of the BaseClass constructor.  What I have
> > makes more sense and eliminates the unecessary constructors.
>
> > ie.
>
> > class BaseClass:
> > @classmethod
> > def loadData(params):
> > #expensive load here
>
> > class ChildClass1(BaseClass):
> >  dataset = BaseClass.loadData(params)
>
> > This is pretty much along the lines of what you suggested, thank you
> > for the hint in the right direction.
>
> > I realized that this still doesn't meet my needs exactly as I only
> > want the expensive dataset to be loaded if/when a class is actually
> > used (there are potentially many of these and only a few will be
> > used).
>
> Seems like you want a lazy class attribute. How about something like::
>
>  >>> class LazyClassAttribute(object):
> ... def __init__(self, func):
> ... self.func = func
> ... def __get__(self, obj, cls=None):
> ... value = self.func(cls)
> ... setattr(cls, self.func.__name__, value)
> ... return value
> ...
>  >>> class Base(object):
> ... @LazyClassAttribute
> ... def dataset(cls):
> ... print 'calculating dataset'
> ... return 'dataset(%s)' % cls.params
> ...
>  >>> class Child1(Base):
> ... params = 'foo'
> ...
>  >>> class Child2(Base):
> ... params = 'bar'
> ...
>  >>> Child1.dataset
> calculating dataset
> 'dataset(foo)'
>  >>> Child1.dataset
> 'dataset(foo)'
>  >>> Child2.dataset
> calculating dataset
> 'dataset(bar)'
>  >>> Child2.dataset
> 'dataset(bar)'
>
> The idea is basically similar to the @classmethod approach except that
> instead of @classmethod, we use a custom descriptor that calls the
> method the first time it's accessed and then stores that value
> afterwards. This means that instead of explicitly calling the
> @classmethod, the method will be called whenever the attribute is first
> accessed.
>
> STeVe

This is a pretty interesting idea, I hadn't thought of using a
decorator to get this behavior.  I'm evaluating it and will see if it
fits in with the rest of the system well, but it certainly is a unique
solution to this problem.

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


Re: Python-URL! - weekly Python news and links (Jun 11)

2007-06-14 Thread Alex Martelli
Gabriel Genellina <[EMAIL PROTECTED]> wrote:

> QOTW:  "That's the Martellibot for you.  Never use a word where a paragraph
> with explanatory footnotes will do.
> 
> Sigh.  I miss him on c.l.py." - Simon Brunning

Funny -- didn't Simon write this in 2005 referring to an essay of mine
that I had posted in 2002,
 ?

Longest "week" on record, unless I'm missing something!-)


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


Re: Method much slower than function?

2007-06-14 Thread Matimus
> The first time you read the file, it has to read it from disk.
> The second time, it's probably just reading from the buffer
> cache in RAM.

I can verify this type of behavior when reading large files. Opening
the file doesn't take long, but the first read will take a while
(multiple seconds depending on the size of the file). When the file is
opened a second time, the initial read takes significantly less time.

Matt

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


Re: Method much slower than function?

2007-06-14 Thread Diez B. Roggisch
Grant Edwards schrieb:
> On 2007-06-14, Leo Kislov <[EMAIL PROTECTED]> wrote:
>> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>>> Hi all,
>>>
>>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>>> is substantially slower when in a method than in a function.
>>>
>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>>  20004 function calls in 10.214 CPU seconds
>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>>  20004 function calls in 0.041 CPU seconds
>>>
>> I suspect open files are cached
> 
> They shouldn't be.
> 
>> so the second reader picks up where the first one left: at the
>> of the file.
> 
> That sounds like a bug.  Opening a file a second time should
> produce a "new" file object with the file-pointer at the
> beginning of the file.

It's a OS thing.

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


Re: for web application development

2007-06-14 Thread Evan Klitzke
On 6/14/07, james_027 <[EMAIL PROTECTED]> wrote:
> My problem is looking for a web framework for python, that could
> provide a natural way of developing web app. I am avoiding to learn
> template language as much as possible.

You should definitely reconsider avoiding templates -- it's hard to
imagine building a reasonably large webapp that didn't use them at
all. Cheetah templates are a really, really good Python framework for
writing templates, and the templates themselves that can use
Python-like constructs, which make them pretty powerful.

The standard way to do web programming in Python would of course be
using something that ties into mod_python, but WSGI[1] has been
brought into the language as of Python 2.5. Between Cheetah templates
and the WSGI modules, you can put together a simple lightweight web
framework in a couple hundred lines (or less) of Python. For my own
personal website I threw together a really simple Python "framework"
that runs as a daemon and uses WSGI and Cheetah templates, and it all
ended up being only 150 lines of code, so I think this is a fairly
good solution if you don't envision a huge/complex site. If you want
to build a a much bigger site you should obviously look into the other
frameworks that are available, particularly Django.

[1] http://www.python.org/dev/peps/pep-0333/

-- 
Evan Klitzke <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically generated runtime methods & reflection

2007-06-14 Thread Jay Loden


Josiah Carlson wrote:
>
> Ahh, so you want to pass the method name to the method that you are 
> returning to be called.  No problem.
> 
>  >>> import functools
>  >>>
>  >>> class foo:
> ... def __getattr__(self, name):
> ... return functools.partial(self.ActualMethod, name)
> ...
> ... def ActualMethod(self, name, *args, **kwargs):
> ... #handle *args and **kwargs based on name!
> ... print name, args, kwargs
> ...
>  >>> foo().bar('hello', world=1)
> bar ('hello',) {'world': 1}
>  >>>

Thanks, this is exactly what I was looking for! For some reason functools 
didn't even show up at all during Google searches...must have just had the 
wrong search terms.

-Jay

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


Moving items from list to list

2007-06-14 Thread HMS Surprise

Just wondered if there was some python idiom for moving a few items
from one list to another. I often need to delete 2 or 3 items from one
list and put them in another. Delete doesn't seem to have a return
value. I don't care which items I get so now I just use a couple of
pops or a for loop for more than two.

Thanks

jh

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


qt4 setFlags

2007-06-14 Thread luca72
Hello

I make this code:
row = self.tableWidget.rowCount()
for a in range(row):
self.tableWidget.item(row, 0).setFlags(Qt.IsSelectable)

i have this erroror :

global name Qt is not definied

Regards

Luca

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


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Peter Otten wrote:

> Leo Kislov wrote:
> 
>> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>>> Hi all,
>>>
>>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
>>> is substantially slower when in a method than in a function.
>>>
>>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>>>
>>>  20004 function calls in 10.214 CPU seconds
>> 
>>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>>>
>>>  20004 function calls in 0.041 CPU seconds
>>>
>> 
>> I suspect open files are cached so the second reader
>> picks up where the first one left: at the of the file.
>> The second call doesn't do any text processing at all.
>> 
>>   -- Leo
> 
> Indeed, the effect of attribute access is much smaller than what the OP is
> seeing:

I have to take that back
 
> $ cat iadd.py
> class A(object):
> def add_attr(self):
> self.x = 0
> for i in xrange(10):
> self.x += 1
> def add_local(self):
> x = 0
> for i in xrange(10):
> x += 1
> 
> add_local = A().add_local
> add_attr = A().add_attr
> $ python2.5 -m timeit -s 'from iadd import add_local' 'add_local()'
> 10 loops, best of 3: 21.6 msec per loop
> $ python2.5 -m timeit -s 'from iadd import add_attr' 'add_attr()'
> 10 loops, best of 3: 52.2 msec per loop

Iddo, adding integers is not a good model for the effect you are seeing.
Caching, while happening on the OS-level isn't, either.

As already mentioned in this thread there is a special optimization for

some_string += another_string

in the Python C-source. This optimization works by mutating on the C-level
the string that is immutable on the Python-level, and is limited to cases
where there are no other names referencing some_string. The statement

self.s += t

is executed internally as

tmp = self.s [1]
tmp += t [2]
self.s = tmp [3]

where tmp is not visible from the Python level. Unfortunately after [1]
there are two references to the string in question (tmp and self.s) so the
optimization cannot kick in.

Peter

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


Re: Method much slower than function?

2007-06-14 Thread Chris Mellon
On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
> Peter Otten wrote:
>
> > Leo Kislov wrote:
> >
> >> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
> >>> Hi all,
> >>>
> >>> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> >>> is substantially slower when in a method than in a function.
> >>>
> >>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
> >>>
> >>>  20004 function calls in 10.214 CPU seconds
> >>
> >>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
> >>>
> >>>  20004 function calls in 0.041 CPU seconds
> >>>
> >>
> >> I suspect open files are cached so the second reader
> >> picks up where the first one left: at the of the file.
> >> The second call doesn't do any text processing at all.
> >>
> >>   -- Leo
> >
> > Indeed, the effect of attribute access is much smaller than what the OP is
> > seeing:
>
> I have to take that back
>

Your tests (which I have snipped) show attribute access being about 3x
slower than local access, which is consistent with my own tests. The
OP is seeing a speed difference of 2 orders of magnitude. That's far
outside the range that attribute access should account for.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: In C extension .pyd, sizeof INT64 = 4?

2007-06-14 Thread Laurent Pointal
Allen wrote:

> On 6 13 ,   11 55 , "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
>> > I used INT64 and initialize its value from PyArg_ParseTuple.
>> > The code is PyArg_ParseTuple(args, "l", &nValue).
>> > It should be PyArg_ParseTuple(args, "L", &nValue).
>>
>> That's still incorrect. For the L format flag, use PY_LONG_LONG,
>> not your own INT64 type. More generally: always use the type
>> documented in
>>
>> http://docs.python.org/api/arg-parsing.html
>>
>> Regards,
>> Martin
> 
> PY_LONG_LONG is decleared as __int64 on windows. There is no
> difference.

IMHO. Make your code platform independant and use your compiler capacities. 

If for an 'L' argument PyArg_ParseTuple requires a PY_LONG_LONG, then give
it a PY_LONG_LONG and assign the parsed value into an INT64 after parsing.
 
You dont know if PY_LONG_LONG or INT64 wil not be defined differently in the
future, so use them where they are specified, do the assignment, and leave
the compiler warn you if anything become invalid in the futur.

My 2 cents.

A+

Laurent.

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

Re: Method much slower than function?

2007-06-14 Thread Steven D'Aprano
On Thu, 14 Jun 2007 00:40:12 +, idoerg wrote:


 cProfile.run("bar.readgenome(open('cb_foo'))")
>  20004 function calls in 10.214 CPU seconds

This calls the method on the CLASS, instead of an instance. When I try it,
I get this:

TypeError: unbound method readgenome() must be called with bar instance as
first argument (got file instance instead)

So you're running something subtly different than what you think you're
running. Maybe you assigned bar = bar() at some point?

However, having said that, the speed difference does seem to be real: even
when I correct the above issue, I get a large time difference using
either cProfile.run() or profile.run(), and timeit agrees:

>>> f = bar().readgenome
>>> timeit.Timer("f(open('cb_foo'))", "from __main__ import f").timeit(5)
18.515995025634766
>>> timeit.Timer("readgenome(open('cb_foo'))", "from __main__ import 
>>> readgenome").timeit(5)
0.1940619945526123

That's a difference of two orders of magnitude, and I can't see why.


-- 
Steven.

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


Re: Method much slower than function?

2007-06-14 Thread Grant Edwards
On 2007-06-14, Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> However, having said that, the speed difference does seem to be real: even
> when I correct the above issue, I get a large time difference using
> either cProfile.run() or profile.run(), and timeit agrees:
>
 f = bar().readgenome
 timeit.Timer("f(open('cb_foo'))", "from __main__ import f").timeit(5)
> 18.515995025634766
 timeit.Timer("readgenome(open('cb_foo'))", "from __main__ import 
 readgenome").timeit(5)
> 0.1940619945526123
>
> That's a difference of two orders of magnitude, and I can't see why.

Is it independent of the test order?

What happens when you reverse the order?

What happens if you run the same test twice in a row?

-- 
Grant Edwards   grante Yow! Thank god!! ... It's
  at   HENNY YOUNGMAN!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Moving items from list to list

2007-06-14 Thread Evan Klitzke
On 6/14/07, HMS Surprise <[EMAIL PROTECTED]> wrote:
>
> Just wondered if there was some python idiom for moving a few items
> from one list to another. I often need to delete 2 or 3 items from one
> list and put them in another. Delete doesn't seem to have a return
> value. I don't care which items I get so now I just use a couple of
> pops or a for loop for more than two.

I'm not sure if this is what you're asking, but if the elements in the
list are contiguous you can just use list slicing/addition, like this:

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

b = a[2:] + b
a = a[:2]

Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
5, 6, 7, 8, 9, 10].

-- 
Evan Klitzke <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


OS X install confusion

2007-06-14 Thread John Fisher
Hi Groupies,

I have an Intel Macbook running OS X 10.4.

It came installed with Python 2.3.5. I have since installed MacPython
with version 2.4.4, cool.

When I open a bash terminal session and type python, it brings up
version 2.3.5. If I type IDLE it brings up version 2.4.4. 

My question: what do I have to do to get it to bring up 2.4.4 with the
"python" command?

Thanks for bringing light to my ignorance.

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


Re: Is there any way to catch expections when call python method in C++

2007-06-14 Thread Gabriel Genellina
En Wed, 13 Jun 2007 12:03:00 -0300, Robert Bauck Hamar  
<[EMAIL PROTECTED]> escribió:

> Allen wrote:
>
>> I use try catch, but cannot catch the execeptions of execution python
>> method.
>
> No. CPython is written in C, not C++, and C has no concept of exceptions.
> Exceptions in Python is usually indicated by return value in the
> interpreter, and has no mapping to the C++ exception model. You should
> never let C++ exceptions propagate into the python functions either.
> PyImport_ImportModule will return NULL if an exception occured, and so  
> will
> also PyObject_GetAttrString and PyEval_CallObject do.

For an example on how to do this, see "Extending and Embedding the Python  
Interpreter"  specially section 1.2

-- 
Gabriel Genellina

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


FTP Date Format Function

2007-06-14 Thread samuraisam
FTP LST/LIST/NLST date field formatting function for all those seekers
out there...

import time
import datetime

def ftpdateformat(value):
"""Formats dates from most FTP servers"""
if ":" in value: # within 6 months
return datetime.datetime(
*time.strptime( # have to guess this calculation
"%s %s" % (value, datetime.datetime.now().year),
"%b %d %H:%M %Y"
)[0:5]
).strftime("%B %d, %Y %H:%M")
else: # before six months
return datetime.datetime(
*time.strptime(value, "%b %d %Y")[0:5]
).strftime("%B %d, %Y")

I'm not sure if there is a proper algorithm for deciding on a proper
year within the last 6 months as it isn't given by most FTP servers.
I'd love to amend the function with the correct solution. :)

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


Re: Method much slower than function?

2007-06-14 Thread Steven D'Aprano
On Thu, 14 Jun 2007 00:40:12 +, idoerg wrote:

> Hi all,
> 
> I am running Python 2.5 on Feisty Ubuntu. I came across some code that
> is substantially slower when in a method than in a function.


After further testing, I think I have found the cause of the speed
difference -- and it isn't that the code is a method.

Here's my test code:


def readgenome(filehandle):
s = ""
for line in filehandle.xreadlines():
s += line.strip()

class SlowClass:
def readgenome(self, filehandle):
self.s = ""
for line in filehandle.xreadlines():
self.s += line.strip()

class FastClass:
def readgenome(self, filehandle):
s = ""
for line in filehandle.xreadlines():
s += line.strip()
self.s = s


Now I test them. For brevity, I am leaving out the verbose profiling
output, and just showing the total function calls and CPU time.


>>> import cProfile
>>> cProfile.run("readgenome(open('cb_foo'))")
 20005 function calls in 0.071 CPU seconds

>>> cProfile.run("SlowClass().readgenome(open('cb_foo'))")
 20005 function calls in 4.030 CPU seconds

>>> cProfile.run("FastClass().readgenome(open('cb_foo'))")
 20005 function calls in 0.077 CPU seconds


So you can see that the slow-down for calling a method (compared to a
function) is very small.

I think what we're seeing in the SlowClass case is the "normal" speed of
repeated string concatenations. That's REALLY slow. In the function and
FastClass cases, the compiler optimization is able to optimize that slow
behaviour away.

So, nothing to do with methods vs. functions, and everything to do with
the O(N**2) behaviour of repeated string concatenation.


-- 
Steven.

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


Re: OS X install confusion

2007-06-14 Thread Kevin Walzer
John Fisher wrote:
> Hi Groupies,
> 
> I have an Intel Macbook running OS X 10.4.
> 
> It came installed with Python 2.3.5. I have since installed MacPython
> with version 2.4.4, cool.
> 
> When I open a bash terminal session and type python, it brings up
> version 2.3.5. If I type IDLE it brings up version 2.4.4. 
> 
> My question: what do I have to do to get it to bring up 2.4.4 with the
> "python" command?
> 
> Thanks for bringing light to my ignorance.
> 
> JF

Sounds like a path problem. Apple's system Python is installed in 
/usr/bin. Your installation is probably in /usr/local/bin. Edit your 
profile or use the full path.

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


Re: Method much slower than function?

2007-06-14 Thread Peter Otten
Chris Mellon wrote:

> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Peter Otten wrote:
>>
>> > Leo Kislov wrote:
>> >
>> >> On Jun 13, 5:40 pm, [EMAIL PROTECTED] wrote:
>> >>> Hi all,
>> >>>
>> >>> I am running Python 2.5 on Feisty Ubuntu. I came across some code
>> >>> that is substantially slower when in a method than in a function.
>> >>>
>> >>> >>> cProfile.run("bar.readgenome(open('cb_foo'))")
>> >>>
>> >>>  20004 function calls in 10.214 CPU seconds
>> >>
>> >>> >>> cProfile.run("z=r.readgenome(open('cb_foo'))")
>> >>>
>> >>>  20004 function calls in 0.041 CPU seconds
>> >>>
>> >>
>> >> I suspect open files are cached so the second reader
>> >> picks up where the first one left: at the of the file.
>> >> The second call doesn't do any text processing at all.
>> >>
>> >>   -- Leo
>> >
>> > Indeed, the effect of attribute access is much smaller than what the OP
>> > is seeing:
>>
>> I have to take that back
>>
> 
> Your tests (which I have snipped) show attribute access being about 3x
> slower than local access, which is consistent with my own tests. The
> OP is seeing a speed difference of 2 orders of magnitude. That's far
> outside the range that attribute access should account for.

Not if it conspires to defeat an optimization for string concatenation

$ cat iadd.py
class A(object):
def add_attr(self):
self.x = ""
for i in xrange(1):
self.x += " yadda"
def add_local(self):
x = ""
for i in xrange(1):
x += " yadda"

add_local = A().add_local
add_attr = A().add_attr
$ python2.5 -m timeit -s'from iadd import add_local' 'add_local()'
100 loops, best of 3: 3.15 msec per loop
$ python2.5 -m timeit -s'from iadd import add_attr' 'add_attr()'
10 loops, best of 3: 83.3 msec per loop

As the length of self.x grows performance will continue to degrade.
The original test is worthless as I tried to explain in the section you
snipped.

Peter

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


Re: OS X install confusion

2007-06-14 Thread Ted
On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> John Fisher wrote:
> > Hi Groupies,
>
> > I have an Intel Macbook running OS X 10.4.
>
> > It came installed with Python 2.3.5. I have since installed MacPython
> > with version 2.4.4, cool.
>
> > When I open a bash terminal session and type python, it brings up
> > version 2.3.5. If I type IDLE it brings up version 2.4.4.
>
> > My question: what do I have to do to get it to bring up 2.4.4 with the
> > "python" command?
>
> > Thanks for bringing light to my ignorance.
>
> > JF
>
> Sounds like a path problem. Apple's system Python is installed in
> /usr/bin. Your installation is probably in /usr/local/bin. Edit your
> profile or use the full path.
>
> --
> Kevin Walzer
> Code by Kevinhttp://www.codebykevin.com

The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
and /usr/bin/pythonw.

I found it easier to relink to the new installation path. This also
leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
original version if you want to quickly check something.

Cheers,
Ted

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


lagrange multipliers in python

2007-06-14 Thread [EMAIL PROTECTED]
Hi all,

Sorry for the cross-posting.

I'm trying to find the minimum of a multivariate function F(x1, x2, ...,
xn) subject to multiple constraints G1(x1, x2, ..., xn) = 0, G2(...) =
0, ..., Gm(...) = 0.

The conventional way is to construct a dummy function Q,

$$Q(X, \Lambda) = F(X) + \lambda_1 G1(X) + \lambda_2 G2(X) + ... + \lambda_m 
Gm(X)$$

and then calculate the value of X and \Lambda when the gradient of function Q 
equals 0.

I think this is a routine work, so I want to know if there are available
functions in python(mainly scipy) to do this? Or maybe there is already
a better way in python?

I have googled but haven't found helpful pages.

Thanks a lot.

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


Re: qt4 setFlags

2007-06-14 Thread Diez B. Roggisch
luca72 schrieb:
> Hello
> 
> I make this code:
> row = self.tableWidget.rowCount()
> for a in range(row):
> self.tableWidget.item(row, 0).setFlags(Qt.IsSelectable)
> 
> i have this erroror :
> 
> global name Qt is not definied

import Qt might help. And reading the python tutorial.

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


Re: Method much slower than function?

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:12 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 14 Jun 2007 01:39:29 -0300, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> escribió:
>
>
>
> > Gabriel Genellina wrote:
> >> In addition, += is rather inefficient for strings; the usual idiom is
> >> using ''.join(items)
>
> > Ehh.  Python 2.5 (and probably some earlier versions) optimize += on
> > strings pretty well.
>
> > a=""
> > for i in xrange(10):
> > a+="a"
>
> > and:
>
> > a=[]
> > for i in xrange(10):
> > a.append("a")
> > a="".join(a)
>
> > take virtually the same amount of time on my machine (2.5), and the
> > non-join version is clearer, IMO.  I'd still use join in case I wind
> > up running under an older Python, but it's probably not a big issue
> > here.
>
> Yes, for concatenating a lot of a's, sure... Try again using strings
> around the size of your expected lines - and make sure they are all
> different too.
>
> py> import timeit
> py>
> py> def f1():
> ...   a=""
> ...   for i in xrange(10):
> ...   a+=str(i)*20
> ...
> py> def f2():
> ...   a=[]
> ...   for i in xrange(10):
> ...   a.append(str(i)*20)
> ...   a="".join(a)
> ...
> py> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
> [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
> py> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
>
> ...after a few minutes I aborted the process...

Are you using an old version of python?  I get a fairly small
difference between the 2:

Python 2.5 (r25:51908, Jan 23 2007, 18:42:39)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on ELIDED
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> a=""
>>> def f1():
...   a=""
...   for i in xrange(10):
...  a+=str(i)*20
...
>>> def f2():
...   a=[]
...   for i in xrange(10):
... a.append(str(i)*20)
...   a="".join(a)
...
>>> print timeit.Timer("f2()", "from __main__ import f2").repeat(number=1)
[0.91355299949645996, 0.86561012268066406, 0.84371185302734375]
>>> print timeit.Timer("f1()", "from __main__ import f1").repeat(number=1)
[0.94637894630432129, 0.89946198463439941, 1.170320987701416]

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

Re: SimplePrograms challenge

2007-06-14 Thread Steven Bethard
rzed wrote:
> Steven Bethard <[EMAIL PROTECTED]> wrote in
>> def iter_primes():
>>  # an iterator of all numbers between 2 and +infinity
>>  numbers = itertools.count(2)
>>
>>  # generate primes forever
>>  while True
>>
>>  # generate the first number from the iterator,
>>  # which should always be a prime
>>  prime = numbers.next()
>>  yield prime
>>
>>  # lazily remove all numbers from the iterator that
>>  # are divisible by prime we just selected
>>  numbers = itertools.ifilter(prime.__rmod__, numbers)
>>
>> I think that's 17-ish, though you could shrink it down by
>> removing some of the spaces.
> 
> How about including a driver?

Yes, absolutely a good idea. Fortunately, the other Steve borrowed the 
time machine already and added this to the end::

 for p in iter_primes():
 if p > 1000: break
 print p

http://wiki.python.org/moin/SimplePrograms

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


Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread py_genetic
Hi,

I'm looking to generate x alphabetic strings in a list size x.  This
is exactly the same output that the unix command "split" generates as
default file name output when splitting large files.

Example:

produce x original, but not random strings from english alphabet, all
lowercase.  The length of each string and possible combinations is
dependent on x.  You don't want any repeats.

[aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]

I'm assumming there is a slick, pythonic way of doing this, besides
writing out a beast of a looping function.  I've looked around on
activestate cookbook, but have come up empty handed.  Any suggestions?

Thanks,
Conor

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


Re: Method much slower than function?

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:10 am, Paul Rubin  wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > take virtually the same amount of time on my machine (2.5), and the
> > non-join version is clearer, IMO.  I'd still use join in case I wind
> > up running under an older Python, but it's probably not a big issue here.
>
> You should not rely on using 2.5

I use generator expressions and passed-in values to generators and
other features of 2.5.  Whether or not to rely on a new version is
really a judgement call based on how much time/effort/money the new
features save you vs. the cost of losing portability to older
versions.

> or even on that optimization staying in CPython.

You also shouldn't count on dicts being O(1) on lookup, or "i in
myDict" being faster than "i in myList".  A lot of quality of
implementation issues outside of the language specification have to be
considered when you're worried about running time.

Unlike fast dictionary lookup at least the += optimization in CPython
is specified in the docs (as well as noting that "".join is greatly
preferred if you're working across different versions and
implementations).

> Best is to use StringIO or something comparable.

Yes, or the join() variant.

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


Re: How can I capture all exceptions especially when os.system() fail? Thanks

2007-06-14 Thread mike
On Jun 14, 2:55 am, Michael Hoffman <[EMAIL PROTECTED]> wrote:
> Gabriel Genellina wrote:
> > En Wed, 13 Jun 2007 21:47:16 -0300, mike <[EMAIL PROTECTED]> escribió:
>
> >> Following piece of code can capture IOError when the file doesn't
> >> exist, also, other unknown exceptions can be captured when I press
> >> Ctrl-C while the program is sleeping(time.sleep). Now the question is:
> >> when I run the non-exist command, the exception cannot be captured.
>
> >> So far so good, then I changed the code to run a non-exist command
> >> "wrong_command_test"(commented the open and sleep lines), then the
> >> script printed:
> >> sh: wrong_command_test: command not found
> >> well Done
>
> > That's because it is not an exception, it is an error message coming
> > from your shell, not from Python.
>
> Of course if you use subprocess.check_call() instead of os.system(), it
> will become an exception (CalledProcessError).
> --
> Michael Hoffman

Really helps. Thanks Michael

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

Re: save class

2007-06-14 Thread nik
On Jun 13, 10:04 pm, Josiah Carlson <[EMAIL PROTECTED]>
wrote:
> Gabriel Genellina wrote:
> > En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:
> >> It would seem that I want to actually save the source code for the
> >> class. I know that I could of course open up an editor and just make
> >> it, but my ideal would be to have the base class, Map, be able to make
> >> the sub-classes. I don't want the class definition. What I want is an
> >> actual class that I could later import and use somewhere else. I am
> >> planning to have each one of these map objects contain a different
> >> dictionary and then be able to import the map into the application,
> >> but have certain methods defined in the Map super-class to draw data
> >> out of the specific map's specific dictionary. I hope that makes
> >> sense.
>
> >> Something like,
> >> class Map:
> >>  dict = {}
> >>  def DoSomething(self):
> >>  pass
>
> >>  def MakeNewMapSubClass(self, newclassname):
> >>  """ make a new file, newclassname.py that contains a new
> >> class
> >>  newclassname(Map) that inherits from base-class Map.
>
> > And are you sure you actually need different subclasses? Will you
> > construct them several instances of each subclass? From the above
> > description I feel you want just different Map *instances*, each with
> > its own dict, not different *subclasses*.
>
> What you said, and that his solution sounds like a Java approach to the
> problem (subclass an abstract base class that calls specific methods on
> the subclass to "do the right thing").
>
> To offer the OP source he can use...
>
> class Map:
>  def __init__(self):
>  self.dict = {}
>  def DoSomething(self):
>  #do something with self.dict
>
> Every instance gets a new dictionary.  Now, if he actually wants to
> change the behavior of the DoSomething method, of course then it would
> make sense to subclass Map.
>
>   - Josiah

I am hoping to change the self.dict for each subclass. I realize that
I could save self.dict to file and then load in different dicts each
time I get a new instance of class. But I want to be able to make
subclasses of map that each have different self.dict. Then when I need
to use them, just import the module and use the specific dict, instead
of having to keep track of a separate dictionary file. I am new to
this, but I thought that this would be a regular thing to do in
python, because people must make classes in the interactive console
and then export them somehow for later use.

Thank you for your responses.

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

Re: dynamically generated runtime methods & reflection

2007-06-14 Thread Josiah Carlson
Jay Loden wrote:
> Josiah Carlson wrote:
>> Ahh, so you want to pass the method name to the method that you are 
>> returning to be called.  No problem.
>>
>>  >>> import functools
>>  >>>
>>  >>> class foo:
>> ... def __getattr__(self, name):
>> ... return functools.partial(self.ActualMethod, name)
>> ...
>> ... def ActualMethod(self, name, *args, **kwargs):
>> ... #handle *args and **kwargs based on name!
>> ... print name, args, kwargs
>> ...
>>  >>> foo().bar('hello', world=1)
>> bar ('hello',) {'world': 1}
>>  >>>
> 
> Thanks, this is exactly what I was looking for! For some reason functools 
> didn't even show up at all during Google searches...must have just had the 
> wrong search terms.

Well, the particular operation is typically called 'currying a 
function', and unless you know what to look for, it isn't very easy to 
make happen.

On the other hand, it is also relatively easy to implement by hand if 
necessary.

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


Re: Method much slower than function?

2007-06-14 Thread Josiah Carlson
[EMAIL PROTECTED] wrote:
> On Jun 14, 1:10 am, Paul Rubin  wrote:
>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
>>> take virtually the same amount of time on my machine (2.5), and the
>>> non-join version is clearer, IMO.  I'd still use join in case I wind
>>> up running under an older Python, but it's probably not a big issue here.
>> You should not rely on using 2.5
> 
> I use generator expressions and passed-in values to generators and
> other features of 2.5.

For reference, generator expressions are a 2.4 feature.

>> or even on that optimization staying in CPython.
> 
> You also shouldn't count on dicts being O(1) on lookup, or "i in
> myDict" being faster than "i in myList".

Python dictionaries (and most decent hash table implementations) may not 
be O(1) technically, but they are expected O(1) and perform O(1) in 
practice (at least for the Python implementations).  If you have 
particular inputs that force Python dictionaries to perform poorly (or 
as slow as 'i in lst' for large dictionaries and lists), then you should 
post a bug report in the sourceforge tracker.


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


Re: Moving items from list to list

2007-06-14 Thread HMS Surprise
Thanks.

That will work. The 2nd, smaller lst starts out empty but this is
easily adapted.

jh

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


Re: Method much slower than function?

2007-06-14 Thread Josiah Carlson
Francesco Guerrieri wrote:
> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Gabriel Genellina wrote:
>> > ...
>> > py> print timeit.Timer("f2()", "from __main__ import 
>> f2").repeat(number=1)
>> > [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
>> > py> print timeit.Timer("f1()", "from __main__ import 
>> f1").repeat(number=1)
>> >
>> > ...after a few minutes I aborted the process...
>>
>> I can't confirm this.
> 
> [...]
> 
>> $ python2.5 -m timeit -s 'from join import f1' 'f1()'
>> 10 loops, best of 3: 212 msec per loop
>> $ python2.5 -m timeit -s 'from join import f2' 'f2()'
>> 10 loops, best of 3: 259 msec per loop
>> $ python2.5 -m timeit -s 'from join import f3' 'f3()'
>> 10 loops, best of 3: 236 msec per loop
> 
> On my machine (using python 2.5 under win xp) the results are:
 print timeit.Timer("f2()", "from __main__ import f2").repeat(number 
 = 1)
> [0.19726834822823575, 0.19324697456408974, 0.19474492594212861]
 print timeit.Timer("f1()", "from __main__ import f1").repeat(number 
 = 1)
> [21.982707133304167, 21.905312587963252, 22.843430035622767]
> 
> so it seems that there is a rather sensible difference.
> what's the reason of the apparent inconsistency with Peter's test?

It sounds like a platform memory resize difference.


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


a_list.count(a_callable) ?

2007-06-14 Thread Ping
Hi,

I'm wondering if it is useful to extend the count() method of a list
to accept a callable object?  What it does should be quite intuitive:
count the number of items that the callable returns True or anything
logically equivalent (non-empty sequence, non-zero number, etc).

This would return the same result as len(filter(a_callable, a_list)),
but without constructing an intermediate list which is thrown away
after len() is done.

This would also be equivalent to
n = 0
for i in a_list:
if a_callable(i):  n += 1
but with much shorter and easier-to-read code.  It would also run
faster.

This is my first post and please bear with me if I'm not posting it in
the right way.

Regards,
Ping

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 1:41 pm, py_genetic <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm looking to generate x alphabetic strings in a list size x.  This
> is exactly the same output that the unix command "split" generates as
> default file name output when splitting large files.
>
> Example:
>
> produce x original, but not random strings from english alphabet, all
> lowercase.  The length of each string and possible combinations is
> dependent on x.  You don't want any repeats.
>
> [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> I'm assumming there is a slick, pythonic way of doing this, besides
> writing out a beast of a looping function.  I've looked around on
> activestate cookbook, but have come up empty handed.  Any suggestions?

If you allow numbers also, you can use Base 36:

>>> import gmpy
>>> int('aaa',36)
13330
>>> for n in xrange(13330,13330+1000):
print gmpy.digits(n,36),

aaa aab aac aad aae aaf aag aah aai aaj aak aal
aam aan aao aap aaq aar aas aat aau aav aaw aax
aay aaz ab0 ab1 ab2 ab3 ab4 ab5 ab6 ab7 ab8 ab9
aba abb abc abd abe abf abg abh abi abj abk abl
abm abn abo abp abq abr abs abt abu abv abw abx
aby abz ac0 ac1 ac2 ac3 ac4 ac5 ac6 ac7 ac8 ac9
aca acb acc acd ace acf acg ach aci acj ack acl
acm acn aco acp acq acr acs act acu acv acw acx
acy acz ad0 ad1 ad2 ad3 ad4 ad5 ad6 ad7 ad8 ad9
ada adb adc add ade adf adg adh adi adj adk adl
adm adn ado adp adq adr ads adt adu adv adw adx
ady adz ae0 ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 ae9
aea aeb aec aed aee aef aeg aeh aei aej aek ael
aem aen aeo aep ...


>
> Thanks,
> Conor


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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread Rob Wolfe
py_genetic <[EMAIL PROTECTED]> writes:

> Hi,
>
> I'm looking to generate x alphabetic strings in a list size x.  This
> is exactly the same output that the unix command "split" generates as
> default file name output when splitting large files.
>
> Example:
>
> produce x original, but not random strings from english alphabet, all
> lowercase.  The length of each string and possible combinations is
> dependent on x.  You don't want any repeats.
>
> [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> I'm assumming there is a slick, pythonic way of doing this, besides
> writing out a beast of a looping function.  I've looked around on
> activestate cookbook, but have come up empty handed.  Any suggestions?

You didn't try hard enough. :)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465

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


Convert to C/C++?

2007-06-14 Thread SpreadTooThin
I am wondering if someone who knows the implemention of python's time
could help converting this to c/c++

nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between 
the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01
00:00:00.
self.timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
self.clock_seq = random.randrange(1<<14L) # instead of stable
storage
self.time_low = self.timestamp & 0xL
self.time_mid = (self.timestamp >> 32L) & 0xL
self.time_hi_version = (self.timestamp >> 48L) & 0x0fffL
self.clock_seq_low = self.clock_seq & 0xffL
self.clock_seq_hi_variant = (self.clock_seq >> 8L) & 0x3fL
#print 'timestamp ', self.timestamp, self.time_low, 
self.time_mid,
self.time_hi_version
#print 'clock_seq ', self.clock_seq, self.clock_seq_low,
self.clock_seq_hi_variant

vs unix gettimeofday

int gettimeofday(struct timeval *tp, struct timezone *tzp);

struct timeval {
   long tv_sec; /* seconds since Jan. 1, 1970 */
   long tv_usec;/* and microseconds */
};

struct timezone {
   int tz_minuteswest; /* of Greenwich */
   int tz_dsttime; /* type of dst correction to apply */
};

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


Re: Moving items from list to list

2007-06-14 Thread George Sakkis
On Jun 14, 12:30 pm, HMS Surprise <[EMAIL PROTECTED]> wrote:

> Just wondered if there was some python idiom for moving a few items
> from one list to another. I often need to delete 2 or 3 items from one
> list and put them in another. Delete doesn't seem to have a return
> value. I don't care which items I get so now I just use a couple of
> pops or a for loop for more than two.
>
> Thanks
>
> jh

>>> x = range(10)
>>> y = []

>>> y.append(x.pop(4))
>>> print x, y
[0, 1, 2, 3, 5, 6, 7, 8, 9] [4]

>>> y.append(x.pop(7))
>>> print x, y
[0, 1, 2, 3, 5, 6, 7, 9] [4, 8]


HTH,
George

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


Proper licensing and copyright attribution for extracted Python code

2007-06-14 Thread Douglas Alan
Hi.  I extracted getpath.c out of Python and modified it to make a
generally useful facility for C and C++ programming.  These comments
are at the top of my .c file, and I would like to know if they pass
muster for meeting licensing, copyright, and aesthetics requirements:

// -*- Mode: C; fill-column: 79 -*-

//=
// Description:
//
//  pathToExecutable.c is a module that allows a Unix program to find the
//  location of its executable.  This capability is extremely useful for
//  writing programs that don't have to recompiled in order to be relocated
//  within the filesystem.  Any auxiliary files (dynamically loaded
//  libraries, help files, configuration files, etc.) can just be placed in
//  the same directory as the executable, and the function
//  pathToExecutable() can be used by the program at runtime to locate its
//  executable file and from there the program can locate any auxiliary
//  files it needs in order to operate.
//
//  pathToExecutable() is smart enough to follow a symlink (or even a chain
//  of symlinks) in order to find the true location of the executable.  In
//  this manner, for instance, you might install all of the files used by a
//  program (let's say it's called "my-program"), including the executable,
//  into the directory /usr/local/lib/my-program, and then put a symlink
//  into /usr/local/bin that points to the executable
//  /usr/local/lib/my-program/my-program.  Initially pathToExecutable()
//  will identify /usr/local/bin/my-program as the executable, but it will
//  then notice that this "file" is really a symbolic link.
//  pathToExecutable() will then follow the symbolic link and return
//  "/usr/local/lib/my-program/my-pogram" instead.
//
//  Before a program can call pathToExecutable(), setArgv() must be called
//  (canonically in main()) so that pathToExecutable() can fetch the value
//  of argv[0] and use it to help figure out where the executable is
//  located.
//
// Copyright and licensing information:
//
//  This software is a heavily modified version of getpath.c from the
//  Python 2.5.1 release.  Both this software and the original software
//  from which it is derived are freely distributable under the terms of
//  the permissive freeware license, Python Software Foundation License
//  Version 2.  You can read more about this license here:
//
//   http://www.python.org/psf/license
//
//  The original software from which this software is derived carries the
//  following copyright:
//
// Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python
// Software Foundation.
//
//  The modifications to the original software, which are contained herein,
//  are
//
// Copyright (c) 2007 Douglas Alan 
//
//=

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


python i2c ioctl

2007-06-14 Thread luca

Hi,
 I was trying to make to work directly l i2c with python with the
calls ioctl. But I have of the problems and I do not succeed to go
ahead.

this and l error

[EMAIL PROTECTED] /usr/local/wrap]129# python pcf8591_ioctl.py
Reading from 4 ch 8 bit A/D converter PCF8591
Traceback (most recent call last):
  File "pcf8591_ioctl.py", line 269, in ?
main()
  File "pcf8591_ioctl.py", line 229, in main
if i2c_open () < 0:
  File "pcf8591_ioctl.py", line 168, in i2c_open
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), 0,
iomask)
TypeError: ioctl requires a file or file descriptor, an integer and
optionally a integer or buffer argument

the code

import fcntl
import os
###
#etrax.h
ETRAXGPIO_IOCTYPE = 43
GPIO_MINOR_A = 0
GPIO_MINOR_B = 1
GPIO_MINOR_LEDS = 2
GPIO_MINOR_G = 3
GPIO_MINOR_LAST = 3
IO_READBITS = 0x1
IO_SETBITS =  0x2
IO_CLRBITS =  0x3
IO_HIGHALARM = 0x4
IO_LOWALARM = 0x5
IO_CLRALARM = 0x6
IO_LEDACTIVE_SET = 0x7
IO_READDIR  =  0x8
IO_SETINPUT =  0x9
IO_SETOUTPUT = 0xA
IO_LED_SETBIT = 0xB
IO_LED_CLRBIT = 0xC
IO_SHUTDOWN  = 0xD
IO_GET_PWR_BT = 0xE
IO_CFG_WRITE_MODE = 0xF

def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )

IO_READ_INBITS =  0x10
IO_READ_OUTBITS = 0x11
IO_SETGET_INPUT = 0x12
IO_SETGET_OUTPUT = 0x13
###
###
#ioctl.h

_IOC_NRBITS = 8
_IOC_TYPEBITS=  8
_IOC_SIZEBITS   =14
_IOC_DIRBITS = 2
_IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
_IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
_IOC_SIZEMASK   =((1 << _IOC_SIZEBITS)-1)
_IOC_DIRMASK=((1 << _IOC_DIRBITS)-1)
_IOC_NRSHIFT=0
_IOC_TYPESHIFT  =(_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT  =(_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT   =(_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE=0
_IOC_WRITE=1
_IOC_READ=2
def _IOC(dir,type,nr,size):
(((dir)  << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr)   << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT))

def _IO(type,nr):
_IOC(_IOC_NONE,(type),(nr),0)

def _IOR(type,nr,size):
_IOC(_IOC_READ,(type),(nr),sizeof(size))

def _IOW(type,nr,size):
_IOC(_IOC_WRITE,(type),(nr),sizeof(size))

def _IOWR(type,nr,size):
_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))

def _IOC_DIR(nr):
(((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
def _IOC_TYPE(nr):
(((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
def _IOC_NR(nr):
(((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
def _IOC_SIZE(nr):
(((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
def IOC_IN():
(_IOC_WRITE << _IOC_DIRSHIFT)

def IOC_OUT():
(_IOC_READ << _IOC_DIRSHIFT)
def IOC_INOUT():
((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
def IOCSIZE_MASK():
(_IOC_SIZEMASK << _IOC_SIZESHIFT)
def IOCSIZE_SHIFT():
(_IOC_SIZESHIFT)
##
##
##



I2C_DATA_LINE  = 1<<24
I2C_CLOCK_LINE = 1<<25


#Get the SDA line state
def i2c_getbit():
value=fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS))
if ((value&(I2C_DATA_LINE))==0):
return 0
else:
return 1

#Set the SDA line as output
def i2c_dir_out():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), iomask)

#Set the SDA line as input
def i2c_dir_in():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_INPUT), iomask)

#Set the SDA line state
def i2c_data(state):
if (state==1):
i2c_dir_in()
else:
i2c_dir_out()
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS),
I2C_DATA_LINE)
#Set the SCL line state
def i2c_clk(state):
if (state==1):
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), 
I2C_CLOCK_LINE)
else:
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS), 
I2C_CLOCK_LINE)

#Read a byte from I2C bus and send the ack sequence
#Put islast = 1 is this is the last byte to receive from the slave

def i2c_inbyte(islast):
value = 0

#Read data byte

i2c_clk(0)
i2c_dir_in()
for i in range (0,5):
i2c_clk(1)
bitvalue = i2c_getbit()
value |= bitvalue
if (i<7):
value = 1
i2c_clk(0)

if (islast==0):
#Send Ack if is not the last byte to read
i2c_dir_out()
i2c_data(0)
i2c_clk(1)
i2c_clk(0)
i2c_dir_in()
else:
#Doesn't send Ack if is the last byte to read
i2c_dir_in()
   

os.path.normpath bug?

2007-06-14 Thread billiejoex
Hi there,
I've noticed that os.path.normpath does not collapse redundant
separators if they're located at the beginning of the string:

>>> print os.path.normpath('/a//b//c')
\a\b\c
>>> print os.path.normpath('//a//b//c')
\\a\b\c

Is it intentional or is it a bug?

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


Re: os.path.normpath bug?

2007-06-14 Thread Michael Hoffman
billiejoex wrote:
> Hi there,
> I've noticed that os.path.normpath does not collapse redundant
> separators if they're located at the beginning of the string:
> 
 print os.path.normpath('/a//b//c')
> \a\b\c
 print os.path.normpath('//a//b//c')
> \\a\b\c
> 
> Is it intentional or is it a bug?

Intentional.

http://en.wikipedia.org/wiki/Path_(computing)#Universal_Naming_Convention
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 2:53 pm, Ping <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm wondering if it is useful to extend the count() method of a list
> to accept a callable object?  What it does should be quite intuitive:
> count the number of items that the callable returns True or anything
> logically equivalent (non-empty sequence, non-zero number, etc).
>
> This would return the same result as len(filter(a_callable, a_list)),

map and filter are basically obsolete after the introduction of list
comprehensions; your expression is equivalent to:
len([i for i in a_list if a_callable(i)])

Which can then be converted into a generator expression (round
brackets instead of square brackets) to avoid the intermediate list:
len((i for i in a_list if a_callable(i)))

Or syntactically equivalent (avoiding lispy brackets):
len(i for i in a_list if a_callable(i))

> but without constructing an intermediate list which is thrown away
> after len() is done.
>
> This would also be equivalent to
> n = 0
> for i in a_list:
> if a_callable(i):  n += 1
> but with much shorter and easier-to-read code.  It would also run
> faster.
>
> This is my first post and please bear with me if I'm not posting it in
> the right way.
>
> Regards,
> Ping


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


Serialization across languages?

2007-06-14 Thread Tobiah
I want to do SOAP like calls from a device who's libraries
don't include SOAP.  I'm thinking of using simple HTTP posts,
but I'm going to want to send arrays and hashes.

First, what do I need to be aware of when sending arbitrary
data by a POST, and Second, is there a universally supported
version of what python can do with pickle?  I mostly need
python and PHP, but perl would be nice too.

Thanks,

Toby

-- 
Posted via a free Usenet account from http://www.teranews.com

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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> map and filter are basically obsolete after the introduction of list
> comprehensions

It is probably worth noting that list comprehensions do not require
that you write a new function; they take any expression where
appropriate. For more information on list comprehensions, see:

http://docs.python.org/tut/node7.html#SECTION00714

Generator expressions are the same, except syntactically they have
round brackets instead of square, and they return a generator instead
of a list, which allows for lazy evaluation.

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


Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Talbot Katz
Greetings Pythoners!

I hope you'll indulge an ignorant outsider.  I work at a financial software 
firm, and the tool I currently use for my research is R, a software 
environment for statistical computing and graphics.  R is designed with 
matrix manipulation in mind, and it's very easy to do regression and time 
series modeling, and to plot the results and test hypotheses.  The kinds of 
functionality we rely on the most are standard and robust versions of 
regression and principal component / factor analysis, bayesian methods such 
as Gibbs sampling and shrinkage, and optimization by linear, quadratic, 
newtonian / nonlinear, and genetic programming; frequently used graphics 
include QQ plots and histograms.  In R, these procedures are all available 
as functions (some of them are in auxiliary libraries that don't come with 
the standard distribution, but are easily downloaded from a central 
repository).

For a variety of reasons, the research group is considering adopting Python. 
  Naturally, I am curious about the mathematical, statistical, and graphical 
functionality available in Python.  Do any of you out there use Python in 
financial research, or other intense mathematical/statistical computation?  
Can you compare working in Python with working in a package like R or S-Plus 
or Matlab, etc.?  Which of the procedures I mentioned above are available in 
Python?  I appreciate any insight you can provide.  Thanks!

--  TMK  --
212-460-5430home
917-656-5351cell


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


Re: Serialization across languages?

2007-06-14 Thread Martin v. Löwis
> First, what do I need to be aware of when sending arbitrary
> data by a POST, and Second, is there a universally supported
> version of what python can do with pickle?  I mostly need
> python and PHP, but perl would be nice too.

You might want to use WDDX. There are WDDX libraries for Python,
PHP, and Perl.

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


Re: os.path.normpath bug?

2007-06-14 Thread billiejoex
On 14 Giu, 22:35, Michael Hoffman <[EMAIL PROTECTED]> wrote:

> Intentional.
>
> http://en.wikipedia.org/wiki/Path_(computing)#Universal_Naming_Conven...
> --
> Michael Hoffman

Got it.
Thank you.


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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Dustan
On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> Which can then be converted into a generator expression (round
> brackets instead of square brackets) to avoid the intermediate list:
> len((i for i in a_list if a_callable(i)))

Sorry for the excess of posts everybody.

I just realized that the generator expression would not work. I'm not
sure how else could be implemented efficiently and without using up
memory besides by accumulating the count as your earlier example shows.

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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Carsten Haese
On Thu, 2007-06-14 at 21:06 +, Dustan wrote:
> On Jun 14, 3:37 pm, Dustan <[EMAIL PROTECTED]> wrote:
> > Which can then be converted into a generator expression (round
> > brackets instead of square brackets) to avoid the intermediate list:
> > len((i for i in a_list if a_callable(i)))
> 
> Sorry for the excess of posts everybody.
> 
> I just realized that the generator expression would not work. I'm not
> sure how else could be implemented efficiently and without using up
> memory besides by accumulating the count as your earlier example shows.

sum(1 for i in a_list if a_callable(i))

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread kyosohma
On Jun 14, 4:02 pm, "Talbot Katz" <[EMAIL PROTECTED]> wrote:
> Greetings Pythoners!
>
> I hope you'll indulge an ignorant outsider.  I work at a financial software
> firm, and the tool I currently use for my research is R, a software
> environment for statistical computing and graphics.  R is designed with
> matrix manipulation in mind, and it's very easy to do regression and time
> series modeling, and to plot the results and test hypotheses.  The kinds of
> functionality we rely on the most are standard and robust versions of
> regression and principal component / factor analysis, bayesian methods such
> as Gibbs sampling and shrinkage, and optimization by linear, quadratic,
> newtonian / nonlinear, and genetic programming; frequently used graphics
> include QQ plots and histograms.  In R, these procedures are all available
> as functions (some of them are in auxiliary libraries that don't come with
> the standard distribution, but are easily downloaded from a central
> repository).
>
> For a variety of reasons, the research group is considering adopting Python.
>   Naturally, I am curious about the mathematical, statistical, and graphical
> functionality available in Python.  Do any of you out there use Python in
> financial research, or other intense mathematical/statistical computation?
> Can you compare working in Python with working in a package like R or S-Plus
> or Matlab, etc.?  Which of the procedures I mentioned above are available in
> Python?  I appreciate any insight you can provide.  Thanks!
>
> --  TMK  --
> 212-460-5430home
> 917-656-5351cell

I'd look at following modules:

matplotlib - http://matplotlib.sourceforge.net/
numpy - http://numpy.scipy.org/

Finally, this website lists other resources: 
http://www.astro.cornell.edu/staff/loredo/statpy/

Mike

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


Re: a_list.count(a_callable) ?

2007-06-14 Thread Carsten Haese
On Thu, 2007-06-14 at 12:53 -0700, Ping wrote:
> Hi,
> 
> I'm wondering if it is useful to extend the count() method of a list
> to accept a callable object?  What it does should be quite intuitive:
> count the number of items that the callable returns True or anything
> logically equivalent (non-empty sequence, non-zero number, etc).
> 
> This would return the same result as len(filter(a_callable, a_list)),
> but without constructing an intermediate list which is thrown away
> after len() is done.
> 
> This would also be equivalent to
> n = 0
> for i in a_list:
> if a_callable(i):  n += 1
> but with much shorter and easier-to-read code.  It would also run
> faster.

As an alternative to the generator-sum approach I posted in reply to
Dustan's suggestion, you could (ab)use the fact that count() uses
equality testing and do something like this:

>>> class Oddness(object):
...def __eq__(self, other): return other%2==1
... 
>>> [1,2,3,4,5].count(Oddness())
3

I don't know which approach is faster. Feel free to compare the two.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: MS Word parser

2007-06-14 Thread Ben C
On 2007-06-13, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> On Jun 13, 1:28 am, Tim Golden <[EMAIL PROTECTED]> wrote:
>> [EMAIL PROTECTED] wrote:
>> > Hi all,
>> > I'm currently using antiword to extract content from MS Word files.
>> > Is there another way to do this without relying on any command prompt
>> > application?
>>
>> Well you haven't given your environment, but is there
>> anything to stop you from controlling Word itself via
>> COM? I'm no Word expert, but looking around, this
>> seems to work:
>>
>> 
>> import win32com.client
>> word = win32com.client.Dispatch ("Word.Application")
>> doc = word.Documents.Open ("c:/temp/temp.doc")
>> text = doc.Range ().Text
>>
>> open ("c:/temp/temp.txt", "w").write (text.encode ("UTF-8"))
>> 
>>
>> TJG
>
> Tim,
> I'm on Linux (RedHat) so using Word is not an option for me.  Any
> other suggestions?

There is OpenOffice which has a Python API to it (called UNO). But
piping through antiword is probably easier.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Michael Hoffman
Talbot Katz wrote:

> I hope you'll indulge an ignorant outsider.  I work at a financial 
> software firm, and the tool I currently use for my research is R, a 
> software environment for statistical computing and graphics.  R is 
> designed with matrix manipulation in mind, and it's very easy to do 
> regression and time series modeling, and to plot the results and test 
> hypotheses.  The kinds of functionality we rely on the most are standard 
> and robust versions of regression and principal component / factor 
> analysis, bayesian methods such as Gibbs sampling and shrinkage, and 
> optimization by linear, quadratic, newtonian / nonlinear, and genetic 
> programming; frequently used graphics include QQ plots and histograms.  
> In R, these procedures are all available as functions (some of them are 
> in auxiliary libraries that don't come with the standard distribution, 
> but are easily downloaded from a central repository).

I use both R and Python for my work. I think R is probably better for 
most of the stuff you are mentioning. I do any sort of heavy 
lifting--database queries/tabulation/aggregation in Python and load the 
resulting data frames into R for analysis and graphics.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread py_genetic
>
> You didn't try hard enough. :)
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
>
> --
> HTH,
> Rob

Thanks Rob, "permutation" was the keyword I shcould have used!

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


RE: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread John Krukoff
On Jun 14, 4:02 pm, "Talbot Katz" <[EMAIL PROTECTED]> wrote: 
> Greetings Pythoners!
> 
> I hope you'll indulge an ignorant outsider.  I work at a financial
> software
> firm, and the tool I currently use for my research is R, a software
> environment for statistical computing and graphics.  R is designed with
> matrix manipulation in mind, and it's very easy to do regression and time
> series modeling, and to plot the results and test hypotheses.  The kinds
> of
> functionality we rely on the most are standard and robust versions of
> regression and principal component / factor analysis, bayesian methods
> such
> as Gibbs sampling and shrinkage, and optimization by linear, quadratic,
> newtonian / nonlinear, and genetic programming; frequently used graphics
> include QQ plots and histograms.  In R, these procedures are all available
> as functions (some of them are in auxiliary libraries that don't come with
> the standard distribution, but are easily downloaded from a central
> repository).
> 
> For a variety of reasons, the research group is considering adopting
> Python.
>   Naturally, I am curious about the mathematical, statistical, and
> graphical
> functionality available in Python.  Do any of you out there use Python in
> financial research, or other intense mathematical/statistical computation?
> Can you compare working in Python with working in a package like R or S-
> Plus
> or Matlab, etc.?  Which of the procedures I mentioned above are available
> in
> Python?  I appreciate any insight you can provide.  Thanks!
> 
> --  TMK  --
> 212-460-5430  home
> 917-656-5351  cell
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

It is worth noting that there's a bridge available to allow python to
integrate cleanly with R, the Rpy project:
http://rpy.sourceforge.net/

Which should allow you to use python for whatever it is you need without
abandoning R for your mathematical/statistical work.

-
John Krukoff
[EMAIL PROTECTED]

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


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Tim Churches
Michael Hoffman wrote:
> Talbot Katz wrote:
> 
>> I hope you'll indulge an ignorant outsider.  I work at a financial 
>> software firm, and the tool I currently use for my research is R, a 
>> software environment for statistical computing and graphics.  R is 
>> designed with matrix manipulation in mind, and it's very easy to do 
>> regression and time series modeling, and to plot the results and test 
>> hypotheses.  The kinds of functionality we rely on the most are standard 
>> and robust versions of regression and principal component / factor 
>> analysis, bayesian methods such as Gibbs sampling and shrinkage, and 
>> optimization by linear, quadratic, newtonian / nonlinear, and genetic 
>> programming; frequently used graphics include QQ plots and histograms.  
>> In R, these procedures are all available as functions (some of them are 
>> in auxiliary libraries that don't come with the standard distribution, 
>> but are easily downloaded from a central repository).
> 
> I use both R and Python for my work. I think R is probably better for 
> most of the stuff you are mentioning. I do any sort of heavy 
> lifting--database queries/tabulation/aggregation in Python and load the 
> resulting data frames into R for analysis and graphics.

I would second that. It is not either/or. Use Python, including Numpy
and matplotlib and packages from SciPy, for some things, and R for
others. And you can even embed R in Python using RPy - see
http://rpy.sourceforge.net/

We use the combination of Python, Numpy (actually, the older Numeric
Python package, but soon to be converted to Numpy), RPy and R in our
NetEpi Analysis project - exploratory epidemiological analysis of large
data sets - see http://sourceforge.net/projects/netepi - and it is a
good combination - Python for the Web interface, data manipulation and
data heavy-lifting, and for some of the more elementary statistics, and
R for more involved statistical analysis and graphics (with teh option
of using matplotlib or other Python-based graphics packages for some
tasks if we wish). The main thing to remember, though, is that indexing
is zero-based in Python and 1-based in R...

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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 3:08 pm, Rob Wolfe <[EMAIL PROTECTED]> wrote:
> py_genetic <[EMAIL PROTECTED]> writes:
> > Hi,
>
> > I'm looking to generate x alphabetic strings in a list size x.  This
> > is exactly the same output that the unix command "split" generates as
> > default file name output when splitting large files.
>
> > Example:
>
> > produce x original, but not random strings from english alphabet, all
> > lowercase.  The length of each string and possible combinations is
> > dependent on x.  You don't want any repeats.
>
> > [aaa, aab, aac, aad,  aax, .. bbc, bbd,  bcd]
>
> > I'm assumming there is a slick, pythonic way of doing this, besides
> > writing out a beast of a looping function.  I've looked around on
> > activestate cookbook, but have come up empty handed.  Any suggestions?
>
> You didn't try hard enough. :)
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465


Unfortunately, that's a very poor example. The terminaology is
all wrong.

"xpermutations takes all elements from the sequence, order matters."
This ought to be the Cartesian Product, but it's not (no replacement).

"xcombinations takes n distinct elements from the sequence, order
matters."
If order matters, it's a PERMUTATION, period.

"xuniqueCombinations takes n distinct elements from the sequence,
order is irrelevant."
No such thing, a Combination is unique by definition.

"xselections takes n elements (not necessarily distinct) from the
sequence, order matters."
Ah, this allows a size operator, so if size = length, we get full
Cartesian Product.

The proper terminology for the Cartesian Product and
its subsets is:

Permutations with replacement
Combinations with replacement
Permutations without replacement
Combinations without replacement

And if the functions were properly labeled, you would get:

permutation without replacement - size 4

Permutations of 'love'
love loev lvoe lveo leov levo olve olev ovle ovel oelv oevl vloe vleo
vole voel velo veol elov elvo eolv eovl evlo evol


permutation without replacement - size 2

Combinations of 2 letters from 'love'
lo lv le ol ov oe vl vo ve el eo ev


combination without replacement - size 2

Unique Combinations of 2 letters from 'love'
lo lv le ov oe ve


permutation with replacement - size 2

Selections of 2 letters from 'love'
ll lo lv le ol oo ov oe vl vo vv ve el eo ev ee


full Cartesian Product, permutations with replacement - size 4

Selections of 4 letters from 'love'
 lllo lllv llle llol lloo llov lloe llvl llvo llvv llve llel lleo
llev llee loll lolo lolv lole lool looo loov looe lovl lovo lovv love
loel loeo loev loee lvll lvlo lvlv lvle lvol lvoo lvov lvoe lvvl lvvo
lvvv lvve lvel lveo lvev lvee lell lelo lelv lele leol leoo leov leoe
levl levo levv leve leel leeo leev leee olll ollo ollv olle olol oloo
olov oloe olvl olvo olvv olve olel oleo olev olee ooll oolo oolv oole
oool  ooov oooe oovl oovo oovv oove ooel ooeo ooev ooee ovll ovlo
ovlv ovle ovol ovoo ovov ovoe ovvl ovvo ovvv ovve ovel oveo ovev ovee
oell oelo oelv oele oeol oeoo oeov oeoe oevl oevo oevv oeve oeel oeeo
oeev oeee vlll vllo vllv vlle vlol vloo vlov vloe vlvl vlvo vlvv vlve
vlel vleo vlev vlee voll volo volv vole vool vooo voov vooe vovl vovo
vovv vove voel voeo voev voee vvll vvlo vvlv vvle vvol vvoo vvov vvoe
vvvl vvvo  vvve vvel vveo vvev vvee vell velo velv vele veol veoo
veov veoe vevl vevo vevv veve veel veeo veev veee elll ello ellv elle
elol eloo elov eloe elvl elvo elvv elve elel eleo elev elee eoll eolo
eolv eole eool eooo eoov eooe eovl eovo eovv eove eoel eoeo eoev eoee
evll evlo evlv evle evol evoo evov evoe evvl evvo evvv evve evel eveo
evev evee eell eelo eelv eele eeol eeoo eeov eeoe eevl eevo eevv eeve
eeel eeeo eeev 


And Combinations with replacement seems to be missing.


>
> --
> HTH,
> Rob- Hide quoted text -
>
> - Show quoted text -


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


Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-14 Thread [EMAIL PROTECTED]
On Jun 14, 4:39 pm, py_genetic <[EMAIL PROTECTED]> wrote:
> > You didn't try hard enough. :)
>
> >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
>
> > --
> > HTH,
> > Rob
>
> Thanks Rob, "permutation" was the keyword I shcould have used!

See my other post to see if that is indeed what you mean.

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


Re: Method much slower than function?

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 05:54:25 -0300, Francesco Guerrieri  
<[EMAIL PROTECTED]> escribió:

> On 6/14/07, Peter Otten <[EMAIL PROTECTED]> wrote:
>> Gabriel Genellina wrote:
>> > ...
>> > py> print timeit.Timer("f2()", "from __main__ import  
>> f2").repeat(number=1)
>> > [0.42673663831576358, 0.42807591467630662, 0.44401481193838876]
>> > py> print timeit.Timer("f1()", "from __main__ import  
>> f1").repeat(number=1)
>> >
>> > ...after a few minutes I aborted the process...
>>
>> I can't confirm this.
>
> [...]
>
>> $ python2.5 -m timeit -s 'from join import f1' 'f1()'
>> 10 loops, best of 3: 212 msec per loop
>> $ python2.5 -m timeit -s 'from join import f2' 'f2()'
>> 10 loops, best of 3: 259 msec per loop
>> $ python2.5 -m timeit -s 'from join import f3' 'f3()'
>> 10 loops, best of 3: 236 msec per loop
>
> On my machine (using python 2.5 under win xp) the results are:
 print timeit.Timer("f2()", "from __main__ import f2").repeat(number =  
 1)
> [0.19726834822823575, 0.19324697456408974, 0.19474492594212861]
 print timeit.Timer("f1()", "from __main__ import f1").repeat(number =  
 1)
> [21.982707133304167, 21.905312587963252, 22.843430035622767]
>
> so it seems that there is a rather sensible difference.
> what's the reason of the apparent inconsistency with Peter's test?

I left the test running and went to sleep. Now, the results:

C:\TEMP>python -m timeit -s "from join import f1" "f1()"
10 loops, best of 3: 47.7 sec per loop

C:\TEMP>python -m timeit -s "from join import f2" "f2()"
10 loops, best of 3: 317 msec per loop

C:\TEMP>python -m timeit -s "from join import f3" "f3()"
10 loops, best of 3: 297 msec per loop

Yes, 47.7 *seconds* to build the string using the += operator.
I don't know what's the difference: python version (this is not 2.5.1  
final), hardware, OS... but certainly in this PC it is *very* important.
Memory usage was around 40MB (for a 10MB string) and CPU usage went to 99%  
(!).

-- 
Gabriel Genellina

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


Failing on string exceptions in 2.4

2007-06-14 Thread Stephen R Laniel
Reading the Python docs, it looks like string exceptions
will be a DeprecationWarning in Python 2.5. Is there any way
to make them so in 2.4? Now how about if I want to turn all
DeprecationWarnings into compile-time errors? Is there some
way to do this?

End goal being that string exceptions would cause
compilation to fail. A few times now, I've found myself
doing

class SomeClass:
"""docstring"""
pass

raise SomeClass, "Some description"

and I've gotten a weird compiler error about the constructor
for SomeClass. I'd prefer it just to fail there and not let
me raise an exception that isn't subclassed beneath
Exception.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Moving items from list to list

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 14:23:14 -0300, Evan Klitzke <[EMAIL PROTECTED]> escribió:

> On 6/14/07, HMS Surprise <[EMAIL PROTECTED]> wrote:
>>
>> Just wondered if there was some python idiom for moving a few items
>> from one list to another. I often need to delete 2 or 3 items from one
>> list and put them in another. Delete doesn't seem to have a return
>> value. I don't care which items I get so now I just use a couple of
>> pops or a for loop for more than two.
>
> I'm not sure if this is what you're asking, but if the elements in the
> list are contiguous you can just use list slicing/addition, like this:
>
> a = [1, 2, 3, 4, 5]
> b = [6, 7, 8, 9, 10]
>
> b = a[2:] + b
> a = a[:2]
>
> Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
> 5, 6, 7, 8, 9, 10].

When lists become large this is less convenient because it has to build  
three intermediate lists. At least on my box, pop+append is 5 orders of  
magnitude faster (but as shown on another posts, you should test on your  
box to see what happens):

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a.po
p())"
100 loops, best of 3: 1.35 usec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b+=a[-1:];a=a
[:-1]"
10 loops, best of 3: 107 msec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a[-1
]);a=a[:-1]"
10 loops, best of 3: 107 msec per loop

c:\temp>call python -m timeit -s "a=range(100);b=range(1000)"  
"b.append(a[0]
);a=a[1:]"
10 loops, best of 3: 110 msec per loop

-- 
Gabriel Genellina

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


Re: save class

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 16:05:14 -0300, nik <[EMAIL PROTECTED]> escribió:

> On Jun 13, 10:04 pm, Josiah Carlson <[EMAIL PROTECTED]>
> wrote:
>> Gabriel Genellina wrote:
>> > En Wed, 13 Jun 2007 23:11:22 -0300, nik <[EMAIL PROTECTED]> escribió:

>> >> It would seem that I want to actually save the source code for the
>> >> class. I know that I could of course open up an editor and just make
>> >> it, but my ideal would be to have the base class, Map, be able to  
>> make
>> >> the sub-classes. I don't want the class definition. What I want is an
>> >> actual class that I could later import and use somewhere else. I am
>> >> planning to have each one of these map objects contain a different
>> >> dictionary and then be able to import the map into the application,
>> >> but have certain methods defined in the Map super-class to draw data
>> >> out of the specific map's specific dictionary. I hope that makes
>> >> sense.
>> > And are you sure you actually need different subclasses? Will you
>> > construct them several instances of each subclass? From the above
>> > description I feel you want just different Map *instances*, each with
>> > its own dict, not different *subclasses*.
>>
>> What you said, and that his solution sounds like a Java approach to the
>> problem (subclass an abstract base class that calls specific methods on
>> the subclass to "do the right thing").
>>
>> To offer the OP source he can use...
>>
>> class Map:
>>  def __init__(self):
>>  self.dict = {}
>>  def DoSomething(self):
>>  #do something with self.dict
>>
>> Every instance gets a new dictionary.  Now, if he actually wants to
>> change the behavior of the DoSomething method, of course then it would
>> make sense to subclass Map.
>>
>>   - Josiah
>
> I am hoping to change the self.dict for each subclass. I realize that
> I could save self.dict to file and then load in different dicts each
> time I get a new instance of class. But I want to be able to make
> subclasses of map that each have different self.dict. Then when I need
> to use them, just import the module and use the specific dict, instead
> of having to keep track of a separate dictionary file. I am new to

As Josiah said, I still don't see why do you want a *subclass*. If the  
only difference between your "subclasses" is their dict, they're not  
subclasses but just Map *instances*.
Let's say, have a class Person, with attributes "name" and "email". If I  
want to represent two different persons, I would create two Person  
*instances*: Person(name="Gabriel", email="[EMAIL PROTECTED]") and  
Person(name="nik", email="[EMAIL PROTECTED]")
Classes try to capture behavior and structure; instances contain state  
(very roughly said). One *could* use two subclasses here, and in certain  
circumstances it may be useful, but it's not the most common case.

> this, but I thought that this would be a regular thing to do in
> python, because people must make classes in the interactive console
> and then export them somehow for later use.

I've never done that. I only use the interactive interpreter for testing  
purposes, I never "develop" code inside the interpreter.

-- 
Gabriel Genellina

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


Re: OS X install confusion

2007-06-14 Thread John Fisher
Ted <[EMAIL PROTECTED]> wrote:

> On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> > John Fisher wrote:
> > > Hi Groupies,
> >
> > > I have an Intel Macbook running OS X 10.4.
> >
> > > It came installed with Python 2.3.5. I have since installed MacPython
> > > with version 2.4.4, cool.
> >
> > > When I open a bash terminal session and type python, it brings up
> > > version 2.3.5. If I type IDLE it brings up version 2.4.4.
> >
> > > My question: what do I have to do to get it to bring up 2.4.4 with the
> > > "python" command?
> >
> > > Thanks for bringing light to my ignorance.
> >
> > > JF
> >
> > Sounds like a path problem. Apple's system Python is installed in
> > /usr/bin. Your installation is probably in /usr/local/bin. Edit your
> > profile or use the full path.
> >
> > --
> > Kevin Walzer
> > Code by Kevinhttp://www.codebykevin.com
> 
> The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
> and /usr/bin/pythonw.
> 
> I found it easier to relink to the new installation path. This also
> leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
> original version if you want to quickly check something.
> 
> Cheers,
> Ted

OK, please give a little more information how I can accomplish this
"re-link".

Thanks,

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


Re: OS X install confusion

2007-06-14 Thread Paul McNett
John Fisher wrote:
> Ted <[EMAIL PROTECTED]> wrote:
> 
>> On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
>>> John Fisher wrote:
 Hi Groupies,
 I have an Intel Macbook running OS X 10.4.
 It came installed with Python 2.3.5. I have since installed MacPython
 with version 2.4.4, cool.
 When I open a bash terminal session and type python, it brings up
 version 2.3.5. If I type IDLE it brings up version 2.4.4.
 My question: what do I have to do to get it to bring up 2.4.4 with the
 "python" command?
 Thanks for bringing light to my ignorance.
 JF
>>> Sounds like a path problem. Apple's system Python is installed in
>>> /usr/bin. Your installation is probably in /usr/local/bin. Edit your
>>> profile or use the full path.
>>>
>>> --
>>> Kevin Walzer
>>> Code by Kevinhttp://www.codebykevin.com
>> The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
>> and /usr/bin/pythonw.
>>
>> I found it easier to relink to the new installation path. This also
>> leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
>> original version if you want to quickly check something.
>>
>> Cheers,
>> Ted
> 
> OK, please give a little more information how I can accomplish this
> "re-link".

Your Python 2.5 is likely installed here:

/Library/Frameworks/Python.framework/Versions/Current/bin

But OS X comes with a "system Python", version 2.3.5, likely installed here:

/usr/bin

If you look at /usr/bin, you'll see:

lrwxr-xr-x   1 root  wheel  9 Jan 31 17:24 python -> python2.3
lrwxr-xr-x   1 root  wheel 72 Jan 31 17:24 python2.3 -> 
../../System/Library/Frameworks/Python.framework/Versions/2.3/bin/python
lrwxr-xr-x   1 root  wheel 10 Jan 31 17:24 pythonw -> pythonw2.3
-rwxr-xr-x   1 root  wheel  29704 Aug 19  2006 pythonw2.3

So, python is linked to python2.3, and python2.3 is in turn linked to 
/System/Library/Frameworks/Python.framework/Versions/2.3/bin/python

You need to (warning: watch for line wrap):

sudo -s

cd /usr/bin
ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python 
python_current
ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/pythonw 
pythonw_current
rm python
rm pythonw
ln -s python python_current
ln -s pythonw pythonw_current

However, that isn't what I did. I like the system being able to find and 
use the system-installed python, but I like my scripts to use the python 
version I installed (2.5). To get that, skip the above symlinking and 
instead edit your .bash_profile file (hidden file inside your home 
directory) and put these lines at the top:

PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}
export PATH

-- 
pkm ~ http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list


poking around a running program

2007-06-14 Thread Paul Rubin
I have a long-running program that has lots of net connections open on
separate threads.  I'm twiddling my thumbs waiting for it to finish
(each run takes maybe an hour) and although I'm logging various info
that I can monitor as the run progresses, it would be cool to be able
to actually poke around the program's data while it's running.  

I'm thinking of adding a "console" thread that would basically be a
read-eval-print loop that I could use to inspect data interactively.
Maybe it would just eval stuff or maybe it would use pdb.  Of course
it would be a bit dangerous since I could mutate things with it but I
guess it would be enough to just be careful not to do that.

Is this kind of thing common?  Has it been done before and are there
some recipes?  I did a quick search and didn't find anything obvious.
One thing I'm wondering is how to examine the internal state of
iterators, especially the C implementations from itertools.

More advanced might be some kind of hot-patching scheme (to be able to
change the code without stopping it) but for now I think I'm ok just
restarting the program every so often, modifying the code between runs
based on what happens in a given run.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS X install confusion

2007-06-14 Thread 7stud
On Jun 14, 11:21 am, [EMAIL PROTECTED] (John Fisher) wrote:
> Hi Groupies,
>
> I have an Intel Macbook running OS X 10.4.
>
> It came installed with Python 2.3.5. I have since installed MacPython
> with version 2.4.4, cool.
>
> When I open a bash terminal session and type python, it brings up
> version 2.3.5. If I type IDLE it brings up version 2.4.4.
>
> My question: what do I have to do to get it to bring up 2.4.4 with the
> "python" command?
>
> Thanks for bringing light to my ignorance.
>
> JF

Strange.  I installed macpython 2.4.4 on an imac this year, and when I
type python on the command line of a bash shell, python 2.4.4 starts
up:

$ python
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>


I looked in:

/System/Library/Frameworks/Python.framework/Versions/Current/bin/

and inside that directory are the programs:

idle
pydoc
python
python2.3

So I tried typing python2.3 on the command line, and lo and behold
python 2.3.5 started up:

$ python2.3
Python 2.3.5 (#1, Jul 25 2006, 00:38:48)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

So it looks like the macpython install changed the name of the pre-
installed python program from python to python2.3.  What do you see in
that directory?


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


Problems with regular expressions

2007-06-14 Thread Carlos Luis Pérez Alonso
I have the next piece of code:
 

 if re.search('^(taskid|bugid):\\d+',logMessage):
return 0 
else:
sys.stderr.write("El comentario tiene que contener el taskid: o el 
bugid:")
return 1
-
 
The regular exprexión is "^(taskid|bugid):\\d+"
 
Mi problem is that if logMessage == "taskid:234" the regular expression matched 
teorically, but the result is always "None" and the function returns 1.
 
¿Does anybody has an idea of what is happening here?
 
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about mathematical and statistical functionality in Python

2007-06-14 Thread Josh Gilbert
On Thursday 14 June 2007 5:54 pm, Tim Churches wrote:
> Michael Hoffman wrote:
> > Talbot Katz wrote:
> >> I hope you'll indulge an ignorant outsider.  I work at a financial
> >> software firm, and the tool I currently use for my research is R, a
> >> software environment for statistical computing and graphics.  R is
> >> designed with matrix manipulation in mind, and it's very easy to do
> >> regression and time series modeling, and to plot the results and test
> >> hypotheses.  The kinds of functionality we rely on the most are standard
> >> and robust versions of regression and principal component / factor
> >> analysis, bayesian methods such as Gibbs sampling and shrinkage, and
> >> optimization by linear, quadratic, newtonian / nonlinear, and genetic
> >> programming; frequently used graphics include QQ plots and histograms.
> >> In R, these procedures are all available as functions (some of them are
> >> in auxiliary libraries that don't come with the standard distribution,
> >> but are easily downloaded from a central repository).
> >
> > I use both R and Python for my work. I think R is probably better for
> > most of the stuff you are mentioning. I do any sort of heavy
> > lifting--database queries/tabulation/aggregation in Python and load the
> > resulting data frames into R for analysis and graphics.
>
> I would second that. It is not either/or. Use Python, including Numpy
> and matplotlib and packages from SciPy, for some things, and R for
> others. And you can even embed R in Python using RPy - see
> http://rpy.sourceforge.net/
>
> We use the combination of Python, Numpy (actually, the older Numeric
> Python package, but soon to be converted to Numpy), RPy and R in our
> NetEpi Analysis project - exploratory epidemiological analysis of large
> data sets - see http://sourceforge.net/projects/netepi - and it is a
> good combination - Python for the Web interface, data manipulation and
> data heavy-lifting, and for some of the more elementary statistics, and
> R for more involved statistical analysis and graphics (with teh option
> of using matplotlib or other Python-based graphics packages for some
> tasks if we wish). The main thing to remember, though, is that indexing
> is zero-based in Python and 1-based in R...
>
> Tim C

Thirded. I use R, Python, Matlab along with other languages (I hate pipeline 
pilot) in my work and from what I've seen nothing can compare with R when it 
comes to stats. I love R, from its brilliant CRAN system (PyPI needs serious 
work to be considered in the same class as CPAN et al) to its delicious Emacs 
integration. 

I just wish there was a way to distribute R packages without requiring the 
user to separately install R. 

In a similar vein, I wish there was a reasonable Free Software equivalent to 
Spotfire. The closest I've found (and they're nowhere near as good) are 
Orange (http://www.ailab.si/orange) and WEKA 
(http://www.cs.waikato.ac.nz/ml/weka/). Orange is written in Python, but its 
tied to QT 2.x as the 3.x series was not available on Windows under the GPL. 


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


Re: SimplePrograms challenge

2007-06-14 Thread Steve Howell

--- Steven Bethard <[EMAIL PROTECTED]> wrote:

> > How about including a driver?
> 
> Yes, absolutely a good idea. Fortunately, the other
> Steve borrowed the 
> time machine already and added this to the end::
> 
>  for p in iter_primes():
>  if p > 1000: break
>  print p
> 

I think rzed (sorry for bad quoting) was
(partly)making the point that we could have more
explicitly showed how .next() worked.  I'm fine with
the current example, because I think the examples
should solve problems at face value, and here it's
very natural to use a loop with an iterator/generator
(never mind the underlying magic), but I do think some
future example should show how one can have more
control over getting values from an iterator.





   

Looking for a deal? Find great prices on flights and hotels with Yahoo! 
FareChase.
http://farechase.yahoo.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Failing on string exceptions in 2.4

2007-06-14 Thread Gabriel Genellina
En Thu, 14 Jun 2007 18:45:08 -0300, Stephen R Laniel <[EMAIL PROTECTED]>  
escribió:

> Reading the Python docs, it looks like string exceptions
> will be a DeprecationWarning in Python 2.5. Is there any way
> to make them so in 2.4? Now how about if I want to turn all
> DeprecationWarnings into compile-time errors? Is there some
> way to do this?

Yes, using the warnings module.
First thing is to start Python using the -Wd argument so you can see ALL  
the warnings, even the supressed ones:

C:\temp>python24 -Wd

Then see what happens when you raise a string exception:

py> raise "Error"
__main__:1: PendingDeprecationWarning: raising a string exception is  
deprecated
Traceback (most recent call last):
   File "", line 1, in ?
Error

Now we know it is a PendingDeprecationWarning, and the message says  
"raising a string exception is deprecated"
With this information we can build a filter. Do this at startup of your  
program:

 from warnings import filterwarnings
filterwarnings(action="error", message="raising a string exception",  
category=PendingDeprecationWarning)

Or maybe, place it in sitecustomize.py.
Or, start Python using -W:

C:\temp>python24 -W "error:raising a string  
exception:PendingDeprecationWarning"
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on  
win32
Type "help", "copyright", "credits" or "license" for more information.
py> raise "Error"
Traceback (most recent call last):
   File "", line 1, in ?
   File "c:\apps\python\lib\warnings.py", line 61, in warn
 warn_explicit(message, category, filename, lineno, module, registry,
   File "c:\apps\python\lib\warnings.py", line 96, in warn_explicit

PendingDeprecationWarning: raising a string exception is deprecated

(btw, the right format for the -W option isn't documented, or at least  
I've been unable to find it; each time I want to use -W I have to reread  
the warnings module source...)

> End goal being that string exceptions would cause
> compilation to fail. A few times now, I've found myself
> doing
>
> class SomeClass:
> """docstring"""
> pass
>
> raise SomeClass, "Some description"
>
> and I've gotten a weird compiler error about the constructor
> for SomeClass. I'd prefer it just to fail there and not let
> me raise an exception that isn't subclassed beneath
> Exception.

The compiler won't complain here - you will have to inspect the code (or  
use a tool like pylint or pychecker). The easy fix is just inherit from  
Exception.

-- 
Gabriel Genellina

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


  1   2   >