Re: Trouble converting hex to decimal?

2005-02-05 Thread Miki Tebeka
Hello Earl,

> I'm trying to process the IP packet length field, as recorded by pcap
> (Ethereal) and recovered using pcapy.  When I slice out those bytes, I
> get a value that shows in '\x00' format, rather than '0x00'.  Neither
> int() nor eval() are working.  How do I handle this?
Try using "array" or "struct" modules.

HTH.
--

Miki Tebeka <[EMAIL PROTECTED]>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Alex Martelli
Arthur <[EMAIL PROTECTED]> wrote:

> Do the STUPID firms use Python as well.

Yes, they're definitely starting to do so.


> Why?  

The single most frequent reason is that some techie sneaked it in, for
example "just for testing" or "to do a prototype" or even without any
actual permission.  Firms hardly ever follow the "build one to throw
away" dictum (and some argue that the dictum is wrong and the firms are
right), so, once it's in, it's in.

There are other reasons, such as "it's intolerable that we're using half
a dozen different languages, we need to unify all our software and
rewrite it into just one language" -- a rather silly reason, and this
plan (to rewrite perfectly good working software just for the purpose of
having it all in one language) generally doesn't come to fruition, but
meanwhile there are very few candidate languages that _could_ fill all
the different niches *and* not bust the PHB's softare-purchase business.

Even the various "success stories" we've collected (both on websites,
and, more impressive to PHBs, into paper booklets O'Reilly has printed)
play a role.  ``NASA uses it for space missions, so of course we must
use it to control our hot dog franchises'' -- we DID say we're talking
about stupid firms, right?

This is a good development, overall.  Against stupidity, the gods
themselves contend in vain; Python's entrance into stupid firms broadens
its potential appeal from less than 10% to around 100% of the market,
which is good news for sellers of books, tools, training, consultancy
services, and for Python programmers everywhere -- more demand always
helps.  *BUT* the price is eternal vigilance...


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


Multiple constructors

2005-02-05 Thread Philip Smith
Call this a C++ programmers hang-up if you like.

I don't seem to be able to define multiple versions of __init__ in my matrix 
class (ie to initialise either from a list of values or from 2 dimensions 
(rows/columns)).

Even if Python couldn't resolve the __init__ to use on the basis of argument 
types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this

Thanks

Phil 


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


Re: Trouble converting hex to decimal?

2005-02-05 Thread Adam DePrince
On Sat, 2005-02-05 at 12:02, Steve Holden wrote:
> Pedro Werneck wrote:
> > The problem is that '\x00' is a escape sequence... 
> > Try something like this:
> x = '\x00'
> int(repr(x)[3:-1], 16)
> > 0
> x = '\x15'
> int(repr(x)[3:-1], 16)
> > 21
> > On Sat, 05 Feb 2005 06:51:32 -0700
> > Earl Eiland <[EMAIL PROTECTED]> wrote:
> >>I'm trying to process the IP packet length field, as recorded by pcap
> >>(Ethereal) and recovered using pcapy.  When I slice out those bytes, I
> >>get a value that shows in '\x00' format, rather than '0x00'.  Neither
> >>int() nor eval() are working.  How do I handle this?
> >>Earl Eiland
> Talk about making things difficult!
>   >>> x = '\x00'
>   >>> ord(x)
> 0
>   >>> x = '\x15'
>   >>> ord(x)
> 21
>   >>>

Ethereal's emission of \x00 shouldn't make your life any more
difficult.  The conversion of \0xx takes place in the eval.  If you
store the string \x00 in a file and call open.read or command.getoutput,
you will get the python string "\\x00".  Stuff you read from a file
arrives un-de-escaped.

The solution is simple.  

mystring.replace( "\x","0x" )




Adam DePrince 


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


Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Michael Hartl
Adam brings up a good point: eval is a very general function which
evaluates an arbitrary Python expression.  As a result, it (and its
close cousin exec) should be used with caution if security is an issue.

Michael

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


Re: variable declaration

2005-02-05 Thread Alex Martelli
Nick Coghlan <[EMAIL PROTECTED]> wrote:
   ...
> > _temp = x.y
> > x.y = type(temp).__irebind__(temp, z)
   ...
> I was thinking of something simpler:
> 
>x.y
>x.y = z
> 
> That is, before the assignment attempt, x.y has to resolve to *something*, but
> the interpreter isn't particularly fussy about what that something is.

OK, I guess this makes sense.  I just feel a tad apprehensive at
thinking that the semantics differ so drastically from that of every
other augmented assignment, I guess.  But probably it's preferable to
NOT let a type override what this one augmented assignment means; that
looks like an "attractive nuisance" tempting people to be too clever.

Still, if you write a PEP, I would mention the possible alternative and
why it's being rejected in favor of this simpler one.


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


Word for a non-iterator iterable?

2005-02-05 Thread Leif K-Brooks
Is there a word for an iterable object which isn't also an iterator, and 
therefor can be iterated over multiple times without being exhausted? 
"Sequence" is close, but a non-iterator iterable could technically 
provide an __iter__ method without implementing the sequence protocol, 
so it's not quite right.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Adam DePrince
On Sat, 2005-02-05 at 17:11, Michael Hartl wrote:
> Use the eval function:
> 
> >>> eval("30/(6+9)")
> 2
> 
> Michael

Sure, if you trust the source of the string you are evaluating.  If this
is a form submission on your web site, be wary of the nefarious user who
might submit to you:

commands.getoutput( "rm -rf %(HOME)s"%os.environ )

as the "expression" to evaluate.


Adam DePrince 


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


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote:
Bo Peng wrote:
Exec is slow since compiling the string and calls to globals() use a 
lot of time.  The last one is most elegant but __getattr__ and 
__setattr__ are costly. The 'evil hack' solution is good since 
accessing x and y takes no additional time.

Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:
 >>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
 >>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']

You are still including the compile overhead in fun2. If you want to see 
how fast the compiled code is you should take the definition of myfun 
out of fun2:

myfun = makeFunction('z = x + y', 'myfun')
def fun2(d):
  for i in xrange(0,N):
myfun(d)
  del d['__builtins__']
Kent
I assumed that most of the time will be spent on N times execution of 
myfunc.

