Python newbie

2008-09-19 Thread Mladen Gogala
I am a Python newbie who decided to see what that Python fuss is all about.
Quite frankly, I am a bit perplexed. After having had few months of
experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:

perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:

Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[1,2,3]
>>> map((lambda x: 2*x),a)
[2, 4, 6]
>>> map((print),a)
  File "", line 1
map((print),a)
 ^
SyntaxError: invalid syntax
>>> for x in a: print x
... 
1
2
3
>>> for x in a: x=2*x
... 
>>> for x in a: print x
... 
1
2
3
>>> 

There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with 
   map?
2) Why is it illegal to pass a built-in function "print" to map?
3) Why is the array "a" unchanged after undergoing an explicit
   transformation with the "for" loop?
4) Is there an equivalent to \$a (Perl "reference") which would allow me to 
   decide when a variable is used by value and when by reference?

PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php


How can I make sure that 
for x in a: x=2*x 

actually changes the elements of the array "a"?


http://mgogala.freehostia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting hte font name from a TrueType font file

2008-09-19 Thread Steve Holden
Aaron "Castironpi" Brady wrote:
> On Sep 18, 7:48 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
>> Fredrik Lundh wrote:
>>> Steve Holden wrote:
 Does anyone have a Python recipe for this?
>> from PIL import ImageFont
>> f = ImageFont.truetype("/windows/fonts/verdanai.ttf", 1)
>> f.font.family
>>> 'Verdana'
>> f.font.style
>>> 'Italic'
>> Thanks so much, Fredrik. The reason I asked is because I found the
>> specification completely opaque ...
>>
>> regards
>>  Steve
>> --
>> Steve Holden+1 571 484 6266   +1 800 494 3119
>> Holden Web LLC  http://www.holdenweb.com/
> 
> Here's the code to parse the spec.
> 
> #customize path
> f= open( '\\windows\\fonts\\arial.ttf', 'rb' )
> from struct import *
> 
> #header
> shead= Struct( '>I' )
> fhead= f.read( shead.size )
> dhead= shead.unpack_from( fhead, 0 )
> 
> #font directory
> stable= Struct( '>4sIII' )
> ftable= f.read( stable.size* dhead[ 1 ] )
> for i in range( dhead[1] ): #directory records
> dtable= stable.unpack_from(
> ftable, i* stable.size )
> if dtable[0]== 'name': break
> assert dtable[0]== 'name'
> 
> #name table
> f.seek( dtable[2] ) #at offset
> fnametable= f.read( dtable[3] ) #length
> snamehead= Struct( '>HHH' ) #name table head
> dnamehead= snamehead.unpack_from( fnametable, 0 )
> 
> sname= Struct( '>HH' )
> for i in range( dnamehead[1] ): #name table records
> dname= sname.unpack_from(
> fnametable, snamehead.size+ i* sname.size )
> if dname[3]== 4: #key == 4: "full name of font"
> s= unpack_from(
> '%is'% dname[4], fnametable,
> dnamehead[2]+ dname[5] )[0]
> print dname, s
> 
> This outputs:
> 
> (0, 3, 0, 4, 10, 318)  A r i a l
> (1, 0, 0, 4, 5, 4081) Arial
> (3, 1, 1033, 4, 10, 318)  A r i a l
> 
> First 3 fields:
> 
> 0, 3, 0= Unicode, Unicode 2.0, English
> 1, 0, 0= Macintosh, Default, English
> 3, 1, 1033= Windows, Version 1.1, Language "1033"

Well you clearly understood it better than *I* did!

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Python newbie

2008-09-19 Thread Tino Wildenhain

Hi,

Mladen Gogala wrote:

I am a Python newbie who decided to see what that Python fuss is all about.
Quite frankly, I am a bit perplexed. After having had few months of
experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:

perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'

The equivalent in Python looks like this:

Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2

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

a=[1,2,3]
map((lambda x: 2*x),a)

[2, 4, 6]

map((print),a)

  File "", line 1
map((print),a)
 ^
SyntaxError: invalid syntax

for x in a: print x
... 
1

2
3

for x in a: x=2*x
... 

for x in a: print x
... 
1

2
3

There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with 
   map?


it isn't transformed. You get a new list as result.


2) Why is it illegal to pass a built-in function "print" to map?


because its not a function but a statement. If it was a function you
would call it print(x) not print x.


3) Why is the array "a" unchanged after undergoing an explicit
   transformation with the "for" loop?


To understand this, python knows immutable objects and mutable.
Immutables are strings, tuples , ... and integers.
This means the operation above all create a new integer object in memory
and assign it to the name x (where you use = )

4) Is there an equivalent to \$a (Perl "reference") which would allow me to 
   decide when a variable is used by value and when by reference?


No, python always uses by reference.


PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php


How can I make sure that 
for x in a: x=2*x 


actually changes the elements of the array "a"?


>>> l=range(10)
>>> for i in range(len(l)):
... l[i]*=2
... 
>>> l
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]


Cheers
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Python newbie

2008-09-19 Thread Steve Holden
Mladen Gogala wrote:
> I am a Python newbie who decided to see what that Python fuss is all about.
> Quite frankly, I am a bit perplexed. After having had few months of
> experience with Perl (started in 1994 with Perl v4, and doing it ever
> since) , here is what perplexes me:
> 
> perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'
> 
> The equivalent in Python looks like this:
> 
> Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
> [GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 a=[1,2,3]
 map((lambda x: 2*x),a)
> [2, 4, 6]
 map((print),a)
>   File "", line 1
> map((print),a)
>  ^
> SyntaxError: invalid syntax
 for x in a: print x
> ... 
> 1
> 2
> 3
 for x in a: x=2*x
> ... 
 for x in a: print x
> ... 
> 1
> 2
> 3
> 
> There are several questions:
> 
> 1) Why is the array "a" unchanged after undergoing a transformation with 
>map?

Because you evaluated an expression in which a was a variable, giving an
entirely new object as a result.

> 2) Why is it illegal to pass a built-in function "print" to map?

Because at present "print" isn't a built-in function, it's a keyword
designating a specific statement type. Try using sys.stdout.write instead.

> 3) Why is the array "a" unchanged after undergoing an explicit
>transformation with the "for" loop?

Because you aren't transforming a. You are extracting references to a's
elements into a separate variable and rebinding that variable to a new
value, leaving the references in a's elements pointing to the original
objects.

> 4) Is there an equivalent to \$a (Perl "reference") which would allow me to 
>decide when a variable is used by value and when by reference?
> 
No. Python implicitly dereferences all names when using them to compute
values, and only uses them as references on the left-hand side of an
assignment.

Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminological inexactitude.

> PHP also allows changing arrays with "foreach" loop:
> #!/usr/local/bin/php
>  $a=array(1,2,3);
> foreach($a as &$x) { $x=$x*2; }
> array_walk($a,create_function('$a','print("$a\n"); '));
> ?>
> 
> How can I make sure that 
> for x in a: x=2*x 
> 
> actually changes the elements of the array "a"?
> 
By saying something like

a = [2*x for x in a]

You don't modify the individual elements, you create a new list and
rebind a to that. AIf you insist on changing the elements of a, a more
cumbersome alternative is

for i, x in enumerate(a):
  a[i] = 2+x

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: XML Processing

2008-09-19 Thread Steve Holden
Robert Rawlins wrote:
>> Some is going to kick themselves when they realise
>> that ElementTree *is* built in to Python 2.5
>>
>> http://docs.python.org/whatsnew/modules.html#SECTION000142
> 
> Tim, Andrii,
> 
> Thanks for the heads up on that! I hadn't noticed they're made it part of
> the platform modules, that's excellent news.
> 
> In theory I should just be able to amend my import paths and we'll be good
> to go, no install external modules.
> 
> Thanks for this,
> 
And don't forget to use cElementTree for production, since it's an order
of magnitude or more faster at many tasks.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Python newbie

2008-09-19 Thread Marc 'BlackJack' Rintsch
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:

> There are several questions:
> 
> 1) Why is the array "a" unchanged after undergoing a transformation with
>map?

Because `map()` creates a new list and doesn't change the elements in `a`.

> 2) Why is it illegal to pass a built-in function "print" to map?

Because ``print`` isn't a function but a keyword.  This will be changed 
in Python 3.0.  On the other hand this use case is discouraged because 
`map()` builds a new list with the return value of the given function.  
So you would build a list full of `None`\s just for the side effect.

> 3) Why is the array "a" unchanged after undergoing an explicit
>transformation with the "for" loop?

Because there is no explicit transformation.  The loop binds elements to 
the name `x` in your example and within the loop you rebind that name to 
another value.  Neither the name `x` nor the objects bound to it have any 
idea that the object might be referenced in some container object.

> 4) Is there an equivalent to \$a (Perl "reference") which would allow me
>to decide when a variable is used by value and when by reference?

No, variables in Python are always name to object bindings.  Don't think 
of variables as boxes with names on it where you put objects in, or 
references to another box.  Think of objects and sticky notes with names 
on it.

> How can I make sure that
> for x in a: x=2*x
> 
> actually changes the elements of the array "a"?

Well, actually change the elements.  ;-)

Either:

for index, item in enumerate(sequence):
sequence[index] = func(item)

Or:

sequence[:] = map(func, sequence)

But the need to do so doesn't seem to be that frequent.  Usually one just 
builds a new list instead of altering an existing one.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list

Re: SMTP via GMAIL

2008-09-19 Thread Steve Holden
Grant Edwards wrote:
> On 2008-09-18, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote:
>> In message
>> <[EMAIL PROTECTED]>, sui
>> wrote:
>>
>>> Traceback (most recent call last):
>>>   File "mail5.py", line 21, in 
>>> session = smtplib.SMTP(SMTPserver,port)
>>>   File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
>>> (code, msg) = self.connect(host, port)
>>>   File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
>>> self.sock.connect(sa)
>>>   File "", line 1, in connect
>>> then conncetion time out.
>> Could it be your ISP is blocking outgoing connections to port
>> 25?
> 
> gmail doesn't accept mail via SMTP on port 25.
> 
I was going to say that's boloney until I checked my settings - it's a
year or more since I set gmail up with Thunderbird.

