Re: import and package confusion

2009-05-01 Thread alex23
On May 1, 4:24 pm, Arnaud Delobelle  wrote:
> It's a challenge to do it in a list comprehension, but here it is!
> >>> data
> 'AAABCDD'
> >>> field_sizes
> [3, 5, 9, 2]
> >>> [data[i:j] for j in [0] for s in field_sizes for i, j in [(j, j+s)]]
> ['AAA', 'B', 'C', 'DD']

Ugh. I know using a listcomp was the point but I find using struct so
much more elegant.

>>> import struct
>>> data = 'AAABCDD'
>>> format = '3s5s9s2s'
>>> struct.unpack(format, data)
('AAA', 'B', 'C', 'DD')

Friends don't let friends abuse list comprehensions.

I made sure the code works this time :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-05-01 Thread smichr
On May 1, 11:13 am, smichr  wrote:
> Also, this approach is limited in that only variables can be arguments
> to functions, not node expressions. If x and d are variables, x/d
> becomes an expression and you cannot compute sin(x/d). Or am I missing
> something?
>

Note to self...it's best to restart with a fresh session if you've
been making modifications. everything appears to work fine without any
of the above observed problems:

>>> a=Number_with_uncert(3,1);y=a+cos(a);y.error
0.85888494187891229
>>> b=Number_with_uncert(4,1)
>>> cos(a/b)
 object with result
0.731688868874 +- 0.213012112511

Sorry for the false alarm,
/c
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why bool( object )?

2009-05-01 Thread Paul Rubin
Steven D'Aprano  writes:
> for x in a or b or c:
> do_something_with(x)

Ugh

  for x in [a,b,c]:
 if len(x) > 0:
   do_something_with(x)
   break
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension question