>>> myfun = makeFunction('z = x + y', 'myfun')
>>> def fun2(d):
...   # myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
>>> def fun21(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
>>> profile.run('fun2(a)')
 23 function calls in 1.930 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0001.9301.930 :1(?)
   100.4600.0000.4600.000 myfun:1(?)
10.0000.0001.9301.930 profile:0(fun2(a))
00.000 0.000  profile:0(profiler)
   100.9800.0001.4400.000 python-162616Io.py:4(f)
10.4900.4901.9301.930 python-16261Ud0.py:1(fun2)
>>> profile.run('fun21(a)')
 24 function calls in 1.920 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0001.9201.920 :1(?)
   100.3900.0000.3900.000 myfun:1(?)
10.0000.0001.9201.920 profile:0(fun21(a))
00.000 0.000  profile:0(profiler)
10.0000.0000.0000.000 
python-162616Io.py:2(makeFunction)
   100.9300.0001.3200.000 python-162616Io.py:4(f)
10.6000.6001.9201.920 python-16261huu.py:1(fun21)
--
http://mail.python.org/mailman/listinfo/python-list


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Perl wrote:
>
>> how is a multipart POST request parsed by CGIHTTPServer?
>
> It isn't; the input stream containing the multipart/form-data content
> is passed to the CGI script, which can choose to parse it or not using
> any code it has to hand - which could be the 'cgi' module, but not
> necessarily.
>
>> Where is the parsing done for the POST data following the header?
>
> If you are using the 'cgi' module, then cgi.parse_multipart.

Thanks, at least I was right not to find that in the CGIHTTPServer and 
BaseHTTPServer code.  So I have to use that instead of FieldStorage?  I was 
expecting FieldStorage to encapsulate all that for all the cases, POST with 
multipart/form-data being just a special case.

I took a brief look at cgi.parse_multipart and I still have to figure out 
how to provide the fp argument.  Any code examples anywhere?  All the 
examples I have found were using only FieldStorage:
http://www.cs.virginia.edu/~lab2q/lesson_7/

http://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1
http://gnosis.cx/publish/programming/feature_5min_python.html
http://mail.python.org/pipermail/edu-sig/2001-June/001368.html

BTW, I also tried the example in the last link and that doesn't work either, 
with similar results/problems as my script.  I think all those examples are 
a few years old, has something changed since then?

>> As a side note, I found other old reports of problems with cgi
>> handling POST  requests, reports that don't seem to have had a
>> resolution.
>
> (in particular?)

I made the comment and now I have to back that up:
http://mail.python.org/pipermail/python-list/2002-February/thread.html#88686
http://mail.python.org/pipermail/python-list/2002-September/thread.html#124109

> FWIW, for interface-style and multipart-POST-file-upload-speed reasons
> I wrote an alternative to cgi, form.py
> (http://www.doxdesk.com/software/py/form.html). But I haven't found
> cgi's POST reading to be buggy in general.

I was quite careful in calling the code "fragile" and I am not normally the 
kind of person to mince words.  I would have said buggy if I meant that or I 
could have used even worse words.  But even the rationale behind the patch 
that caused the bug I mentioned ("Remove dependencies on (deprecated) rfc822 
and mimetools modules, replacing with email.") and even your calling this 
part of the standard library "a bit crufty in places due to age" support my 
view that this code needs work.

Besides, I am not happy at all with the idea of having to use 
cgi.parse_multipart instead of FieldStorage.  It seems a lot more low level 
than I would like to even if it offers more control.  I looked at your 
form.py and you seem to address that.  Sorry though, I will probably not use 
it.  Once I learn enough from the cgi module I will just move on to using a 
framework like CherryPy.

Dan 


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


Re: a type without a __mro__?

2005-02-05 Thread John Lenton
On Sat, Feb 05, 2005 at 11:37:10AM +0100, Alex Martelli wrote:
> Can anybody suggest where to find (within the standard library) or how
> to easily make (e.g. in a C extension) a type without a __mro__, except
> for those (such as types.InstanceType) which are explicitly recorded in
> the dispatch table copy._deepcopy_dispatch...?

would this do what you need?

class C(type):
  def __getattribute__(self, attr):
if attr == '__mro__':
  raise AttributeError, "What, *me*, a __mro__? Nevah!"
return super(C, self).__getattribute__(attr)

class D(object):
  __metaclass__ = C

instances of D have a type that behaves as if it didn't have a
__mro__. This isn't exactly what you asked for, but it might be
enough.

-- 
John Lenton ([EMAIL PROTECTED]) -- Random fortune:
El tiempo cura los dolores y las querellas porque cambiamos. Ya no somos la
misma persona.
-- Blaise Pascal. (1600-1662) Filósofo y escritor francés. 


signature.asc
Description: Digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: sos!

2005-02-05 Thread Dan Perl

"jordan2856977" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Dan Perl" <[EMAIL PROTECTED]> wrote in message 
> news:<[EMAIL PROTECTED]>...
>> <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> >
>> > Dan Perl wrote:
>> >> [...]
>> > Aren't you in the wrong newsgroup? :-)
>>
>> Aren't you funny?
> i think i have solve
> the problem thank you

Jordan, neither my posting nor the posting I followed up to were referring 
to you.  mensanator was trying to be funny about my last name and I followed 
up with a comeback. 


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


Re: sos!

2005-02-05 Thread jordan2856977
"Dan Perl" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> >
> > Dan Perl wrote:
> >> [...]
> > Aren't you in the wrong newsgroup? :-)
> 
> Aren't you funny?
i think i have solve
the problem thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sos!

2005-02-05 Thread jordan2856977
"Dan Perl" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> "jordan2856977" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> > hellow everybody! I'm from china. I'm a beginner of python. in china,
> > python is not a fashionable language, so it's difficult to find some
> > books about python. finally,I find a book named "python how to
> > program" wrote by H.M.Deitel . who can tell me where can I find some
> > interesting source code about python. otherwise, I will fell boring
> > about study!
> 
> The Python Cookbook is a good source of examples that other posters haven't 
> mentioned yet: http://aspn.activestate.com/ASPN/Cookbook/Python/
> 
> It is especially good because it contains simple, short recipes that 
> illustrate a specific use of python and these recipes are organized in 
> categories.
> 
> Dan

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


Re: [EVALUATION] - E01: The Java Failure - May Python Helps?

2005-02-05 Thread Ilias Lazaridis
Terry Reedy wrote:
"Ilias Lazaridis" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

If you ask too much that other people do your searching for you, answers 
will dry up.  
I don't ask people to search for me.
I ask people for their specific knowledge about specific python language 
constructs.

This is a simple cooperation.
I've spend very much time to extract this specifi evaluation template:
http://lazaridis.com/case/stack/index.html#evaluation
Python community can 'fill' it quickly with the relevant technology (if 
it exists).

The evaluation result can serve as a fundamental part for a _practical_ 
showcase how Python beats Java.

But here are a couple that you might not find on google 
anyway, at least not easily.
thank you.
I want to add metadata to everything within my design (functions, data,
classes, ...), if possible with a standard way.
You can annotate, by adding attributes to, modules, functions, classes, and 
class instances.  You can not directly do so with 'basic' types: numbers, 
sequences, and dicts -- and some others.  You can, however, either extend 
or wrap anything with your own classes to get something you can annotate 
(but don't ask me for the details).
=> {annotation via attributes on modules, functions, classes and objects}
=> {not available with basic types (numbers, sequences, dicts, ...) }
=> {unconfirmed: possibility to extend/wrap basic types with own classes}
I want to generate things (code, txt, html etc.) out of my object-model,
whilst using with a flexible generator, if possible a standard one.
One standard way to generate text from objects is to use custom classes, 
each with a custom __str__ method.
[...]
Then 'print html_doc_instance' can print the html doc 
corresponding to the object model.
I understand this procedure.
I would like to use a standard way, which uses the standard metadata 
[implicit/explicit defined].

Does such standard exist?
Like others, I recommend you spend a day with Python if you wish to learn 
more.
I am spending "a day" with it's community.
.
--
http://lazaridis.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl

"M.E.Farmer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Dan,
> I was wondering how you were coming with your project.
> I had wondered if i had missed something going the CherryPy route
> instead of CGI. Now I see that you have had a bit of a snag , sorry to
> hear that.
> I am glad you are at least learning new things, 'cause if you had used
> CherryPy2 you would have be done by now :P
> M.E.Farmer

I chose to go first through the cgi module and later CherryPy exactly 
because I thought I will learn more this way.  And I guess I am learning 
more. 


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


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread M.E.Farmer
Dan Perl wrote:
> I am piggybacking on Hakan's original posting because I am addressing
the
> same group of people (those with good knowledge in the standard web
> programming modules), on a related topic.  However, my question is
> independent of Hakan's.
>
> I have trouble getting a simple CGI script to work because it gets an
empty
> cgi.FieldStorage form.  I have looked into and tried to debug the
internals
> of the standard modules that are used but I cannot figure it out: how
is a
> multipart POST request parsed by CGIHTTPServer?  BTW, I am using
pyton 2.4.
>
> I found the parsing of the header in the rfc822 module used by
> BaseHTTPServer.BaseHTTPRequestHandler (through the mimetools module),
but
> that stops after the header (at the first empty line).  Where is the
parsing
> done for the POST data following the header?  I will appreciate any
pointers
> that will help me debug this problem.
>
> As a side note, I found other old reports of problems with cgi
handling POST
> requests, reports that don't seem to have had a resolution.  There is
even a
> bug reported just a few days ago (1112856) that is exactly about
multipart
> post requests.  If I understand the bug report correctly though, it
is only
> on the latest version in CVS and it states that what is in the 2.4
release
> works.  All this tells me that it could be a "fragile" part in the
standard
> library.  So it could be even a bug in the standard library, but for
now I
> am assuming that I'm doing something wrong.
>
> Thanks,
>
> Dan

Dan,
I was wondering how you were coming with your project.
I had wondered if i had missed something going the CherryPy route
instead of CGI. Now I see that you have had a bit of a snag , sorry to
hear that.
I am glad you are at least learning new things, 'cause if you had used
CherryPy2 you would have be done by now :P
M.E.Farmer

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


Re: An Ode To My Two Loves

2005-02-05 Thread Luis M. Gonzalez
Jamey,

Really, you should try to steer clear from your computer from time to
time...
Your mental health is more important than python or ruby, don't lose
it!

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


RE: bad generator performance

2005-02-05 Thread Robert Brewer
Johannes Ahl mann wrote:
> i am in the process of profiling an application and noticed how much
> time my depth first generator for walking a tree data structure took.
> 
> i wrote the same thing as a recursive function producing an array, and
> this non-generator version is nearly 10 times faster!
> 
> any ideas why this is, what i did wrong...
> for some reason the generator version appears as being called 
> much more
> often than the recursive one. no idea why that is, but the data is
> definitely identical!
> 
> here the output from the python profiler and the respective code:
> 
> ### snip ###
> 
> ncalls  tottime  percall  cumtime  percall filename:lineno(function)
> 94209/81910.5600.0000.5900.000  
> :71(depthFirstIterator2)
> 4095/10.0600.0000.0800.080  :62(depthFirstIterator1)
> 
> ### snip ###
>   
> def depthFirstIterator1(self, depth = 0):
> ret = [[self, True, depth]]
> 
> if self.isFolder():
>   for c in self.children:
> ret = ret + c.depthFirstIterator(depth = depth + 1)
> 
> return ret + [[self, False, depth]]
> 
> def depthFirstIterator2(self, depth = 0):
> yield [self, True, depth]
> 
> if self.isFolder():
>   for c in self.children:
> for y in c.depthFirstIterator(depth = depth + 1):
>   yield y
> 
> yield [self, False, depth]
> 
> ### snip ###
> 
> i'd appreciate any comments or explanations why the generator might be
> so much slower, as i had just decided to use generators more 
> frequently
> and in the respective PEP they are praised as generally fast.

Um. Under my definition of "recursion", you haven't really removed
recursion in the generator version. That is, you're still calling the
depthFirstIterator method upon each iteration--you've just changed from
list-concatenation to yielding instead, which trades off list-management
overhead for closure-maintenance overhead. A truly non-recursive
implementation would probably exist outside of whatever class you've got
this method in, and would keep its own pointer to the current node as it
traversed the tree.

Hope that helps,


Robert Brewer
MIS
Amor Ministries
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: [EVALUATION] - E01: The Java Failure - May Python Helps?

2005-02-05 Thread Courageous

>Well, we don't have to ban them because we have the PSU eliminate them 
>alltogether. So much more efficient. Or do you think it's a coincidence 
>we've never seen or heard Timothy "autocoding" Rue again?

Foolish, man! You said his name! It's almost like you said C'thul'hu, ...
you ... ah no, no, NO! N'YH

*insert ripping and splashing sounds*

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


Re: CGI POST problem was: How to read POSTed data

2005-02-05 Thread and-google
Dan Perl wrote:

> how is a multipart POST request parsed by CGIHTTPServer?

It isn't; the input stream containing the multipart/form-data content
is passed to the CGI script, which can choose to parse it or not using
any code it has to hand - which could be the 'cgi' module, but not
necessarily.

> Where is the parsing done for the POST data following the header?

If you are using the 'cgi' module, then cgi.parse_multipart.

> As a side note, I found other old reports of problems with cgi
> handling POST  requests, reports that don't seem to have had a
> resolution.

(in particular?)

FWIW, for interface-style and multipart-POST-file-upload-speed reasons
I wrote an alternative to cgi, form.py
(http://www.doxdesk.com/software/py/form.html). But I haven't found
cgi's POST reading to be buggy in general.

> There is even a bug reported just a few days ago (1112856) that is
> exactly about multipart post requests. If I understand the bug
> report correctly though, it is only on the latest version in CVS
> and it states that what is in the 2.4 release works.

That's correct.

> All this tells me that it could be a "fragile" part in the standard
> library.

I don't really think so; it's really an old stable part of the library
that is a bit crufty in places due to age. The patch that caused
1112856 was an attempt to rip out and replace the parser stuff, which
as a big change to old code is bound to cause trouble. But that's what
the dev cycle is for.

CGIHTTPServer, on the other hand, I have never really trusted. I would
suspect that fella.

-- 
Andrew Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/

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


ANN: SPE 0.7.2.A IDE with uml, wxGlade and blender support

2005-02-05 Thread http://www.stani.be
Spe is a python IDE with auto-indentation, auto completion, call tips,
syntax coloring, uml viewer, syntax highlighting, class explorer,
source index, auto todo list, sticky notes, integrated pycrust shell,
python file browser, recent file browser, drag&drop, context help, ...
Special is its blender support with a blender 3d object browser and its
ability to run interactively inside blender. Spe ships with wxGlade
(gui designer), PyChecker (source code doctor) and Kiki (regular
expression console). Spe is extensible with wxGlade.

The sidebar now features a file browser. I'm now trying to form a team
for future development of SPE. Besides me four people will join the
project:

- Sam Widmer for CVS and bugfixes. So soon more collaboration on
SPE
  will be possible.
- Nir Aides (author of rpdb) for helping implementing the remote
debugger
- Kevin Walzer for the OS X Port of SPE. Please contact him for Mac
specific
  issues. Spe for the Mac will be distributed through:
  http://www.wordtech-software.com/spe.html
- Jelle Feringa for documentation. The goal is to provide a pdf
manual with
  the SPE distribution based on the SPE wiki:
http://www.stani.be/spe/wiki
  Anyone who has comments or wants to edit the wiki as well can
contact Jelle.

If you like SPE, please contribute by coding, writing documentation or
donating. I would like to thank especially Michael Balk, who gave the
largest
donation ever to SPE.

Also I would like to thank Michael Foord, who made SPE part of a new
Python
distribution, called "movable python". It gives you the possibility to
carry your
favorite developping environment on a USB stick. So you can continue
your work on
any computer or test your modules for different python versions. This
distribution
opens a lot of new possibilities, check it out!! For more info, visit
http://www.voidspace.org.uk/python/programs.shtml#movpy

:Batteries included:
  - Kiki:
  Regular Expression (regex) console. For more info:
  http://project5.freezope.org/kiki/index.html
  - PyChecker:
  PyChecker is a tool for finding bugs in python source code. It
  finds problems that are typically caught by a compiler for
  less dynamic languages, like C and C++. It is similar to lint.
  For more info: http://pychecker.sourceforge.net
  - wxGlade:
  wxGlade is a GUI designer written in Python with the
  popular GUI toolkit wxPython, that helps you create
  wxWindows/wxPython user interfaces. As you can guess by the
  name, its model is Glade, the famous GTK+/GNOME GUI builder,
  with which wxGlade shares the philosophy and the look & feel
  (but not a line of code). For more info:
  http://wxglade.sourceforge.net

:New features:
- sidebar browser (iniated by Attila Magyar)

:Bug fixes:
- segfaults in Browser
- indentation can now be different from 4
- mac osx fixes for kiki, wxGlade & XRC
- scrollbars of UML view
- initial sizing and positioning are now restored

:Donations(177.20euro):
- Michael Balk
- Jason Powell
- David Ko Feng
- Winchell Chung
- Matthias Haberkorn
- Kristjan Kannike
- Robert Cowham
- Andre Roberge
- Chris S

:Contributors:
  - Sam Widmer
  - Attila Magyar
  - Kevin Walzer
  - Thurston Stone

:Requirements:
  - full python 2.3+
  - wxpython 2.5.3.8+
  - optional blender 2.35+

:Links:
- Homepage: http://spe.pycs.net
- Website:  http://projects.blender.org/projects/spe/
- Screenshots:  http://spe.pycs.net/pictures/index.html
- Forum:http://projects.blender.org/forum/?group_id=30
- RSS feed: http://spe.pycs.net/weblog/rss.xml

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


Re: Insane import behaviour and regrtest.py: heeelp

2005-02-05 Thread Nick Coghlan
John J. Lee wrote:
The only change I made to regrtest other than the print statements was
to add Lib to sys.path, so I pick up modules from CVS instead of the
installed 2.4 versions (yeah, I know I should build and install a
python2.5 executable from CVS, but I don't see how that's relevant here).
I didn't read your whole message so I may be off track here, but regrtest.py has 
some arguably demented path handling behaviour, and it's driven mainly by the 
physical location of regrtest.py. In particular:

def findtestdir():
if __name__ == '__main__':
file = sys.argv[0]
else:
file = __file__
testdir = os.path.dirname(file) or os.curdir
return testdir
So intead of adding anything to sys.path, tweak the call to 'main' on the final 
line to set "testdir" appropriately.

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: bad generator performance

2005-02-05 Thread Johannes Ahl mann
sorry, forgot to post the profiling info for the recursive helper
function.
but generator is still FAR slower...

4095/10.0500.0000.1200.120 file.py:135(rek)

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


Re: empty classes as c structs?

2005-02-05 Thread Nick Coghlan
Steven Bethard wrote:
Nick Coghlan wrote:
I think the idea definitely deserves mention as a possible 
implementation strategy in the generic objects PEP, with the data 
argument made optional:

That's basically what the current implementation does (although I use 
'update' instead of '=').  The code is complicated because the 
implementation also takes all the argument types that dicts take.
The main difference I noticed is that by using update, any changes made via the 
attribute view are not reflected in the original dict.

By assigning to __dict__ directly, you can use the attribute view either as it's 
own dictionary (by not supplying one, or supplying a new one), or as a 
convenient way to programmatically modify an existing one. For example, you 
could use it to easily bind globals without needing the 'global' keyword:

Py> class attr_view(object):
... def __init__(self, data):
... self.__dict__ = data
...
Py> def f():
...   gbls = attr_view(globals())
...   gbls.x = 5
...
Py> x
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'x' is not defined
Py> f()
Py> x
5
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: "pickle" vs. f.write()

2005-02-05 Thread Martin Miller
Marc 'BlackJack' Rintsch wrote:
> ...
>
> I write __repr__() methods similar but I think a bit more readable:
>
>   def __repr__(self):
>   return "%s(%r, %r, %r, %r)" % (self.__class__.__name__,
self.name,
>  self.age, self.friends,
self.comment)
>
> And it's robust against changing the class name.  It even works in
> subclasses if the signature of the __init__() method stays the same.

Yes, that's an excellent suggestion and improvement.

Saw the following in a post by Steven Bethard on another thread

that I think would be even better (in the sense of being more
general/generic) which also ought to work in subclasses. Namely:

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__,
   ', '.join('%s=%r' % (k, v)
 for k, v
 in self.__dict__.items()))

Martin

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


bad generator performance

2005-02-05 Thread Johannes Ahl mann
hi,

i am in the process of profiling an application and noticed how much
time my depth first generator for walking a tree data structure took.

i wrote the same thing as a recursive function producing an array, and
this non-generator version is nearly 10 times faster!

any ideas why this is, what i did wrong...
for some reason the generator version appears as being called much more
often than the recursive one. no idea why that is, but the data is
definitely identical!

here the output from the python profiler and the respective code:

### snip ###

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
94209/81910.5600.0000.5900.000  :71(depthFirstIterator2)
4095/10.0600.0000.0800.080  :62(depthFirstIterator1)

### snip ###

def depthFirstIterator1(self, depth = 0):
ret = [[self, True, depth]]

if self.isFolder():
  for c in self.children:
ret = ret + c.depthFirstIterator(depth = depth + 1)

return ret + [[self, False, depth]]

def depthFirstIterator2(self, depth = 0):
yield [self, True, depth]

if self.isFolder():
  for c in self.children:
for y in c.depthFirstIterator(depth = depth + 1):
  yield y

yield [self, False, depth]

### snip ###

i'd appreciate any comments or explanations why the generator might be
so much slower, as i had just decided to use generators more frequently
and in the respective PEP they are praised as generally fast.

thx,

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


Re: variable declaration

2005-02-05 Thread Nick Coghlan
Alex Martelli wrote:
It's not clear to me what semantics, exactly, x.y := z would be defined
to have (assuming := is the syntax sugar for ``rebinding'').  Perhaps,
by analogy with every other augmented operator, it should be equivalent
to:
_temp = x.y
x.y = type(temp).__irebind__(temp, z)
This makes it crystal-clear what happens when type(x).y is a descriptor
with/without __set__ and __get__, when type(x) defines __getattribute__
or __setattr__, etc, etc.  Giving type object an __irebind__ which just
returns the second operand would complete the semantics.  Of course,
doing it this way would open the issue of types overriding __irebind__,
and it's not clear to me that this is intended, or desirable.  So, maybe
some other semantics should be the definition.  But since "rebinding" is
not a primitive, the semantics do need to be somehow defined in terms of
elementary operations of getting and setting (of attributes, items, &c).
I was thinking of something simpler:
  x.y
  x.y = z
That is, before the assignment attempt, x.y has to resolve to *something*, but 
the interpreter isn't particularly fussy about what that something is.

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Swig-friendly distutils.command.build_ext

2005-02-05 Thread George Sakkis
I'm using a custom extension of the build_ext distutils 'command' that 
integrates better with SWIG.
Specifically, it:

1. Adds '-I' (include) swig option for each directory containing a source file 
in the given
Extension.
2. Determines whether to add the '-c++" option by checking the source file 
extensions (even if the
'swig_cpp' and 'swig_opts' do not explicitly specify that it is a c++ wrapper).
3. Builds the swig-generated high-level python module (currently only the low 
level .dll/.so is
built).

If these changes are generally useful, I think they (c|sh)ould make it into the 
next release of
distutils.

George


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


Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote:
Exec is slow since compiling the string and calls to globals() use a 
lot of time.  The last one is most elegant but __getattr__ and 
__setattr__ are costly. The 'evil hack' solution is good since 
accessing x and y takes no additional time.

Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:
 >>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
 >>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
You are still including the compile overhead in fun2. If you want to see how fast the compiled code 
is you should take the definition of myfun out of fun2:

myfun = makeFunction('z = x + y', 'myfun')
def fun2(d):
  for i in xrange(0,N):
myfun(d)
  del d['__builtins__']
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: sos!

2005-02-05 Thread jordan2856977
Bruno Desthuilliers <[EMAIL PROTECTED]> wrote in message news:<[EMAIL 
PROTECTED]>...
> jordan2856977 a écrit :
> > hellow everybody! I'm from china. I'm a beginner of python. in china,
> > python is not a fashionable language, so it's difficult to find some
> > books about python. finally,I find a book named "python how to
> > program" wrote by H.M.Deitel . who can tell me where can I find some
> > interesting source code about python. otherwise, I will fell boring
> > about study!
> 
> Well, if you want to study some interesting source code, you have the 
> whole Python standard lib, and *many* open source programs (Zope, 
> Twisted, etc...)
> 
> You can also find some good free books online, like Dive Into Python, or 
>   Text Processing With Python. (google should find relevant URLs).
> 
> HTH, and welcome.

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


Re: Pickling and inheritance are making me hurt

2005-02-05 Thread Tim Peters
[Kirk Strauser]
> I have a module that defines a Search class and a SearchResult class.

Try posting a minimal self-contained code sample that fails.

> I use these classes by writing other modules that subclass both of them as
> needed to interface with particular search engines.
> 
> My problem is that Search defines a method (called automatically by __del__)
> to save its results between invocations:
>
>def _saveresults(self):
>self._oldresults = self._results
>file = open(self._storefile(), 'w')
>pickle.dump(self._oldresults, file)
>file.close()
>
> The problem I'm having is the the pickle.dump call is failing whenever the
> objects in "self.data" are instances of derivatives of SearchResult rather
> than instances of SearchResult itself (which is pretty much always the
> case):
>
>Exception pickle.PicklingError:  0xb7f7ad6c> in  0xb7ec954c>> ignored
> 
> Now, if I overload _saveresults inside a subclass of Search, then it works.
> It seems like the problem is that _saveresults is only looking inside the
> same namespace as Search (where it's originally defined), even if it's
> actually been inherited by another class in a different module.  Is there a
> way around this?

Not enough relevant information to say.  __del__ looks irrelevant
here, apart from that you happen to be pickling while __del__ is
executing.  Try pickling an instance of a subclass of Search directly
to see what happens.  There are many reasons for why PicklingError may
get raised.

It's certainly true that any function in Python (method or not)
executes with the globals of the module in which it's defined.  While
you mentioned that as a possible issue, it seemed to come out of the
blue (didn't seem to follow from anything said before it).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IPython colors in windows

2005-02-05 Thread Gary Bishop
On SourceForge you will find release 1.12 of my Python readline
module. If you don't want to hack the colors, there is no reason to
upgrade from 1.11 to 1.12. They *should* work the same.

But if you'd like to hack the iPython colors this new version makes it
possible.  In your ipythonrc file add a line like:

execfile hackcolors.py

Now in hackcolors.py you can change colors and backgrounds like this:

hackcolors.py
import readline

# reach deep into the bowels of readline to get the color table
escape_to_color = readline.rl.console.escape_to_color

# change a color
escape_to_color['0;32'] = 0x72

del escape_to_color
del readline

#

The two hex digits are the background and foreground color
respectively. In the example above I'm setting the color to green on a
grey background. Here is the table that is normally used to translate
colors.

  escape_to_color = { '0;30': 0x0, #black
  '0;31': 0x4, #red
  '0;32': 0x2, #green
  '0;33': 0x4+0x2, #brown?
  '0;34': 0x1, #blue
  '0;35': 0x1+0x4, #purple
  '0;36': 0x2+0x4, #cyan
  '0;37': 0x1+0x2+0x4, #grey
  '1;30': 0x1+0x2+0x4, #dark gray
  '1;31': 0x4+0x8, #red
  '1;32': 0x2+0x8, #light green
  '1;33': 0x4+0x2+0x8, #yellow
  '1;34': 0x1+0x8, #light blue
  '1;35': 0x1+0x4+0x8, #light purple
  '1;36': 0x1+0x2+0x8, #light cyan
  '1;37': 0x1+0x2+0x4+0x8, #white
  '0': None,
  }

An interested party should be able to arbitrarily map colors and their
backgrounds.

Enjoy,
gb

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


Re: Definitive documentation on newstyle classes? (WAS: Pickling and inheritance are making me hurt)

2005-02-05 Thread Daniel Bickett
Bruno Desthuilliers wrote:
> Well, the fact is that __[get|set]state__() have nothing to do with new
> style classes, but with the Pickle protocol:
> http://www.python.org/doc/2.3.4/lib/pickle-inst.html

Thank you for pointing that out, but all the same ;)

-- 
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Definitive documentation on newstyle classes? (WAS: Pickling and inheritance are making me hurt)

2005-02-05 Thread Brian van den Broek
Daniel Bickett said unto the world upon 2005-02-05 19:46:
I was reading the "Pickling and inheritance are making me hurt"
thread, and the latest suggestion (as of this posting) was to do with
the __setstate__ and __getstate__ methods. They caught my attention
because I hadn't encountered them before, and it reminded me that in
the past I've never been able to very good, definitive documentation
on newstyle classes. Googling for it gives little python tutorials on
various sites, and even searching this newsgroup returns very specific
questions, as a rule.
Alas, the question: Does there exist a page that enumerates all of the
features of the newstyle classes, and explains what they all do? If
so, can anyone provide me with such a link?
Thanks :-)
Hi Daniel,
it doesn't yet answer the "definitive" part, but
 is worth a look.
Best,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: Definitive documentation on newstyle classes? (WAS: Pickling and inheritance are making me hurt)

2005-02-05 Thread Bruno Desthuilliers
Daniel Bickett a écrit :
I was reading the "Pickling and inheritance are making me hurt"
thread, and the latest suggestion (as of this posting) was to do with
the __setstate__ and __getstate__ methods. They caught my attention
because I hadn't encountered them before, and it reminded me that in
the past I've never been able to very good, definitive documentation
on newstyle classes. 
Well, the fact is that __[get|set]state__() have nothing to do with new 
style classes, but with the Pickle protocol:
http://www.python.org/doc/2.3.4/lib/pickle-inst.html

(snip)
Bruno
--
http://mail.python.org/mailman/listinfo/python-list


Definitive documentation on newstyle classes? (WAS: Pickling and inheritance are making me hurt)

2005-02-05 Thread Daniel Bickett
I was reading the "Pickling and inheritance are making me hurt"
thread, and the latest suggestion (as of this posting) was to do with
the __setstate__ and __getstate__ methods. They caught my attention
because I hadn't encountered them before, and it reminded me that in
the past I've never been able to very good, definitive documentation
on newstyle classes. Googling for it gives little python tutorials on
various sites, and even searching this newsgroup returns very specific
questions, as a rule.

Alas, the question: Does there exist a page that enumerates all of the
features of the newstyle classes, and explains what they all do? If
so, can anyone provide me with such a link?

Thanks :-)
-- 
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling and inheritance are making me hurt

2005-02-05 Thread John J. Lee
Kirk Strauser <[EMAIL PROTECTED]> writes:

> I have a module that defines a Search class and a SearchResult class.  I use
> these classes by writing other modules that subclass both of them as needed
> to interface with particular search engines.
> 
> My problem is that Search defines a method (called automatically by __del__)
  ^^^

Don't Do That.  __del__ methods are bad.  They mess up cyclic garbage
collection, IIRC.  ISTR other problems with them, too...


[...]
> Exception pickle.PicklingError:  0xb7f7ad6c> in  0xb7ec954c>> ignored
[...]

...yeah, there's one: thanks for reminding me ;-)

You may want to read up on __getstate__ and __setstate__, BTW.


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


Re: IPython colors in windows

2005-02-05 Thread Gary Bishop
Claudio Grondi <[EMAIL PROTECTED]> wrote:
> "Ashot" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
> news:[EMAIL PROTECTED]
> > whoa, that was quick, looks like it works for me.  Thanks a lot!
> > It would be nice to be able to set the colors in the prefs file, although
> > its possible to edit the pyColorize file as Claudio mentioned.
> To get the coloured texts on a grey (instead of white) background
> I have extended the Console.py to accept sequences as e.g.
>   "0;33;47" (see attachment)
> have extended also the
>   def make_color_table(in_class):
> in the ColorANSI.py of IPython
> and adjusted the pyColorize.py file, but it had not the desired effect.
> IPython seems not to be sending to Console.py the sequences
> defined in the

The problem *may* be the way I insert the background color in
Console.py. I simply hacked it to OR the initial value into the colors
I get out of my table. If you want to have some colors with the
background set, then we'd need to work out a way to signal
that. Perhaps instead of or'ing in the value all the time, we should
insert the background value into the table entries that don't specify
the background.

I'm happy to see someone hacking on it. The source is all there and it
is all Python. The Win32 API stuff is ugly but well documented online.

If you change the "if 0:" up at the top of Console.py to "if 1:"
you'll get a file debug.txt with the contents of the various calls to
"log". That way you can "print" stuff from within console without
messing up the window.

gb

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


Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Steven Bethard
Alex Martelli wrote:
Steven Bethard <[EMAIL PROTECTED]> wrote:

Here's a solution that works for iterables other than lists:
py> def collapse(iterable):
... enumeration = enumerate(iterable)
... _, lastitem = enumeration.next()
... yield lastitem
... for i, item in enumeration:
... if item != lastitem:
... yield item
... lastitem = item
...
py> lst = [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]
py> list(collapse(lst))
[0, 1, 2, 3, 2, 4, 5]
Again, I'm still not sure I'd call this more elegant...

H, what role does the enumeration play here?
None =) Sorry, it was an accidental carry-over from the list solution 
that looked at lst[i-1].  I thought about correcting it when I realized 
this (after posting), but I was too lazy. ;)

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