smtp.gmail.com accepts TLS connections on port 587.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Python newbie

2008-09-19 Thread Steven D'Aprano
On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:

> I am a Python newbie who decided to see what that Python fuss is all
> about. Quite frankly, I am a bit perplexed. 

Naturally you will be perplexed if you assume that Python is just Perl 
with a more verbose syntax. It isn't.


> After having had few months
> of experience with Perl (started in 1994 with Perl v4, and doing it ever
> since) , here is what perplexes me:
> 
> perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'
> 
> The equivalent in Python looks like this:

Actually, no it doesn't. The equivalent looks more like this:


$ python -c 'a=[1,2,3]; a=[2*x for x in a];
for x in a:
print x'
2
4
6



Now, to go on with your questions:



> 1) Why is the array "a" unchanged after undergoing a transformation with
>map?


Because it hasn't undergone a transformation at all. The list (not array) 
a remains unchanged, because map returns a new list.



> 2) Why is it illegal to pass a built-in function "print" to map?

Because print is not a function, it is a statement. However, in Python 3, 
that design wart has been corrected.


> 3) Why is the array "a" unchanged after undergoing an explicit
>transformation with the "for" loop?


Because the list a hasn't undergone any such transformation. Rebinding a 
name is not an explicit transformation to the object that used to be 
bound to the name. And if that sentence is gobbledygook to you, that 
demonstrates that Python is not Perl and you shouldn't assume a one-to-
one semantic relationship between syntactical elements.



> 4) Is there an equivalent to \$a (Perl "reference") which would allow me
> to decide when a variable is used by value and when by reference?

Python is neither call-by-reference nor call-by-value, although you will 
have many people swear black and blue that it is one or the other. With 
respect to all of them, they are wrong. Python's calling model is "call 
by object". See here for more detail:

http://effbot.org/zone/call-by-object.htm


If you insist on a false dichotomy between call-by-reference and call-by-
value, then you won't often go wrong if you think of Python:

- being almost always call-by-reference, except for some odd corner cases 
which look almost, but not quite, like call-by-value;

- being almost always call-by-value, but only if you think of the values 
as pointers.


> How can I make sure that
> for x in a: x=2*x
> 
> actually changes the elements of the array "a"?

By explicitly changing the elements of the list. Here's one very old-
fashioned way of doing so:

>>> a = [1,2,3]
>>> for i in range(len(a)):
... a[i] = a[i]*2
...
>>> a
[2, 4, 6]


Here's a more Pythonic alternative:

>>> a = [1,2,3]
>>> a = [2*x for x in a]




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


Re: Python newbie

2008-09-19 Thread Steven D'Aprano
On Fri, 19 Sep 2008 03:34:50 -0400, Steve Holden wrote:

> Please note the above statement is contentious, and will likely bring a
> horde of screaming fanatics of various flavors down on my head for
> terminological inexactitude.

A pox on you and your descendants onto the tenth generation!


*wink*


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


Re: Python newbie

2008-09-19 Thread Mladen Gogala
Steve Holden wrote:

> No. Python implicitly dereferences all names when using them to compute
> values, and only uses them as references on the left-hand side of an
> assignment.
> 
> Please note the above statement is contentious, and will likely bring a
> horde of screaming fanatics of various flavors down on my head for
> terminological inexactitude.

Actually, I am not a "screaming fanatic". I just want to learn. I am not
trying to compare Perl or PHP with Python. I don't know enough Python to
do that. I see an exponential increase in the use of Python and I want to
find out why is that. I don't plan on discarding my knowledge of Perl and
PHP and I don't know enough Python to make a reasonable comparison. whether 
I will use Python on regular basis or not is still to be decided. One thing
is certain: it's easier to use what I already know but then, that's not the
point. I've heard very good things about Django and TurboGears and I got
my hands on the tool called orasachemadoc which is written in Python and
I want to check it out. True, I am coming from the "dark side", but I have
no preconceptions and am not a "screaming fanatic". Let me introduce myself:

http://www.dba-oracle.com/t_dbi_interface1.htm
http://www.rampant-books.com/book_2005_2_php_oracle.htm
http://www.oracle.com/technology/pub/articles/gogala-symfony.html

Those articles are the only ones that are scripting related. There are some
other articles about Oracle, of no interest for this group. I am 47 years
old and I have learned many programming languages in my time. Learning
Python doesn't seem like a huge task. Conquering my own preconceptions
stemming from a very long use of other programming languages will not be a
big problem, either.

Hopefully, this explains who am I and what is my motivation for delving into
Python. I share your fears of screaming fanatics, too.

-- 
http://mgogala.freehostia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Open Office

2008-09-19 Thread Hartmut Goebel

Terry Reedy schrieb:

Hartmut Goebel wrote:



The API docs are a bit hidden on the webpage. Here is the link:



I wrote my comment *after* looking at the above, which I found easily 
enough.  After 7 pages of (helpful) explanatory text, there follow 88 
pages with hundreds of entries like this:


[...]

Well, I wrote my comment *before* looking at the API docs. Your are 
absolutely right: It is not helpful.



Additionally teh ODF sepcs may help:



v1.0 is the adopted international (ISO/IEC) standard.


Specs for ODF 1.0 are available the oasis-open.org, too. So if you want 
to keep close the the ISO standard you surely are better off using 1.0. 
Alternativly you may use the 1.1 docs and skim the appendix about changes.


Regards
H. Goebel
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie

2008-09-19 Thread Tim Golden

Mladen Gogala wrote:

Steve Holden wrote:


No. Python implicitly dereferences all names when using them to compute
values, and only uses them as references on the left-hand side of an
assignment.

Please note the above statement is contentious, and will likely bring a
horde of screaming fanatics of various flavors down on my head for
terminological inexactitude.


Actually, I am not a "screaming fanatic". I just want to learn. 


I'm sure Steve can speak for himself, but just to clarify:
the "screaming fanatics" referred to are those within the
Python community whose opinions of the terminology used are
at variance with Steve's. I think it was a comment which would
mean more to those of us who've seen such terminology-battles
come and go over the years.

But thanks for introducing yourself anyway. Knowing more about
where a poster is coming from never hurts when trying to answer
their questions. :)

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


Re: improving a huge double-for cycle

2008-09-19 Thread Alex Ziller
Thank you all for replying me!

special thanks to Tino. Your code rocks!

I got all the double nodes in couple of seconds
and in a useful output with the coupled nodes pro line..

cheers from Germany!




2008/9/18 Tino Wildenhain <[EMAIL PROTECTED]>

> Tino Wildenhain wrote:
>
>> Hi,
>>
>> Alexzive wrote:
>>
>>> Hello there :) ,
>>>
>>> I am a python newbie and need to run following code for a task in an
>>> external simulation programm called "Abaqus" which makes use of python
>>> to access the mesh (ensamble of nodes with xy coordinates) of a
>>> certain geometrical model.
>>>
>>> [IN is the starting input containing the nodes to be check, there are
>>> some double nodes with the same x and y coordinates which need to be
>>> removed. SN is the output containing such double nodes]
>>>
>>> Code: Select all
>>>for i in range(len(IN)): #scan all elements of the list IN
>>>  for j in range(len(IN)):
>>>if i <> j:
>>> if IN[i].coordinates[0] == IN[j].coordinates[0]:
>>>   if IN[i].coordinates[1] == IN[j].coordinates[1]:
>>>  SN.append(IN[i].label)
>>>
>>>
>> data=dict()
>> for item in IN: # (what a name! ;)
>>data.setdefault(item.coordinates,[]).append(item)
>># provided coordinates is a tuple
>>
>> dupes=[items for items in data.values() if len(items)>1]
>>
>> should give you a resulting list of lists of elements in IN
>> which occur more then once per coordinates.
>> You can then decide which ones to remove or whatever.
>>
>> If you want to have the output only containing nodes to remove,
>> the following would work:
>>
>> dupes=[]
>> for items in data.values():
>>dupes.extend(items[1:])
>>
>>
> I did a small test - I don't know if it reflects
> the actual problem good enough but it seems it
> all runs below 1 sec each so this would be very
> fast compared to 15h:
>
> >>> import random
> >>> class Node(object):
> ... def __init__(self,x,y):
> ... self.coordinates=(x,y)
>
> >>> IN=[Node(random.randint(0,100),random.randint(0,100)) for i in
> xrange(10)]
>
> >>> data=dict()
> >>> for item in IN: data.setdefault(item.coordinates,[]).append(item)
> ...
>
> >>> dupes=[items for items in data.values() if len(items)>1]
> >>> len(dupes)
> 10190
> >>>
>
> Cheers
> Tino
>



-- 
Ing. Alessandro Zivelonghi Ziller

http://www.tecnopolis.eu

+49 176 83112471
skype: alexzive

"Energy and persistence conquer all things."
Benjamin Franklin (1706 - 1790)
--
http://mail.python.org/mailman/listinfo/python-list

Re: Python newbie

2008-09-19 Thread kaer
On 19 sep, 10:13, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
> > I am a Python newbie who decided to see what that Python fuss is all
> > about. Quite frankly, I am a bit perplexed., here is what perplexes me:

> > perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'


>
> Naturally you will be perplexed if you assume that Python is just Perl
> with a more verbose syntax. It isn't.
>

Indeed, it isn't ... at all:
$ python -c 'for s in [2*x for x in (1, 2, 3)]: print s'
2
4
6

And it's always (a lot) more readable :-)

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


Re: improving a huge double-for cycle

2008-09-19 Thread Gabriel Genellina
En Fri, 19 Sep 2008 02:11:51 -0300, Tino Wildenhain <[EMAIL PROTECTED]>  
escribió:



Also I never saw a list where the threading often goes wrong like
this here - is there any special setup or is it just peoples MUA
which screws up?


Perhaps it's due to the newsgroup/list duality...

--
Gabriel Genellina

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


Re: Intercepting printed strings

2008-09-19 Thread Gabriel Genellina
En Thu, 18 Sep 2008 19:24:26 -0300, Robert Dailey <[EMAIL PROTECTED]>  
escribió:


I'm currently using Python 3.0 b3 and I'm curious as to how I can go  
about
intercepting things send to print() for some intermediate processing  
before

they're actually sent to sys.stdout. Right now I've thought of the
following:

Replace sys.stdout with a class named PrintStream. PrintStream is  
defined as

follows:

class PrintStream:
def write( self, message ):
sys.__stdout__.write( '\t{0}'.format( message ) )

Will this work? Basically I want to add a tab character in front of every
message printed. Thanks.


Why don't you try it yourself?
You may replace builtins.print with your own function too. It's not  
exactly the same thing, but given your request "intercepting things send  
to print() before they're sent to sys.stdout" it may be more adequate.


--
Gabriel Genellina

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


Re: Login to website using urllib2

2008-09-19 Thread Gabriel Genellina
En Thu, 18 Sep 2008 10:23:50 -0300, Mohit Ranka <[EMAIL PROTECTED]>  
escribió:



 I am trying to fetch HTML content from a website that has
different version of pages for "logged" users and "guseuests" users. I  
need
to fetch the "logged" user pages. The problem is, even with the use of  
basic

authentication, I am getting "guest" user page with urllib2.urlopen. The
code can be seen here.

http://pastebin.com/m7301084a

Any suggestions/pointers/solutions?


Your second call to urllib2.install_opener completely replaces the former  
opener; you should combine both handlers before installing ONE opener.


--
Gabriel Genellina

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


Re: Python, Factset, and Excel - Oh my!

2008-09-19 Thread Gabriel Genellina
En Thu, 18 Sep 2008 16:41:36 -0300, Desmond Scott E <[EMAIL PROTECTED]>  
escribió:



I'm still a Python novice.
I'm still attempting to migrate a Python-based process from
Windows2000/Python v2.4.2 (#67)/Excel2000 to WindowsXP/Python v2.5.2
(r252:60911)/Excel2003.
I now have a new "opportunity"!
I need some help with the step in the Python script that calls Excel and
invokes the Factset API to download data into a spreadsheet.  I'm
thinking that this is a Python / Factset API setup issue.  Is the
Factset API installed as a component of Python?  Or does the Factset API
exist in Windows and available to any application, like Python?
Any insights would be greatly appreciated!


What's the Factset API? If you're talking about http://www.factset.com/  
I'm pretty sure you have to install something to make it available.


--
Gabriel Genellina

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


Re: webbrowser fragment identifier

2008-09-19 Thread Tim Golden

scottbvfx wrote:

Hi,

I'm trying to launch a web browser along with an html file with a
fragment identifier in its path. I'm using the webbrowser module for
this.
ie. webbrowser.open('file:///C:/myfile.html#SomeEntryInTheHTML')

for some reason it is truncating the path to 'file:///C:/myfile.html'.

Does anyone know a way of getting the fragment identifier in there
(with webbrowser module or any other)?


No expertise here: just poking around. This seems to be
a slight peculiarity of browser and os cooperation. As
a comparison, try running os.startfile ("file://#..")
or just pasting it into the command line. I don't understand
why this should be but it seems to lose it there as well.

Randomly tested on my XP box with Firefox 3 & IE 7:

Input: Start > Run > firefox c:\temp\t.html#chapter-i
Result: URL is escaped so ff3 starts with non-existent 
file:///c:/temp/t.html%23chapter-i

Input: Start > Run > file:///c:\temp\t.html#chapter-i (with ff3 as default)
Result: URL fragment is stripped so ff3 starts with file:///C:/temp/t.html

Input: Start > Run iexplore c:\temp\t.html#chapter-i
Result: Gets it right: ie7 starts with file:///C:/temp/t.html#chapter-i

Input: Start > Run > file:///c:\temp\t.html#chapter-i (with ie7 as default)
Result: URL fragment is stripped so IE7 starts with file:///C:/temp/t.html


A very quick perusal of the webbrowser source suggests it does nothing
more sophisticated than finding possible browsers, starting with firefox,
and stopping when it finds it. It then uses that as the command-line prefix
to whatever url is passed. So no stripping of url fragments happening there.

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


Re: webbrowser fragment identifier

2008-09-19 Thread Gabriel Genellina
En Thu, 18 Sep 2008 21:53:22 -0300, scottbvfx <[EMAIL PROTECTED]>  
escribió:



I'm trying to launch a web browser along with an html file with a
fragment identifier in its path. I'm using the webbrowser module for
this.
ie. webbrowser.open('file:///C:/myfile.html#SomeEntryInTheHTML')

for some reason it is truncating the path to 'file:///C:/myfile.html'.

Does anyone know a way of getting the fragment identifier in there
(with webbrowser module or any other)?


It's not Python which truncates the path - webbrowser.open doesn't modify  
the url given.
Looks like you're on Windows. Internet Explorer exposes a COM interface  
that you may use to control it. Google for "python IE automation"


--
Gabriel Genellina

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


Problem with Python shell through Cygwin Screen (Python/Vim/Screen combo)

2008-09-19 Thread Ant
Hi all,

There's a sweet combination of tools that you can assemble using Vim,
a Python shell (or any shell for that matter) and GNU screen, where
you essentially send selected text from Vim to the Python shell. (See
http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/ for
more details - though I had to tweak the script a bit to get it to
work on Windows - line ending interpretation problem)

This works great in Linux, and also in Windows using the Cygwin build
of Python, but when you call:

screen -S py -s python

using the Windows build of Python, the shell just hangs. Does anyone
know if this is a known issue with screen/Python, or if there is a
workaround, as I'd prefer to have just a single Python instance
installed rather than having Cygwin python and windows Python both
installed.

Cheers,

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


Re: dict generator question

2008-09-19 Thread Boris Borcic

Gerard flanagan wrote:

George Sakkis wrote:

..


Note that this works correctly only if the versions are already sorted
by major version.



Yes, I should have mentioned it. Here's a fuller example below. There's 
maybe better ways of sorting version numbers, but this is what I do.


Indeed, your sort takes George's objection too litterally, what's needed for a 
correct endresult is only that major versions be grouped together, and this is 
most simply obtained by sorting the input data in (default) string order, is it 
not ?





data = [ "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.1.1.1", "1.3.14.5", 
"1.3.21.6" ]


from itertools import groupby
import re

RXBUILDSORT = re.compile(r'\d+|[a-zA-Z]')

def versionsort(s):
key = []
for part in RXBUILDSORT.findall(s.lower()):
try:
key.append(int(part))
except ValueError:
key.append(ord(part))
return tuple(key)

data.sort(key=versionsort)
print data

datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))
print datadict




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



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


Re: Deflate with urllib2...

2008-09-19 Thread Gabriel Genellina

En Thu, 18 Sep 2008 23:29:30 -0300, Sam <[EMAIL PROTECTED]> escribió:

On Sep 18, 2:10 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:

En Tue, 16 Sep 2008 21:58:31 -0300, Sam <[EMAIL PROTECTED]> escribió:
The code is correct - try with another server. I tested it with a  
LightHTTPd server and worked fine.


Gabriel...

I found a bunch of servers to test it on.  It fails on every server I
could find (sans one).


I'll try to check later. Anyway, why are you so interested in deflate?  
Both "deflate" and "gzip" coding use the same algorithm and generate  
exactly the same compressed stream, the only difference being the header  
and tail format. Have you found any server supporting deflate that doesn't  
support gzip as well?


--
Gabriel Genellina

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


Re: Installing pySerial

2008-09-19 Thread Joe G (Home)
Yep   up and running now.

Many thanks

Joe


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


Re: PEP proposal optparse

2008-09-19 Thread Pete Forman
James <[EMAIL PROTECTED]> writes:

 > I would like to know your thoughts on a proposed change to optparse
 > that I have planned. It is possible to add default values to
 > multiple options using the set_defaults. However, when adding
 > descriptions to options the developer has to specify it in each
 > add_option() call.

-1

I see no advantage to swelling optparse when the language offers many
solutions already.  E.g.

desc = {
'verbose': 'Output less information',
'castordir': 'specify the wanted CASTOR directory where to store '
'the results tarball',
'previousrel': 'Top level dir of previous release for regression '
'analysis'}

parser.add_option('-q', '--quiet', action="store_false",
  dest='verbose', help = desc['verbose'])
...


Or another approach might be like this.

for ... in zip(...):
parser.add_option(...)

-- 
Pete Forman-./\.-  Disclaimer: This post is originated
WesternGeco  -./\.-   by myself and does not represent
[EMAIL PROTECTED]-./\.-   the opinion of Schlumberger or
http://petef.22web.net   -./\.-   WesternGeco.
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict generator question

2008-09-19 Thread Gerard flanagan

Boris Borcic wrote:

Gerard flanagan wrote:

George Sakkis wrote:

..


Note that this works correctly only if the versions are already sorted
by major version.



Yes, I should have mentioned it. Here's a fuller example below. 
There's maybe better ways of sorting version numbers, but this is what 
I do.


Indeed, your sort takes George's objection too litterally, what's needed 
for a correct endresult is only that major versions be grouped together, 
and this is most simply obtained by sorting the input data in (default) 
string order, is it not ?




Yes, I see what you mean - the fact that a default sort orders "1.10" 
before "1.9" doesn't actually matter for the required result.


datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))


And, s[:3] is wrong. So:

data.sort()
datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s:
'.'.join(s.split('.',2)[:2])))

should work, I hope.