2009-05-01 Thread thor
On May 1, 2:28 pm, Arnaud Delobelle  wrote:
> Ross  writes:
> > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
> > return a new list of each individual element in these tuples, I can do
> > it with a nested for loop but when I try to do it using the list
> > comprehension b = [j for j in i for i in a], my output is b =
> > [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
> > doing wrong?
>
> When writing nested list comprehension, the for loops are in the same
> order as you would write a normal nested for loop (which is not
> necessarily intuitive when you first find out but is very handy in the
> long run I think).
>
> So write:
>
>     [j for i in a for j in i]
>
> --
> Arnaud
an trick
>>> a
[(1, 2), (3, 4), (5, 6)]
>>> sum(a, ())
(1, 2, 3, 4, 5, 6)
>>>
you may search the maillist , somebody questioned before
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 and threading

2009-05-01 Thread Paul Rubin
robean  writes:
> reach the urls with urllib2. The actual program will involve fairly
> elaborate scraping and parsing (I'm using Beautiful Soup for that) but
> the example shown here is simplified and just confirms the url of the
> site visited.

Keep in mind Beautiful Soup is pretty slow, so if you're doing a lot
of pages and have multiple cpu's, you probably want parallel processes
rather than threads.

> wrong? I am new to both threading and urllib2, so its possible that
> the SNAFU is quite obvious..
> ...
> ulock = threading.Lock()

Without looking at the code for more than a few seconds, using an
explicit lock like that is generally not a good sign.  The usual
Python style is to send all inter-thread communications through
Queues.  You'd dump all your url's into a queue and have a bunch of
worker threads getting items off the queue and processing them.  This
really avoids a lot of lock-related headache.  The price is that you
sometimes use more threads than strictly necessary.  Unless it's a LOT
of extra threads, it's usually not worth the hassle of messing with
locks.

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


Re: subprocess & shared environments

2009-05-01 Thread Piet van Oostrum
> Robert Dailey  (RD) wrote:

>RD> I'm currently calling subprocess.call() on a batch file (in Windows)
>RD> that sets a few environment variables that are needed by further
>RD> processes started via subprocess.call(). How can I persist the
>RD> environment modifications by the first call() function? I've done my
>RD> own searching on this and I came up with nothing.

Environments are propagated down the subprocess chain, not up. (I don't
know Windows good enough but I think this is also true on Windows.) What
you could do is have the batch file print the environment, read it in
your script and passing that environment to the other subprocesses. But
then why would you do it in a batch file? You could as well copy the
setting of the environment variables in your Python script. Or if the
batch file is supplied by another source, parse it and extract the
environment variables from it.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


How to measure the memory cost in Python?

2009-05-01 Thread Li Wang
Hi everyone:

I want to measure the actual memory cost of a particular step in my program
(Python program), does anyone know if there is some function in Python could
help me to do this job? Or should I seek other tools to help me?

Thank you very much!

-- 
Li
--
Time is all we have
and you may find one day
you have less than you think
--
http://mail.python.org/mailman/listinfo/python-list


Re: string processing question

2009-05-01 Thread Kurt Mueller
Paul McGuire schrieb:
> --
> Weird. What happens if you change the second print statement to:
> print b.center(6,u"-")
Same behavior.


I have an even more minimal example:


:> python -c 'print unicode("ä", "utf8")'
ä

:> python -c 'print unicode("ä", "utf8")' | cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-1: ordinal not in range(128)


Just the difference of having piped the output to another program or to
a file.
Maybe we leave the other issue with the different centering for the moment.

My goal is to have my python programs unicode enabled.





TIA
-- 
Kurt Mueller

-- 
Kurt Müller, [email protected] 


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


Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 00:22:22 -0700, Paul Rubin wrote:

> Steven D'Aprano  writes:
>> for x in a or b or c:
>> do_something_with(x)
> 
> Ugh
> 
>   for x in [a,b,c]:
>  if len(x) > 0:
>do_something_with(x)
>break

What ugly, wasteful code. And it's *wrong* -- it doesn't do what my code 
does.

(1) Why build a list [a, b, c] that you don't need?

(2) Why assume that a, b and c are sequences with a fast __len__ method? 
They might be (say) linked lists that take O(N) to calculate the length, 
or binary trees that don't even have a length, but can be iterated over.

(3) And your code is wrong. I pass each element of the first non-empty 
sequence to do_something_with(). You pass the entire sequence. To do what 
my code does, you would need a nested for-loop like this:

for seq in [a,b,c]:
if len(seq) > 0:
for x in seq:
do_something_with(x)
break



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


Re: How to measure the memory cost in Python?

2009-05-01 Thread Chris Rebert
On Fri, May 1, 2009 at 1:03 AM, Li Wang  wrote:
> Hi everyone:
>
> I want to measure the actual memory cost of a particular step in my program
> (Python program), does anyone know if there is some function in Python could
> help me to do this job? Or should I seek other tools to help me?

See Gabriel's answer in your other thread "Measure the memory cost in Python".
And please don't post duplicate threads asking essentially the same
question, especially when you were given a perfectly acceptable answer
and not that much time has past since you started the other thread.

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


Re: Multiprocessing.Queue - I want to end.

2009-05-01 Thread Hendrik van Rooyen
 "Luis Zarrabeitia"  wrote:

8< ---explanation and example of one producer, 
8< ---more consumers and one queue 

>As you can see, I'm sending one 'None' per consumer, and hoping that no 
>consumer will read more than one None. While this particular implementation 

You don't have to hope. You can write the consumers that way to guarantee it.

>ensures that, it is very fragile. Is there any way to signal the consumers? 

Signalling is not easy - you can signal a process, but I doubt if it is 
possible to signal a thread in a process.

>(or better yet, the queue itself, as it is shared by all consumers?) 
>Should "close" work for this? (raise the exception when the queue is 
>exhausted, not when it is closed by the producer).

I haven't the foggiest if this will work, and it seems to me to be kind
of involved compared to passing a sentinel or sentinels.

And while we are on the subject - Passing None as a sentinel is IMO as
good as or better than passing "XXZulu This is the End uluZXX",
or any other imaginative string that is not likely to occur naturally
in the input.

I have always wondered why people do the one queue many getters thing.

Given that the stuff you pass is homogenous in that it will require a
similar amount of effort to process, is there not a case to be made
to have as many queues as consumers, and to round robin the work?

And if the stuff you pass around needs disparate effort to consume,
it seems to me that you can more easily balance the load by having
specialised consumers, instead of instances of one humungous 
"I can eat anything" consumer.

I also think that having a queue per consumer thread makes it easier
to replace the threads with processes and the queues with pipes or
sockets if you need to do serious scaling later.

In fact I happen to believe that anything that does any work needs 
one and only one input queue and nothing else, but I am peculiar
that way.

- Hendrik


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


returning values of a particular key from a dictionary

2009-05-01 Thread Saurabh
arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'})
print foo(arr, 'y')
['a','b','c']

I can write the function foo to return ['a','b','c'].
Is there some 'automatic'/built-in way to get this in Python ?

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


Re: Why bool( object )?

2009-05-01 Thread Paul Rubin
Steven D'Aprano  writes:
> (2) Why assume that a, b and c are sequences with a fast __len__ method? 
> They might be (say) linked lists that take O(N) to calculate the length, 
> or binary trees that don't even have a length, but can be iterated over.

Why assume they have a bool method?  Or a __or__ operator?

> for seq in [a,b,c]:
> if len(seq) > 0:
> for x in seq:
> do_something_with(x)
> break

Eh.

   for seq in [a,b,c]:
 if sum(1 for x in imap(do_something_with, seq)) > 0:
   break
--
http://mail.python.org/mailman/listinfo/python-list


Re: returning values of a particular key from a dictionary

2009-05-01 Thread Paul Rubin
Saurabh  writes:
> arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'})
> print foo(arr, 'y')
> ['a','b','c']

print list(a['y'] for a in arr)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why bool( object )?

2009-05-01 Thread Hendrik van Rooyen
"Paul Rubin"  wrote:


> Steven D'Aprano  writes:
> > for x in a or b or c:
> > do_something_with(x)
> 
> Ugh
> 
>   for x in [a,b,c]:
>  if len(x) > 0:
>do_something_with(x)
>break
> 
Ugh

for x in [a,b,c]:
if x:
do_something_with(x)
break

:-)

- Hendrik

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


Re: returning values of a particular key from a dictionary

2009-05-01 Thread Chris Rebert
On Fri, May 1, 2009 at 1:53 AM, Saurabh  wrote:
> arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'})
> print foo(arr, 'y')
> ['a','b','c']
>
> I can write the function foo to return ['a','b','c'].
> Is there some 'automatic'/built-in way to get this in Python ?

from operator import itemgetter
print map(itemgetter("y"), arr)

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


Re: bug with os.rename in 2.4.1?

2009-05-01 Thread Steven D'Aprano
On Thu, 30 Apr 2009 03:30:04 -0500, Nick Craig-Wood wrote:

>>  The race condition is still there. The only difference is that in the
>>  first case it fails noisily, with an exception, and in the second it
>>  fails quietly and does nothing.
> 
> I'd argue that since os.rename implements the syscall rename() and that
> is defined to be atomic (give or take) then the above is atomic and
> can't possibly be racy.

Ah, you're right, at least for systems where rename is atomic.

What I was trying to say is that the risk of the file *not* being renamed 
is still there, but in the first case the code failed noisily with an 
exception and in the second it just failed quietly. But that's wrong, the 
test is to see if the source file exists, not whether the destination 
file exists. If the destination file exists, and you have write-access to 
it, then it will be over-written no matter what.


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


Re: returning values of a particular key from a dictionary

2009-05-01 Thread John O'Hagan
On Fri, 1 May 2009, Saurabh wrote:
> arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'})
> print foo(arr, 'y')
> ['a','b','c']
>
> I can write the function foo to return ['a','b','c'].
> Is there some 'automatic'/built-in way to get this in Python ?

List comprehension:

[i['y'] for i in arr]


Regards,

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


Re: string processing question

2009-05-01 Thread Kurt Mueller
Scott David Daniels schrieb:
> To discover what is happening, try something like:
> python -c 'for a in "ä", unicode("ä"): print len(a), a'
>
> I suspect that in your encoding, "ä" is two bytes long, and in
> unicode it is converted to to a single character.

:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a'
2 ä
1 ä
:>

Yes it is. That is one of the two problems I see.
The solution for this is to unicode(, ) each string.


I'd like to have my python programs unicode enabled.




:> python -c 'for a in "ä", unicode("ä"): print len(a), a'
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:
ordinal not in range(128)

It seems that the default encoding is "ascii", so unicode() cannot cope
with "ä".
If I specify "utf8" for the encoding, unicode() works.

:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a'
2 ä
1 ä
:> 


But the print statement yelds an UnicodeEncodeError
if I pipe the output to a program or a file.

:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a' | cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)
2 ä
1 :>


So it seems to me, that piping the output changes the behavior of the
print statement:

:> python -c 'for a in "ä", unicode("ä", "utf8", "ignore"): print a,
len(a), type(a)'
ä 2 
ä 1 

:> python -c 'for a in "ä", unicode("ä", "utf8", "ignore"): print a,
len(a), type(a)'  | cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)
ä 2 
:>




How can I achieve that my python programs are unicode enabled:
- Input strings can have different encodings (mostly ascii, latin_1 or utf8)
- My python programs should always output "utf8".

Is that a good idea??



TIA
-- 
Kurt Müller, [email protected]

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


Re: Light (general) Inter-Process Mutex/Wait/Notify Synchronization?

2009-05-01 Thread Lawrence D'Oliveiro
In message <[email protected]>, John Nagle wrote:

>  Linux doesn't do interprocess communication very well.
> ... and shared memory (unsafe).

What about with a futex?

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


Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 01:56:50 -0700, Paul Rubin wrote:

> Steven D'Aprano  writes:
>> (2) Why assume that a, b and c are sequences with a fast __len__
>> method? They might be (say) linked lists that take O(N) to calculate
>> the length, or binary trees that don't even have a length, but can be
>> iterated over.
> 
> Why assume they have a bool method?  Or a __or__ operator?

What programming language are you using?

I'm using Python, where objects don't require either a bool or __or__ 
method (not operator) to work with the or operator.

>>> hasattr(None, '__or__') or hasattr(None, '__bool__') or \
... hasattr(None, '__nonzero__')
False
>>> 
>>> x = object()
>>> hasattr(x, '__or__') or hasattr(x, '__bool__') or \
... hasattr(x, '__nonzero__')
False
>>>
>>> None or x


Any object can be used as an operand to the boolean operators.


> Eh.
> 
>for seq in [a,b,c]:
>  if sum(1 for x in imap(do_something_with, seq)) > 0:
>break

Did I stumble into an Obfuscated Python competition?




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


Re: Replacing files in a zip archive

2009-05-01 Thread Дамјан Георгиевски
> Which will produce the same output as the original, confounding
> your user.  You could just write the new values out, since .read
> picks the last entry (as I believe it should).  Alternatively, if
> you want to replace it "in place", you'll need a bit more smarts
> when there is more than one copy of a file in the archive (when
> z.namelist.count(filename) > 1).

I think I'll just hope that there will never be an ODF file like that, 
and if there is, then it's not my problem :)


-- 
дамјан ( http://softver.org.mk/damjan/ )

Give me the knowledge to change the code I do not accept, 
the wisdom not to accept the code I cannot change, 
and the freedom to choose my preference.

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


Re: string processing question

2009-05-01 Thread Sion Arrowsmith
Kurt Mueller   wrote:
>:> python -c 'print unicode("ä", "utf8")'
>ä
>
>:> python -c 'print unicode("ä", "utf8")' | cat
>Traceback (most recent call last):
>  File "", line 1, in 
>UnicodeEncodeError: 'ascii' codec can't encode characters in position
>0-1: ordinal not in range(128)

$ python -c 'import sys; print sys.stdout.encoding'
UTF-8
$ python -c 'import sys; print sys.stdout.encoding' | cat
None

If print gets a Unicode string, it does an implicit
.encode(sys.stdout.encoding or sys.getdefaultencoding()) on it.
If you want your output to be guaranteed UTF-8, you'll need to
explicitly .encode("utf8") it yourself.

(I dare say this is slightly different in 3.x .)

-- 
\S

   under construction

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


problem in with keyword

2009-05-01 Thread Murali kumar
hi all..

my application runs fine in windows xp using python 2.6 and wxpython 2.8.9

but in ubuntu 8.10 following error appears..  using python 2.5.2 and
wxpython 2.8.9

/home/murali/Desktop/mathdemo-configfinal/manageprofile.py:63: Warning:
'with' will become a reserved keyword in Python 2.6
Traceback (most recent call last):
  File "test.py", line 8, in 
import manageprofile as managepf
  File "/home/murali/Desktop/mathdemo-configfinal/manageprofile.py", line 63
with open(self.profile, 'wb') as configfile:
^
SyntaxError: invalid syntax

help for this problem..

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


Re: problem in with keyword

2009-05-01 Thread Chris Rebert
On Fri, May 1, 2009 at 3:56 AM, Murali kumar  wrote:
> hi all..
>
> my application runs fine in windows xp using python 2.6 and wxpython 2.8.9
>
> but in ubuntu 8.10 following error appears..  using python 2.5.2 and
> wxpython 2.8.9
>
> /home/murali/Desktop/mathdemo-configfinal/manageprofile.py:63: Warning:
> 'with' will become a reserved keyword in Python 2.6
> Traceback (most recent call last):
>   File "test.py", line 8, in 
>     import manageprofile as managepf
>   File "/home/murali/Desktop/mathdemo-configfinal/manageprofile.py", line 63
>     with open(self.profile, 'wb') as configfile:
>     ^
> SyntaxError: invalid syntax

According to PEP 343 (http://www.python.org/dev/peps/pep-0343/), the
`with` keyword was only added in Python 2.6

To use it in Python 2.5, you'd need to add:

from __future__ import with_statement

to any files using the `with` syntax. Or you could just upgrade to Python 2.6

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


Dynamically declared shared constant/variable imported twice problem

2009-05-01 Thread Gabriel Rossetti

Hello everyone,

I have three modules A, B, C;

A declares this globally :

 UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()

Then they import stuff from each other:

- A imports a constant from module B (in "__main__")
- A imports a class and some constants from module C
- B imports a constant from module A
- C imports UPDATE_EVENT_ID from module A

What happens is that since A imports stuff from B which then in turn
loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
an event to C, C doesn't do anything since it doesn't have the same
UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
been a statically defined variable then that wouldn't be "bad", but
since it's dynamic it does pose problems. More globally, how cn I
prevent this even with static constants? I don't think it's that great
to redefine the stuff multiple times and I've already had this problem
in the past with a constant dict.

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


where to start with

2009-05-01 Thread venky
Hi,

As iam very new to python i would like explore python. Can any body
guide me as your guidance is more worth than googling and finding it.

Thanks,
Venkat
http://www.prog2impress.com/
--
http://mail.python.org/mailman/listinfo/python-list


Generator oddity

2009-05-01 Thread opstad
I'm a little baffled by the inconsistency here. Anyone have any
explanations?

>>> def gen():
...   yield 'a'
...   yield 'b'
...   yield 'c'
...
>>> [c1 + c2 for c1 in gen() for c2 in gen()]
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

>>> list(c1 + c2 for c1 in gen() for c2 in gen())
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

>>> it1 = gen()
>>> it2 = gen()
>>> list(c1 + c2 for c1 in it1 for c2 in it2)
['aa', 'ab', 'ac']

Why does this last list only have three elements instead of nine?

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


Re: where to start with

2009-05-01 Thread Chris Rebert
On Fri, May 1, 2009 at 4:37 AM, venky  wrote:
> Hi,
>
> As iam very new to python i would like explore python. Can any body
> guide me as your guidance is more worth than googling and finding it.

Good tutorials:
http://docs.python.org/tutorial/
http://diveintopython.org/toc/index.htm
http://openbookproject.net/thinkCSpy/index.xhtmll

To gain insight about Python's features and design:
http://www.python.org/dev/peps/pep-/

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


Re: Dynamically declared shared constant/variable imported twice problem

2009-05-01 Thread Peter Otten
Gabriel Rossetti wrote:

> I have three modules A, B, C;
> 
> A declares this globally :
> 
>   UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()
> 
> Then they import stuff from each other:
> 
> - A imports a constant from module B (in "__main__")
> - A imports a class and some constants from module C
> - B imports a constant from module A
> - C imports UPDATE_EVENT_ID from module A
> 
> What happens is that since A imports stuff from B which then in turn
> loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
> an event to C, C doesn't do anything since it doesn't have the same
> UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
> been a statically defined variable then that wouldn't be "bad", but
> since it's dynamic it does pose problems. More globally, how cn I
> prevent this even with static constants? I don't think it's that great
> to redefine the stuff multiple times and I've already had this problem
> in the past with a constant dict.

Imported modules are cached, so this problem shouldn't occur unless you use 
A.py as your main script. In this case it will be cached as "__main__", and 
if you import it elsewhere a second copy will be executed and cached as "A".

To fix it create a wrapper script that doesn't create any objects that will 
be used elsewhere, e. g.

#!/usr/bin/env python
import A
A.main()

assuming the function A.main() contains the code to start your application 
and invoke that instead of A.py

Peter

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


Re: Generator oddity

2009-05-01 Thread Pascal Chambon

[email protected] a écrit :

I'm a little baffled by the inconsistency here. Anyone have any
explanations?

  

def gen():


...   yield 'a'
...   yield 'b'
...   yield 'c'
...
  

[c1 + c2 for c1 in gen() for c2 in gen()]


['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  

list(c1 + c2 for c1 in gen() for c2 in gen())


['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  

it1 = gen()
it2 = gen()
list(c1 + c2 for c1 in it1 for c2 in it2)


['aa', 'ab', 'ac']

Why does this last list only have three elements instead of nine?

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


  
When you use "for c2 in gen()]",  at each loop of c1, a new gen is 
instanciated and looped-over to continue building the list.
Whereas when you use "for c2 in it2)", it's always the same instance of 
gen which is used (it2), so after 3 loops on c2 this instance is 
exhausted, and the following iterations on c1 (and then attempts of 
looping on c2) don't give anything because the looping on c2 gives an 
exception "StopIteration".


Regards,
pascal


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


Re: Generator oddity

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 04:58:16 -0700, opstad wrote:

> I'm a little baffled by the inconsistency here. Anyone have any
> explanations?
> 
 def gen():
> ...   yield 'a'
> ...   yield 'b'
> ...   yield 'c'
> ...
 [c1 + c2 for c1 in gen() for c2 in gen()]
> ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
> 
 list(c1 + c2 for c1 in gen() for c2 in gen())
> ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
> 
 it1 = gen()
 it2 = gen()
 list(c1 + c2 for c1 in it1 for c2 in it2)
> ['aa', 'ab', 'ac']
> 
> Why does this last list only have three elements instead of nine?


Ah, good one! That had me puzzled for a while too.

The answer is to write it out as a nested for-loop, using print in place 
of yield. Here's the first way:

for c1 in gen():
for c2 in gen():
print c1 + c2

And the second:

it1 = gen()
it2 = gen()
for c1 in it1:
for c2 in it2:
print c1 + c2


In the first example, the inner loop gets refreshed each time through the 
outer loop with a brand new instance of the generator. Expanding the 
loops in full:

# First method:
c1 = 'a'
call gen() to make an iterable
step through the fresh iterable, giving 'aa' 'ab' 'ac'
c1 = 'b'
call gen() to make an iterable
step through the fresh iterable, giving 'ba' 'bb' 'bc'
c1 = 'c'
call gen() to make an iterable
step through the fresh iterable, giving 'ca' 'cb' 'cc'


# Second method:
c1 = 'a'
inner iterable already exists
step through the iterable, giving 'aa' 'ab' 'ac'
c1 = 'b'
inner iterable is exhausted, so do nothing
c1 = 'c'
inner iterable is exhausted, so do nothing


And there you have it. A nice Gotcha for the books.



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


Re: Generator oddity

2009-05-01 Thread Duncan Booth
[email protected] wrote:

 it1 = gen()
 it2 = gen()
 list(c1 + c2 for c1 in it1 for c2 in it2)
> ['aa', 'ab', 'ac']
> 
> Why does this last list only have three elements instead of nine?

First time through the c1 in it1 loop you use up all of the it2 values.
Second and third times through the c1 in it1 loop the it2 generator is 
still exhausted: you need to create a new generator each time if you want 
to repeat its values.


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


Re: Generator oddity

2009-05-01 Thread opstad
D'oh! Many thanks for the explanation; I should have seen that myself.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically declared shared constant/variable imported twice problem

2009-05-01 Thread Gabriel Rossetti

Peter Otten wrote:

Gabriel Rossetti wrote:

  

I have three modules A, B, C;

A declares this globally :

  UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()

Then they import stuff from each other:

- A imports a constant from module B (in "__main__")
- A imports a class and some constants from module C
- B imports a constant from module A
- C imports UPDATE_EVENT_ID from module A

What happens is that since A imports stuff from B which then in turn
loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
an event to C, C doesn't do anything since it doesn't have the same
UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
been a statically defined variable then that wouldn't be "bad", but
since it's dynamic it does pose problems. More globally, how cn I
prevent this even with static constants? I don't think it's that great
to redefine the stuff multiple times and I've already had this problem
in the past with a constant dict.



Imported modules are cached, so this problem shouldn't occur unless you use 
A.py as your main script. In this case it will be cached as "__main__", and 
if you import it elsewhere a second copy will be executed and cached as "A".


To fix it create a wrapper script that doesn't create any objects that will 
be used elsewhere, e. g.


#!/usr/bin/env python
import A
A.main()

assuming the function A.main() contains the code to start your application 
and invoke that instead of A.py


Peter

  
Thank you Peter, I understand better now. I am indeed using A.py as my 
main script. I will create a launcher then, thank you.


Gabriel


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


Re: string processing question

2009-05-01 Thread Kurt Mueller
Sion Arrowsmith wrote:
> Kurt Mueller  wrote:
>> :> python -c 'print unicode("ä", "utf8")'
>> ä
>> :> python -c 'print unicode("ä", "utf8")' | cat
>> Traceback (most recent call last):
>> File "", line 1, in 
>> UnicodeEncodeError: 'ascii' codec can't encode characters in position
>> 0-1: ordinal not in range(128)
> $ python -c 'import sys; print sys.stdout.encoding'
> UTF-8
> $ python -c 'import sys; print sys.stdout.encoding' | cat
> None
>
> If print gets a Unicode string, it does an implicit
> .encode(sys.stdout.encoding or sys.getdefaultencoding()) on it.
> If you want your output to be guaranteed UTF-8, you'll need to
> explicitly .encode("utf8") it yourself.

This works now correct with and without piping:

python -c 'a=unicode("ä", "utf8") ; print (a.encode("utf8"))'



In my python source code I have these two lines first:
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

So the source code itself and the strings in the source code
are interpreted as utf-8.

But from the command line python interprets the code
as 'latin_1' I presume. That is why I have to convert
the "ä" with unicode().
Am I right?


> (I dare say this is slightly different in 3.x .)
I heard about it but I wait to go to 3.x until its time to...




Thanks
-- 
Kurt Mueller

-- 
Kurt Müller, [email protected] 


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


Re: Generator oddity

2009-05-01 Thread Dave Angel

[email protected] wrote:

I'm a little baffled by the inconsistency here. Anyone have any
explanations?

  

def gen():


...   yield 'a'
...   yield 'b'
...   yield 'c'
...
  

[c1 + c2 for c1 in gen() for c2 in gen()]


['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  

list(c1 + c2 for c1 in gen() for c2 in gen())


['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

  

it1 = gen()
it2 = gen()
list(c1 + c2 for c1 in it1 for c2 in it2)


['aa', 'ab', 'ac']

Why does this last list only have three elements instead of nine?


  
The problem is in it2, which is initialized only once.  Thus the second 
time you're going through the c2 loop, it doesn't have any more values.


If you need the indirection provided by those it1 and it2, you need to 
postpone the function evaluation, at least for it2.


I'd do
it1 = gen
it2 = gen

list(c1 + c2 for c1 in it1() for c2 in it2())
or more simply:
[c1 + c2 for c1 in it1() for c2 in it2()]




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


Re: Generator oddity

2009-05-01 Thread Andre Engels
On Fri, May 1, 2009 at 4:25 PM, Dave Angel  wrote:

> The problem is in it2, which is initialized only once.  Thus the second time
> you're going through the c2 loop, it doesn't have any more values.
>
> If you need the indirection provided by those it1 and it2, you need to
> postpone the function evaluation, at least for it2.
>
> I'd do
> it1 = gen
> it2 = gen
>
> list(c1 + c2 for c1 in it1() for c2 in it2())
> or more simply:
> [c1 + c2 for c1 in it1() for c2 in it2()]

Another possibility would be to use the outcome of looping through the
generator rather than the generator itself:

it1 = list(gen())
it2 = list(gen())
[c1 + c2 for c1 in it1() for c2 in it2()]


-- 
André Engels, [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension question

2009-05-01 Thread J Kenneth King
Chris Rebert  writes:

> On Thu, Apr 30, 2009 at 5:56 PM, Ross  wrote:
>> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
>> return a new list of each individual element in these tuples, I can do
>> it with a nested for loop but when I try to do it using the list
>> comprehension b = [j for j in i for i in a], my output is b =
>> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
>> doing wrong?
>
> Your comprehension is the identity comprehension (i.e. it effectively
> just copies the list as-is).
> What you're trying to do is difficult if not impossible to do as a
> comprehension.
>
> Here's another approach:
> b = list(itertools.chain.from_iterable(a))
>
> And without using a library function:
> b = []
> for pair in a:
> for item in pair:
> b.append(item)

This is much more clear than a nested comprehension.

I love comprehensions, but abusing them can lead to really dense and
difficult to read code.
--
http://mail.python.org/mailman/listinfo/python-list


Passing a function as an argument from within the same class?

2009-05-01 Thread zealalot
So, I'm trying to come up with a way to pass a method (from the same
class) as the default argument for another method in the same class.
Unfortunately though, I keep getting "self not defined" errors since
the class hasn't been read completely before it references itself.

Is there a better way of doing this?

--- CODE ---

class SomeClass():
def doNothing(self):
pass
def function1(self):
print "Running function 1."
def function2(self, passedFunction=self.doNothing):
print "Running passed function."
passedFunction()

someObject = SomeClass()
someObject.function2(someobject.function1)

--- CODE ---

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


Re: where to start with

2009-05-01 Thread CTO
I'd add http://diveintopython.org/ to that list as you gain more
experience with Python, or if you already know at least one language.

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


Re: Python servlet for Java applet ?

2009-05-01 Thread Linuxguy123

On Thu, 2009-04-30 at 14:06 +0200, Piet van Oostrum wrote:

> Well, for a start, in the Python world the word 'servlet' isn't used
> much. So I assume it comes from your Java legacy and you actually mean
> 'any server-side python script' (in webware they use the term
> 'servlet', however).



I agree that servlet isn't a Python term.  I should have put it in
quotes. 

Your reply was very instructive.  Thanks for taking the time to post
it. 

I am probably going to build my "servlet" as a simple Python socket
service. 

LG



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


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread bearophileHUGS
Zealalot, probably there are some ways to do that, but a simple one is
the following (not tested):

def function2(self, passed_function=None):
if passed_function is None:
passed_function = self.doNothing
...

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


Re: What do you think of ShowMeDo

2009-05-01 Thread Peter Pearson
On Fri, 1 May 2009 07:53:35 +1000, Paul Hemans  wrote:
[snip]
> them as they have been recorded in the anals of the web, however I 
.^

That's the second time in this thread.  The first might have been
deliberate gross wordplay, but now it's time for somebody to point
out that maybe this word doesn't mean what you think it means.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
--
http://mail.python.org/mailman/listinfo/python-list



Re: Passing a function as an argument from within the same class?

2009-05-01 Thread CTO
Make doNothing a classmethod.

class SomeClass:

@classmethod
def doNothing(cls):
pass

def function1(self):
print "Running function 1"

def function2(self, passedFunction=SomeClass.doNothing):
print "Running passed function"
passedFunction()

someObject = SomeClass()
someObject.function2()
someObject.function2(someObject.function1)


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


Re: How to measure the memory cost in Python?

2009-05-01 Thread CTO
Not OP, but I'd actually like to know if there's an answer to this one
that doesn't involve platform-specific tools.

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


Re: What do you think of ShowMeDo

2009-05-01 Thread Grant Edwards
On 2009-05-01, Peter Pearson  wrote:
> On Fri, 1 May 2009 07:53:35 +1000, Paul Hemans  wrote:
> [snip]
>> them as they have been recorded in the anals of the web, however I 
>.^
>
> That's the second time in this thread.  The first might have been
> deliberate gross wordplay, but now it's time for somebody to point
> out that maybe this word doesn't mean what you think it means.

You've got to admit it's rather funny. :)

-- 
Grant Edwards   grante Yow! I just heard the
  at   SEVENTIES were over!!  And
   visi.comI was just getting in touch
   with my LEISURE SUIT!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to measure the memory cost in Python?

2009-05-01 Thread Chris Rebert
On Fri, May 1, 2009 at 7:54 AM, CTO  wrote:
> Not OP, but I'd actually like to know if there's an answer to this one
> that doesn't involve platform-specific tools.

sys.getsizeof() [a suggested solution] isn't platform-specific.

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


Re: urllib2 and threading

2009-05-01 Thread robean
Thanks for your reply. Obviously you make several good points about
Beautiful Soup and Queue. But here's the problem: even if I do nothing
whatsoever with the threads beyond just visiting the urls with
urllib2, the program chokes. If I replace

  else:
ulock.acquire()
print page.geturl() # obviously, do something more useful
here,eventually
page.close()
ulock.release()

with

  else:
pass

the urllib2 starts raising URLErrros after the first 3 - 5 urls have
been visited. Do you have any sense what in the threads is corrupting
urllib2's behavior?  Many thanks,

Robean



On May 1, 12:27 am, Paul Rubin  wrote:
> robean  writes:
> > reach the urls with urllib2. The actual program will involve fairly
> > elaborate scraping and parsing (I'm using Beautiful Soup for that) but
> > the example shown here is simplified and just confirms the url of the
> > site visited.
>
> Keep in mind Beautiful Soup is pretty slow, so if you're doing a lot
> of pages and have multiple cpu's, you probably want parallel processes
> rather than threads.
>
> > wrong? I am new to both threading and urllib2, so its possible that
> > the SNAFU is quite obvious..
> > ...
> > ulock = threading.Lock()
>
> Without looking at the code for more than a few seconds, using an
> explicit lock like that is generally not a good sign.  The usual
> Python style is to send all inter-thread communications through
> Queues.  You'd dump all your url's into a queue and have a bunch of
> worker threads getting items off the queue and processing them.  This
> really avoids a lot of lock-related headache.  The price is that you
> sometimes use more threads than strictly necessary.  Unless it's a LOT
> of extra threads, it's usually not worth the hassle of messing with
> locks.

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


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread zealalot
On May 1, 10:50 am, CTO  wrote:
> Make doNothing a classmethod.
>
> class SomeClass:
>
>     @classmethod
>     def doNothing(cls):
>         pass
>
>     def function1(self):
>         print "Running function 1"
>
>     def function2(self, passedFunction=SomeClass.doNothing):
>         print "Running passed function"
>         passedFunction()
>
> someObject = SomeClass()
> someObject.function2()
> someObject.function2(someObject.function1)

It's not surprising, but I've never heard of a classmethod before.
Basically, I read that it basically removes the need for the 'self'
argument.  Very cool!

And thanks for the quick response,

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


Re: where to start with

2009-05-01 Thread Kyle Terry
On Fri, May 1, 2009 at 7:38 AM, CTO  wrote:

> I'd add http://diveintopython.org/ to that list as you gain more
> experience with Python, or if you already know at least one language.


Wasn't diveintopython in his list?

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


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Peter Otten
CTO wrote:

> Make doNothing a classmethod.
> 
> class SomeClass:
> 
> @classmethod
> def doNothing(cls):
> pass
> 
> def function1(self):
> print "Running function 1"
> 
> def function2(self, passedFunction=SomeClass.doNothing):
> print "Running passed function"
> passedFunction()
> 
> someObject = SomeClass()
> someObject.function2()
> someObject.function2(someObject.function1)

To make that run without error you have to jump through a lot of hoops:

class SomeClass(object):
@classmethod
def doNothing(cls):
pass
def function1(self):
print "Running function 1"

def function2(self, passedFunction=SomeClass.doNothing):
print "Running passed function"
passedFunction()

SomeClass.function2 = function2

someObject = SomeClass()
someObject.function2()
someObject.function2(someObject.function1)

And if you don't need access to the instance you may not need access to the 
class either. In this case you can simplify:

def doNothing():
pass

class SomeClass(object):
def function1(self):
print "Running function 1"
def function2(self, passedFunction=doNothing):
print "Running passed function"
passedFunction()

If you do need information about the state of the instance you can either 
pass it explicitly

class SomeClass(object):
def doNothing(self):
pass
def function1(self):
print "Running function 1"
def function2(self, passedFunction=doNothing):
print "Running passed function"
passedFunction.__get__(self)()

or (better, I think) use a sentinel as shown by Bearophile.

Peter

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


Re: list comprehension question

2009-05-01 Thread Emile van Sebille

On 5/1/2009 7:31 AM J Kenneth King said...

Chris Rebert  writes:

b = []
for pair in a:
for item in pair:
b.append(item)


This is much more clear than a nested comprehension.

I love comprehensions, but abusing them can lead to really dense and
difficult to read code.


I disagree on dense and difficult, although I'll leave open the question 
of abuse.


b = [ item for pair in a for item in pair ]

This is exactly the code above expressed in comprehension form.

It's worth knowing that a list comprehension is structured identically 
to the equivalent for loop.  So it really is neither more dense nor more 
difficult to read.  Further, you can tell immediately from the start of 
the list comprehension what you've got -- in this case a list of item(s).


Here with some slight changes...

>>> a = [(1, 2), (3, 4, 7), (5, 6)]
>>> [ item for j in a if len(j)==2 for item in j if item % 2 ]
[1, 5]

...opposed to...

>>> for j in a:
... if len(j)==2:
... for item in j:
... if item % 2:
... b.append(item)
...
>>> b
[1, 5]
>>>

YMMV,

Emile


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


Re: string processing question

2009-05-01 Thread Scott David Daniels

Kurt Mueller wrote:

Scott David Daniels schrieb:

To discover what is happening, try something like:
python -c 'for a in "ä", unicode("ä"): print len(a), a'

I suspect that in your encoding, "ä" is two bytes long, and in
unicode it is converted to to a single character.


:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a'
2 ä
1 ä
:>

Yes it is. That is one of the two problems I see.
The solution for this is to unicode(, ) each string.


I'd like to have my python programs unicode enabled.




:> python -c 'for a in "ä", unicode("ä"): print len(a), a'
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:
ordinal not in range(128)

It seems that the default encoding is "ascii", so unicode() cannot cope
with "ä".
If I specify "utf8" for the encoding, unicode() works.

:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a'
2 ä
1 ä
:> 



But the print statement yelds an UnicodeEncodeError
if I pipe the output to a program or a file.

:> python -c 'for a in "ä", unicode("ä", "utf8"): print len(a), a' | cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)
2 ä
1 :>


So it seems to me, that piping the output changes the behavior of the
print statement:

:> python -c 'for a in "ä", unicode("ä", "utf8", "ignore"): print a,
len(a), type(a)'
ä 2 
ä 1 

:> python -c 'for a in "ä", unicode("ä", "utf8", "ignore"): print a,
len(a), type(a)'  | cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)
ä 2 
:>




How can I achieve that my python programs are unicode enabled:
- Input strings can have different encodings (mostly ascii, latin_1 or utf8)
- My python programs should always output "utf8".

Is that a good idea??


OK, the issue here is your use of -c, rather than an actual source file.
I don't know how to make -c take the magic initial encoding line.
If you rely on ascii source, you are safe, but have to write things like
 ms = u'That would be na\u00EFve'
 or  ms = u'That would be na\xEFve.'
 or  ms = u'That would be na\N{LATIN SMALL LETTER I WITH DIAERESIS}ve.'

If you do put an encoding line in your source (first or second line):
 # -*- coding: utf-8 -*-
 or  # -*- coding: iso-8859-1 -*-
 or  # -*- coding: latin-1 -*-

you can (later in that file) simply use:
 ms = u'That would be naïve.'

That is, I would avoid non-ascii source for plain strings in 2.X unless
you have a _very_ good reason; use it, instead, for unicode strings.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille  writes:

> On 5/1/2009 7:31 AM J Kenneth King said...
>> Chris Rebert  writes:
>>> b = []
>>> for pair in a:
>>> for item in pair:
>>> b.append(item)
>>
>> This is much more clear than a nested comprehension.
>>
>> I love comprehensions, but abusing them can lead to really dense and
>> difficult to read code.
>
> I disagree on dense and difficult, although I'll leave open the
> question of abuse.
>
> b = [ item for pair in a for item in pair ]
>
> This is exactly the code above expressed in comprehension form.

If the comprehension above is an abuse, then every nested list
comprehension is an abuse of comprehensions so they might as well not be
in the language...

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


Re: [Python-Dev] .pth files are evil

2009-05-01 Thread Chris Withers

M.-A. Lemburg wrote:

"""
If the package really requires adding one or more directories on sys.path (e.g.
because it has not yet been structured to support dotted-name import), a "path
configuration file" named package.pth can be placed in either the site-python or
site-packages directory.
...
A typical installation should have no or very few .pth files or something is
wrong, and if you need to play with the search order, something is very wrong.
"""


I'll say! I think .pth files are absolute evil and I wish they could 
just be banned.


+1 on anything that makes them closer to going away or reduces the 
possibility of yet another similar feature from hurting the 
comprehensibility of a python setup.


Chris

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


object query assigned variable name?

2009-05-01 Thread warpcat
I've passed this around some other groups, and I'm being told
"probably not possible".  But I thought I'd try here as well :)   I
*did* search first, and found several similar threads, but they
quickly tangented into other specifics of the language that were a bit
over my head :)  At any rate, here's a simple example, I'd love to
know if as shown, is somehow possible:

Given an object:

class Spam(object):
def __init__(self):
# stuff

I'd like it to print, when instanced, something like this:

>>> s = Spam()
I’m assigned to s!

But it seems prohibitively hard (based on my web and forum searches)
for an object to know what variable name is has been assigned to when
created.  Querying 'self' in __init__ returns a memory location, not
the variable name passed in.

If you're wondering why I'm trying to figure this out, this is just
part of my continued learning of the language and pushing the bounds,
to see what is possible ;)

Any thoughts?
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: little help for stupid people?

2009-05-01 Thread Chris Withers

M.-A. Lemburg wrote:

The much more common use case is that of wanting to have a base package
installation which optional add-ons that live in the same logical
package namespace.

The PEP provides a way to solve this use case by giving both developers
and users a standard at hand which they can follow without having to
rely on some non-standard helpers and across Python implementations.

My proposal tries to solve this without adding yet another .pth
file like mechanism - hopefully in the spirit of the original Python
package idea.


Okay, I need to issue a plea for a little help.

I think I kinda get what this PEP is about now, and as someone who wants 
 to ship a base package with several add-ons that live in the same 
logical package namespace, I'm very interested.


However, despite trying to follow this thread *and* having tried to read 
the PEP a couple of times, I still don't know how I'd go about doing this.


I did give some examples from what I'd be looking to do much earlier.

I'll ask again in the vague hope of you or someone else explaining 
things to me like I'm a 5 year old - something I'm mentally equipped to 
be well ;-)


In either of the proposals on the table, what code would I write and 
where to have a base package with a set of add-on packages?


Simple examples would be greatly appreciated, and might bring things 
into focus for some of the less mentally able bystanders - like myself!


cheers,

Chris

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-05-01 Thread Chris Withers

P.J. Eby wrote:

At 06:15 PM 4/15/2009 +0200, M.-A. Lemburg wrote:

The much more common use case is that of wanting to have a base package
installation which optional add-ons that live in the same logical
package namespace.


Please see the large number of Zope and PEAK distributions on PyPI as 
minimal examples that disprove this being the common use case.  


If you mean "the common use case as opposed to having code in the 
__init__.py of the namespace package", I think you'll find that's 
because people (especially me!) don't know how to do this, not because 
we don't want to!


Chris - who would actually like to know how to do this, with or without 
the PEP, and how to indicate interdependencies in situations like this 
to setuptools...


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


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Scott David Daniels

zealalot wrote:

On May 1, 10:50 am, CTO  wrote:

Make doNothing a classmethod.

class SomeClass:
@classmethod
def doNothing(cls):
...
def function2(self, passedFunction=SomeClass.doNothing):
print "Running passed function"
passedFunction()
...

It's not surprising, but I've never heard of a classmethod before.
Basically, I read that it basically removes the need for the 'self'
argument.  Very cool!


Careful, bearophiles' answer remains the best one.

The only reason your example worked is that you had already had
SomeClass defined (probably from a previous experiment).
At the time you are executing the "def" statements for the
methods of SomeClass, SomeClass is not yet defined.  the default
arguments are evaluated at "def" time, but the method body is
evaluated at methd invokation time (well past the time SomeClass
is fully built.

Typically, classmethod is used to provide alternate constructors
for a class, though it does have other uses.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 and threading

2009-05-01 Thread Stefan Behnel
robean wrote:
> I am writing a program that involves visiting several hundred webpages
> and extracting specific information from the contents. I've written a
> modest 'test' example here that uses a multi-threaded approach to
> reach the urls with urllib2. The actual program will involve fairly
> elaborate scraping and parsing (I'm using Beautiful Soup for that)

Try lxml.html instead. It often parses HTML pages better than BS, can parse
directly from HTTP/FTP URLs, frees the GIL doing so, and is generally a lot
faster and more memory friendly than the combination of urllib2 and BS,
especially when threading is involved. It also supports CSS selectors for
finding page content, so your "elaborate scraping" might actually turn out
to be a lot simpler than you think.

http://codespeak.net/lxml/

These might be worth reading:

http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/
http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/

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


Re: list comprehension question

2009-05-01 Thread Shane Geiger
from goopy.functional import flatten  # 
http://sourceforge.net/projects/goog-goopy/

b = [(1,2), (3,4), (5,6)]
print flatten(b)



#from goopy.functional import flatten  # 
http://sourceforge.net/projects/goog-goopy/


def flatten(seq):
 """
 Returns a list of the contents of seq with sublists and tuples "exploded".
 The resulting list does not contain any sequences, and all inner sequences
 are exploded.  For example:

 >>> flatten([7,(6,[5,4],3),2,1])
 [7,6,5,4,3,2,1]
 """
 lst = []
 for el in seq:
   if type(el) == list or type(el) is tuple:
 lst.extend(flatten(el))
   else:
 lst.append(el)
 return lst



Chris Rebert wrote:

On Thu, Apr 30, 2009 at 5:56 PM, Ross  wrote:
  

If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to
return a new list of each individual element in these tuples, I can do
it with a nested for loop but when I try to do it using the list
comprehension b = [j for j in i for i in a], my output is b =
[5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I
doing wrong?



Your comprehension is the identity comprehension (i.e. it effectively
just copies the list as-is).
What you're trying to do is difficult if not impossible to do as a
comprehension.

Here's another approach:
b = list(itertools.chain.from_iterable(a))

And without using a library function:
b = []
for pair in a:
for item in pair:
b.append(item)

Cheers,
Chris
  



--
Shane Geiger, IT Director
Council For Economic Education / www.councilforeconed.org
[email protected]  / 402-438-8958

Teaching Opportunity

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


Re: object query assigned variable name?

2009-05-01 Thread Scott David Daniels

warpcat wrote:

I've passed this around some other groups, and I'm being told
"probably not possible".  But I thought I'd try here as well :)  ...
Given an object:
class Spam(object):
def __init__(self):
# stuff
I'd like it to print, when instanced, something like this:

s = Spam()

I’m assigned to s!

But it seems prohibitively hard (based on my web and forum searches)
for an object to know what variable name is has been assigned to when
created.


Here's an example of why it is impossibly hard.  What would you want the
following code to print?:

print 'NoneUnique:', Spam() is Spam()

Objects are created and then used somehow (including being put
somewhere), not created int a "variable."

--Scott David Daniels
[email protected]

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


Re: How to measure the memory cost in Python?

2009-05-01 Thread Jean
On May 1, 7:54 am, CTO  wrote:
> Not OP, but I'd actually like to know if there's an answer to this one
> that doesn't involve platform-specific tools.

Depending on what you need and the O/S you are using, this recipe may
help

  

That recipe also appeared in the 2nd edition of the Python Cookbook,
see

  

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


Re: [Python-Dev] PEP 382: little help for stupid people?

2009-05-01 Thread Martin v. Löwis
> In either of the proposals on the table, what code would I write and
> where to have a base package with a set of add-on packages?

I don't quite understand the question. Why would you want to write code
(except for the code that actually is in the packages)?

PEP 382 is completely declarative - no need to write code.

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


Re: [Python-Dev] .pth files are evil

2009-05-01 Thread Scott David Daniels

Chris Withers wrote:

M.-A. Lemburg wrote:

"""
If the package really requires adding one or more directories on 
sys.path (e.g. because it has not yet been structured to support

dotted-name import), a "path configuration file" named package.pth
can be placed in either the site-python or site-packages directory
I'll say! I think .pth files are absolute evil and I wish they could 
just be banned.
+1 on anything that makes them closer to going away or reduces the 
possibility of yet another similar feature from hurting the 
comprehensibility of a python setup.


".pth" files are great when used in moderation. Especially when used
with the new per-user location searched when setting up sys.path.
I can move my current project's directory name into a .pth file that
I change as I switch projects.  I can also throw in a reference to
my convenience tools (dinky little functions and classes that I would
otherwise waste time scratching together each time I needed them).
They obviate the need to fiddle with site.py.

--Scott David Daniels
[email protected]

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


Re: Multiprocessing Pool and functions with many arguments

2009-05-01 Thread Piet van Oostrum
> "[email protected]"  (P) wrote:

>P> I'm trying to get to grips with the multiprocessing module, having
>P> only used ParallelPython before.

>P> based on this example:

>P> http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers

>P> what happens if I want my "f" to take more than one argument? I want
>P> to have a list of tuples of arguments and have these correspond the
>P> arguments in f, but it keeps complaining that I only have one argument
>P> (the tuple). Do I have to pass in a tuple and break it up inside f? I
>P> can't use multiple input lists, as I would with regular map.

You give the tuple of the arguments for the function:

def f(a, b, c):
return a + b * c

pool = Pool(processes=4)  # start 4 worker processes

result = pool.apply(f, (2, 3, 4))# evaluate "f(2, 3, 4)" 

Or if you have a list:

args = [ (2, 3, 4), # arguments for call 1
  (5, 6, 7) # arguments for call 2
]

print [pool.apply(f, a) for a in args]

However, as each call to apply wait for its results, this will execute
sequentially instead of parallel.

You can't use map directly as it works only with single argument functions. 
>>> print pool.map(f, args) 
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py",
 line 148, in map
return self.map_async(func, iterable, chunksize).get()
  File 
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py",
 line 422, in get
raise self._value
TypeError: f() takes exactly 3 arguments (1 given)

Is that what you mean?

But you can use a wrapper function:

def wrapf(abc):
return f(*abc)

[later...]

print pool.map(wrapf, args) 

This is covered in the examples section of multiprocessing (see
calculate and calculatestar for example).

Or you can use apply_async and later wait for the results:

results = [pool.apply_async(f, a) for a in args]
print [r.get() for r in results]

Now the calls to f are done in parallel, which you can check by putting
a sleep inside f.

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


Re: object query assigned variable name?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote:

> I'd like it to print, when instanced, something like this:
> 
 s = Spam()
> I’m assigned to s!
> 
> But it seems prohibitively hard (based on my web and forum searches) for
> an object to know what variable name is has been assigned to when
> created.

Can't be done. Objects don't know what names they are bound to.

Furthermore, your question is based on an invalid assumption. Objects 
aren't necessarily bound to a single name, they can be bound to zero, 
one, or more names.

What name should Spam print in these examples?

alist = [1, 2, Spam(), 4]
alist.append(Spam())
x = y = z = Spam()



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


Re: [Python-Dev] PEP 382: little help for stupid people?

2009-05-01 Thread Chris Withers

Martin v. Löwis wrote:

In either of the proposals on the table, what code would I write and
where to have a base package with a set of add-on packages?


I don't quite understand the question. Why would you want to write code
(except for the code that actually is in the packages)?

PEP 382 is completely declarative - no need to write code.


"code" is anything I need to write to make this work...

So, what do I need to do?

Chris

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


Re: Re: list comprehension question

2009-05-01 Thread John Posner

Shane Geiger wrote:

   if type(el) == list or type(el) is tuple:

A tiny improvement:

   if type(el) in (list, tuple):

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


Re: object query assigned variable name?

2009-05-01 Thread David Robinow
On Fri, May 1, 2009 at 12:24 PM, warpcat  wrote:
> I've passed this around some other groups, and I'm being told
> "probably not possible".  But I thought I'd try here as well :)   I
> *did* search first, and found several similar threads, but they
> quickly tangented into other specifics of the language that were a bit
> over my head :)  At any rate, here's a simple example, I'd love to
> know if as shown, is somehow possible:
>
> Given an object:
>
> class Spam(object):
>    def __init__(self):
>        # stuff
>
> I'd like it to print, when instanced, something like this:
>
 s = Spam()
> I’m assigned to s!
>
> But it seems prohibitively hard (based on my web and forum searches)
> for an object to know what variable name is has been assigned to when
> created.  Querying 'self' in __init__ returns a memory location, not
> the variable name passed in.
>
> If you're wondering why I'm trying to figure this out, this is just
> part of my continued learning of the language and pushing the bounds,
> to see what is possible ;)
>
> Any thoughts?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Others have explained to you that this is not possible.
I'll just point out that your method for learning the language is not optimal.
If you had gotten a recipe to do what you asked, how would it help you
write better programs?
I suggest that you start programming. If you get stumped, feel free to ask here.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 08:11:01 -0700, zealalot wrote:

> On May 1, 10:50 am, CTO  wrote:
>> Make doNothing a classmethod.
>>
>> class SomeClass:
>>
>>     @classmethod
>>     def doNothing(cls):
>>         pass
>>
>>     def function1(self):
>>         print "Running function 1"
>>
>>     def function2(self, passedFunction=SomeClass.doNothing):
>>         print "Running passed function"
>>         passedFunction()
>>
>> someObject = SomeClass()
>> someObject.function2()
>> someObject.function2(someObject.function1)
> 
> It's not surprising, but I've never heard of a classmethod before.
> Basically, I read that it basically removes the need for the 'self'
> argument.  Very cool!

Not so.

When you call an ordinary method (an "instance method"), Python 
automatically inserts the object itself as the first argument. So you 
need to define the method with one extra parameter, self.

(Using the name self is just a convention. You can call it anything you 
like.)

For classmethods, Python automatically inserts not the object itself, but 
the object's *class* as the first argument. So you still need to define 
the method with an extra parameter, only now the convention is to call is 
cls rather than self.

Here's an example:

class Test(object):
x = 0  # class attribute
def __init__(self, x):
self.x = x  # instance attribute
def spam(self):
print self, self.x
@classmethod
def ham(cls):
print cls, cls.x


And in use:

>>> t = Test(42)
>>> t.spam()
<__main__.Test object at 0xb76c> 42
>>> t.ham()
 0


There is one other related built-in decorator, staticmethod(). 
staticmethod() tells Python not to pass any extra argument to the method. 
That means the inside a static method, you can't refer to either the 
instance (self) or the class! Needless to say, there aren't very many 
uses for staticmethod(). 



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


Re: object query assigned variable name?

2009-05-01 Thread AKEric
On May 1, 9:48 am, Steven D'Aprano  wrote:
> On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote:
> > I'd like it to print, when instanced, something like this:
>
>  s = Spam()
> > I’m assigned to s!
>
> > But it seems prohibitively hard (based on my web and forum searches) for
> > an object to know what variable name is has been assigned to when
> > created.
>
> Can't be done. Objects don't know what names they are bound to.
>
> Furthermore, your question is based on an invalid assumption. Objects
> aren't necessarily bound to a single name, they can be bound to zero,
> one, or more names.
>
> What name should Spam print in these examples?
>
> alist = [1, 2, Spam(), 4]
> alist.append(Spam())
> x = y = z = Spam()
>
> --
> Steven

And getting back all the pointers is fine with me.  But this sounds
like a dead end, and I was just looking for confirmation of that.
Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 07:35:40 -0700, zealalot wrote:

> So, I'm trying to come up with a way to pass a method (from the same
> class) as the default argument for another method in the same class.
> Unfortunately though, I keep getting "self not defined" errors since the
> class hasn't been read completely before it references itself.
> 
> Is there a better way of doing this?

My first instinct is to say "Don't do that!", but let's see if there's a 
way to get what you want. It's actually very easy: just put the 
definition of the passed method before the method you want to use it in, 
then refer to it by name *without* self.

However, there is a catch: you need to manually pass in the instance, 
instead of letting Python do it for you.

class Spam(object):
def ham(self):
return "ham"
def spam(self, func=ham):
return "spam is a tasty %s-like food product" % func(self)


And in use:

>>> obj = Spam()
>>> obj.ham()
'ham'
>>> obj.spam()
'spam is a tasty ham-like food product'



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


Re: Importing modules

2009-05-01 Thread norseman

Gabriel Genellina wrote:

En Thu, 30 Apr 2009 14:33:38 -0300, Jim Carlock escribió:


I'm messing around with a program right at the moment. It
ends up as two applications, one runs as a server and one
as a client which presents a Window. It almost works, so I
need to work through it to work out it's bugs, and I'll be
rewriting it in a couple other languages.

Python looks fairly simple. It's a lot of "import" commands
and then "from" statements to do more importing. So I need
to figure out what the difference is between the two, as
the "from" seems to provide a way to identify what it wants
to import from the library/module.


Start by reading http://docs.python.org/tutorial
In particular, section 6: Modules


I find it odd that no one includes the FQFN and employs a
short name.


fully.qualified.module.name is long to type, error prone, and slow -- 
Python has to resolve each dotted name at runtime, every time it's used.


So writting this is common:
from fully.qualified.module import name
and then, just use `name` in the code. By looking at the `import` lines, 
usually located at the top, you know where a certain name comes from.


...unless there are statements like this:
from somemodule import *
which are considered bad practice anyway.


Nothing seems to get SET in the Environment to
identify where the library gets configured. I have to run
off to find help on the differences between "import" and
"from".


No need for that, usually. There is a default library search path that 
is built relative to the interpreter location. That is, if the Python 
interpreter used is /usr/some/fancy/directory/python, then the standard 
library is at /usr/some/fancy/directory/lib/python2.6, additional 
packages are at /usr/some/fancy/directory/lib/python2.6/site-packages, etc.


You *can* alter the search path by setting some environment variables, 
but I don't like that.



--
I would like to make a small comment here on Python locations.
Windows seems to have to have a slight difference.

...\python\Lib   #py libs go here
...\pythonVer\Lib\site-packages  #addons usually go here same as Linux
...\pythonVer\libs #seems to hold MicroSoft specific py libs


Like Gabriel - my Linux is same as he describes.
   except I don't have a /usr/...ory/python
   in my case it's all in   /usr/local/lib/python2.5
 to keep it all in one place
 /usr/local/bin   has the soft links to run it
 (reduces PATH size.  /usr/local/bin  already there)

Which shows how flexible Python is.


OH - something you mentioned that didn't seem to be addressed.
import - load a complete library
from   - obtain specific 'function'(s) from a library

(from mylib import fopen, fclose or  from mylib import f*
   one or more   gets fopen, fclose, etc

 from mylib import fopen as myfopen   can be useful  myfopen(...)
  won't be confused with the system
  fopen(...)
 which is great for not having to  MyVeryLongNameLib.fopen()  :)
)

The cautionary note here is, IF two libs have a function with the same 
name and 'from' is used to get it from each, you wind up the last one

requested.
To alleviate the problem you can  open(...)  or mylib.open(...) and thus 
access both (or even more) in the same program.  Check the built-ins and 
the libs for name problems and use the techniques noted to get what you 
want.


Not sure what is in a given library?
import somelib
help(somelib)
  (and read)

When it come to showing off, Python has its moments!

HTH

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


Re: object query assigned variable name?

2009-05-01 Thread AKEric
On May 1, 10:03 am, David Robinow  wrote:
> On Fri, May 1, 2009 at 12:24 PM, warpcat  wrote:
> > I've passed this around some other groups, and I'm being told
> > "probably not possible".  But I thought I'd try here as well :)   I
> > *did* search first, and found several similar threads, but they
> > quickly tangented into other specifics of the language that were a bit
> > over my head :)  At any rate, here's a simple example, I'd love to
> > know if as shown, is somehow possible:
>
> > Given an object:
>
> > class Spam(object):
> >    def __init__(self):
> >        # stuff
>
> > I'd like it to print, when instanced, something like this:
>
>  s = Spam()
> > I’m assigned to s!
>
> > But it seems prohibitively hard (based on my web and forum searches)
> > for an object to know what variable name is has been assigned to when
> > created.  Querying 'self' in __init__ returns a memory location, not
> > the variable name passed in.
>
> > If you're wondering why I'm trying to figure this out, this is just
> > part of my continued learning of the language and pushing the bounds,
> > to see what is possible ;)
>
> > Any thoughts?
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> Others have explained to you that this is not possible.
> I'll just point out that your method for learning the language is not optimal.
> If you had gotten a recipe to do what you asked, how would it help you
> write better programs?
> I suggest that you start programming. If you get stumped, feel free to ask 
> here.

wow

No one has yet told me it isn't possible (until other posts in this
thread), they just alluded to it.  Common replies are "well, maybe
this way, or maybe that way".  I myself help on other language forums
(that I know far better than Python), and sometimes it's helpful for
someone to just say "no, you can't do it" (if you truly know you
can't), which is what I was looking for here.

But to argue the way I'm learning the language isn't optimal is a bit
short-sited, based on a single-post query.  It's valuable to both know
what you can, can't, should and shouldn't do, and one shouldn't be
debased for asking such questions.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread CTO
> Careful, bearophiles' answer remains the best one.
>
> The only reason your example worked is that you had already had
> SomeClass defined (probably from a previous experiment).

Scott is correct, and if bearophile and I ever give you conflicting
advice, take bearophile's.

A (corrected) bit of code that might serve your purpose would be
as follows:

class SomeClass:

def doNothing():
pass

def function1(self):
print "running function 1"

def function2(self, passedFunction=doNothing):
print "running passed function"
passedFunction()

again, though- bearophile's is the best, and most standard,
answer here
--
http://mail.python.org/mailman/listinfo/python-list


Re: object query assigned variable name?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 13:03:56 -0400, David Robinow wrote:

> Others have explained to you that this is not possible. I'll just point
> out that your method for learning the language is not optimal. If you
> had gotten a recipe to do what you asked, how would it help you write
> better programs?

Exactly the same way that knowing that lists have an append() method, but 
dicts do not, will help you to write better programs. If you don't know 
what tools the language offers, how are you supposed to write *any* 
programs, let alone "better" ones?


> I suggest that you start programming. If you get stumped, feel free to
> ask here.

There's nothing wrong with experimenting with your tools, getting a feel 
for what they can do, pushing the boundaries with them. You might not get 
any useful code out of such games, but when you start writing code, you 
will already know what tools are available.

Warpcat, don't be ashamed about experimenting with Python and learning 
the language before getting into "serious" programming. There's 
absolutely nothing wrong with such curiosity.


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


Re: list comprehension question

2009-05-01 Thread Emile van Sebille

On 5/1/2009 9:19 AM Arnaud Delobelle said...

Emile van Sebille  writes:

On 5/1/2009 7:31 AM J Kenneth King said...

I love comprehensions, but abusing them can lead to really dense and
difficult to read code.

I disagree on dense and difficult, although I'll leave open the
question of abuse.

b = [ item for pair in a for item in pair ]

This is exactly the code above expressed in comprehension form.


If the comprehension above is an abuse, then every nested list
comprehension is an abuse of comprehensions so they might as well not be
in the language...



I hope you didn't understand me to say that this usage is abuse.  That's 
certainly not what I mean.  I have, however, examples in my code base 
that _would_ qualify as abuse (mostly from pre dictionary comprehension 
vintage where I would build a dict from within a list comp), and I have 
seen enough from this group to know that abuse abounds, with only slight 
provocation.  :)


Fondly-remembering-byte-code-hacks-and-introspection-ly y'rs,

Emile

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


Re: [Python-Dev] PEP 382: little help for stupid people?

2009-05-01 Thread Martin v. Löwis
>>> In either of the proposals on the table, what code would I write and
>>> where to have a base package with a set of add-on packages?
>>
>> I don't quite understand the question. Why would you want to write code
>> (except for the code that actually is in the packages)?
>>
>> PEP 382 is completely declarative - no need to write code.
> 
> "code" is anything I need to write to make this work...
> 
> So, what do I need to do?

Ok, so create three tar files:

1. base.tar, containing

   simplistix/
   simplistix/__init__.py

2. addon1.tar, containing

   simplistix/addon1.pth (containing a single "*")
   simplistix/feature1.py

3. addon2.tar, containing

   simplistix/addon2.pth
   simplistix/feature2.py

Unpack each of them anywhere on sys.path, in any order.

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


ctypes: reference of a struct member?

2009-05-01 Thread ma
If I have this struct in C:

struct spam {
 int ham;
  char foo;
};


if I have this declaration:
struct spam s_;

If I wanted to pass a reference to a function of s_'s foo character, I
can do something like this:

somefunc(&s_.foo)

How do I do the same thing in ctypes?
ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1)
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille  writes:

> On 5/1/2009 9:19 AM Arnaud Delobelle said...
>> Emile van Sebille  writes:
>>> On 5/1/2009 7:31 AM J Kenneth King said...
 I love comprehensions, but abusing them can lead to really dense and
 difficult to read code.
>>> I disagree on dense and difficult, although I'll leave open the
>>> question of abuse.
>>>
>>> b = [ item for pair in a for item in pair ]
>>>
>>> This is exactly the code above expressed in comprehension form.
>>
>> If the comprehension above is an abuse, then every nested list
>> comprehension is an abuse of comprehensions so they might as well not be
>> in the language...
>>
>
> I hope you didn't understand me to say that this usage is abuse.

Emile: sorry, the way I was quoting earlier posts was unclear.  I was
refering to the post you quoted in your message, which describe the
nested list comprehension as abuse.  As supporting evidence for my reply
I included your example of nested list comprehension, which is one of
the simplest possible ones one can think of.

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


Re: How to measure the memory cost in Python?

2009-05-01 Thread CTO
> sys.getsizeof() [a suggested solution] isn't platform-specific.

So, to answer the OP's question, you'd just do something like

def get_totalsize(obj):
total_size = sys.getsizeof(obj)
for value in vars(obj).values():
try: total_size += get_total_size(value)
except: total_size += sys.getsizeof(value)
return totalSize

def get_current_size(env):
size = 0
for value in env.values():
try: size += get_total_size(value)
except: pass
return size

get_current_size(vars())

and discount the weight of the interpreter?
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 and threading

2009-05-01 Thread shailen . tuli
For better performance, lxml easily outperforms Beautiful Soup.

For what its worth, the code runs fine if you switch from urllib2 to
urllib (different exceptions are raised, obviously). I have no
experience using urllib2 in a threaded environment, so I'm not sure
why it breaks; urllib does OK, though.

- Shailen

On May 1, 9:29 am, Stefan Behnel  wrote:
> robean wrote:
> > I am writing a program that involves visiting several hundred webpages
> > and extracting specific information from the contents. I've written a
> > modest 'test' example here that uses a multi-threaded approach to
> > reach the urls with urllib2. The actual program will involve fairly
> > elaborate scraping and parsing (I'm using Beautiful Soup for that)
>
> Try lxml.html instead. It often parses HTML pages better than BS, can parse
> directly from HTTP/FTP URLs, frees the GIL doing so, and is generally a lot
> faster and more memory friendly than the combination of urllib2 and BS,
> especially when threading is involved. It also supports CSS selectors for
> finding page content, so your "elaborate scraping" might actually turn out
> to be a lot simpler than you think.
>
> http://codespeak.net/lxml/
>
> These might be worth reading:
>
> http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-sc...http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/
>
> Stefan

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


Re: ctypes: reference of a struct member?

2009-05-01 Thread CTO
ctypes.byref()

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


Re: object query assigned variable name?

2009-05-01 Thread Nick Craig-Wood
warpcat  wrote:
>  I've passed this around some other groups, and I'm being told
>  "probably not possible".  But I thought I'd try here as well :)   I
>  *did* search first, and found several similar threads, but they
>  quickly tangented into other specifics of the language that were a bit
>  over my head :)  At any rate, here's a simple example, I'd love to
>  know if as shown, is somehow possible:
> 
>  Given an object:
> 
>  class Spam(object):
>  def __init__(self):
>  # stuff
> 
>  I'd like it to print, when instanced, something like this:
> 
> >>> s = Spam()
>  I’m assigned to s!
> 
>  But it seems prohibitively hard (based on my web and forum searches)
>  for an object to know what variable name is has been assigned to when
>  created.  Querying 'self' in __init__ returns a memory location, not
>  the variable name passed in.
> 
>  If you're wondering why I'm trying to figure this out, this is just
>  part of my continued learning of the language and pushing the bounds,
>  to see what is possible ;)
> 
>  Any thoughts?

Read up on introspection and learn how to look up through the stack
frames.

When you've mastered that look for an object matching self in all the
locals in those stack frames.

That will give some kind of answer.

I have no idea whether this will work - the keyboard of my phone is
too small to produce a proof ;-)

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


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Dave Angel

zealalot wrote:

On May 1, 10:50 am, CTO  wrote:
  

Make doNothing a classmethod.

class SomeClass:

@classmethod
def doNothing(cls):
pass

def function1(self):
print "Running function 1"

def function2(self, passedFunction=meClass.doNothing):
print "Running passed function"
passedFunction()

someObject =omeClass()
someObject.function2()
someObject.function2(someObject.function1)



It's not surprising, but I've never heard of a classmethod before.
Basically, I read that it basically removes the need for the 'self'
argument.  Very cool!

And thanks for the quick response,

- Zealalot

  
As you've probably figured out by now, that also gets an error, since 
you can't refer to the SomeClass  until the definition is complete.  
Better is either the sentinel approach, or defining the function outside 
the class.  Note that since the signature must match the passed 
function, you presumably need neither a self nor a cls.  So keep it 
simple and define doNothing as a top-level function.


But it's worth expanding on the notion of a classmethod.  This type of 
function is callable with any object, or just with the class name 
itself.  But it doesn't have an access to instance attributes.  That's 
good, but not very common.  Class attributes are more common, and 
they're attributes which are shared among all the objects of the class.



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


Re: ctypes: reference of a struct member?

2009-05-01 Thread ma
ctypes.byref() does not work for struct members.
Try it out.
class s(ctypes.Structure):
_fields_ = [('x',ctypes.c_int)]

a = s()
ctypes.byref(a.x) //this won't work.



On Fri, May 1, 2009 at 2:28 PM, CTO  wrote:
> ctypes.byref()
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension question

2009-05-01 Thread Scott David Daniels

John Posner wrote:

Shane Geiger wrote:

   if type(el) == list or type(el) is tuple:

A tiny improvement:

   if type(el) in (list, tuple):


or (even better)
if isinstance(el, (list, tuple))

However, it is my contention that you shouldn't be flattening by type --
you should know where, explicitly to flatten.  If I have 3-D points as
triples, a tree of points would, by this code, get flattened into a
a list of numbers.   If, however, I create a class with the same
elements, but a method or two, suddenly flatten will produce a list
of points.  It is my contention that the tree abstraction should be
responsible for producing its leaves, rather than a blunderbus that
doesn't know where one container abstraction ends, and its contents
begin.  In other words, I'd like to see thigs like "flatten one layer."

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: don't understand namespaces...

2009-05-01 Thread norseman

Simon Forman wrote:

On Apr 30, 10:11 am, Lawrence Hanser  wrote:

Dear Pythoners,

I think I do not yet have a good understanding of namespaces.  Here is
what I have in broad outline form:


import Tkinter

Class App(Frame)
  define two frames, buttons in one and Listbox in the other

Class App2(Frame)
  define one frame with a Text widget in it

root = Tk()
app = App(root)
win2 = Toplevel(root)
app2 = App2(win2)
root.mainloop()


My understanding of the above goes like this:
1) create a root window
2) instantiate a class that defines a Frame in the root window
3) create another Toplevel window
4) instantiate another class that defines a frame in the Toplevel window (win2)

What I cannot figure out is how to reference a widget in app2 from app...

I hope this is sort of clear.

Any assistance appreciated.

Thanks,

Larry


...(snip) # you already have Simon's approach and I'm just shorting the 
overall reply.


-

IF you think each widget is supposed to be a program,
 I would have to say NO!

Each widget is just a pretty interface to the human.

build your program!
use the GUI to communicate with the human IN LIEU OF A COMMAND LINE. !!
That is all the GUI is for.(teacher stomping foot, shaking finger :)

instead of print()  do  msgbox()
instead of getch()  do  textbox()
instead of (command line thingy) do (GUI thingy)

and so on.
Your program is a program.
It just communicates differently.
Remember: Designing for the clicker is designing for the brain dead. :)
  (True - not in all cases, but mostly)

import Tkinter
help(Tkinter)
  (read!)

in short

import Tkinter   # load lib

root= Tk # use template, go live

main= Frame(root,... # the mockup board
frame1= Frame(main,...   # frame1 will be found inside main
frame2= Frame(main,  # frame2 will be found inside main
button1= Button(frame1,...   # button1 will be found inside frame1
frame3= Frame(root,...   # frame3 will float about on its own
msgbox1= MsgBox(where,...# and so it goes
.
.
root.mainloop()  # run it

Those widgets with a callback= allowed can send their answers to a 
function that has has been defined previously *above* in the code.


MyYNVal= StrVal() # here they are before example below
MyYNVal.set("*")  # to be used with above example they
  #
  #
def somehandler():# need to be in same order and
  a_global= MyYNVal.get() # before the root= line
  root.quit() #



rbframe= Frame(Tk,...
rb1= Radiobutton(rbframe,., variable=MyYNVal, value= "N", 
command=somehandler, ... )
rb2= Radiobutton(rbframe,., variable=MyYNVal, value= "Y", 
command=somehandler, ... )




program calls GUI# how?  encapsulate widgets in a   def name()
 # name()
human pushes button of choice which sets MyYNVal and calls somehandler
which returns to program
human_said= a_global
if human_said == "Y":
  program process accordingly
else:
  program process accordingly
.
.

Does that help?

And Yes - you can pre-edit/validate the GUI entries while still in the 
GUI. It gets a bit complicated because anything Tkinter is to use is 
already defined by time of use.  Read that sentence again.

(One pass interpreter. The pre-scan is for syntax only. It does not
  build address or set variables until actual run.
)


Today: 20090430
snippets are generalized for concept

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


Re: fcntl and siginfo_t in python

2009-05-01 Thread ma
According to man signal,
"The default action for an unhandled real-time signal is to terminate
the receiving process."

This means that my registered callback and sigaction does not work. I
think the only solution would be to try this with a C-extension. Has
anyone had any experience with this before?

I attached my latest copy. Any insight is appreciated.

On Thu, Apr 30, 2009 at 7:37 PM, ma  wrote:
> I attached a clean copy of the .py file in case others couldn't read
> it in their emails.
> I'll try that and let you know how SIGRTMIN+1 goes!
> What about this part?
>
> #sigemptyset(&act.sa_mask);
> #python2.6 has byref(act, offset),how can i port this over?
> #maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction)
> rc = __clib.sigemptyset(byref(act))
>
> Thanks!
> Mahmoud
>
>
>
> On Thu, Apr 30, 2009 at 7:33 PM, Philip  wrote:
>>
>> ma  gmail.com> writes:
>>
>> >
>> >
>> >
>> >
>> > Here's something that I came up with so far, I'm having some issues with
>> segfaulting, if I want to pass a struct member by ref in ctypes(see below), 
>> if
>> not, I just get a
>> > "Real-time signal 0" sent back to me.
>> >
>> >
>> > Any ideas?
>>
>> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html
>>
>> Philip
>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>


ctypes_fcntl.py
Description: Binary data
--
http://mail.python.org/mailman/listinfo/python-list


Re: Passing a function as an argument from within the same class?

2009-05-01 Thread Scott David Daniels

Steven D'Aprano wrote:

On Fri, 01 May 2009 07:35:40 -0700, zealalot wrote:

So, I'm trying to come up with a way to pass a method (from the same
class) as the default argument for another method in the same class


My first instinct is to say "Don't do that!", but let's see if there's a 
way to get what you want. It's actually very easy: just put the 
definition of the passed method before the method you want to use it in, 
then refer to it by name *without* self.


However, there is a catch: you need to manually pass in the instance, 
instead of letting Python do it for you.


class Spam(object):
def ham(self):
return "ham"
def spam(self, func=ham):
return "spam is a tasty %s-like food product" % func(self)


But you don't quite get the behavior the OP wanted, which was to allow
(text-producing in this case) methods as the argument:

>>> obj = Spam()
>>> obj.spam()
 'spam is a tasty ham-like food product'
>>> obj.spam(obj.ham)
Traceback (most recent call last):
  File "", line 1, in 
obj.spam(obj.ham)
  File "", line 5, in spam
return "spam is a tasty %s-like food product" % func(self)
TypeError: ham() takes exactly 1 argument (2 given)

Now it is tough to use obj.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] .pth files are evil

2009-05-01 Thread Tim Golden

Chris Withers wrote:
I'll say! I think .pth files are absolute evil and I wish they could 
just be banned.


+1 on anything that makes them closer to going away or reduces the 
possibility of yet another similar feature from hurting the 
comprehensibility of a python setup.


I've seen this view expressed by you (and others) a number
of times, but I use them myself in my admittedly simple
circumstances without any problem. Would you mind sharing
what's so bad about them?

TJG

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


Re: wxPython having trouble with frame objects

2009-05-01 Thread Dave Angel

Soumen banerjee wrote:

Hello,
The code works almost perfectly. I was able to correct some small
things, and get it to work brilliantly. I would like to thank you for
your dedicated help. My understanding of the matter here has improved
by a lot due to your kind help. I will further work on your
suggestions. By the way why should the general rule in 5 apply with
respect to private and global variables?
Regards
Soumen

On Fri, May 1, 2009 at 7:10 PM, Dave Angel  wrote:
  

Soumen banerjee wrote:



You accidentally top-posted, so your response is at the beginning, 
rather than in sequence. You also forgot to CC the PythonList, so nobody 
else will see it.


One typo I thought of after hitting "send" was I omitted the 'self' 
parameter when calling the parent __init__() methods.


function variables are preferred over globals for several reasons.  One 
is that there's less likelihood of accidentally getting the wrong 
value.  If two different functions use the same name for something, 
there's no confusion unless one of them decides to make it global.  This 
is referred to as namespace pollution.  Generally, the fewer names that 
are visible at any moment, the better.  Much better to get an error than 
use the worng one.  Other namespace cases is my preference of simple 
import over   from xxx import yyy.  It means that a reference to 
something from the import takes more typing, but is clearer to the reader.


Second problem with globals is that if you write a successful program to 
do something once, it very well might not work if you just repeat it,  
because you might forget to re-initialize the globals.  If this were the 
only problem, I might solve it by making a "init_globals()" function 
that uses the global keyword to export the names it's initializing.  
Then make sure you run init_globals() before running main().  
Unfortunately, there are more problems, so forget this "solution."


Third problem with globals is involved in refactoring.  When you first 
write a glob of code, you tend to put lots in one module.  Then when you 
better understand it, you refactor it into separate modules.  If it was 
coded with globals, this refactoring is much more painful.


Fourth problem with globals is in multithreading.  Any reference to 
mutable shared information has to be considered carefully, and each 
global is a potential hazard.


So, are there uses for globals?  Certainly.   First use, simple utility 
programs that fit in one file, run non-interactively, and for whom 
development pretty much stops when they "work."  Second, any immutable 
variable, whether or not it's literally protected.  There are many of 
these already in the system.  For example, all your function names are 
global variables.  Third, the system already defines some, such as in 
the sys module.  Some of these are constant, such as the path 
separator.  Others are changeable, but not usually while the application 
is running, like sys.path.


If you have to define new ones, either put them in a separate module (as 
sys does), or put them in a single class, as instance attributes.  Then, 
when refactoring, at least you only have one thing that's got to 
beshared between modules.  Any any code that refers to them will be 
using the same name for them.



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


Re: How to measure the memory cost in Python?

2009-05-01 Thread Jean
On May 1, 10:56 am, CTO  wrote:
> > sys.getsizeof() [a suggested solution] isn't platform-specific.
>
> So, to answer the OP's question, you'd just do something like
>
> def get_totalsize(obj):
>         total_size = sys.getsizeof(obj)
>         for value in vars(obj).values():
>                 try: total_size += get_total_size(value)
>                 except: total_size += sys.getsizeof(value)
>         return totalSize
>
> def get_current_size(env):
>         size = 0
>         for value in env.values():
>                 try: size += get_total_size(value)
>                 except: pass
>         return size
>
> get_current_size(vars())
>
> and discount the weight of the interpreter?

Keep in mind, sys.getsizeof(obj) returns only the size of the given
object.  Any referenced objects are not included.  You can get the
latter from gc.get_referents(obj).

/Jean Brouwers

PS) The asizeof(obj) function from this recipe  does size the object plus its
references, recursively.

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


Question about the wording in the python documents.

2009-05-01 Thread grocery_stocker
At the following url...

http://docs.python.org/library/urllib2.html

They have the following...

"urllib2.urlopen(url[, data][, timeout])

Open the URL url, which can be either a string or a Request
object"

I don't get how urllib2.urlopen() can take a Request object. When I do
the following

[cdal...@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>>
>>> url = 'http://www.google.com'
>>> req = urllib2.Request(url)
>>> response = urllib2.urlopen(req)
>>>

req is clearly an instance of urllib2.Request and not a Request object.
--
http://mail.python.org/mailman/listinfo/python-list


[ANN] Python testing client for Second Life virtual world

2009-05-01 Thread Lawson English
If anyone is curious, I'm working on a GUI shell for the python second 
life client, pyogp.


Temp home for pyogp intro: 
https://wiki.secondlife.com/wiki/Pyogp/Client_Lib
Basically the [first] plan is to have a GUI wrapper around the sample 
code with a batch file option, and to allow multiple avatar execution 
and control in the same process. It's been tested in the command line 
with up to 20 avatars at once, and the goal is to handle 200 at least, 
for QA purposes, so the GUI has to handle that as well.


Its a pretty interesting project, IMHO. Gives complete source for 
non-graphical aspects of interface with the Second Life virtual world. 
Apache V2 licensed.



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


Profiling gives very different predictions of best algorithm

2009-05-01 Thread Rick Muller
I'm the main programmer for the PyQuante package, a quantum chemistry
package in Python. I'm trying to speed up one of my rate determining
steps. Essentially, I have to decide between two algorithms:

1. Packed means that I compute N**4/8 integrals, and then do a bunch
of indexing operations to unpack;
2. Unpacked means that I compute all N**4 integrals, but don't have to
do any indexing.

Raw timing the two options show that packed is clearly faster (12.5
sec vs 20.6 sec). However, the profilings show very different results.
I have the results below. Clearly I'm going to use the packed scheme.
My question to the mailing list is what am I doing wrong with my
profiling that it shows such poor predictions? I rely on profiling a
great deal to tune my algorithms, and I'm used to seeing differences,
but nothing close to this magnitude.

Here is packed:
 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 11021725   84.4930.000   84.4930.000 :0(ijkl2intindex)
   18   62.0643.448  119.8656.659 Ints.py:150(getK)
   18   32.0631.781   61.1863.399 Ints.py:131(getJ)
529759.4040.000   19.6580.000 CGBF.py:189(coulomb)
   3136432.5420.0002.5420.000 :0(range)
529752.2600.0002.2600.000 :0(contr_coulomb)
   2182001.3770.0001.3770.000 CGBF.py:51(norm)
   2119001.3370.0001.3370.000 CGBF.py:53(powers)
   2119001.3360.0001.3360.000 CGBF.py:56(exps)
   2119001.3290.0001.3290.000 CGBF.py:58(pnorms)
   2119001.3280.0001.3280.000 CGBF.py:52(origin)
   2119001.3280.0001.3280.000 CGBF.py:57(coefs)
10.9790.979   21.108   21.108 Ints.py:112(get2ints)
117900.1970.0000.1970.000 :0(dot)
118280.1660.0000.1660.000 :0(zeros)

Here is unpacked:
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   18   16.1580.898   17.5440.975 Ints.py:167(getK)
529759.3010.000   19.5150.000 CGBF.py:189(coulomb)
   184.5840.2555.9040.328 Ints.py:146(getJ)
   3136432.6300.0002.6300.000 :0(range)
529752.2540.0002.2540.000 :0(contr_coulomb)
   2182001.3750.0001.3750.000 CGBF.py:51(norm)
   2119001.3300.0001.3300.000 CGBF.py:58(pnorms)
   2119001.3250.0001.3250.000 CGBF.py:53(powers)
   2119001.3250.0001.3250.000 CGBF.py:57(coefs)
   2119001.3230.0001.3230.000 CGBF.py:56(exps)
   2119001.3210.0001.3210.000 CGBF.py:52(origin)
10.7820.782   20.373   20.373 Ints.py:114(get2ints)
 18750.1560.0000.3840.000 CGBF.py:106(nuclear)
117900.1470.0000.1470.000 :0(dot)
178560.1120.0000.1120.000 PGBF.py:63(coef)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Profiling gives very different predictions of best algorithm

2009-05-01 Thread Brian
quantum chemistry sounds complicated. that means any advice i can give you
makes me a genius! just kidding. i've heard through the grapevine that
reentrant functions mess up profilers.

On Fri, May 1, 2009 at 2:54 PM, Rick Muller  wrote:

> I'm the main programmer for the PyQuante package, a quantum chemistry
> package in Python. I'm trying to speed up one of my rate determining
> steps. Essentially, I have to decide between two algorithms:
>
> 1. Packed means that I compute N**4/8 integrals, and then do a bunch
> of indexing operations to unpack;
> 2. Unpacked means that I compute all N**4 integrals, but don't have to
> do any indexing.
>
> Raw timing the two options show that packed is clearly faster (12.5
> sec vs 20.6 sec). However, the profilings show very different results.
> I have the results below. Clearly I'm going to use the packed scheme.
> My question to the mailing list is what am I doing wrong with my
> profiling that it shows such poor predictions? I rely on profiling a
> great deal to tune my algorithms, and I'm used to seeing differences,
> but nothing close to this magnitude.
>
> Here is packed:
>  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>  11021725   84.4930.000   84.4930.000 :0(ijkl2intindex)
>   18   62.0643.448  119.8656.659 Ints.py:150(getK)
>   18   32.0631.781   61.1863.399 Ints.py:131(getJ)
>529759.4040.000   19.6580.000 CGBF.py:189(coulomb)
>   3136432.5420.0002.5420.000 :0(range)
>529752.2600.0002.2600.000 :0(contr_coulomb)
>   2182001.3770.0001.3770.000 CGBF.py:51(norm)
>   2119001.3370.0001.3370.000 CGBF.py:53(powers)
>   2119001.3360.0001.3360.000 CGBF.py:56(exps)
>   2119001.3290.0001.3290.000 CGBF.py:58(pnorms)
>   2119001.3280.0001.3280.000 CGBF.py:52(origin)
>   2119001.3280.0001.3280.000 CGBF.py:57(coefs)
>10.9790.979   21.108   21.108 Ints.py:112(get2ints)
>117900.1970.0000.1970.000 :0(dot)
>118280.1660.0000.1660.000 :0(zeros)
>
> Here is unpacked:
> ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>   18   16.1580.898   17.5440.975 Ints.py:167(getK)
>529759.3010.000   19.5150.000 CGBF.py:189(coulomb)
>   184.5840.2555.9040.328 Ints.py:146(getJ)
>   3136432.6300.0002.6300.000 :0(range)
>529752.2540.0002.2540.000 :0(contr_coulomb)
>   2182001.3750.0001.3750.000 CGBF.py:51(norm)
>   2119001.3300.0001.3300.000 CGBF.py:58(pnorms)
>   2119001.3250.0001.3250.000 CGBF.py:53(powers)
>   2119001.3250.0001.3250.000 CGBF.py:57(coefs)
>   2119001.3230.0001.3230.000 CGBF.py:56(exps)
>   2119001.3210.0001.3210.000 CGBF.py:52(origin)
>10.7820.782   20.373   20.373 Ints.py:114(get2ints)
> 18750.1560.0000.3840.000 CGBF.py:106(nuclear)
>117900.1470.0000.1470.000 :0(dot)
>178560.1120.0000.1120.000 PGBF.py:63(coef)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Importing modules

2009-05-01 Thread Terry Reedy

norseman wrote:


OH - something you mentioned that didn't seem to be addressed.
import - load a complete library
from   - obtain specific 'function'(s) from a library


from can also be used to get a specific module from a package

If you 'import pack.mod', then you have to write 'pack.mod.ob' to access 
anything within mod.  But if you do 'from pack import mod', then just 
'mod.ob' works.


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


  1   2   >