Re: Computing class variable on demand?

2005-02-05 Thread Steven Bethard
fortepianissimo wrote:
Thanks Steve - actually my question was simpler than that. I just
wanted to use Daniels' recipe of lazy initialization on objects with
__slots__:
class Lazy(object):
def __init__(self, calculate_function):
self._calculate = calculate_function
def __get__(self, obj, _=None):
if obj is None:
return self
value = self._calculate(obj)
setattr(obj, self._calculate.func_name, value)
return value
class SomeClass(object):
__slots__ = 'someprop'
@Lazy
def someprop(self):
print 'Actually calculating value'
return 13
o = SomeClass()
print o.someprop
print o.someprop

Running the code above will produce:
Actually calculating value
Traceback (most recent call last):
  File "Lazy.py", line 26, in ?
print o.someprop
  File "Lazy.py", line 11, in __get__
setattr(obj, self._calculate.func_name, value)
AttributeError: 'SomeClass' object attribute 'someprop' is read-only
Removing the __slots__ statement, everything would run normally.
Is there any workaround?

Hmm...  Well, there's a thread on a similar topic:
http://mail.python.org/pipermail/python-dev/2003-May/035575.html
The summary is basically that '__slots__' creates descriptors for all 
the names in the __slots__ iterable.  If you define a function (or a 
Lazy object) at the class level with the same name as a slot, it 
replaces the descriptor for that slot name.  So then when setattr gets 
called, it can't find the descriptor anymore, so it can't write the value...