Cheers,

Gerard

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


Re: dict generator question

2008-09-19 Thread bearophileHUGS
Gerard flanagan:

> data.sort()
> datadict = \
> dict((k, len(list(g))) for k,g in groupby(data, lambda s:
>  '.'.join(s.split('.',2)[:2])))

That code may run correctly, but it's quite unreadable, while good
Python programmers value high readability. So the right thing to do is
to split that line into parts, giving meaningful names, and maybe even
add comments.

len(list(g))) looks like a good job for my little leniter() function
(or better just an extension to the semantics of len) that time ago
some people here have judged as useless, while I use it often in both
Python and D ;-)

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


Re: Intercepting printed strings

2008-09-19 Thread Robert Dailey
On Fri, Sep 19, 2008 at 4:21 AM, Gabriel Genellina
<[EMAIL PROTECTED]>wrote:

> En Thu, 18 Sep 2008 19:24:26 -0300, Robert Dailey <[EMAIL PROTECTED]>
> escribió:
>
>
>  I'm currently using Python 3.0 b3 and I'm curious as to how I can go about
>> intercepting things send to print() for some intermediate processing
>> before
>> they're actually sent to sys.stdout. Right now I've thought of the
>> following:
>>
>> Replace sys.stdout with a class named PrintStream. PrintStream is defined
>> as
>> follows:
>>
>> class PrintStream:
>>def write( self, message ):
>>sys.__stdout__.write( '\t{0}'.format( message ) )
>>
>> Will this work? Basically I want to add a tab character in front of every
>> message printed. Thanks.
>>
>
> Why don't you try it yourself?
> You may replace builtins.print with your own function too. It's not exactly
> the same thing, but given your request "intercepting things send to print()
> before they're sent to sys.stdout" it may be more adequate.


I did try the code I posted and it doesn't work.
--
http://mail.python.org/mailman/listinfo/python-list

generator exceptions

2008-09-19 Thread Alexandru Mosoi
i have a generator that raises an exception when calling next(),
however if I try to catch the exception and print the traceback i get
only the line where next() was called


while True:
  try:
iterator.next()
  except StopIteration:
break
  except Exception, e:
traceback.print_exc()

how do I get the traceback starting from where exception was raised in
first place?
--
http://mail.python.org/mailman/listinfo/python-list


Multimapping and string converting

2008-09-19 Thread Ron Brennan
Hello,

I have a multimap dictionary with a 1 Key to N values.  I want to convert
the N values to a string to be used elsewhere in my program.

So I have dict[(1,[1, 2 ,3 ,4])] which I have sorted

When I do a print ''.join(str(dict.value())) I get [1, 2, 3, 4] as an output
when I really want 1 2 3 4

Here is my code:

dmapItems = dictionary.items()
dmapItems.sort()

for tcKey, tcValue in dmapItems:
file.write('Key = %s\nValue = %s" % (tcKey, tcValue)

stinger = ''.join(str(tcValue))

print stringer

The Output = [145, 2345, 567, 898]
I need it to be 145 2345 567 898


Can anyone see the errors of my ways?

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

Re: Intercepting printed strings

2008-09-19 Thread Gabriel Genellina
En Fri, 19 Sep 2008 10:37:00 -0300, Robert Dailey <[EMAIL PROTECTED]>  
escribió:



On Fri, Sep 19, 2008 at 4:21 AM, Gabriel Genellina
<[EMAIL PROTECTED]>wrote:


En Thu, 18 Sep 2008 19:24:26 -0300, Robert Dailey <[EMAIL PROTECTED]>
escribió:


 I'm currently using Python 3.0 b3 and I'm curious as to how I can go  
about

intercepting things send to print() for some intermediate processing
before
they're actually sent to sys.stdout. Right now I've thought of the
following:

Replace sys.stdout with a class named PrintStream. PrintStream is  
defined

as
follows:

class PrintStream:
   def write( self, message ):
   sys.__stdout__.write( '\t{0}'.format( message ) )

Will this work? Basically I want to add a tab character in front of  
every

message printed. Thanks.



Why don't you try it yourself?
You may replace builtins.print with your own function too. It's not  
exactly
the same thing, but given your request "intercepting things send to  
print()

before they're sent to sys.stdout" it may be more adequate.



I did try the code I posted and it doesn't work.


Works for me:

p3> class PrintStream:
...def write( self, message ):
...sys.__stdout__.write( '\t{0}'.format( message ) )
...
p3> sys.stdout = PrintStream()
p3> print("Hello world!")
Hello world!
p3> print(1, 2, 3)
1   2   3

(note the double spacing between elements)
You may want to replace the print() function instead (this was not so easy  
in previous versions):


p3> sys.stdout = sys.__stdout__
p3> def _print(*args): # simplified print() signature
...   sys.stdout.write('\t' + ' '.join(str(arg) for arg in args) + '\n')
...
p3> import builtins
p3> builtins.print = _print
p3> print("Hello world!")
Hello world!
p3> print(1, 2, 3)
1 2 3
p3>

--
Gabriel Genellina

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


Re: generator exceptions

2008-09-19 Thread Michael Palmer
On Sep 19, 9:40 am, Alexandru  Mosoi <[EMAIL PROTECTED]> wrote:
> i have a generator that raises an exception when calling next(),
> however if I try to catch the exception and print the traceback i get
> only the line where next() was called
>
> while True:
>   try:
> iterator.next()
>   except StopIteration:
> break
>   except Exception, e:
> traceback.print_exc()
>
> how do I get the traceback starting from where exception was raised in
> first place?

What happens if you simply remove the 'except Exception' clause? It
should print the entire traceback by default.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie

2008-09-19 Thread Grant Edwards
On 2008-09-19, Mladen Gogala <[EMAIL PROTECTED]> wrote:
> Steve Holden wrote:
>
>> No. Python implicitly dereferences all names when using them
>> to compute values, and only uses them as references on the
>> left-hand side of an assignment.
>> 
>> Please note the above statement is contentious, and will
>> likely bring a horde of screaming fanatics of various flavors
>> down on my head for terminological inexactitude.
>
> Actually, I am not a "screaming fanatic".

Steve was refering hyperbolically to people like me who find it
entertaining to argue about obscure technical points involving
useless, degenerate bits of example code.  It's one of the
things we do here instead of Perl golfing. Though off hand I
don't really see anything much wrong with the statement for
which Steve predicted an impending onslaught of language
lawyers -- unless one wanted to jump right away into the stock
discussion on name-rebinding vs. dereferencing-pointer-lvalues
that we generally save for C/C++ programmers.  I only used Perl
once about 15 years ago (that was more than enough for me), but
I would have guessed that an assigment in Perl was a
name-(re)binding more akin to Python than a store to a
variable's memory location like C/C++/Pascal/etc.

> Hopefully, this explains who am I and what is my motivation
> for delving into Python. I share your fears of screaming
> fanatics, too.

We're actually quite harmless and much friendlier than those in
most other newsgroups.

-- 
Grant Edwards   grante Yow! My EARS are GONE!!
  at   
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Twisted vs Python Sockets

2008-09-19 Thread Michael Palmer
On Sep 18, 4:24 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> James Matthews wrote:
> > I am wondering what are the major points of twisted over regular python
> > sockets. I am looking to write a TCP server and want to know the pros
> > can cons of using one over the other.
>
> Twisted is a communication framework with lots of ready-made components:
>
>http://twistedmatrix.com/trac/wiki/TwistedAdvantage
>
> Regular sockets are, well, regular sockets.  No more, no less.
>
> 

Depends on what you want your TCP server to do. Just to mention it,
there is module SocketServer in the standard library that already
contains various server classes, including a TCP server.
--
http://mail.python.org/mailman/listinfo/python-list


Re: improving a huge double-for cycle

2008-09-19 Thread Jake Anderson

Bruno Desthuilliers wrote:

Alexzive a écrit :

Hello there :) ,

I am a python newbie and need to run following code for a task in an
external simulation programm called "Abaqus" which makes use of python
to access the mesh (ensamble of nodes with xy coordinates) of a
certain geometrical model.

[IN is the starting input containing the nodes to be check, there are
some double nodes with the same x and y coordinates which need to be
removed. SN is the output containing such double nodes]

Code: Select all
for i in range(len(IN)): #scan all elements of the list IN
  for j in range(len(IN)):
if i <> j:
 if IN[i].coordinates[0] == IN[j].coordinates[0]:
   if IN[i].coordinates[1] == IN[j].coordinates[1]:
  SN.append(IN[i].label)



Unfortunately my len(IN) is about 100.000 and the running time about
15h  :(

Any idea to improve it?


A couple ones have been submitted. Harald gets a point about the 
redundant tests (even if his solution seems to be broken, cf below) - 
your inner loop should have looked like :


  for j in xrange(i+1, len(IN))

Now the obvious winner is pruebono - even unoptimized, using sets seems 
to be *way* faster than even the most optimized corrected version of 
your algorithm.


Here's a quick bench - please everyone doublecheck it to make sure it's ok:




Results here (py2.5, gentoo linux, athlonxp1800, 512 ram):

 >>> test_results()
True
 >>> test_times()
doubles0 : 1.55667901039
doubles1 : 0.719144105911
doubles2 : 0.703393936157
doubles3 : 0.700654983521
doubles4 : 0.706257104874
doubles5 : 0.528184890747
doubles6 : 0.461633205414
doubles8 : 0.0134379863739
doubles9 : 0.0108540058136
 >>>

Not surprisingly, half less iterations makes for half less time. 
Aliasing, as often, proves to be a good optimization too. But obviously, 
using the correct data structure / algorithm combo is the key : simpler 
code, and 115 times faster (143 times with aliasing). If pruebono 
solution's is correct (and it as AFAICT), your 15 hours computation 
should by now take less than 10 minutes...





Ubuntu 8.04 core2 2.6(i think)
without psycho
doubles0 : 0.610555171967
doubles1 : 0.29314494133
doubles2 : 0.286273956299
doubles3 : 0.281984090805
doubles4 : 0.28240609169
doubles5 : 0.207377910614
doubles6 : 0.156388044357
doubles8 : 0.00533080101013
doubles9 : 0.00458884239197

with psycho
doubles0 : 0.127684116364
doubles1 : 0.069571018219
doubles2 : 0.064826965332
doubles3 : 0.0702300071716
doubles4 : 0.0647261142731
doubles5 : 0.0522589683533
doubles6 : 0.0437579154968
doubles8 : 0.00190806388855
doubles9 : 0.00214099884033

On this small test its a variance between ~6x to 2X still its basically 
free so why not ;->

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


Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread r0g
erikcw wrote:
> Hi,
> 
> I have a cgi script where users are uploading large files for
> processing.  I want to launch a subprocess to process the file so the
> user doesn't have to wait for the page to load.
> 
> What is the correct way to launch subprocess without waiting for the
> result to return?
> 
> Thanks!

Try exec() with " &" at the end of your command line.

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


Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread r0g
erikcw wrote:
> Hi,
> 
> I have a cgi script where users are uploading large files for
> processing.  I want to launch a subprocess to process the file so the
> user doesn't have to wait for the page to load.
> 
> What is the correct way to launch subprocess without waiting for the
> result to return?
> 
> Thanks!

Whoops, that was PHP! Imeant...

os.system(yourcommandline+" &")

;-)

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


Re: SMTP via GMAIL

2008-09-19 Thread Grant Edwards
On 2008-09-19, Steve Holden <[EMAIL PROTECTED]> wrote:
> Grant Edwards wrote:
>> On 2008-09-18, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote:
>>> In message
>>> <[EMAIL PROTECTED]>, sui
>>> wrote:
>>>
 Traceback (most recent call last):
   File "mail5.py", line 21, in 
 session = smtplib.SMTP(SMTPserver,port)
   File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
 (code, msg) = self.connect(host, port)
   File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
 self.sock.connect(sa)
   File "", line 1, in connect
 then conncetion time out.
>>> Could it be your ISP is blocking outgoing connections to port
>>> 25?
>> 
>> gmail doesn't accept mail via SMTP on port 25.
>> 
> I was going to say that's boloney until I checked my settings - it's a
> year or more since I set gmail up with Thunderbird.
>
> smtp.gmail.com accepts TLS connections on port 587.

I should have been a bit more specific and said that the
relay/smarthosts at smtp.gmail.com don't accept SMTP mail via
port 25.

The normal incoming SMTP servers pointed to by gmail.com's MX
records do accept non-relay e-mail on port 25. They are,
however, picky about IP addresses from which they'll accept
connections (trying to connect via a commercial VPN server
fails, but connecting via other machines works).

-- 
Grant Edwards   grante Yow! WHO sees a BEACH BUNNY
  at   sobbing on a SHAG RUG?!
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Deflate with urllib2... (solved)

2008-09-19 Thread Gabriel Genellina

En Thu, 18 Sep 2008 23:29:30 -0300, Sam <[EMAIL PROTECTED]> escribió:


On Sep 18, 2:10 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:

En Tue, 16 Sep 2008 21:58:31 -0300, Sam <[EMAIL PROTECTED]> escribió:
The code is correct - try with another server. I tested it with a  
LightHTTPd server and worked fine.


Gabriel...

I found a bunch of servers to test it on.  It fails on every server I
could find (sans one).

Here's the ones it fails on:
slashdot.org
hotmail.com
godaddy.com
linux.com
lighttpd.net

I did manage to find one webserver it succeeded on---that is
kenrockwel.com --- a domain squatter with a typoed domain of one of my
favorite photographer's websites (the actual website should be
kenrockwell.com)

This squatter's site is indeed running lighttpd---but it appears to be
an earlier version, because the official lighttpd site fails on this
test.

We have all the major web servers failing the test:
* Apache 1.3
* Apache 2.2
* Microsoft-IIS/6.0
* lighttpd/1.5.0

So I think it's the python side that is wrong, regardless of what the
standard is.


I've found the problem. The zlib header is missing (2 bytes), data begins  
right with the compressed stream. You may decode it if you pass a negative  
value for wsize:


  try:
data = zlib.decompress(data)
  except zlib.error:
data = zlib.decompress(data, -zlib.MAX_WBITS)

Note that this is clearly in violation of RFC 1950: the header is *not*  
optional.


BTW, the curl developers had this same problem some time ago  
 and the proposed solution  
is the same as above.


This is the output from your test script modified as above. (Note that in  
some cases, the compressed stream is larger than the uncompressed data):


Trying:  http://slashdot.org
  http://slashdot.org - Apache/1.3.41 (Unix) mod_perl/1.31-rc4 (deflate)  
len(def

late)=73174 len(gzip)=73208
  Able to decompress...went from 73174 to 73073.

Trying:  http://www.hotmail.com
  http://www.hotmail.com - Microsoft-IIS/6.0 (deflate) len(deflate)=1609  
len(gzi

p)=1635
  Able to decompress...went from 1609 to 3969.

Trying:  http://www.godaddy.com
  http://www.godaddy.com - Microsoft-IIS/6.0 (deflate) len(deflate)=40646  
len(gz

ip)=157141
  Able to decompress...went from 40646 to 157141.

Trying:  http://www.linux.com
  http://www.linux.com - Apache/2.2.8 (Unix) PHP/5.2.5 (deflate)  
len(deflate)=52

862 len(gzip)=52880
  Able to decompress...went from 52862 to 52786.

Trying:  http://www.lighttpd.net
  http://www.lighttpd.net - lighttpd/1.5.0 (deflate) len(deflate)=5669  
len(gzip)

=5687
  Able to decompress...went from 5669 to 15746.

Trying:  http://www.kenrockwel.com
  http://www.kenrockwel.com - lighttpd (deflate) len(deflate)=414  
len(gzip)=426

  Able to decompress...went from 414 to 744.

--
Gabriel Genellina

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


Re: Python newbie

2008-09-19 Thread Peter Pearson
On 19 Sep 2008 08:13:05 GMT, Steven D'Aprano  wrote:
> On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
[snip]
>> perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'
>> 
>> The equivalent in Python looks like this:
>
> Actually, no it doesn't. The equivalent looks more like this:
>
>
> $ python -c 'a=[1,2,3]; a=[2*x for x in a];
> for x in a:
> print x'
> 2
> 4
> 6

Good job, SD'A.

A note for Mladen Gogala: the [...for...] construction in

  a=[2*x for x in a];

is called a list comprehension, and is wonderful for shortening
and clarifying code.  One of the pleasures of following this
group is watching the old hands answer noob questions with one
or two list comprehensions.

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


Re: improving a huge double-for cycle

2008-09-19 Thread giltay
On Sep 18, 7:42 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> I'm not being snarky about losing priority here, but I submitted
> essentially the same solution two hours earlier than pruebono.

My apologies (seriosuly).  In this case, I think it might just be
haste.  For what it's worth, I saw your post (on Google Groups), but I
skipped over it.  You wrote two solutions, one slow and one fast (the
latter being the same as pruebono's).  You put the slow one at the
top, I saw

for ...
for ...

and went straight to the next message without reading the better
solution.  I knew that there was only one for loop necessary, so I
didn't bother reading on.  Actually, I missed pruebono's post, too,
until after I figured it out myself (but before I posted).

That several people came up with the nigh exact same solution, modulo
variable names only, says something about the Zen of Python.

Geoff G-T

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


Re: Multimapping and string converting

2008-09-19 Thread Gabriel Genellina
En Fri, 19 Sep 2008 10:59:26 -0300, Ron Brennan <[EMAIL PROTECTED]>  
escribió:



Hello,

I have a multimap dictionary with a 1 Key to N values.  I want to convert
the N values to a string to be used elsewhere in my program.

So I have dict[(1,[1, 2 ,3 ,4])] which I have sorted

When I do a print ''.join(str(dict.value())) I get [1, 2, 3, 4] as an  
output

when I really want 1 2 3 4

Here is my code:

dmapItems = dictionary.items()
dmapItems.sort()

for tcKey, tcValue in dmapItems:
file.write('Key = %s\nValue = %s" % (tcKey, tcValue)

stinger = ''.join(str(tcValue))

print stringer

The Output = [145, 2345, 567, 898]
I need it to be 145 2345 567 898


I guess you probably tried using ' '.join(value) and got an error like  
this:

TypeError: sequence item 0: expected string, int found
so you inserted that str(...), but you don't still get what you want.
You want "145 2345 567 898". *If* you had a list like this: value =  
["145", "2345", "567", "898"] (that is, a list of strings) *then* '  
'.join(value) would do what you want. But you have this to start with  
instead: value = [145, 2345, 567, 898] (a list of numbers), how to convert  
it into a list of strings?
str(value) returns a single string that "looks like" a list but isn't:  
"[145, 2345, 567, 898]". Instead, you need to convert each element  
individually, and keep them in a list. Try this:

value = [str(elem) for elem in value]
Also, I'd use sorted() to iterate over all items. PUtting all together:

for tcKey, tcValue in sorted(dictionary.items()):
values = [str(elem) for elem in tcValue]
values = ' '.join(values)
print tcKey, values

or perhaps:

for tcKey, tcValue in sorted(find_a_good_name_for_me.iteritems()):
values_str = ' '.join(str(elem) for elem in tcValue)
print tcKey, values_str

(using lazy objects (iteritems and a generator expression) instead of  
concrete lists)


--
Gabriel Genellina

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


Re: generator exceptions

2008-09-19 Thread Gabriel Genellina
En Fri, 19 Sep 2008 10:40:00 -0300, Alexandru Mosoi <[EMAIL PROTECTED]>  
escribió:



i have a generator that raises an exception when calling next(),
however if I try to catch the exception and print the traceback i get
only the line where next() was called


while True:
  try:
iterator.next()
  except StopIteration:
break
  except Exception, e:
traceback.print_exc()

how do I get the traceback starting from where exception was raised in
first place?


I get a complete traceback:

py> import traceback
py>
py> def a(x):
... raise ValueError
...
py> def b(x):
... return a(x)
...
py> def c(n):
... for i in range(n):
... yield b(i)
...
py> it = c(5)
py> while True:
...   try:
... it.next()
...   except StopIteration:
... break
...   except Exception:
... print traceback.print_exc()
...
Traceback (most recent call last):
  File "", line 3, in 
  File "", line 3, in c
  File "", line 2, in b
  File "", line 2, in a
ValueError
None
py>

Could you provide a more complete example that fails?

--
Gabriel Genellina

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


Re: improving a huge double-for cycle

2008-09-19 Thread Harald Luessen
On Thu, 18 Sep 2008 Bruno Desthuilliers wrote:

># Harald : uncomment this and run test_results. As far as I can tell, it
># doesn't yields the expected results
>
>## IN7 = IN[:]
>## def sortk7(n):
>## return n.coordinates[0]
>
>## def doubles7():
>## "is ordering better ? - Nope Sir, it's broken..."
>## IN7.sort(key=sortk)
>## SN = []
>## sn_append = SN.append
>## in_len = len(IN)
>## for i in xrange(in_len):
>## node_i = IN[i]
>## coords_i = node_i.coordinates
>## for j in xrange(i+1, in_len):
>## if coords_i[0] == IN[j].coordinates[0]:
>## if coords_i[1] == IN[j].coordinates[1]:
>## sn_append(node_i)
>## else:
>## break
>## return SN
...
>Results here (py2.5, gentoo linux, athlonxp1800, 512 ram):
>
> >>> test_results()
>True
> >>> test_times()
>doubles0 : 1.55667901039
>doubles1 : 0.719144105911
>doubles2 : 0.703393936157
>doubles3 : 0.700654983521
>doubles4 : 0.706257104874
>doubles5 : 0.528184890747
>doubles6 : 0.461633205414
>doubles8 : 0.0134379863739
>doubles9 : 0.0108540058136

When you change my code then do it right. :-)
You forgot to change the IN to IN7 at _every_ place.
And the sortk should be sortk7 in _both_ places.

I never let the code run before myself. I just wrote it 
in the newsreader. But now i did and I have a second 
version as bonus.


IN7 = IN[:]

def sortk7(n):
return n.coordinates[0], n.coordinates[1]

def doubles7():
IN7.sort(key=sortk7)
SN = []
sn_append = SN.append
in_len = len(IN7)
for i in xrange(in_len):
node_i = IN7[i]
coords_i = node_i.coordinates
for j in xrange(i+1, in_len):
if coords_i[0] == IN7[j].coordinates[0]:
if coords_i[1] == IN7[j].coordinates[1]:
sn_append(node_i)
else:
break
return SN


def comp7( x, y ):
 return cmp( x.coordinates, y.coordinates )

def doubles7a():
IN7.sort( comp7 )
SN = []
sn_append = SN.append
in_len = len(IN7)
for i in xrange(in_len):
node_i = IN7[i]
for j in xrange(i+1, in_len):
node_j = IN7[j]
if comp7( node_i, node_j ) == 0:
sn_append(node_i)
else:
break
return SN


Here are the results. (py2.5, WindowsXP, Pentium4, 2.6GHz, 1.5GB):
My version is not so bad.

doubles0 : 1.03830598582
doubles1 : 0.47943719104
doubles2 : 0.487412506338
doubles3 : 0.475924733451
doubles4 : 0.466548681466
doubles5 : 0.340487967046
doubles6 : 0.278480365521
doubles7 : 0.0953190978183
doubles7a : 0.0784233750379
doubles8 : 0.010236496538
doubles9 : 0.00742803903848


Harald

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


Re: improving a huge double-for cycle

2008-09-19 Thread pruebauno
On Sep 18, 7:42 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Thu, 18 Sep 2008 20:43:00 +0200, Bruno Desthuilliers wrote:
> > Now the obvious winner is pruebono - even unoptimized, using sets seems
> > to be *way* faster than even the most optimized corrected version of
> > your algorithm.
>
> I'm not being snarky about losing priority here, but I submitted
> essentially the same solution two hours earlier than pruebono.
>
> Are people not seeing my posts? Have I been kill-filed by everyone except
> Mensator? I also asked a question about HTTPError and I haven't seen any
> responses at all.
>
> --
> Steven

I see your post now, but I didn't notice it at the time I posted.
Could be because I am using the noticeable bad Google groups interface
that is known for being unreliable. Duplicate solutions on Usenet are
almost a given and I consider duplicate solutions a good thing, it
means that other people will be able to understand that code.

In any case I am not here for glory I am posting under a pseudonym so
nobody discovers that I am slacking off at work reading Usen[carrier
lost]
--
http://mail.python.org/mailman/listinfo/python-list


Re: improving a huge double-for cycle

2008-09-19 Thread Terry Reedy

Gabriel Genellina wrote:
En Fri, 19 Sep 2008 02:11:51 -0300, Tino Wildenhain <[EMAIL PROTECTED]> 
escribió:



Also I never saw a list where the threading often goes wrong like
this here - is there any special setup or is it just peoples MUA
which screws up?


Perhaps it's due to the newsgroup/list duality...


Actually, the situation is a c.l.p <==> python-list <==> 
gmane.c.p.general triality.


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


Re: Problem with Python shell through Cygwin Screen (Python/Vim/Screen combo)

2008-09-19 Thread Jason Tishler
Ant,

On Fri, Sep 19, 2008 at 03:10:10AM -0700, Ant wrote:
> [snip]
> This works great in Linux, and also in Windows using the Cygwin build
> of Python, but when you call:
>
> screen -S py -s python
>
> using the Windows build of Python, the shell just hangs. Does anyone
> know if this is a known issue with screen/Python, or if there is a
> workaround, as I'd prefer to have just a single Python instance
> installed rather than having Cygwin python and windows Python both
> installed.

There are known issues when trying to run a Windows program that
directly accesses the console under Cygwin.  For example:

http://mail.python.org/pipermail/python-list/2004-June/21.html

AFAICT, you will have to use Cygwin Python with screen.

Jason

-- 
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D  8784 1AFD E4CC ECF4 8EF6
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie

2008-09-19 Thread bearophileHUGS
Mladen Gogala:

Welcome to Python, you will need only one or few weeks to be able to
write useful Python programs. But even with the help of a good book
(that I suggest you to read) you may need several months or one year
to master it :-)

> 2) Why is it illegal to pass a built-in function "print" to map?

Maybe because one of the few original design mistakes of Python. The
new Python 3.0 "fixes" that problem, in Python 3 print is now a
function, so you can use it where you can use functions.
But in general I think it's better to not give map() a function that
has side effects like print().
So you want to use a list comp there instead, and create a new list:
[2 * el for el in a]

In Python (and several other modern languages, like Scala, plus some
more functional languages) you will find that people often prefer to
create new data structures instead of modifying old ones. This may
sound like a waste of memory and time, but using a more immutable-data
style has several advantages (often in code clarity) that are a bit
complex to explain here.

As you probably know, the good thing of learning new languages is that
they carry some principles, and they are the expression of some ideas
of the computer science. So learning Python may show you some new
ideas, or new ways to look at old computer science ideas you already
know. I think Alan J. Perlis said "A language that doesn't affect the
way you think about programming, is not worth knowing."

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


Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread Almar Klein
>
> Ah, no, that's a different thing. If the parent exits, the child will
> also be killed I believe.


Not if it's stuck in some endless loop...

If you want to spawn a process and have it live on independent of the
> parent, you want to make the child process a "daemon", detatching
> itself from the parent's environment. I don't recall how that's done
> immediately, but those are the terms to search for.


I'm curious how this can be done, does anyone know this?

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

Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread Marco Bizzarri
On Fri, Sep 19, 2008 at 10:48 PM, Almar Klein <[EMAIL PROTECTED]> wrote:
>> Ah, no, that's a different thing. If the parent exits, the child will
>> also be killed I believe.
>
> Not if it's stuck in some endless loop...
>
>> If you want to spawn a process and have it live on independent of the
>> parent, you want to make the child process a "daemon", detatching
>> itself from the parent's environment. I don't recall how that's done
>> immediately, but those are the terms to search for.
>
> I'm curious how this can be done, does anyone know this?
>
> Almar
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


First result in "making a daemon in python with google":

http://mail.python.org/pipermail/python-list/2007-February/427692.html

(not tested)

Regards
Marco
-- 
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread Gary Herron

Almar Klein wrote:


Ah, no, that's a different thing. If the parent exits, the child will
also be killed I believe.


Not if it's stuck in some endless loop...

If you want to spawn a process and have it live on independent of the
parent, you want to make the child process a "daemon", detatching
itself from the parent's environment. I don't recall how that's done
immediately, but those are the terms to search for.


I'm curious how this can be done, does anyone know this?


I just dove into this several day ago for a small project.   

On Linux it's easy -- it involves a couple of forks and other system 
calls.   Google for daemonize.py. 



On Windows, a bit of searching seems to find a consensus that the way to 
do something similar is as a Window's service.  I'm just now looking 
into how to register and start a service, and how to stop and remove it 
later.  Google finds lots of information on this -- perhaps I'll post my 
result when I've pulled it all together.


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


ScrolledCanvas/ScrolledText

2008-09-19 Thread lekin2
So what the heck is going on.  I have a Pmw.ScrollingCanvas and inside  
I put a Pmw.ScrollingText and that all works but I can't get it to  
scroll.  I was thinking I needed an event like:


  self.sc = Pmw.ScrolledCanvas(parent,
  borderframe = 5,
  labelpos = 'n',
  usehullsize = 1,
  hull_width = 700,
  hull_height = 500,
  hscrollmode = 'static',
  vscrollmode = 'static',)

   self._title = Pmw.ScrolledText(self.sc.interior(),
labelpos = 'w',
text_wrap='word',
usehullsize=1,
hull_height= 50,
hull_width=500)
titleVal = "Equation of state for %s" % (materialName)
self._title.insert(1.0, titleVal)
self._title.component("label").config(text="Title:")

  self.sc.component('canvas').bind('', self.scrolling)

def scrolling(self, event):
x = self.sc.canvasx(event.x)
y = self.sc.canvasy(event.y)
self.sc.resizescrollregion()

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


Re: Launching a subprocess without waiting around for the result?

2008-09-19 Thread Michael Palmer
On Sep 18, 5:33 pm, erikcw <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a cgi script where users are uploading large files for
> processing.  I want to launch a subprocess to process the file so the
> user doesn't have to wait for the page to load.
>
> What is the correct way to launch subprocess without waiting for the
> result to return?
>
> Thanks!

both os.spawn or subprocess can be used. I actually find subprocess
hard to remember so usually prefer os.spawn. For various examples and
explanations, see

http://effbot.org/librarybook/os.htm
--
http://mail.python.org/mailman/listinfo/python-list


Re: curses.setsyx()?

2008-09-19 Thread linkmaster032000
On Sep 19, 1:24 am, Tim Roberts <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
>
> >I tried curses.setsyx(2,3) in my script and it doesn't move the curses
> >cursor. Any alternatives/solutions?
>
> Did you call doupdate after?  setsyx just manipulates the data structures.
> It takes a call to doupdate to force those changes to take effect visually.
> --
> Tim Roberts, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.

I added it and it still doesn't work. This is what I'm doing when I
want to display the cursor and prepare it for input at 2,3:

curses.echo()
curses.curs_set(1)
curses.setsyx(2,3)
curses.doupdate()
--
http://mail.python.org/mailman/listinfo/python-list


migrating processess to avoid the GIL

2008-09-19 Thread Patrick Stinson
I need to migrate calls to CPython to another process in my C++ app to
get around the GIL. Does anyone know of a good way to do this on
windows and Mac? All calls and callbacks can be blocking, I just need
to share some data structures.

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


Re: Pexpect echoes twice for each read

2008-09-19 Thread RDarrelBonner
On Sep 9, 4:01 pm, "Sriram Rajan" <[EMAIL PROTECTED]> wrote:
> For some reason, Using pexpect causes my output to echo twice when I
> connect from my MAC Darwin (10.4) to Linux (CentOS release 5 ):
>
> The program:
> -
> #!/usr/bin/python
> # Automatic scp to remote host
> # Input 1 : filename
> # Input 2 : destination folder
> # Input 3 : hostname
>
> import pexpect
> import sys,re
>
> ssh_cmd = "ssh " + sys.argv[3]
> ssh_handle = pexpect.spawn (ssh_cmd)
> ssh_handle.logfile = sys.stdout
> PROMPT = "\$\ $"
>
> try:
>   ssh_handle.expect(PROMPT)
>
>   ssh_handle.sendline ("scp "+ sys.argv[1] +" [EMAIL PROTECTED]:" +
> sys.argv[2] )
>
>   ssh_handle.expect("password:")
>
>   ssh_handle.sendline(" ")
>
>   ssh_handle.expect(PROMPT)
>
> except pexpect.TIMEOUT:
> ssh_handle.logfile.write("\n Pexpect timeout !!\n")
> sys.exit(1)
> except KeyboardInterrupt:
> ssh_handle.logfile.write("\n User interrupt!\n")
> sys.exit(2)
> ssh_handle.close()
>
> Output:
> ---
> $ python pexpect_test.py replace_line_break.sh /tmp/ 10.5.254.18
> Last login: Tue Sep  9 15:45:05 2008 from sriram-macbook.dhcp.2wire.com
> $ scp replace_line_break.sh [EMAIL PROTECTED]:/tmp/
> scp replace_line_break.sh [EMAIL PROTECTED]:/tmp/
> [EMAIL PROTECTED]'s password:
>
> replace_line_break.sh 100%  296 0.3KB/s   00:00
> $ $

Hi,

I had this issue as well. To correct it I upgraded to pexpect 2.3 from
2.1 and changed the following:

OLD: ssh_handle.logfile = sys.stdout
NEW: ssh_handle.logfile_read = sys.stdout

This eliminated the double display of characters as only the
characters being sent back from the device are displayed.

Let me know if this works for you.

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


Re: dict generator question

2008-09-19 Thread MRAB
On Sep 19, 2:01 pm, [EMAIL PROTECTED] wrote:
> Gerard flanagan:
>
> > data.sort()
> > datadict = \
> > dict((k, len(list(g))) for k,g in groupby(data, lambda s:
> >      '.'.join(s.split('.',2)[:2])))
>
> That code may run correctly, but it's quite unreadable, while good
> Python programmers value high readability. So the right thing to do is
> to split that line into parts, giving meaningful names, and maybe even
> add comments.
>
> len(list(g))) looks like a good job for my little leniter() function
> (or better just an extension to the semantics of len) that time ago
> some people here have judged as useless, while I use it often in both
> Python and D ;-)
>
Extending len() to support iterables sounds like a good idea, except
that it could be misleading when:

len(file(path))

returns the number of lines and /not/ the length in bytes as you might
first think! :-)

Anyway, here's another possible implementation using bags (multisets):

def major_version(version_string):
"convert '1.2.3.2' to '1.2'"
return '.'.join(version_string.split('.')[:2])

versions = ["1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

bag_of_versions = bag(major_version(x) for x in versions)
dict_of_counts = dict(bag_of_versions.items())

Here's my implementation of the bag class in Python (sorry about the
length):

class bag(object):
def __init__(self, iterable = None):
self._counts = {}
if isinstance(iterable, dict):
for x, n in iterable.items():
if not isinstance(n, int):
raise TypeError()
if n < 0:
raise ValueError()
self._counts[x] = n
elif iterable:
for x in iterable:
try:
self._counts[x] += 1
except KeyError:
self._counts[x] = 1
def __and__(self, other):
new_counts = {}
for x, n in other._counts.items():
try:
new_counts[x] = min(self._counts[x], n)
except KeyError:
pass
result = bag()
result._counts = new_counts
return result
def __iand__(self):
new_counts = {}
for x, n in other._counts.items():
try:
new_counts[x] = min(self._counts[x], n)
except KeyError:
pass
self._counts = new_counts
def __or__(self, other):
new_counts = self._counts.copy()
for x, n in other._counts.items():
try:
new_counts[x] = max(new_counts[x], n)
except KeyError:
new_counts[x] = n
result = bag()
result._counts = new_counts
return result
def __ior__(self):
for x, n in other._counts.items():
try:
self._counts[x] = max(self._counts[x], n)
except KeyError:
self._counts[x] = n
def __len__(self):
return sum(self._counts.values())
def __list__(self):
result = []
for x, n in self._counts.items():
result.extend([x] * n)
return result
def __repr__(self):
return "bag([%s])" % ", ".join(", ".join([repr(x)] * n) for x,
n in self._counts.items())
def __iter__(self):
for x, n in self._counts.items():
for i in range(n):
yield x
def keys(self):
return self._counts.keys()
def values(self):
return self._counts.values()
def items(self):
return self._counts.items()
def __add__(self, other):
for x, n in other.items():
self._counts[x] = self._counts.get(x, 0) + n
def __contains__(self, x):
return x in self._counts
def add(self, x):
try:
self._counts[x] += 1
except KeyError:
self._counts[x] = 1
def __add__(self, other):
new_counts = self._counts.copy()
for x, n in other.items():
try:
new_counts[x] += n
except KeyError:
new_counts[x] = n
result = bag()
result._counts = new_counts
return result
def __sub__(self, other):
new_counts = self._counts.copy()
for x, n in other.items():
try:
new_counts[x] -= n
if new_counts[x] < 1:
del new_counts[x]
except KeyError:
pass
result = bag()
result._counts = new_counts
return result
def __iadd__(self, other):
for x, n in other.items():
try:
self._counts[x] += n
except KeyError:
self._counts[x] = n
def __isub__(self, other):
for x, n in other.items():
try:
self._counts[x] -= n
if self._counts[x] < 1:
del self._counts[x]
except KeyError:
pass
def clear(

Re: dict generator question

2008-09-19 Thread Steven D'Aprano
On Fri, 19 Sep 2008 17:00:56 -0700, MRAB wrote:

> Extending len() to support iterables sounds like a good idea, except
> that it could be misleading when:
> 
> len(file(path))
> 
> returns the number of lines and /not/ the length in bytes as you might
> first think!

Extending len() to support iterables sounds like a good idea, except that 
it's not.

Here are two iterables:


def yes():  # like the Unix yes command
while True:
yield "y"

def rand(total):
"Return random numbers up to a given total."
from random import random
tot = 0.0
while tot < total:
x = random()
yield x
tot += x


What should len(yes()) and len(rand(100)) return?



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


Re: Out of memory issue with dialog box

2008-09-19 Thread Gabriel Genellina

En Thu, 18 Sep 2008 13:32:32 -0300, <[EMAIL PROTECTED]> escribió:


We have a mutilthreaded process in which one of the threads uses too
much memory causing the process to run out of memory. However when
this happens we see a dialog box pop up with the message "fatal error
in GC : too many heap sections." When you click "ok" only then does
the process die.


That message comes from the Boehm garbage collection library; Python does  
not use it. Check any compiled extension in use.


--
Gabriel Genellina

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


Re: dict generator question

2008-09-19 Thread bearophileHUGS
MRAB:
> except that it could be misleading when:
> len(file(path))
> returns the number of lines and /not/ the length in bytes as you might
> first think! :-)

Well, file(...) returns an iterable of lines, so its len is the number
of lines :-)
I think I am able to always remember this fact.


> Anyway, here's another possible implementation using bags (multisets):

This function looks safer/faster:

def major_version(version_string):
"convert '1.2.3.2' to '1.2'"
return '.'.join(version_string.strip().split('.', 2)[:2])

Another version:

import re
patt = re.compile(r"^(\d+\.\d+)")

dict_of_counts = defaultdict(int)
for ver in versions:
dict_of_counts[patt.match(ver).group(1)] += 1

print dict_of_counts

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


Re: Deflate with urllib2...

2008-09-19 Thread Sam
Gabriel...

Awesome!  Thank you so much for the solution.

And yeah, I found exactly one website that strangely enough only does
deflate, not gzip.  I'd rather not say what website it is, since it's
small and not mine.  They may be few and in between, but they do
exist.

Thanks


On Sep 19, 3:48 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 18 Sep 2008 23:29:30 -0300, Sam <[EMAIL PROTECTED]> escribió:
>
> > On Sep 18, 2:10 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > wrote:
> >> En Tue, 16 Sep 2008 21:58:31 -0300, Sam <[EMAIL PROTECTED]> escribió:
> >> The code is correct - try with another server. I tested it with a  
> >> LightHTTPd server and worked fine.
>
> > Gabriel...
>
> > I found a bunch of servers to test it on.  It fails on every server I
> > could find (sans one).
>
> I'll try to check later. Anyway, why are you so interested in deflate?  
> Both "deflate" and "gzip" coding use the same algorithm and generate  
> exactly the same compressed stream, the only difference being the header  
> and tail format. Have you found any server supporting deflate that doesn't  
> support gzip as well?
>
> --
> Gabriel Genellina

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


report a BUG of package setuptools-0.6c8.

2008-09-19 Thread 为爱而生
Traceback (most recent call last):
  File "setup.py", line 176, in 
main()
  File "setup.py", line 172, in main
setup(**args)
  File "/usr/lib/python2.5/distutils/core.py", line 151, in setup
dist.run_commands()
  File "/usr/lib/python2.5/distutils/dist.py", line 974, in run_commands
self.run_command(cmd)
  File "/usr/lib/python2.5/distutils/dist.py", line 994, in run_command
cmd_obj.run()
  File "/usr/lib/python2.5/site-packages/setuptools/command/install.py",
line 76, in run
self.do_egg_install()
  File "/usr/lib/python2.5/site-packages/setuptools/command/install.py",
line 96, in do_egg_install
self.run_command('bdist_egg')
  File "/usr/lib/python2.5/distutils/cmd.py", line 333, in run_command
self.distribution.run_command(command)
  File "/usr/lib/python2.5/distutils/dist.py", line 994, in run_command
cmd_obj.run()
  File "/usr/lib/python2.5/site-packages/setuptools/command/bdist_egg.py",
line 167, in run
self.run_command("egg_info")
  File "/usr/lib/python2.5/distutils/cmd.py", line 333, in run_command
self.distribution.run_command(command)
  File "/usr/lib/python2.5/distutils/dist.py", line 994, in run_command
cmd_obj.run()
  File "/usr/lib/python2.5/site-packages/setuptools/command/egg_info.py",
line 171, in run
self.find_sources()
  File "/usr/lib/python2.5/site-packages/setuptools/command/egg_info.py",
line 252, in find_sources
mm.run()
  File "/usr/lib/python2.5/site-packages/setuptools/command/egg_info.py",
line 306, in run
self.add_defaults()
  File "/usr/lib/python2.5/site-packages/setuptools/command/egg_info.py",
line 333, in add_defaults
rcfiles = list(walk_revctrl())
  File "/usr/lib/python2.5/site-packages/setuptools/command/sdist.py", line
45, in walk_revctrl
for item in ep.load()(dirname):
  File "/usr/lib/python2.5/site-packages/setuptools/command/sdist.py", line
52, in _default_revctrl
for path in finder(dirname,path):
  File "/usr/lib/python2.5/site-packages/setuptools/command/sdist.py", line
98, in entries_finder
log.warn("unrecognized .svn/entries format in %s", dirname)
NameError: global name 'log' is not defined

global name 'log' is not defined to the line 98!!!

-- 
"OpenBookProject"-开放图书计划邮件列表
详情: http://groups.google.com/group/OpenBookProject
维基: http://wiki.woodpecker.org.cn/
--
http://mail.python.org/mailman/listinfo/python-list

Re: Installing pySerial

2008-09-19 Thread eliben
On Sep 18, 6:01 pm, Tim Golden <[EMAIL PROTECTED]> wrote:
> Joe G (Home) wrote:
> > Hi All,
>
> > Background
> > ===
> > I have installed Python for windows today from the python web site  .I also
> > installed pySerial using the Windows installer from the sourceforge web
> > site. Both installs use the default directories.
>
> > Phyton  version    : Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC
> > v.1310 32 bit (Intel)] on win32
>
> > pySerial   2.4   July 6th
>
> > Problem :  Errors Screen output
> > 
>  import serial
>
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >     import serial
> >   File "C:\Python25\Lib\site-packages\serial\__init__.py", line 18, in
> > 
> >     from serialwin32 import *
> >   File "C:\Python25\Lib\site-packages\serial\serialwin32.py", line 9, in
> > 
> >     import win32file  # The base COM port and file IO functions.
> > ImportError: No module named win32file
>
> You need to install the pywin32 extensions from:
>
>  http://pywin32.sf.net
>
> They're so commonly used (and, if you install the ActiveState
> distro of Python, even bundled) that I imagine many Windows
> Pythoneers like myself simply install them automatically as
> soon as we've installed the main python.org Python.
>
> Once you've done that, the rest should just work: it's
> clear from the traceback that the serial module is getting
> imported; it's just trying to find the win32file module.
>

Why are people preferring the python.org package over ActiveState's,
which seems to be more complete and includes more modules (like
pywin32) ?

Eli

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


Re: migrating processess to avoid the GIL

2008-09-19 Thread Aaron "Castironpi" Brady
On Sep 19, 6:40 pm, "Patrick Stinson" <[EMAIL PROTECTED]>
wrote:
> I need to migrate calls to CPython to another process in my C++ app to
> get around the GIL. Does anyone know of a good way to do this on
> windows and Mac? All calls and callbacks can be blocking, I just need
> to share some data structures.
>
> Cheers

You should look into 'mmap' and 'struct'.  mmap shares memory between
processes, and is a random-access read-write file buffer.  struct
packs data from primitive types (integers, floats, & short strings)
into a buffer.

If you need advice on structuring your shared file, feel free to write
back.
--
http://mail.python.org/mailman/listinfo/python-list


Enjoy seeing Enjoy seeingEnjoy seeingEnjoy seeing Enjoy seeing

2008-09-19 Thread [EMAIL PROTECTED]
Enjoy seeing
with
   new
  video pictures
   http://nagaaraja.blogspots.com\
--
http://mail.python.org/mailman/listinfo/python-list


Re: XML-schema 'best practice' question

2008-09-19 Thread Frank Millman
On Sep 18, 8:28 am, Frank Millman <[EMAIL PROTECTED]> wrote:
> Hi all
>
> This is not strictly a Python question, but as I am writing in Python,
> and as I know there are some XML gurus on this list, I hope it is
> appropriate here.
>
> XML-schemas are used to define the structure of an xml document, and
> to validate that a particular document conforms to the schema. They
> can also be used to transform the document, by filling in missing
> attributes with default values.
>
[..]
>
> Or maybe the best practice is to *always* validate a document before
> processing it.
>

I have realised that my question was irrelevant.

xml's raison d'etre is to facilitate the exchange of information
between separate entities. If I want to use xml as a method of
serialisation within my own system, I can do what I like, but there
can be no question of 'best practice' in this situation.

When xml is used as intended, and you want to process a document
received from a third party, there is no doubt that you should always
validate it first before processing it. Thank you, Lorenzo, for
pointing out the obvious. It may take me a while to catch up, but at
least I can see things a little more clearly now.

As to why I am using xml at all, I know that there is a serious side
to Skip's light-hearted comment, so I will try to explain.

I want to introduce an element of workflow management (aka Business
Process Management) into the business/accounting system I am
developing. I used google to try to find out what the current state of
the art is. After several months of very confusing research, this is
the present situation, as best as I can figure it out.

There is an OMG spec called BPMN, for Business Process Modeling
Notation. It provides a graphical notation, intended to be readily
understandable by all business users, from business analysts, to
technical developers, to those responsible for actually managing and
monitoring the processes. Powerful though it is, it does not provide a
standard method of serialsing the diagram, so there is no standard way
of exchanging a diagram between different vendors, or of using it as
input to a workflow engine.

There is an OASIS spec called WS-BPEL, for Web Services Business
Process Execution Language. It defines a language for specifying
business process behavior based on Web Services. This does have a
formal xml-based specification. However, it only covers processes
invoked via web services - it does not cover workflow-type processes
within an organisation. To try to fill this gap, a few vendors got
together and submitted a draft specification called BPEL4People. This
proposes a series of extensions to the WS-BPEL spec. It is still at
the evaluation stage.

The BPMN spec includes a section which attempts to provide a mapping
between BPMN and BPEL, but the authors state that there are areas of
incompatibility, so it is not a perfect mapping.

Eventually I would like to make sense of all this, but for now I want
to focus on BPMN, and ignore BPEL. I can use wxPython to design a BPMN
diagram, but I have to invent my own method of serialising it so that
I can use it to drive the business process. For good or ill, I decided
to use xml, as it seems to offer the best chance of keeping up with
the various specifications as they evolve.

I don't know if this is of any interest to anyone, but it was
therapeutic for me to try to organise my thoughts and get them down on
paper. I am not expecting any comments, but if anyone has any thoughts
to toss in, I will read them with interest.

Thanks

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