One workaround would be to have Lazy store the values instead of setting 
the attribute on the instance -- something like:

py> class Lazy(object):
... def __init__(self, calculate_function):
... self._calculate = calculate_function
... self._values = {}
... def __get__(self, obj, _=None):
... if obj is None:
... return self
... obj_id = id(obj)
... if obj_id not in self._values:
... self._values[obj_id] = self._calculate(obj)
... return self._values[obj_id]
...
py>
py> class SomeClass(object):
... __slots__ = []
... @Lazy
... def someprop(self):
... print 'Actually calculating value'
... return 13
...
py> o = SomeClass()
py> o.someprop
Actually calculating value
13
py> o.someprop
13
Basically Lazy just stores a mapping from object ids to calculated 
values.  Not as pretty as the original solution, but if you have to have 
__slots__ defined[1], I guess this might work.

Note that I don't need to declare any slots since the someprop 
descriptor is a class-level attribute, not an instance-level one...

Steve
[1] Are you sure you have to use __slots__?  Unless you're creating a 
very large number of these objects, you probably won't notice the 
difference in memory consumption...
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Arvid Andersson
On Sat, Feb 05, 2005 at 02:11:07PM -0800, Michael Hartl wrote:
> Use the eval function:
> 
> >>> eval("30/(6+9)")
> 2
> 

Thanks, just what I was looking for!

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


Re: How do I enter/receive webpage information?

2005-02-05 Thread John J. Lee
Jorgen Grahn <[EMAIL PROTECTED]> writes:
[...]
> - subclassed sgmllib.SGMLParser once for each kind of page I expected to
>   receive. This class knew how to pull the information from a HTML document,
>   provided it looked as I expected it to.  Very tedious work. It can be easier
>   and safer to just use module re in some cases.
[...]

BeautifulSoup is often recommended (never tried it myself).

Remember HTMLtidy and its offshoots (eg. tidylib, mxTidy) are
available for cleaning horrid HTML while-u-scrape, too.

Alternatively, some people swear by automating Internet Explorer;
other people would rather be hit on the head with a ball-peen hammer
(not only the MS-haters)...


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


Re: How do I enter/receive webpage information?

2005-02-05 Thread John J. Lee
Jorgen Grahn <[EMAIL PROTECTED]> writes:
[...]
> I did it this way successfully once ... it's probably the wrong approach in 
> some ways, but It Works For Me.
> 
> - used httplib.HTTPConnection for the HTTP parts, building my own requests
>   with headers and all, calling h.send() and h.getresponse() etc.
> 
> - created my own cookie container class (because there was a session
>   involved, and logging in and such things, and all of it used cookies)
> 
> - subclassed sgmllib.SGMLParser once for each kind of page I expected to
>   receive. This class knew how to pull the information from a HTML document,
>   provided it looked as I expected it to.  Very tedious work. It can be easier
>   and safer to just use module re in some cases.
> 
> Wrapped in classes this ended up as (fictive):
> 
> client = Client('somehost:80)
> client.login('me', 'secret)
> a, b = theAsAndBs(client, 'tomorrow', 'Wiltshire')
> foo = theFoo(client, 'yesterday')
> 
> I had to look deeply into the HTTP RFCs to do this, and also snoop the
> traffic for a "real" session to see what went on between server and client.

I see little benefit and significant loss in using httplib instead of
urllib2, unless and until you get a particulary stubborn problem and
want to drop down a level to debug.  It's easy to see and modify
urllib2's headers if you need to get low level.

One starting point for web scraping with Python:

http://wwwsearch.sourceforge.net/bits/GeneralFAQ.html

There are some modules you may find useful there, too.

Google Groups for urlencode.  Or use my module ClientForm, if you
prefer.  Experiment a little with an HTML form in a local file and
(eg.) the 'ethereal' sniffer to see what happens when you click
submit.

The stdlib now has cookie support (in Python 2.4):

import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

r = opener.open("http://example.com/";)
print r.read()

Unfortunately, it's true that network sniffing and a reasonable
smattering of knowledge about HTTP &c., does often turn out to be
necessary to scrape stuff.  A few useful tips:

http://wwwsearch.sourceforge.net/ClientCookie/doc.html#debugging


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


Re: IPython colors in windows

2005-02-05 Thread Claudio Grondi
"Ashot" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> whoa, that was quick, looks like it works for me.  Thanks a lot!
> It would be nice to be able to set the colors in the prefs file, although
> its possible to edit the pyColorize file as Claudio mentioned.
To get the coloured texts on a grey (instead of white) background
I have extended the Console.py to accept sequences as e.g.
  "0;33;47" (see attachment)
have extended also the
  def make_color_table(in_class):
in the ColorANSI.py of IPython
and adjusted the pyColorize.py file, but it had not the desired effect.
IPython seems not to be sending to Console.py the sequences
defined in the
 LightBGColors = ColorScheme( ... )
section of pyColorize.py (or there is a problem with the
extension I have done to Console.py)
I have just forced Console.py to treat "0;33" as "0;33;47", to
achieve the result I want (see attachment).
It seems, that the  problem with Console.py of the readline module,
is not the only one which existed - there is maybe also a problem
with IPython color schemes handling, so I gave up to try to achieve
a clean nice solution supporting also selection of background colors
for any colorized text output in IPython.
It works for me as it is now, so probably it is better to wait for the
next release of IPython with a cleaner implementation of color
schemes before further efforts towards support for choosing
of background colors for each colorized text output in IPython
via extension of Console.py of the readline module.

Claudio

attachment:
  # Mapping of ANSI color escape sequences into  wAttributes  (Windows
Console
  # Attributes) according to constants #define(d) in WinCon.h for usage
with:
  #   WINBASEAPI BOOL WINAPI SetConsoleTextAttribute(
  # IN HANDLE hConsoleOutput,
  # IN WORD wAttributes );
  escape_to_color = {
 '0'   : None# reset colors
to default setting
  # text color (color of characters):
# #Foreground   Background
# #   I , R , G , BI , R , G , B   #
Intensified, Red, Green, Blue
# ,'0;30':  (0x0)  # (dark)black
(#00)
# ,'0;31':  (0x4)  # (dark)red
(#80)
# ,'0;32':  (0x2)  # (dark)green
(#008000)
# ,'0;33':  (0x4+0x2)  #
(dark)yellow (#808000)
# ,'0;34':  (0x1)  # (dark)blue
(#80)
# ,'0;35':  (0x4+0x1)  #
(dark)purple (#800080)
# ,'0;36':  (0x4+0x2)  # (dark)cyan
(#808000)
# ,'0;37':  (0x4+0x2+0x1)  # (dark)white
(#C0C0C0)
# #Foreground   Background
# #   I , R , G , BI , R , G , B   #
Intensified, Red, Green, Blue
# ,'1;30':  (0x8)  # light black
(#808080)
# ,'1;31':  (0x8+0x4)  # light red
(#FF)
# ,'1;32':  (0x8+0x2)  # light green
(#00FF00)
# ,'1;33':  (0x8+0x4+0x2)  # light
yellow (#00)
# ,'1;34':  (0x8+0x1)  # light blue
(#FF)
# ,'1;35':  (0x8+0x4+0x1)  # light
purple (#FF00FF)
# ,'1;36':  (0x8+0x4+0x2)  # light cyan
(#00)
# ,'1;37':  (0x8+0x4+0x2+0x1)  # light white
(#FF)
  # text background color (color of rectangles in which text characters are
displayed):
#Foreground   Background
#   I , R , G , BI , R , G , B   # Intensified,
Red, Green, Blue
,'0;40':  ((0x0)<<4) # dark  black
(#00)
,'0;41':  ((0x4)<<4) # dark  red
(#80)
,'0;42':  ((0x2)<<4) # dark  green
(#008000)
,'0;43':  ((0x4+0x2)<<4) # dark  yellow
(#808000)
,'0;44':  ((0x1)<<4) # dark  blue
(#80)
,'0;45':  ((0x4+0x1)<<4) # dark  purple
(#800080)
,'0;46':  ((0x4+0x2)<<4) # dark  cyan
(#808000)
,'0;47':  ((0x4+0x2+0x1)<<4) # dark  white
(#C0C0C0)
#Foreground   Background
#   I , R , G , BI , R , G , B   # Intensified,
Red, Green, Blue
,'1;40':  ((0x8)<<4) # light black
(#808080)
,'1;41':  ((0x8+0x4)<<4) # light red
(#FF)
,'1;42':  ((0x8+0x2)<<4) # light green
(#00FF00)
,'1;43': 

Re: sos!

2005-02-05 Thread Dan Perl

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> Dan Perl wrote:
>> [...]
> Aren't you in the wrong newsgroup? :-)

Aren't you funny? 


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


Re: [EVALUATION] - E01: The Java Failure - May Python Helps?

2005-02-05 Thread Terry Reedy

"Ilias Lazaridis" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

If you ask too much that other people do your searching for you, answers 
will dry up.  But here are a couple that you might not find on google 
anyway, at least not easily.

> I want to add metadata to everything within my design (functions, data,
> classes, ...), if possible with a standard way.

You can annotate, by adding attributes to, modules, functions, classes, and 
class instances.  You can not directly do so with 'basic' types: numbers, 
sequences, and dicts -- and some others.  You can, however, either extend 
or wrap anything with your own classes to get something you can annotate 
(but don't ask me for the details).

> I want to generate things (code, txt, html etc.) out of my object-model,
>  whilst using with a flexible generator, if possible a standard one.

One standard way to generate text from objects is to use custom classes, 
each with a custom __str__ method.  Assuming you have a hierachical model 
without loops, each such method can recursively call each data attribute 
object of the instancefor it to generate its substring.  (With loops you 
need a loop-cutting mechanism to prevent infinite recursion.)  I know this 
has been done for html (but again, I won't google for you).  Define a class 
for each type of element and give each instance a list of element instances 
it contains.  Then 'print html_doc_instance' can print the html doc 
corresponding to the object model.

Like others, I recommend you spend a day with Python if you wish to learn 
more.

Terry J. Reedy




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


Insane import behaviour and regrtest.py: heeelp

2005-02-05 Thread John J. Lee
I'm tearing my hair out at what seems like weird import behaviour I'm
getting from Python's stdlib test script, regrtest.py (not for the
first time: seem to have forgotten the resolution from last time, and
the time before, and the time before that, when this damn test script
of Python's had me screaming at the screen... I don't seem to be
learning my lesson here :( )

*Please* somebody inform me *what* affects what file Python will pick
up, other than:

0. Python executable

1. sys.path

2. current working directory

3. Directory in which script lives
 
4. Directory from which script was invoked

5. The contents of one's hard drive

Even with all of these constant (and not all of them should directly
affect the outcome in any case, I know), I get different modules
picked up from imports in two scripts, and I'm damned if I can see
why.

Here's the first script, just a test script:

import os, sys
sys.path = ['/hda/usr/local/buf/python/python/dist/src/Lib', 
'/home/john/lib/python', '/usr/local/lib/python24.zip', 
'/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', 
'/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', 
'/usr/local/lib/python2.4/site-packages']
abstest = "test.test_urllib2"
print 'abstest', abstest
print 'sys.path', sys.path
print 'cwd', os.getcwd()
the_package = __import__(abstest, globals(), locals(), [])
print 'the_package.__path__', the_package.__path__

If I stick that script in my Python CVS Lib/test directory as blah.py,
then cd to Lib and:

$ python2.4 test/blah.py
abstest test.test_urllib2
sys.path ['/hda/usr/local/buf/python/python/dist/src/Lib', 
'/home/john/lib/python', '/usr/local/lib/python24.zip', 
'/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', 
'/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', 
'/usr/local/lib/python2.4/site-packages']
cwd /hda/usr/local/buf/python/python/dist/src/Lib
*
the_package.__path__ ['/hda/usr/local/buf/python/python/dist/src/Lib/test']


All as expected: I pick up Lib/test/test_urllib2.py -- that ***
business is coming from a print >> sys.stderr statement I inserted at
module level in that copy of the module (though the >> sys.stderr is
probably unnecessary, since IIRC regrtest does not rebind sys.stdout
when the -v option is given, as in my usage below).


But, when I run the second script, Lib/test/regrtest.py, with a
modified sys.path (see paragraph below) and the same print statements
as in my test script inserted:

$ python2.4 test/regrtest.py -f test/torun -v
test_urllib2
abstest test.test_urllib2
sys.path ['/hda/usr/local/buf/python/python/dist/src/Lib', 
'/home/john/lib/python', '/usr/local/lib/python24.zip', 
'/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', 
'/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', 
'/usr/local/lib/python2.4/site-packages']
cwd /hda/usr/local/buf/python/python/dist/src/Lib
the_package.__path__ ['/usr/local/lib/python2.4/test']

I get the test_urllib2.py from /usr/local/lib/python2.4 (note __path__
and lack of *s), though everything else (sys.path, cwd, etc.)
remains the same as my test script above!

The only change I made to regrtest other than the print statements was
to add Lib to sys.path, so I pick up modules from CVS instead of the
installed 2.4 versions (yeah, I know I should build and install a
python2.5 executable from CVS, but I don't see how that's relevant here).

Why???  Help!

stupid-stupid-stupid-gh--ly y'rs,


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


Re: sos!

2005-02-05 Thread [EMAIL PROTECTED]

Dan Perl wrote:
> "jordan2856977" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > hellow everybody! I'm from china. I'm a beginner of python. in
china,
> > python is not a fashionable language, so it's difficult to find
some
> > books about python. finally,I find a book named "python how to
> > program" wrote by H.M.Deitel . who can tell me where can I find
some
> > interesting source code about python. otherwise, I will fell boring
> > about study!
>
> The Python Cookbook is a good source of examples that other posters
haven't
> mentioned yet: http://aspn.activestate.com/ASPN/Cookbook/Python/
>
> It is especially good because it contains simple, short recipes that
> illustrate a specific use of python and these recipes are organized
in 
> categories.
> 
> Dan

Aren't you in the wrong newsgroup? :-)

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


Re: Computing class variable on demand?

2005-02-05 Thread fortepianissimo
Thanks Steve - actually my question was simpler than that. I just
wanted to use Daniels' recipe of lazy initialization on objects with
__slots__:

class Lazy(object):
def __init__(self, calculate_function):
self._calculate = calculate_function

def __get__(self, obj, _=None):
if obj is None:
return self
value = self._calculate(obj)
setattr(obj, self._calculate.func_name, value)
return value


class SomeClass(object):

__slots__ = 'someprop'

@Lazy
def someprop(self):
print 'Actually calculating value'
return 13


o = SomeClass()
print o.someprop
print o.someprop



Running the code above will produce:

Actually calculating value
Traceback (most recent call last):
  File "Lazy.py", line 26, in ?
print o.someprop
  File "Lazy.py", line 11, in __get__
setattr(obj, self._calculate.func_name, value)
AttributeError: 'SomeClass' object attribute 'someprop' is read-only


Removing the __slots__ statement, everything would run normally.

Is there any workaround?

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


Re: empty classes as c structs?

2005-02-05 Thread Brian van den Broek
Carlos Ribeiro said unto the world upon 2005-02-05 16:35:
On Sat, 05 Feb 2005 15:59:00 -0500, Brian van den Broek
<[EMAIL PROTECTED]> wrote:
(I'm just a hobbyist, so if this suggestion clashes with some well
established use of 'Bag' in CS terminology, well, never mind.)

There's already a well know know use for the 'bag' name, including a
recipe in Python. A bag is an arbitrary collection of objects. It's
similar to a set. The cookbook code is at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259174
OK then, never mind ;-)
Thanks for the link. Best,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: sos!

2005-02-05 Thread Dan Perl

"jordan2856977" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> hellow everybody! I'm from china. I'm a beginner of python. in china,
> python is not a fashionable language, so it's difficult to find some
> books about python. finally,I find a book named "python how to
> program" wrote by H.M.Deitel . who can tell me where can I find some
> interesting source code about python. otherwise, I will fell boring
> about study!

The Python Cookbook is a good source of examples that other posters haven't 
mentioned yet: http://aspn.activestate.com/ASPN/Cookbook/Python/

It is especially good because it contains simple, short recipes that 
illustrate a specific use of python and these recipes are organized in 
categories.

Dan 


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


CGI POST problem was: How to read POSTed data

2005-02-05 Thread Dan Perl
I am piggybacking on Hakan's original posting because I am addressing the 
same group of people (those with good knowledge in the standard web 
programming modules), on a related topic.  However, my question is 
independent of Hakan's.

I have trouble getting a simple CGI script to work because it gets an empty 
cgi.FieldStorage form.  I have looked into and tried to debug the internals 
of the standard modules that are used but I cannot figure it out: how is a 
multipart POST request parsed by CGIHTTPServer?  BTW, I am using pyton 2.4.

I found the parsing of the header in the rfc822 module used by 
BaseHTTPServer.BaseHTTPRequestHandler (through the mimetools module), but 
that stops after the header (at the first empty line).  Where is the parsing 
done for the POST data following the header?  I will appreciate any pointers 
that will help me debug this problem.

As a side note, I found other old reports of problems with cgi handling POST 
requests, reports that don't seem to have had a resolution.  There is even a 
bug reported just a few days ago (1112856) that is exactly about multipart 
post requests.  If I understand the bug report correctly though, it is only 
on the latest version in CVS and it states that what is in the 2.4 release 
works.  All this tells me that it could be a "fragile" part in the standard 
library.  So it could be even a bug in the standard library, but for now I 
am assuming that I'm doing something wrong.

Thanks,

Dan 


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


Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Michael Hartl
Use the eval function:

>>> eval("30/(6+9)")
2

Michael

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


Re: pickle/marshal internal format 'life expectancy'/backward compatibility

2005-02-05 Thread Tim Peters
[Philippe C. Martin]
> I am looking into using the pickle format to store object/complex data
> structures into a smart card as it would make the design of the embedded
> application very simple.
>
> Yet the card might have to stay in the pocket of the customer for a few
> years, during which the back office application responsible for
> retrieving the information from the card might evolve as well as the
> python release it relies upon.
>
> Is there a commitment for python releases to be able to interpret
> 'older' pickle/marshal internal formats ?

Pickle and marshal have nothing in common, and there's no
cross-release guarantee about marshal behavior.  Pickles are safe in
this respect; indeed, each Python released to date has been able to
load pickles created by all earlier Python releases.  Of course a
pickle produced by a later Python may not be loadable by an earlier
Python, although even in that direction you can get cross-release
portability by forcing the newer Python to restrict itself to produce
obsolete pickle formats.  Reading Lib/pickletools.py in a current
release will explain all this.
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Arvid Andersson
Hello, I need to convert a string to a number, but the string can 
contain +,-,* and / as well as parenthesis. For example, if I have the 
string "30/(6+9)" I would like a function that returned the number 2.

I actually wrote a java function that did this a couple of years ago, in 
school, as an excersise in "binary trees". I lost it, and most of my 
programming knowledge, but I figured perhaps there is a way to do this 
easily in python? It seems to me like it could be a common problem.

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


pickle/marshal internal format 'life expectancy'/backward compatibility

2005-02-05 Thread Philippe C. Martin
Hi,


I am looking into using the pickle format to store object/complex data
structures into a smart card as it would make the design of the embedded
application very simple.

Yet the card might have to stay in the pocket of the customer for a few
years, during which the back office application responsible for
retrieving the information from the card might evolve as well as the
python release it relies upon.

Is there a commitment for python releases to be able to interpret
'older' pickle/marshal internal formats ?

Regards,


Philippe





-- 
***
Philippe C. Martin
SnakeCard LLC
www.snakecard.com
***

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


Re: How to read POSTed data

2005-02-05 Thread Dan Perl

"Håkan Persson" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi.
>
> I am trying to set up a simple HTTP-server but I have problems reading 
> data that is beeing POSTed.
>
> class httpServer(BaseHTTPServer.BaseHTTPRequestHandler):
>def do_POST(self):
>input = self.rfile.read()
>
> The self.rfile.read() will hang on the
> data = self._sock.recv(recv_size)
> line in the read() function in socket.py.
>
> Is there another way to read the data?

I'm not an expert on this subject, but I am in the process of looking at the 
code in BaseHTTPServer and other related modules.  What I see used is 
normally self.rfile.readline() and that makes more sense to me (intuitively) 
than read().  Have you tried that? 


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


Re: empty classes as c structs?

2005-02-05 Thread Carlos Ribeiro
On Sat, 05 Feb 2005 15:59:00 -0500, Brian van den Broek
<[EMAIL PROTECTED]> wrote:
> (I'm just a hobbyist, so if this suggestion clashes with some well
> established use of 'Bag' in CS terminology, well, never mind.)

There's already a well know know use for the 'bag' name, including a
recipe in Python. A bag is an arbitrary collection of objects. It's
similar to a set. The cookbook code is at:

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

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: [EMAIL PROTECTED]
mail: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: empty classes as c structs?

2005-02-05 Thread Carlos Ribeiro
On Sat, 05 Feb 2005 12:05:13 -0700, Steven Bethard
<[EMAIL PROTECTED]> wrote:
> The type suggested in this PEP also allows a simple means of
> representing hierarchical data that allows attribute-style access::
> 
>  >>> x = Bunch(spam=Bunch(rabbit=1, badger=[2, 3, 4]), ham='neewom')
>  >>> x.spam.badger
>  [2, 3, 4]
>  >>> x.ham
>  'neewom'

Static nested data structures are particularly tricky to declare in
Python. Your example works, but IMHO it's rather a workaround than a
real solution. Other languages (such as Delphi) have a record type
that can be nested naturally. Nested classes in Python don't work the
same as Delphi records; on instantiaton, only the outer (parent) class
is instantiated, and the nested (children) classes stay as classes.
This can lead to subtle bugs as class attribute acesses are mixed with
instance attribute accesses with unpredictable results. I have tried
to deal with these problems on MetaTemplate, a library for
declarative-style code in Python. You can see some ideas on this page:

http://metatemplate.python-hosting.com/wiki/MetaTemplate

One of my goals was to model nested data structures as nested classes
in Python. My solution to the nesting problem involves some
autoinstantiation magic which breaks traditional semantics, but works
for the particular application ('practicality beats purity').

I'm interested to see where this will lead us. Other related problems
are named tuples, and ordered attribute access. I deal with the later
in MetaTemplate, but the former is out of scope for the library. In
the long term, I wish Python grows a true record type. The semantic
differences between records and classes are enough to justify it in my
opinion.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: [EMAIL PROTECTED]
mail: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Exec is slow since compiling the string and calls to globals() use a lot 
of time.  The last one is most elegant but __getattr__ and __setattr__ 
are costly. The 'evil hack' solution is good since accessing x and y 
takes no additional time.
Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:

>>> import profile
>>> a = {'x':1, 'y':2}
>>> N = 10
>>> # solution one: use dictionary directly
... def fun1(d):
...   for i in xrange(0,N):
... d['z'] = d['x'] + d['y']
...
>>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
>>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
...
... # solution three: update local dictionary
>>> # Note that locals() is NOT d after update() so
... #   z = x + y
... # does not set z in d
... def fun3(d):
...   exec "locals().update(d)"
...   for i in xrange(0,N):
... d['z'] = x + y
...
>>> # solution four: use dict wrapper
... # this makes code easier to write and read
... class wrapdict(object):
...   """Lazy attribute access to dictionary keys.  Will not access
...  keys that are not valid attribute names!"""
...   def __init__(self, mydict):
... self.__dict__ = mydict
...
... # use wrapper
>>> def fun4(d):
...   wd = wrapdict(d)
...   for i in xrange(0,N):
... wd.z = wd.x + wd.y
...
>>> profile.run('fun1(a)')
 3 function calls in 0.060 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0000.0600.060 :1(?)
10.0000.0000.0600.060 profile:0(fun1(a))
00.000 0.000  profile:0(profiler)
10.0600.0600.0600.060 python-10176FWs.py:2(fun1)
>>> profile.run('fun2(a)')
 24 function calls in 2.130 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0002.1302.130 :1(?)
   100.5200.0000.5200.000 myfun:1(?)
10.0000.0002.1302.130 profile:0(fun2(a))
00.000 0.000  profile:0(profiler)
10.5900.5902.1302.130 python-10176EqB.py:1(fun2)
10.0000.0000.0000.000 
python-10176Sgy.py:2(makeFunction)
   101.0200.0001.5400.000 python-10176Sgy.py:4(f)

>>> profile.run('fun3(a)')
 4 function calls (3 primitive calls) in 0.070 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  2/10.0000.0000.0700.070 :1(?)
10.0000.0000.0700.070 profile:0(fun3(a))
00.000 0.000  profile:0(profiler)
10.0700.0700.0700.070 python-10176R0H.py:4(fun3)
>>> profile.run('fun4(a)')
 4 function calls in 0.100 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0000.1000.100 :1(?)
10.0000.0000.1000.100 profile:0(fun4(a))
00.000 0.000  profile:0(profiler)
10.0000.0000.0000.000 
python-10176e-N.py:6(__init__)
10.1000.1000.1000.100 python-10176rIU.py:1(fun4)

Since
  d['x'] is fast but cumbersome
  exec "z=x+y' is still slow.
  exec "locals().update(d)" is evil
  d.x is elegant and only a little slower than d['x']
I am announcing the winner of the contest: dictwrap! (applause)
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Arthur
On Sat, 5 Feb 2005 20:02:44 +0100, [EMAIL PROTECTED] (Alex Martelli)
wrote:

>Arthur <[EMAIL PROTECTED]> wrote:
>
>> On Sat, 5 Feb 2005 17:00:15 +0100, [EMAIL PROTECTED] (Alex Martelli)
>> wrote:
>> >
>> >I consider this one of the worst ideas to have been proposed on this
>> >newsgroup over the years, which _IS_ saying something. \
>> 
>> I would disagree, but only to the extent that nothing that is only a
>> request for an option toggle should qualify for this award.  For
>> anyone not interested in it's effects, it's business as usual.
>
>You must have lead a charmed life, I think, unusually and blissfully
>free from pointy-haired bosses (PHBs).  In the sublunar world that most
>of us inhabit, ``optional'' idiocies of this kind soon become absolutely
>mandatory -- thanks to micromanagement by PHBs.

It seems to me that you would be more accurate (and calmer) if you
generalized from your own experience, rather than in direct
contradiction to it.

That the firms that advocate the use of Python are the one's  least
likely to be dominated by PHB's who DKTAFTE (who don't know their ass
from their elbows).

Unless I am misintepreting  you. 

Do the STUPID firms use Python as well.   

Why?  

Clearly there are stupider choices.

I prefer to use VB when doing certain kinds of stupid things.  It's
the right tool in those cases.  

Really.

Perhaps the answer here has more to do with backing off efforts to
make Python ubiquitous. 

Why shouldn't the community stay a bit elitist? 

Or else maybe that's where you started on this thread, in fact - and
in your own way.

If so, I agree - within reason.  

One doesn't toggle between VB and Python, perhaps is the point.  They
are of different species. 

Art

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


RE: Python Processes for Win32

2005-02-05 Thread Robert Brewer
[EMAIL PROTECTED] wrote:
> I have a daemon type script (daemon.py -- we'll say) that I would like
> to have run continuously. I'd like to be able to do something like
> this:
> 
> daemon.py start
> 
> ... and then to have it stop I'd like to do this:
> 
> daemon.py stop
> 
> I am having a hard time googling for a clue as to how to accomplish
> this. If anyone can point me in the right direction, I'd really
> appreciate it.

Write a Windows service (natively, see
http://www.python.org/windows/win32 or using py2exe, see
http://starship.python.net/crew/theller/py2exe/).

Then use:

net start daemonsvc
net stop daemonsvc


Robert Brewer
MIS
Amor Ministries
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote:
You can part way there using keyword arguments. You just have to use 
dictionary syntax for changing values in the dictionary:

 >>> def f(d, x=None, y=None):
 ...   d['z'] = x + y
 ...
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>>
 >>> f(a, **a)
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> f(b, **b)
 >>> b
{'y': 3, 'x': 3, 'z': 6}
This is not possible in my case since my dictionary have many more items 
than just x and y. So, if there is are other items in the dictionary

>>> a = {'x':1, 'y':2, 'else':4}
>>> f(a,**a)
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/tmp/python-10176RtT.py", line 1, in ?
f(a,**a)
TypeError: f() got an unexpected keyword argument 'else'
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: empty classes as c structs?

2005-02-05 Thread Brian van den Broek
Steven Bethard said unto the world upon 2005-02-05 14:05:
Nick Coghlan wrote:
Steven Bethard wrote:
Yes -- help me rally behind my generic object PEP which proposes a 
Bunch type (probably to be renamed) for the Python standard lib. =)

Did you see the suggestion of 'namespace' as a name?

Yup, it's in the current PEP draft.  See the "Open Issues" section:
PEP: XXX
Title: Generic Object Data Type
Version: $Revision: 1.0 $
Last-Modified: $Date: 2004/11/29 16:00:00 $
Author: Steven Bethard <[EMAIL PROTECTED]>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 29-Nov-2004
Python-Version: 2.5
Post-History: 29-Nov-2004

Open Issues
===
What should the type be named?  Some suggestions include 'Bunch',
'Record', 'Struct' and 'Namespace'.
A quick check of the google groups archive didn't lead me to believe 
that I'm repeating old suggestions, so: how about 'Bag'?

It's has the small virtue of being short and the great virtue of 
employing a good metaphor, I think. A (loose enough) bag takes 
whatever shape you impose on it by the things that you stick into it. 
If I understand the PEP draft aright, the idea is quite similar -- a 
python object with no internal structure other than that imposed by 
what the programmer decides to put into it.

(I'm just a hobbyist, so if this suggestion clashes with some well 
established use of 'Bag' in CS terminology, well, never mind.)

Best to all,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: Which IDE supports python and wxpython?

2005-02-05 Thread http://www.stani.be
Try SPE, I just released (GPL) a new version: http://spe.pycs.net

Stani

http://www.stani.be

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


Re: empty classes as c structs?

2005-02-05 Thread Michael Spencer
Steven Bethard wrote:
Nick Coghlan wrote:

class attr_view(object):
def __init__(self, data):
self.__dict__ = data

I think the idea definitely deserves mention as a possible 
implementation strategy in the generic objects PEP, with the data 
argument made optional:

That's basically what the current implementation does (although I use 
'update' instead of '=').  The code is complicated because the 
implementation also takes all the argument types that dicts take.

STeVe
Have you noted the similarity of bunch and types.ModuleType?
perhaps module.__init__  could take an additional keyword argument to set its 
__dict__

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


Re: empty classes as c structs?

2005-02-05 Thread Michael Spencer
Alex Martelli wrote:
Nick Coghlan <[EMAIL PROTECTED]> wrote:
   ...
Michael Spencer also posted ...
Wasted indirection, IMHO.  A better implementation:
class attr_view(object):
def __init__(self, data):
self.__dict__ = data
Alex
Indeed!  A complete brain-blip
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: [EVALUATION] - E01: The Java Failure - May Python Helps?

2005-02-05 Thread Jeremy Bowers
On Sat, 05 Feb 2005 16:44:11 +0200, Ilias Lazaridis wrote:
>> * Deployment: I don't generally have enough problems with this to be
>> worth thinking about. I don't know what the state of the remote
>> debugging is on Python; Google "remote debugging Python".
> 
> [I like to avoid interaction with google.]

OK, this, combined with a reputation that didn't quite precede you, but
came close, leads me to say this: I've given you what you're going to get.
You want more, you're going to have to put some effort into it. Lots of
effort. 

But I can give you this: If you aren't already there, you're a textbook
case of somebody heading for analysis paralysis. It's time to stop
researching and start programming; grab Python, do a couple tutorials, and
start acquiring the skills you're going to need to do these other tasks
you want to do anyhow. 

Barring evidence that you are taking responsibility for yourself, I'm not
inclined to help much more.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Tony

Alan McIntyre wrote:
> Tony,
>
> Actually I only want to remove a certain kind of duplication; 

How about this one liner?
def condense(m):
print [m[0]]+[m[k] for k in range(1,len(m)) if
m[k]!=m[k-1]]
b=[1,1,1,2,2,2,1,1,1]
condense(b)
Tony Clarke

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


Re: Alternative to standard C "for"

2005-02-05 Thread Georg Brandl
Alex Martelli wrote:
> Georg Brandl <[EMAIL PROTECTED]> wrote:
> 
>> Slight terminology glitch -- it does return an iterator, not a
>> generator. Generators are functions that return iterators.
> 
> xrange returns an ITERABLE, not an ITERATOR.  Videat:
> 
 a = xrange(23, 43)
 a.next()
> Traceback (most recent call last):
>   File "", line 1, in ?
> AttributeError: 'xrange' object has no attribute 'next'
 
> 
> No next method -> not an iterator.  iter(xrange(...)) DOES return an
> iterator, btw.

Thanks! Somehow it's always these little corrections that contain errors.

And, considering your other posts today, I got away quite well...

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


access problem with DCOM server written in python

2005-02-05 Thread Achim Domma (Procoders)
Hi,
I have a problem with a DCOM server written in python. Here is my 
minimal test object:

class TestObject:
_reg_clsid_ = "{ECDBB3BC-F0BF-4eef-87C0-D179A928DAB5}"
_reg_progid_ = "DComTest.Object"
_reg_desc_ = "DComTest.Object"
_public_methods_ = ['testit']
def __init__(self):
pass
def testit(self): pass
if __name__=='__main__':
#   register object
from win32com.server.register import UseCommandLine
UseCommandLine(TestObject)
The object is registered on server S and on client C. The user U exists 
on both machines with the same password. Accessing 'normal' C++ COM 
objects from C on S works fine, which means the DCOM is working at all.

U is able to create the object localy, but accessing my test object via 
DCOM gives me an 'access denied'. I used dcomcnfg to grand U the right 
to start and access the object on S (same settings as for working C++ 
objects), but I still get 'access denied'.

The server was newly setup. The script was working on another setup, but 
I have no idea what might be the differnce.

BTW: Accessing the object with an admin account works just fine.
Any hint on how to solve, or at least debug, the problem?
regards,
Achim
--
http://mail.python.org/mailman/listinfo/python-list


Re: Which IDE supports python and wxpython?

2005-02-05 Thread Daniel Bickett
I know of two:

Boa Constructor:   http://boa-constructor.sourceforge.net/
wxGlade:  http://wxglade.sourceforge.net/

-- 
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to standard C "for"

2005-02-05 Thread Alex Martelli
Georg Brandl <[EMAIL PROTECTED]> wrote:

> Slight terminology glitch -- it does return an iterator, not a
> generator. Generators are functions that return iterators.

xrange returns an ITERABLE, not an ITERATOR.  Videat:

>>> a = xrange(23, 43)
>>> a.next()
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'xrange' object has no attribute 'next'
>>> 

No next method -> not an iterator.  iter(xrange(...)) DOES return an
iterator, btw.


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


Which IDE supports python and wxpython?

2005-02-05 Thread Selfimprover
Hello all,

Is there a good IDE on the market which supports python and wxpython. Goal
is to use it in a big distributed project.

Greetings


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


Re: empty classes as c structs?

2005-02-05 Thread Steven Bethard
Nick Coghlan wrote:
Alex Martelli wrote:
Nick Coghlan <[EMAIL PROTECTED]> wrote:
   ...
Michael Spencer also posted an interesting idea recently about 
setting up
a view of an existing dictionary, rather than as a separate object:

class attr_view(object):
  def __init__(self, data):
object.__setattr__(self, "_data", data)
  def __getattr__(self, attrname):
return self._data[attrname]
  def __setattr__(self, attrname, value):
self._data[attrname] = value
Wasted indirection, IMHO.  A better implementation:
class attr_view(object):
def __init__(self, data):
self.__dict__ = data
I think the idea definitely deserves mention as a possible 
implementation strategy in the generic objects PEP, with the data 
argument made optional:
That's basically what the current implementation does (although I use 
'update' instead of '=').  The code is complicated because the 
implementation also takes all the argument types that dicts take.

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


Re: Alternative to standard C "for"

2005-02-05 Thread Diez B. Roggisch
> First case looks quite nasty, because it's for more complicated
> things, not numerical loops. Second is very nice, but with there's
> problem. If i do ..in range(1, 1).. (what I really need
> sometimes), it takes few hundred megs of memory and slows
> down. Are there other good ways for this simple problem? Generators?

Use xrange(). It computes the values as they are needed - not an entire
list.
-- 
Regards,

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


Re: empty classes as c structs?

2005-02-05 Thread Steven Bethard
Nick Coghlan wrote:
Steven Bethard wrote:
Yes -- help me rally behind my generic object PEP which proposes a 
Bunch type (probably to be renamed) for the Python standard lib. =)
Did you see the suggestion of 'namespace' as a name?
Yup, it's in the current PEP draft.  See the "Open Issues" section:
PEP: XXX
Title: Generic Object Data Type
Version: $Revision: 1.0 $
Last-Modified: $Date: 2004/11/29 16:00:00 $
Author: Steven Bethard <[EMAIL PROTECTED]>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 29-Nov-2004
Python-Version: 2.5
Post-History: 29-Nov-2004
Abstract

This PEP proposes a standard library addition to support the simple
creation of 'generic' objects which can be given named attributes
without the need to declare a class. Such attribute-value mappings are
intended to complement the name-value mappings provided by Python's
builtin dict objects.
Motivation
==
Python's dict objects provide a simple way of creating anonymous
name-value mappings. These mappings use the __getitem__ protocol to
access the value associated with a name, so that code generally appears
like::
mapping['name']
Occasionally, a programmer may decide that dotted-attribute style access
is more appropriate to the domain than __getitem__ style access, and
that their mapping should be accessed like::
mapping.name
Currently, if a Python programmer makes this design decision, they are
forced to declare a new class, and then build instances of this class.
When no methods are to be associated with the attribute-value mappings,
declaring a new class can be overkill.  This PEP proposes adding a
simple type to the collections module of the standard library that can
be used to build such attribute-value mappings.
Providing such a type allows the Python programmer to determine which
type of mapping is most appropriate to their domain and apply this
choice with minimal effort.  Some of the suggested uses include:
Returning Named Results
---
It is often appropriate for a function that returns multiple items to
give names to the different items returned.  The type suggested in this
PEP provides a simple means of doing this that allows the returned
values to be accessed in the usual attribute-style access::
>>> def f(x):
... return Bunch(double=2*x, squared=x**2)
...
>>> y = f(10)
>>> y.double
20
>>> y.squared
100
Representing Hierarchical Data
--
The type suggested in this PEP also allows a simple means of
representing hierarchical data that allows attribute-style access::
>>> x = Bunch(spam=Bunch(rabbit=1, badger=[2, 3, 4]), ham='neewom')
>>> x.spam.badger
[2, 3, 4]
>>> x.ham
'neewom'
Rationale
=
As Bunch objects are intended primarily to replace simple, data-only
classes, simple Bunch construction was a primary concern.  As such,
the Bunch constructor supports creation from keyword arguments, dicts,
and sequences of (attribute, value) pairs::
>>> Bunch(eggs=1, spam=2, ham=3)
Bunch(eggs=1, ham=3, spam=2)
>>> Bunch({'eggs':1, 'spam':2, 'ham':3})
Bunch(eggs=1, ham=3, spam=2)
>>> Bunch([('eggs',1), ('spam',2), ('ham',3)])
Bunch(eggs=1, ham=3, spam=2)
To allow attribute-value mappings to be easily combined, the Bunch type
provides a update staticmethod that supports similar arguments::
>>> bunch = Bunch(eggs=1)
>>> Bunch.update(bunch, [('spam', 2)], ham=3)
>>> bunch
Bunch(eggs=1, ham=3, spam=2)
Note that update is available through the class, not through the
instances, to avoid the confusion that might arise if an 'update'
attribute added to a Bunch instance hid the update method.
If Bunch objects are used to represent hierarchical data, comparison of
such objects becomes a concern.  For this reason, Bunch objects support
object equality which compares Bunch objects by attributes recursively::
>>> x = Bunch(parrot=Bunch(lumberjack=True, spam=42), peng='shrub')
>>> y = Bunch(peng='shrub', parrot=Bunch(spam=42, lumberjack=True))
>>> z = Bunch(parrot=Bunch(lumberjack=True), peng='shrub')
>>> x == y
True
>>> x == z
False
Note that support for the various mapping methods, e.g.
__(get|set|del)item__, __len__, __iter__, __contains__, items, keys,
values, etc. was intentionally omitted as these methods did not seem to
be necessary for the core uses of an attribute-value mapping.  If such
methods are truly necessary for a given use case, this may suggest that
a dict object is a more appropriate type for that use.
Examples
=
Converting an XML DOM tree into a tree of nested Bunch objects::
>>> import xml.dom.minidom
>>> def getbunch(element):
... result = Bunch()
... if element.attributes:
... Bunch.update(result, element.attributes.items())
... children = {}
... for child in element.childNodes:
... if child.nodeType == xml.dom.minidom.Node.TEXT_NODE:
... 

Re: changing local namespace of a function

2005-02-05 Thread Michael Spencer
Alex Martelli wrote:
Hmmm, you do realize that wrapdict uses a lot of indirection while my
equivalent approach, just posted, is very direct, right?  To reiterate
the latter, and dress it up nicely too, it's
class wrapwell(object):
def __init__(self, somedict):
self.__dict__ = somedict
Bad mistake on my part, sorry!
Nick Coghlan wrote:
... a class that combined property access with the above...
 
In a similar vein to Nick's solution:
class AutoProperty(object):
def __init__(self, meth):
   self.meth = meth
   self.name = meth.__name__
   self.__doc__ = meth.__doc__
def __get__(self, obj, cls):
if isinstance(obj, cls):
return obj.__dict__.setdefault(self.name, self.meth(obj))
else:
return self.__doc__
# You could define __set__ and __del__ but they don't seem
# necessary based on what you've said so far
class DataManipulator(object):
def __init__(self, data):
self.__dict__ = data
class Model(DataManipulator):
def z(self):
"""x+y"""
return self.x+self.y
z = AutoProperty(z)
def z1(self):
"""Or any other useful information"""
return self.z + self.x
z1 = AutoProperty(z1)
# You could automate these calls to AutoProperty in a metaclass
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>> d = Model(a)
 >>> d.z
3
 >>> d.z1
4
 >>> a
{'y': 2, 'x': 1, 'z': 3, 'z1': 4}
 >>> d=  Model(b)
 >>> d.z1
9
 >>> b
{'y': 3, 'x': 3, 'z': 6, 'z1': 9}
 >>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to standard C "for"

2005-02-05 Thread Georg Brandl
BJörn Lindqvist wrote:
>> I am quite new to Python, and have a straight & simple question.
>> In C, there is for (init; cond; advance). We all know that.
>> In Python there are two ways to loop over i=A..B (numerical.):
>> 1) i = A
>>while i>   ...do something...
>>   i+=STEP
> 
> This is indeed quite ugly. You rarely need such loops in Python and
> with some thinking you can often translate the C-equivalent to
> something more pythonic. As you guessed, your second problem is best
> solved with a generator function - xrange(). It is completely equal to
> range() except that it returns a generator instead of a list.

Slight terminology glitch -- it does return an iterator, not a
generator. Generators are functions that return iterators.

regards,
Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Alex Martelli
Arthur <[EMAIL PROTECTED]> wrote:

> On Sat, 5 Feb 2005 17:00:15 +0100, [EMAIL PROTECTED] (Alex Martelli)
> wrote:
> >
> >I consider this one of the worst ideas to have been proposed on this
> >newsgroup over the years, which _IS_ saying something. \
> 
> I would disagree, but only to the extent that nothing that is only a
> request for an option toggle should qualify for this award.  For
> anyone not interested in it's effects, it's business as usual.

You must have lead a charmed life, I think, unusually and blissfully
free from pointy-haired bosses (PHBs).  In the sublunar world that most
of us inhabit, ``optional'' idiocies of this kind soon become absolutely
mandatory -- thanks to micromanagement by PHBs.

For the last few years I've been working as a consultant -- mostly
(thanks be!) for a wonderful Swedish customer whose managers are in fact
great techies, but otherwise for a fair sample of typical development
shops.  Such "fair samples" have weaned me from my own mostly-charmed
blissful life, confirming that the amount of utter stupidity in this
world is REALLY high, and far too much is that is in management
positions.

Now, I've recently had a great offer to work, doing mostly Python, for
another incredibly great firm, and, visa issues permitting, I'll gladly
leave the life of consultants' joys and sorrows behind me again -- I've
spent most of my life working as a full-time employee for a few great
firms, and the last few years have confirmed to me that this fits my
character and personality far better than being a consultant does (just
like the years between my two marriages have confirmed to me that I'm
better suited to be a husband, rather than a roving single... I guess
there's a correlation there!-).  So, I'm not speaking for selfish
reasons: at my soon-to-be employer, techies rule, and optional idiocies
won't matter much.  I _am_ speaking on behalf of maybe half of the
million or so Python programmers in the world, who are NOT so lucky as
to be working in environments free from the blight of micromanagement.


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


Re: Alternative to standard C "for"

2005-02-05 Thread Daniel Bickett
Paul Rubin wrote:
> use xrange instead of range.

Woops ;) I wasn't aware such a function existed.

apologies-for-reinventing-the-wheel-ly y'rs,
-- 
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to standard C "for"

2005-02-05 Thread Daniel Bickett
[EMAIL PROTECTED] wrote:
> Are there other good ways for this simple problem? Generators?

Very interesting problem :) That never occured to me.

To prevent python from loading that entire list into memory, one
could, as you suggested, use a generator:

>>> def genrange( start , stop , step = 1 ):
while start < stop:
yield start
start += step

>>> for x in range( 5 ):
print "%s " % str( x ),

0  1  2  3  4 

>>> for x in genrange( 0 , 5 ):
print "%s " % str( x ),

0  1  2  3  4 

-- 
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to standard C "for"

2005-02-05 Thread Paul Rubin
[EMAIL PROTECTED] writes:
> problem. If i do ..in range(1, 1).. (what I really need
> sometimes), it takes few hundred megs of memory and slows
> down. Are there other good ways for this simple problem? Generators?

use xrange instead of range.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to standard C "for"

2005-02-05 Thread BJörn Lindqvist
> I am quite new to Python, and have a straight & simple question.
> In C, there is for (init; cond; advance). We all know that.
> In Python there are two ways to loop over i=A..B (numerical.):
> 1) i = A
>while i   ...do something...
>   i+=STEP

This is indeed quite ugly. You rarely need such loops in Python and
with some thinking you can often translate the C-equivalent to
something more pythonic. As you guessed, your second problem is best
solved with a generator function - xrange(). It is completely equal to
range() except that it returns a generator instead of a list.


-- 
mvh Björn
--
http://mail.python.org/mailman/listinfo/python-list


Alternative to standard C "for"

2005-02-05 Thread adomas . paltanavicius
Hi there,

I am quite new to Python, and have a straight & simple question.
In C, there is for (init; cond; advance). We all know that.
In Python there are two ways to loop over i=A..B (numerical.):
1) i = A
   while ihttp://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Arthur
On Sat, 5 Feb 2005 17:00:15 +0100, [EMAIL PROTECTED] (Alex Martelli)
wrote:
>
>I consider this one of the worst ideas to have been proposed on this
>newsgroup over the years, which _IS_ saying something. \

I would disagree, but only to the extent that nothing that is only a
request for an option toggle should qualify for this award.  For
anyone not interested in it's effects, it's business as usual.  

They can even reward themselves with the knowledge that among the time
and typing they did not waste was in asking for the toggle to be on.

It is also true that mplementation might be a diversion of efforts
from the implementation of features solving more generally recognized
problems. There *are* a lot of bad things to say about the idea. 

But I think it worth mentioning that the OP is requesting only a
toggle.

Why is it worth mentioning?

Because maybe one needs to save the heaviest rhetoric for when this is
not the case.

There was a simple answer to those who hate decorators - don't use
them. I won't.

And the most controversial suggestions about optional static typing
all perserve their sanity by remaining suggestions about something
defined as optional.

Generally speaking, it might then be said to be good community poilcy
to hold one's fire at least a bit if a feature request, or accepted
PEP, will not impact one's own code writing beyond the extent one
might choose  it to.

Though I  do think there *should* be a worst feature request contest
at PyCon,   

What is with this white space business, anyway?

;)

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


Re: IDLE history, Python IDE, and Interactive Python with Vim

2005-02-05 Thread Fernando Perez
Hi,

Ashot wrote:

> This is sort of both Python and Vim related (which is why I've posted to
> both newsgroups).

[...]

I know you've been using ipython recently (the readline color bugs), so perhaps
my reply is a bit redundant.  Forgive me if that's the case, I just want to
give you some useful info.

Just in case you haven't come across these features (the manual is kind of long
and dry), I should note that ipython has pretty much everything you've asked
for here.  %hist -n dumps your history without line numbers (for copy/paste),
%logstart gives you an incremental log (valid python code) of your current
session, %save allows you to save a selected group of lines to a file, and
%edit will open up $EDITOR (or vi in Unix by default) at the source of any
accessible object.  With %pdb, you can even trigger automatically pdb at any
uncaught exception (in Emacs, you'll even get the source simultaneously opened, 
I'm sure something similar could be done for vi).

Ipython is not an IDE, but it does offer an extensible command-line environment
which tries hard to be very efficient.  Hopefully this year I'll be able to
work on integrating it (optionally, since it will never lose its command-line
functionality) with GUIs as well.

Regards,

f

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


Re: variable declaration

2005-02-05 Thread Alex Martelli
Nick Coghlan <[EMAIL PROTECTED]> wrote:

> Alex Martelli wrote:
> > 'global' is an ugly wart, to all intents and purposes working "as if" it
> > was a declaration.  If I had to vote about the one worst formal defect
> > of Python, it would surely be 'global'.
> > 
> > Fortunately, it's reasonably easy to avoid the ugliness, by avoiding
> > rebinding (within functions) global variables, which tend to be easy.
> 
> Hear, hear! And if I could write "gbls = namespace(globals())" in order to
> deal with those rare cases where I *do* need to rebind globals, all uses
> of the keyword could be handled nicely, while entirely eliminating the
> need for the keyword itself.

I entirely agree with you on this.


> Would you be as violently opposed to a 'rebinding' augmented assignment
operator?

Not at all.  The only downside I can see, and it seems a minor one to
me, is having two "obvious ways" to re-bind a name, since '=' would keep
working for the purpose.  But making it explicit that one IS rebinding a
name rather than binding it anew could sometimes make certain code more
readable, I think; quite apart from possible advantages in catching
typos (which may be OK, but appear minor to me), the pure gain in
reaability might make it worth it.  Call my stance a +0.

> the problem of detecting typos in the right-most name in a compound name
(e.g.

It's not clear to me what semantics, exactly, x.y := z would be defined
to have (assuming := is the syntax sugar for ``rebinding'').  Perhaps,
by analogy with every other augmented operator, it should be equivalent
to:

_temp = x.y
x.y = type(temp).__irebind__(temp, z)

This makes it crystal-clear what happens when type(x).y is a descriptor
with/without __set__ and __get__, when type(x) defines __getattribute__
or __setattr__, etc, etc.  Giving type object an __irebind__ which just
returns the second operand would complete the semantics.  Of course,
doing it this way would open the issue of types overriding __irebind__,
and it's not clear to me that this is intended, or desirable.  So, maybe
some other semantics should be the definition.  But since "rebinding" is
not a primitive, the semantics do need to be somehow defined in terms of
elementary operations of getting and setting (of attributes, items, &c).

> Did I mention the possible incidental benefit of reducing the whinging
> about the lack of variable declarations?

It might, with some luck;-).  Probably worth a PEP, although I suspect
it will not be considered before 3.0.


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


Re: Trouble converting hex to decimal?

2005-02-05 Thread Jorgen Grahn
On Sat, 05 Feb 2005 06:51:32 -0700, Earl Eiland <[EMAIL PROTECTED]> wrote:
> I'm trying to process the IP packet length field, as recorded by pcap
> (Ethereal) and recovered using pcapy.  When I slice out those bytes, I
> get a value that shows in '\x00' format, rather than '0x00'.  Neither
> int() nor eval() are working.  How do I handle this?

>From my unpublished protocol analyzing hack:

class Ip:
"IPv4 header"
def __init__(self, frame):
(vi, tos, tlen,
 id, ffoff,
 ttl, proto, checksum,
 source,
 dest) = struct.unpack('! BBH HH BBH LL', frame[:20])
self.len = 4 * (vi & 0xf)
if proto==6:
self.proto=Tcp
elif proto==17:
self.proto=Udp
elif proto==1:
self.proto=Icmp
self.source = Address(source)
self.dest = Address(dest)

That doesn't take IP options into account (or are they part of Ip.len? I
forget.), but it has the nifty feature that IP.proto tells the caller what
factory function (if any) she should feed the rest of the frame into.

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I enter/receive webpage information?

2005-02-05 Thread Jorgen Grahn
On 4 Feb 2005 15:33:50 -0800, Mudcat <[EMAIL PROTECTED]> wrote:
> Hi,
> 
> I'm wondering the best way to do the following.
> 
> I would like to use a map webpage (like yahoo maps) to find the
> distance between two places that are pulled in from a text file. I want
> to accomplish this without displaying the browser.

That's called "web scraping", in case you want to Google for info.

> I am looking at several options right now, including urllib, httplib,
> packet trace, etc. But I don't know where to start with it or if there
> are existing tools that I could incorporate.
> 
> Can someone explain how to do this or point me in the right direction?

I did it this way successfully once ... it's probably the wrong approach in 
some ways, but It Works For Me.

- used httplib.HTTPConnection for the HTTP parts, building my own requests
  with headers and all, calling h.send() and h.getresponse() etc.

- created my own cookie container class (because there was a session
  involved, and logging in and such things, and all of it used cookies)

- subclassed sgmllib.SGMLParser once for each kind of page I expected to
  receive. This class knew how to pull the information from a HTML document,
  provided it looked as I expected it to.  Very tedious work. It can be easier
  and safer to just use module re in some cases.

Wrapped in classes this ended up as (fictive):

client = Client('somehost:80)
client.login('me', 'secret)
a, b = theAsAndBs(client, 'tomorrow', 'Wiltshire')
foo = theFoo(client, 'yesterday')

I had to look deeply into the HTTP RFCs to do this, and also snoop the
traffic for a "real" session to see what went on between server and client.

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


An Ode To My Two Loves

2005-02-05 Thread Jamey Cribbs
At the risk of calling my manhood into question, I humbly submit the 
following little diddy (which is a blatant rip-off of a heart wrenching 
melody of the '70s by that international superstar, Mary Macgregor):

   To the tune of "Torn Between Two Lovers":
   Torn between two languages, both of which are awesome tools
   Lovin' both of you is breakin' all the rules
   Torn between two incredibly awesome scripting languages, I'm 
startin' to drool
   Using you both is breakin' all the rules

Is freedom of choice wonderful or a curse?  I do a lot of scripting at 
my day job.  Through the years, I've gone through what will probably 
seem a somewhat familiar progression of scripting languages.  Started 
out with Tcl, moved to Perl when I began to do some serious text 
munging, moved back to Tcl/Tk when I needed to do some gui stuff, jumped 
to Python as I began to learn object oriented programming, told Python I 
needed more space and moved in with Ruby when I needed to do more gui 
stuff and wanted to use FXRuby.

And that's where I stand now, Torn Between Two Scripting Languages.  
Python and Ruby are both so damn attractive that I can't commit to one 
of them.  I don't want to settle down into a monogamous programming 
relationship.  I mean, if you have Grace Kelly on one arm and Maureen 
O'Hara on the other (I'm old school), should you really have to give one 
of them up?

Why can't my boss understand this?  But no, he has to lecture me about 
the dangers of "foolin' around" with too many languages.  "Oh sure", he 
says, "you're having fun now juggling two beautiful languages in the 
air.  But how long can you keep living two lives?  Sooner or later you 
are going to slip up and make a mistake.  My god, man, what if you start 
leaving off the 'end' in your if statements?  What if you forget that 
indentation DOES matter?!  For the love of all that is sacred and holy, 
think of the programs!"

My boss was always a little melodramatic.
Anyway, I don't know why I'm typing this up.  I just finished a major 
update to KirbyBase and I think I'm a little punch drunk.  I have been 
spending a lot of time with Ruby lately and working on KirbyBase caused 
me to dive back into Python.  Why do these two languages have to be so 
freakin' good!  I know a lot of people see big differences in the 
languages, but I'm not one of them.  I don't think I am a smart enough 
programmer to understand or ever be bothered by some of the more subtle 
or deeper differences that some people have pointed out.  Ah, ignorance 
is bliss.

Well, as long as I can keep my boss from finding out that I am in this 
programming love triangle, I think I will probably stay happily enmeshed 
in my little soap opera.

Well, I think I will go now and listen to my Captain & Tenille 
records...why are you looking at me like that.  I'm a happily married 
man with four kids.  I like football and guns...really, I do!

Under the influence of some kind of strange force and will probably be 
embarrassed when he snaps out of it,

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


Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Something I forgot to mention. . .
Bo Peng wrote:
You know, I have a deep root in C/C++ so performance is the king and 
hacking is part of my daily life. Time to change now. :)
The entire design of C++ is in many ways a regrettable monument to the idea that 
premature optimisation is evil - far too many of the language's default 
behaviours are 'fast in particular cases, but quite simply wrong in most cases, 
and the compiler often can't tell you which case applies'. So you can write 
buggy warning free code just by failing to override the unsafe default behaviours.

That said, one of the reasons I like CPython is that it lets you drop into C/C++ 
really easily when you need to (generally for hardware interface or performance 
reasons). The builtin profiler can also give some decent hints on the 
bottlenecks where you need to focus your performance improvements.

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: Trouble converting hex to decimal?

2005-02-05 Thread Steve Holden
Pedro Werneck wrote:
Hi
The problem is that '\x00' is a escape sequence... 

Try something like this:

x = '\x00'
int(repr(x)[3:-1], 16)
0
x = '\x15'
int(repr(x)[3:-1], 16)
21

On Sat, 05 Feb 2005 06:51:32 -0700
Earl Eiland <[EMAIL PROTECTED]> wrote:

I'm trying to process the IP packet length field, as recorded by pcap
(Ethereal) and recovered using pcapy.  When I slice out those bytes, I
get a value that shows in '\x00' format, rather than '0x00'.  Neither
int() nor eval() are working.  How do I handle this?
Earl Eiland
Talk about making things difficult!
 >>> x = '\x00'
 >>> ord(x)
0
 >>> x = '\x15'
 >>> ord(x)
21
 >>>
regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005  http://www.pycon.org/
Steve Holden   http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list


How to read POSTed data

2005-02-05 Thread Håkan Persson
Hi.
I am trying to set up a simple HTTP-server but I have problems reading 
data that is beeing POSTed.

class httpServer(BaseHTTPServer.BaseHTTPRequestHandler):
   def do_POST(self):
   input = self.rfile.read()
The self.rfile.read() will hang on the
data = self._sock.recv(recv_size)
line in the read() function in socket.py.
Is there another way to read the data?
Thanks,
Håkan Persson
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Bo Peng wrote:
I can not say enough thank you for this.
Don't thank me, thank Guido. He created the property machinery - I just let you 
know it was there :)

But yes, Python's OO is OO the way it should be - something that helps you get 
the job done quickly and cleanly, rather than making you jump through irrelevant 
hoops.

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Nick Coghlan
Alexander Zatvornitskiy wrote:
var epsilon=0
var S
S=0
while epsilon<10:
  S=S+epsilon
  epselon=epsilon+1#interpreter should show error here,if it's in "strict mode"
print S
It is easy, and clean-looking.
Alexander, [EMAIL PROTECTED]
An alternate proposal, where the decision to request rebinding semantics is made 
at the point of assignment:

epsilon = 0
S = 0
while epsilon < 10:
  S .= S + epsilon
  epselon .= epsilon + 1 #interpreter should show error here
print S
Of course, this is a bad example, since '+= ' can be used already:
S = 0
epsilon = 0
while epsilon<10:
  S += epsilon
  epselon += 1 #interpreter DOES show error here
print S
However, here's an example where there is currently no way to make the rebinding 
intent explicit:

def collapse(iterable):
it = iter(iterable)
lastitem = it.next()
yield lastitem
for item in it:
if item != lastitem:
yield item
lastitem = item
With a rebinding operator, the intent of the last line can be made explicit:
def collapse(iterable):
it = iter(iterable)
lastitem = it.next()
yield lastitem
for item in it:
if item != lastitem:
yield item
lastitem .= item
(Note that doing this *will* slow the code down, though, since it has to check 
for the existence of the name before rebinding it)

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Jack Diederich
On Sat, Feb 05, 2005 at 02:31:08PM +1000, Nick Coghlan wrote:
> Jack Diederich wrote:
> >Since this is 2.4 you could also return a generator expression.
> >
> >
> def iter_collapse(myList):
> >
> >...   return (x[0] for (x) in 
> >it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]))
> >... 
> 
> But why write a function that returns a generator expression, when you 
> could just turn the function itself into a generator?
> 
> Py>def iter_collapse(myList):
> ...   for x in it.groupby(myList):
> ... yield x[0]
> 
Here is where I say, "because it is faster!"
except it isn't.  maybe.  The results are unexpected, anyway.

import timeit
import itertools as it

def collapse1(myl):
  for x in it.groupby(myl):
yield x[0]

def collapse2(myl):
  return (x[0] for (x) in it.groupby(myl))

list_str = '[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]'

print "collapse1", timeit.Timer('collapse1(%s)' % (list_str), 'from __main__ 
import collapse1').timeit()
print "collapse2", timeit.Timer('collapse2(%s)' % (list_str), 'from __main__ 
import collapse2').timeit()
print "list(collapse1)", timeit.Timer('list(collapse1(%s))' % (list_str), 'from 
__main__ import collapse1').timeit()
print "list(collapse2)", timeit.Timer('list(collapse2(%s))' % (list_str), 'from 
__main__ import collapse2').timeit()

collapse1 1.06855082512
collapse2 3.40627384186
list(collapse1) 8.31489896774
list(collapse2) 9.49903011322

The overhead of creating the generator expression seems much higher than
creating the equivalent function.  If you subtract our the setup difference
actually running through the whole iterator is slightly faster for genexps
than functions that yield.  At a guess it has something to do with how
they handle lexical scope and some byte code differences.

I said guess, right?

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


Re: Trouble converting hex to decimal?

2005-02-05 Thread Pedro Werneck
Hi

The problem is that '\x00' is a escape sequence... 

Try something like this:


>>> x = '\x00'
>>> int(repr(x)[3:-1], 16)
0
>>> x = '\x15'
>>> int(repr(x)[3:-1], 16)
21
>>> 



On Sat, 05 Feb 2005 06:51:32 -0700
Earl Eiland <[EMAIL PROTECTED]> wrote:

> I'm trying to process the IP packet length field, as recorded by pcap
> (Ethereal) and recovered using pcapy.  When I slice out those bytes, I
> get a value that shows in '\x00' format, rather than '0x00'.  Neither
> int() nor eval() are working.  How do I handle this?
> 
> Earl Eiland
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variable declaration

2005-02-05 Thread Nick Coghlan
Alex Martelli wrote:
'global' is an ugly wart, to all intents and purposes working "as if" it
was a declaration.  If I had to vote about the one worst formal defect
of Python, it would surely be 'global'.
Fortunately, it's reasonably easy to avoid the ugliness, by avoiding
rebinding (within functions) global variables, which tend to be easy. 
Hear, hear! And if I could write "gbls = namespace(globals())" in order to deal 
with those rare cases where I *do* need to rebind globals, all uses of the 
keyword could be handled nicely, while entirely eliminating the need for the 
keyword itself.

*ONLY* that tiny subset of such typos which happened on the left of a
plain '=' (since all others, happening on the RIGHT of an '=' or on the
left of an _augmented_ '=', were already caught), and ONLY regarding
barenames (such typos on any but the rightmost component of compound
names were already caught intrinsically, and catching those on the
rightmost component is trivially easier than introducing a {YEHH}
'vars' as you so stubbornly insist)...
Would you be as violently opposed to a 'rebinding' augmented assignment 
operator?
Since bare assignment statements essentially serve the purpose of variable 
declarations, I sometimes *would* like a way to say 'bind this existing name to 
something different'. A rebinding operation would provide a way to make that 
intention explicit, without cluttering the language with useless declarations.

In addition to detecting typos in local variable names, it would *also* address 
the problem of detecting typos in the right-most name in a compound name (e.g. 
making a habit of always using the rebinding operator when modifying member 
variables outside of __init__ would make it easier to avoid inadvertently 
creating a new instance variable instead of modifying an existing one)

With a rebinding operator available, the only typos left to slip through the net 
are those which match an existing visible name and those where the programmer 
has explicitly requested an unconditional name binding by using '=' and then 
made a typo on the left hand side.

Cheers,
Nick.
Did I mention the possible incidental benefit of reducing the whinging about the 
lack of variable declarations?

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >