Looking for suggestions on improving numpy code

2008-02-22 Thread David Lees
I am starting to use numpy and have written a hack for reading in a 
large data set that has 8 columns and millions of rows.  I want to read 
and process a single column.  I have written the very ugly hack below, 
but am sure there is a more efficient and pythonic way to do this.  The 
file is too big to read by brute force and select a column, so it is 
read in chunks and the column selected. Things I don't like in the code:
1. Performing a transpose on a large array
2. Uncertainty about numpy append efficiency

Is there a way to directly read every n'th element from the file into an 
array?

david


from numpy import *
from scipy.io.numpyio import fread

fd = open('testcase.bin', 'rb')
datatype = 'h'
byteswap = 0
M = 100
N = 8
size = M*N
shape = (M,N)
colNum = 2
sf =1.645278e-04*10
z=array([])
for i in xrange(50):
 data = fread(fd, size, datatype,datatype,byteswap)
 data = data.reshape(shape)
 data = data.transpose()
 z = append(z,data[colNum]*sf)

print z.mean()

fd.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: graphing/plotting with python and interface builder

2008-02-22 Thread Peter Wang
On Feb 22, 10:08 pm, Jacob Davis <[EMAIL PROTECTED]> wrote:
> Hi.
>
> I am developing for mac and using Xcode and Interface Builder 3.0.  I
> can make a simple application, but I am having a hard time trying to
> figure out a good way to create a graph orplotfor a class project.
>
> Does anybody have any tips on where to get started, or on how to do
> this?
>
> I have searched far for several days now, but I don't know if I am on
> the right track.  Any help is much appreciated.
>
> Thanks,
> Jake


Jake, are you using any python-specific GUI libraries like wxPython,
Qt, or Tk?  The Chaco plotting toolkit (http://code.enthought.com/
chaco)  supports several different mechanisms for rendering on OS X;
the default is to render via Quartz, but this requires wx or Qt.  I am
also currently working on a (somewhat experimental) pure OpenGL
backend, which doesn't require wxPython or Qt, just an active OpenGL
context.

Do you want to generate a fairly static plot, or do you want the user
to be able to interact with it in some way?


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


Re: global variables: to be or not to be

2008-02-22 Thread John Henry
On Feb 22, 9:20 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Fri, 22 Feb 2008 19:11:01 -0800 (PST), icarus <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
>
>
>
> > But how do I get around it? How do I update and access a variable
> > anytime I want? Any easy-to-follow examples? Thanks in advance.
>



>
> Oh... and you pass the instance, as a parameter, in/out of all other
> invoked methods that might need it. Thereby there is never a global
> reference to it.
>
> --

For instance:

###
class global:
   def __init__(self, x):
  self.x = x
  return

myGlobal = global(1.0)

def subA(top):
   print top.x

subA(myGlobal)
###

For me, the reason to avoid global variables is that it's much easier
down the road when I want to make my program ran in an multi-process,
or multi-threaded situation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: n00b with urllib2: How to make it handle cookie automatically?

2008-02-22 Thread 7stud
On Feb 21, 11:50 pm, est <[EMAIL PROTECTED]> wrote:
>
> class SmartRequest():
>

You should always define a class like this:

class SmartRequest(object):


unless you know of a specific reason not to.


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


Re: wxPython Plot always comes to focus on redraw

2008-02-22 Thread Jacob Davis
Thanks for the reply.

This is something that I tried.  The documentation for SetFocus() says  
that it is for keyboard input and that Show() is supposed to show the  
window.  I guess I was misusing the terminology.

I tried your suggestion with SetFocus(), Show() and Raise(), with no  
joy.  I think I will try the wxPython list.

Thanks

Jake


On Feb 21, 2008, at 4:36 AM, Steve Holden wrote:

> Jacob Davis wrote:
>> Hi.
>>
>> I am using wxPython, and I have a frame that has a notebook in it.
>> there are 3 pages in the notebook, 2 of which are Panels and 1 of
>> which is a PlotCanvas.  The data for the plot changes when I press a
>> button that is in the frame, but not in the notebook (as designed).
>> the button also makes the plot draw again, since the data has  
>> changed.
>>
>> The problem is that whenever I press my button, the focus of the
>> notebook "view area" (as opposed to the tabs) changes focus to the
>> plot.  if I have multiple plots, the focus goes to that plot which  
>> was
>> drawn last in the code. however, if I click anywhere on the panel,  
>> the
>> page of the notebook that is supposed to be in focus is now shown in
>> the "view area" of the notebook.
>>
>> will try to make a sample .py if anybody needs a visual, let me know
>> pls.
>>
>> Thanks for any help!
>>
>> Jake
>
> All wxWindow objects have a SetFocus() method. I am unsure why the
> system should be changing the focus (it's almost certainly something  
> you
> are asking it to do without realizing), but the easiest way to proceed
> if you can't stop the focus from changing (i.e. remove the problem) is
> to call SetFocus() on the widget that you want to have the focus after
> you've finished (i.e. cure the symptom).
>
> Or perhaps it's just that I haven't understood the problem correctly.
> You probably didn't post the code because it's large (which is good).
> The correct action is to pare the program down to the smallest one you
> can make that demonstrates the problem, then post that. often during
> this process it becomes clear what the problem is!
>
> It may be that your widget relationships aren't as clean as they might
> be: did you produce your layout with an automated tool like wxDesigner
> or BoaConstrictor, or did you hack it together by hand?
>
> regards
>  Steve
> -- 
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Equivalent of system()?

2008-02-22 Thread Max
Thanks for the help!

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


Re: wxPython Plot always comes to focus on redraw

2008-02-22 Thread Jacob Davis
Thanks for the reply.

The parent is a notebook. (the Plotcanvas is a page in the notebook)   
I tried to make the page of the notebook a panel, and then make the  
PlotCanvas a child of this panel, but the Plot would not show up when  
drawn.

I may have time to make a mini program to duplicate the error in the  
future...


On Feb 21, 2008, at 6:14 AM, Mike Driscoll wrote:

> On Feb 21, 2:57 am, Jacob Davis <[EMAIL PROTECTED]> wrote:
>> Hi.
>>
>> I am using wxPython, and I have a frame that has a notebook in it.
>> there are 3 pages in the notebook, 2 of which are Panels and 1 of
>> which is a PlotCanvas.  The data for the plot changes when I press a
>> button that is in the frame, but not in the notebook (as designed).
>> the button also makes the plot draw again, since the data has  
>> changed.
>>
>> The problem is that whenever I press my button, the focus of the
>> notebook "view area" (as opposed to the tabs) changes focus to the
>> plot.  if I have multiple plots, the focus goes to that plot which  
>> was
>> drawn last in the code. however, if I click anywhere on the panel,  
>> the
>> page of the notebook that is supposed to be in focus is now shown in
>> the "view area" of the notebook.
>>
>> will try to make a sample .py if anybody needs a visual, let me know
>> pls.
>>
>> Thanks for any help!
>>
>> Jake
>
> Just a show in the dark, but is the plotcanvas object's parent a frame
> or a panel? If it's the frame, than that's probably what the problem
> is.
>
> You may want to post to the wxPython list where you'll get wx geeks to
> answer your question and you can learn a lot from them too. Here's a
> link where you can sign up: http://wxpython.org/maillist.php
>
> Mike
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Equivalent of system()?

2008-02-22 Thread Jeff Schwab
Max wrote:
> Is there a Python equivalent of C++'s system()?

More or less.  You probably want subprocess.Popen:

 >>> import subprocess
 >>> subprocess.Popen("echo hello", shell=True)
hello


http://docs.python.org/lib/node533.html#CHILD_LINKS
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Equivalent of system()?

2008-02-22 Thread Grant Edwards
On 2008-02-23, Max <[EMAIL PROTECTED]> wrote:

> Is there a Python equivalent of C++'s system()?

The closest thing is probably system().

It's in the os module.

-- 
Grant

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


Equivalent of system()?

2008-02-22 Thread Max
Is there a Python equivalent of C++'s system()?

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


Re: global variables: to be or not to be

2008-02-22 Thread subeen
Another way to avoid using global variables is to return more than one
values from the function.

Here is an example that may help you to understand it:

def foo(a, b, c):
a += c
b += c
return a, b

a = 5
b = 10
c = 2
print a, b
a, b = foo(a, b, c)
print a, b


regards,
Subeen.
http://love-python.blogspot.com/


On Feb 23, 9:11 am, icarus <[EMAIL PROTECTED]> wrote:
> I've read 'global variables' are bad.  The ones that are defined as
> 'global' inside a function/method.
>
> The argument that pops up every now and then is that they are hard to
> keep track of.  I don't know Python well enough to argue with that.
> Just started learning it a few days ago, so I won't get into
> philosophical questions such as "why this? Why not that?".  I'll take
> it as it is, just like I take 1 + 1 = 2 for granted.
>
> So..."global variables taste bad.  Avoid them."
>
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.

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


graphing/plotting with python and interface builder

2008-02-22 Thread Jacob Davis
Hi.

I am developing for mac and using Xcode and Interface Builder 3.0.  I  
can make a simple application, but I am having a hard time trying to  
figure out a good way to create a graph or plot for a class project.

Does anybody have any tips on where to get started, or on how to do  
this?

I have searched far for several days now, but I don't know if I am on  
the right track.  Any help is much appreciated.

Thanks,

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


Re: Return value of an assignment statement?

2008-02-22 Thread George Sakkis
On Feb 22, 1:16 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:

> On Fri, 22 Feb 2008 08:19:07 -0800, Carl Banks wrote:
> > (The perl example wasn't using an assignment operator.)
>
> Hmmm... I see. Obviously I didn't pretend to understand Perl well enough.
>
> (I assume you're ignoring the assignments $name = chop(\1) etc. Fair
> enough.)
>
> [...]
>
> > I can't help but to think that a lot of people's distaste for this
> > natural way to write certain logic is simply defensiveness about one
> > minor little thing that Python doesn't support (without workarounds).
>
> But Python certainly does support set-and-test. You just have
> to separate the set from the test with a newline:

A single "set-and-test" operation is not equivalent to two consecutive
operations, "set" and "test".

> m = re.match(r"name=(.*)",line)  # set
> if m:  # test
> name = m.group(1).strip()

For a single set-and-test the inconvenience is minimal, but stack a
bunch of them together (even more if there are 'else' clauses in the
mix) and the syntactic inefficiency becomes quite visible.

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


Re: global variables: to be or not to be

2008-02-22 Thread Matt Nordhoff
icarus wrote:
> I've read 'global variables' are bad.  The ones that are defined as
> 'global' inside a function/method.
> 
> The argument that pops up every now and then is that they are hard to
> keep track of.  I don't know Python well enough to argue with that.
> Just started learning it a few days ago, so I won't get into
> philosophical questions such as "why this? Why not that?".  I'll take
> it as it is, just like I take 1 + 1 = 2 for granted.
> 
> So..."global variables taste bad.  Avoid them."
> 
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.

If doing everything inside one function doesn't work, you should use a
class to store state.

Here's a made-up example. Say you create a Python module to make HTTP
request. It stores the URL of the current one in a global variable. But
then anyone who uses your module can only work with one request at a
time. Instead, you should use an object called "HTTPResponse" or
something with an "url" attribute.

I don't know about other languages, but in Python, classes are very easy
to work with, so it should be no big deal. (Well, you may want to change
if your object evaluates to True, or if it's greater or smaller than 20,
or what happens when someone calls str() on it . . .  But you usually
shouldn't need to bother with all that.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread George Sakkis
On Feb 22, 2:15 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:

> Nicola Musatti wrote:
> > The real sad thing is that nobody is likely to convince Guido to turn
> > CPython into C++Python ;-)
>
> How difficult would that be?  Could it be done in stages?  I would be
> willing to spend some time on that kind of project.

Yeah right.. what we need is yet another implementation of Python. At
least Jython/IronPython/Pypy (and Pyrex, Cython, Shedskin, etc.) had a
better motivation than "my language is better than yours". I am sure
your time, skills and experience would be much appreciated in more
useful projects.

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


global variables: to be or not to be

2008-02-22 Thread icarus
I've read 'global variables' are bad.  The ones that are defined as
'global' inside a function/method.

The argument that pops up every now and then is that they are hard to
keep track of.  I don't know Python well enough to argue with that.
Just started learning it a few days ago, so I won't get into
philosophical questions such as "why this? Why not that?".  I'll take
it as it is, just like I take 1 + 1 = 2 for granted.

So..."global variables taste bad.  Avoid them."

But how do I get around it? How do I update and access a variable
anytime I want? Any easy-to-follow examples? Thanks in advance.








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


Re: Return value of an assignment statement?

2008-02-22 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes:
> Personally, I think the confusion of augmented assignments is not worth 
> the benefit of saving typing a couple of characters. I think Guido's 
> first decision, to leave += etc out of the language, was the right 
> decision.

It quite helpful to be able to say

  foo['bar'+lookup(baz)][blob(a)+frob(b)] += 1

without having to split it into separate statements to avoid repeating
the function calls and their possible side effects.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why must implementing Python be hard unlike Scheme?

2008-02-22 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> Certainly, "(almost) everything is an object" is a good start.  Are
> there any other axiom like statements one can hang their hat on when
> trying to wrap their brain around Python's architecture?

As John Nagle put it, the data store is a tree of hashes.  Python
objects are closures whose method calls look up internal functions
from a hash table.  Python iterators are closures that call a function
repeatedly, yielding values.  Python generators are closures that
package a (continuation, value) pair at each yield statement, with an
iterator that gives the value at each iteration to the caller, and
calls the continuation.  This doesn't use the full power of Scheme
continuations, but is the main Python feature that isn't
straightforward to code in an imperative language with no coroutines.
Aside from that, the ground types are a little different from Scheme's
(strings are immutable, there are no ratios, lists are implemented as
self-extending vectors, there is a dictionary type that is at the
center of practically everything) but it is all pretty
straightforward.  

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


Re: Globals or objects?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 18:53:54 +, tinnews wrote:

>> > But you're not comparing what the OP posted.  He was comparing a
>> > global with an object with a single variable inside it.  Either would
>> > work with the y = spam(arg) example above.
>> 
>> What do you mean by "an object with a single variable inside it"? I
>> don't understand what that is supposed to mean, or why you think it is
>> the same as a global. Do you mean a Singleton?
>> 
>> If so, then the answer is simple: using a Singleton argument instead of
>> a global is better, because with a global you are stuck to always using
>> the global (at least until you can re-write the code), but with the
>> Singleton argument, you may be enlightened and *not* use a Singleton.
>> 
> But if you stop using the Singleton the code no longer does the same as
> it would with a global does it?

That's a *good* thing, not a problem. The whole idea is to get away from 
the bad behaviour of globals, not find some other way to implement it.



-- 
Steven



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


Re: Return value of an assignment statement?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 11:00:17 -0800, Aahz wrote:

> In article <[EMAIL PROTECTED]>, Bruno Desthuilliers 
> <[EMAIL PROTECTED]> wrote:
>>
>>FWIW, it's IMHO a real wart - given Python's pretention at readability -
>>that augmented assignement has been implemented that way for lists.
> 
> This was debated extensively when augmented assignment was created, and
> it was decided that practicality beat purity.  It's just too convenient
> to be able to write
> 
> L += ['foo']
> 
> without rebinding L.


*scratches head*

Wouldn't L.append('foo') or L.extend(['foo']) be even more convenient, 
and have perfectly obvious behaviour without leading to the confusion of 
augmented assignments?

Personally, I think the confusion of augmented assignments is not worth 
the benefit of saving typing a couple of characters. I think Guido's 
first decision, to leave += etc out of the language, was the right 
decision.



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


Re: Why must implementing Python be hard unlike Scheme?

2008-02-22 Thread Robert Brown
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> I'm learning Scheme and I am amazed how easy it is to start building a
> half baked Scheme implementation that somewhat works.
>
> After knowing Python for *years* I have no idea how to actually
> implement the darn thing.

Since you know Scheme, perhaps the CLPython implementation, which is written
in Common Lisp, will be interesting to you:

http://common-lisp.net/project/clpython/

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


Re: Professional Grant Proposal Writing Workshop (April 2008: Vancouver, British Columbia)

2008-02-22 Thread Michael L Torrie
Anthony Jones wrote:
> The Grant Institute's Grants 101: Professional Grant Proposal Writing 
> Workshop 
> will be held in Vancouver, British Columbia, April 14 - 16, 2008. Interested 
> development professionals, researchers, faculty, and graduate students should 
> register as soon as possible, as demand means that seats will fill up 
> quickly. 
> Please forward, post, and distribute this e-mail to your colleagues and 
> listservs.

Not sure what this has to do with Python programming.  Can someone
please unsubscribe the list from these mailings?  Or if Anthony Jones is
reading this, unsubscribe python-list@python.org please!

thanks.

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


Re: Order in which modules are imported

2008-02-22 Thread Dave Hansen
On Feb 22, 5:21 pm, [EMAIL PROTECTED] wrote:
> I imported two modules (random and matplotlib), and found that the
> functions available to me from the random module depended on the order
> in which the imports occured. In particular, if I import random first,

[...]
>
> >>> import random
> >>> from pylab import *

Change this --^ lint to "import pylab" and see what happens.  Of
course, that may force other changes in your script...

And remember that "from  import *" is never your friend.

Regards,

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


Re: Order in which modules are imported

2008-02-22 Thread Roy Smith
In article 
<[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> I imported two modules (random and matplotlib), and found that the
> functions available to me from the random module depended on the order
> in which the imports occured. In particular, if I import random first,
> none of its functions seem to be available, while if I import it after
> matplotlib, I seem to have access to all of its functions. What could
> the problem be?
> 
> Thomas Philips
> 
> 
> >>> import random
> >>> from pylab import *
> >>> x = random.uniform(0,1)
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> x = random.uniform(0,1)
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'uniform'
> >>> help(random)
> Help on built-in function random_sample:

I'm guessing the module pylab has a symbol in it named "random", which is 
over-writing the name "random" you created with the first import.

Try changing it to:

import random
import pylab

and then referring to the things in pylab by their qualified 
(pylab.whatever) names.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order in which modules are imported

2008-02-22 Thread Tim Chase
 import random
 from pylab import *
 x = random.uniform(0,1)
> 
> Traceback (most recent call last):


I suspect that

 >>> 'random' in dir(pylab)

returns True...this would be one of those reasons that "from
 import *" is scowled upon.  You have to know what
 is dumping into your namespace, or otherwise, it will
likely tromp atop other things you have defined before.

Looking at the source of pylab.py, I see

 from numpy.random import *

and numpy.random has a "random" function in it.  This waltzes
atop your random module.  A modern fandango-on-core...perhaps
"fandango on namespace".

The other alternative, if you must do "from pylab import *",
would be to import the system "random" module under another name:

  import random as pyrandom
  from pylab import *
  ...
  x = pyrandom.uniform(0,1)

-tkc



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


Re: Web Development Project

2008-02-22 Thread Jonathan Gardner
On Feb 21, 3:34 pm, john_sm3853 <[EMAIL PROTECTED]> wrote:
> Hey guys, I am interested in knowing, what new Web Development projects you
> are doing, curious to know, what data base you use, and if you are using
> Linux or Windows platform.  Also, will like to know, if there are any
> alternatives to Adobe products, that you may be using

Linux (Red Hat Fedora)
Apache (for production)
Pylons (with Mako, SQLAlchemy)
PostgreSQL

I don't do image work, however. If I really need to, I will use Gimp.
For vector editing, I have played with some of the open source
alternatives and found them satisfying.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web Development Project

2008-02-22 Thread Berco Beute
linux
lighttpd
fastcgi
django
sqlite3

...rocking combo :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Order in which modules are imported

2008-02-22 Thread tkpmep
I imported two modules (random and matplotlib), and found that the
functions available to me from the random module depended on the order
in which the imports occured. In particular, if I import random first,
none of its functions seem to be available, while if I import it after
matplotlib, I seem to have access to all of its functions. What could
the problem be?

Thomas Philips


>>> import random
>>> from pylab import *
>>> x = random.uniform(0,1)

Traceback (most recent call last):
  File "", line 1, in 
x = random.uniform(0,1)
AttributeError: 'builtin_function_or_method' object has no attribute
'uniform'
>>> help(random)
Help on built-in function random_sample:

random_sample(...)
Return random floats in the half-open interval [0.0, 1.0).

random_sample(size=None) -> random values


In sharp contrast, I get what I expect when I reverse the order of the
imports.
>>> from pylab import *
>>> import random
>>> random.uniform(0,1)
0.75262941795069283
>>> help(random)
Help on module random:

NAME
random - Random variable generators.

FILE
c:\python25\lib\random.py
.
.
.




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


Re: ANN: NUCULAR B3 Full text indexing (now on Win32 too)

2008-02-22 Thread Paul Rubin
Aaron Watters <[EMAIL PROTECTED]> writes:
> [apologies to the list: I would have done this offline,
> but I can't figure out Paul's email address.]
> 
> 1) Paul please forward your email address

Will send it privately.  I don't have a public email address any more
(death to spam!!!).  My general purpose online contact point is
http://paulrubin.com which currently has an expired certificate that
I'll get around to renewing someday.  Meanwhile you have to click
"accept" to connect using the expired cert.

> 3) Since you seem to know about these things: I was thinking
> of adding an optional feature to Nucular which would allow
> a look-up like "given a word find all attributes that contain
> that word anywhere and give a count of the number of times it
> is found in that attribute as well as the entry id for an example
> instance (arbitrarily chosen).  I was thinking about calling
> this "inverted faceting", but you probably know a
> better/standard name, yes?  What is it please?  Thanks!
> Answers from anyone else welcomed also.

In Solr this is called the DisMax (disjunction maximum) handler, I
think.  I tried it and it doesn't work very well, and ended up using a
script written by a co-worker, that expands such queries to more
complex queries that put user-supplied weights on each field.  It is a
somewhat messy problem.  Otis Gospodnetic's book "Lucene in Action"
talks about it some, I believe.  Manning and Schutz are working on a
new book at http://informationretrieval.org that discusses fancier
methods.  I think these are worth looking into, but I haven't had the
bandwidth to spend time on it so far.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul McGuire <[EMAIL PROTECTED]> writes:
> I'm still getting used to 'any' and 'all' as new Python built-ins -
> but they'll do the short-circuiting as well as a for-loop-with-break.
> But I think a set- or dict-based solution will still surpass a list-
> based one for the OP.

I guess I don't understand sufficiently well what the OP is trying to
do.  If it's just a matter of checking for and then appending one
record, then using "any" looks like the most straightforward.  If the
idea is to collect one representative of each value of element[0], and
if the value is hashable, then yes, a set or dict is the best way to
do it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: n00b with urllib2: How to make it handle cookie automatically?

2008-02-22 Thread 7stud
On Feb 21, 11:50 pm, est <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I need urllib2 do perform series of HTTP requests with cookie from
> PREVIOUS request(like our browsers usually do ).
>

Cookies from a previous request made in the currently running
program?  Or cookies from requests that were made when you previously
ran the program?

>
> from cookielib import CookieJar
> class SmartRequest():
>     cj=CookieJar()
>     def __init__(self, strUrl, strContent=None):
>         self.Request    =   urllib2.Request(strUrl, strContent)
>         self.cj.add_cookie_header(self.Request)
>         self.Response   =   urllib2.urlopen(Request)
>         self.cj.extract_cookies(self.Response, self.Request)
>     def url
>     def read(self, intCount):
>         return self.Response.read(intCount)
>     def headers(self, strHeaderName):
>         return self.Response.headers[strHeaderName]
>
> The code does not work because each time SmartRequest is initiated,
> object 'cj' is cleared. How to avoid that?
> The only stupid solution I figured out is use a global CookieJar
> object. Is there anyway that could handle all this INSIDE the class?
>

Examine this code and its output:

class SmartRequest(object):
def __init__(self, id):
if not getattr(SmartRequest, 'cj', None):
SmartRequest.cj = "I'm a cookie jar. Created by request:
%s" % id


r1 = SmartRequest(1)
r2 = SmartRequest(2)

print r1.cj
print r2.cj

--output:--
I'm a cookie jar. Created by request: 1
I'm a cookie jar. Created by request: 1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 3:38 pm, Paul Rubin  wrote:
> Paul McGuire <[EMAIL PROTECTED]> writes:
> > I think you have this backwards.  Should be:
>
> >      if not any(x[0]==element[0] for x in a):
> >         a.append(element)
>
> I think you are right, it was too early for me to be reading code when
> I posted that ;-)

I'm still getting used to 'any' and 'all' as new Python built-ins -
but they'll do the short-circuiting as well as a for-loop-with-break.
But I think a set- or dict-based solution will still surpass a list-
based one for the OP.

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


Re: [ANN] Python 2.5.2 released

2008-02-22 Thread Ben Finney
"Martin v. Löwis" <[EMAIL PROTECTED]> writes:

> On behalf of the Python development team and the Python community, I'm
> happy to announce the release of Python 2.5.2 (FINAL).

Hooray! Thanks Martin, and the entire Python-dev team.

-- 
 \"The best mind-altering drug is truth."  -- Jane Wagner, via |
  `\   Lily Tomlin |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul McGuire <[EMAIL PROTECTED]> writes:
> I think you have this backwards.  Should be:
> 
>  if not any(x[0]==element[0] for x in a):
> a.append(element)

I think you are right, it was too early for me to be reading code when
I posted that ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: n00b with urllib2: How to make it handle cookie automatically?

2008-02-22 Thread 7stud
On Feb 21, 11:50 pm, est <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I need urllib2 do perform series of HTTP requests with cookie from
> PREVIOUS request(like our browsers usually do ). Many people suggest I
> use some library(e.g. pycURL) instead but I guess it's good practise
> for a python beginner to DIY something rather than use existing tools.
>
> So my problem is how to expand the urllib2 class
>
> from cookielib import CookieJar
> class SmartRequest():
>     cj=CookieJar()
>     def __init__(self, strUrl, strContent=None):
>         self.Request    =   urllib2.Request(strUrl, strContent)
>         self.cj.add_cookie_header(self.Request)
>         self.Response   =   urllib2.urlopen(Request)
>         self.cj.extract_cookies(self.Response, self.Request)
>     def url
>     def read(self, intCount):
>         return self.Response.read(intCount)
>     def headers(self, strHeaderName):
>         return self.Response.headers[strHeaderName]
>
> The code does not work because each time SmartRequest is initiated,
> object 'cj' is cleared.

That's because every time you create a SmartRequest, this line
executes:

cj=CookieJar()

That creates a new, *empty* cookie jar, i.e. it has no knowledge of
any previously set cookies.

> How to avoid that?

If you read the docs on the cookielib module, and in particular
CookieJar objects, you will notice that CookieJar objects are
described in a section that is titled:  CookieJar and FileCookieJar
Objects.

Hmm...I wonder what the difference is between a CookieJar object and a
FileCookieJar Object?

--
FileCookieJar implements the following additional methods:

save(filename=None, ignore_discard=False, ignore_expires=False)
Save cookies to a file.

load(filename=None, ignore_discard=False, ignore_expires=False)
Load cookies from a file.


That seems promising.





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


Re: Pydev, Eclipse

2008-02-22 Thread Boris Ozegovic
Preston Landers wrote:

> Shift-Tab does it for me.  It can also dedent whole blocks if you have
> them selected.

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


[ANN] Python 2.5.2 released

2008-02-22 Thread Martin v. Löwis
On behalf of the Python development team and the Python community, I'm
happy to announce the release of Python 2.5.2 (FINAL).

This is the second bugfix release of Python 2.5. Python 2.5 is now in
bugfix-only mode; no new features are being added. According to the
release notes, over 100 bugs and patches have been addressed since
Python 2.5.1, many of them improving the stability of the interpreter,
and improving its portability.

This is a production release of Python, and should be a painless
upgrade from 2.5.1 or 2.5. Since the release candidate, we have backed
out test cases that were incorrect on 64-bit systems, and fixed
another stability problem. See the release notes for more.

For more information on Python 2.5.2, including download links for
various platforms, release notes, and known issues, please see:

   http://www.python.org/2.5.2/

Highlights of this new release include:

   Bug fixes. According to the release notes, at least 100 have been fixed.

Highlights of the previous major Python release (2.5) are available
from the Python 2.5 page, at

   http://www.python.org/2.5/highlights.html

Enjoy this release,
Martin

Martin v. Loewis
[EMAIL PROTECTED]
Python Release Manager
(on behalf of the entire python-dev team)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there any python lib for NAT transversal?

2008-02-22 Thread VanL
Jean-Paul Calderone wrote:
> Divmod Vertex is such a library (it does a few other things as well), but
> it is not nearly complete and has little documentation.

Can you comment on the differences and similarities between Q2Q and 
Jingle? They appear to be targeting similar problems.

Thanks,

Van

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


Re: Pydev, Eclipse

2008-02-22 Thread Preston Landers
Boris Ozegovic([EMAIL PROTECTED])@2008.02.22 19:59:28 +0100:
> Hi
> 
> Suppose I have three blocks:
> if 1:
> if 2:
> if 3:
> # here I want my cursor go back to second block (if 2:)
> 
> What is the standard shortcut for this?  ctrl+arrow keys aren't, arrow keys
> alone aren't either.

Shift-Tab does it for me.  It can also dedent whole blocks if you have
them selected.

cheers,
Preston

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


Re: advanced usage of python threads

2008-02-22 Thread Chris Mellon
On Fri, Feb 22, 2008 at 12:32 PM, hyperboreean
<[EMAIL PROTECTED]> wrote:
> Well, I will be writing the application server of a three-tier
>  architecture system. I will be using Twisted for the communication with
>  the client but from there I have to make several calls to a database and
>  this asks threading. The tables will be filled by another system that
>  gathers some data. I want to implement several threads (workers) that
>  takes care of each client and a coordinator thread that takes care of
>  all the threads. But I have never worked with threads at this level and
>  I feel like I need some coordination. I will be thankful with
>  documentation on advanced threads, I probably can apply that in practice.
>
>  Thanks to all.


Twisted has an asynchronous database API (built on top of threads)
already. Look at twisted.enterprise.adbapi.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to flush child_stdin

2008-02-22 Thread [EMAIL PROTECTED]
On Feb 22, 2:01 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "[EMAIL PROTECTED]"
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>
> > I don't think that is the problem, I'm feeding it newline characters.
>
> It wasn't shown in your sample, so I jumped on the first likely
> thing...
>
> The second is in the hands of the subprocess... While you are
> flushing output /to/ the subprocess, is IT flushing its output (the
> stuff you are trying to read). A common problem seems to be that, as
> soon as the process detects a pipe, it goes to buffered I/O, and if the
> buffer isn't filled, the parent has no access...
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

I'm actually running something like : r, w, e = popen2.popen3('python -
u slave.py')
to try and force unbuffered.  slave.py is basically outputting by
using print.
I guess it might still be buffering?
Anyway, thanks for your thoughts... I may have to take an entirely
difference approach.  I was hoping not to have to touch the code base
represented by slave.py.
-- 
http://mail.python.org/mailman/listinfo/python-list


ILeo (IPython-Leo bridge); a marriage made in heaven?

2008-02-22 Thread Ville Vainio
Here is something cool that will rock your world (ok, excuse the
slight hyperbole):

Introduction


The purpose of ILeo, or leo-ipython bridge, is being a two-way
communication
channel between Leo and IPython. The level of integration is much
deeper than
conventional integration in IDEs; most notably, you are able to store
*data* in
Leo nodes, in addition to mere program code. The possibilities of this
are
endless, and this degree of integration has not been seen previously
in the python
world.

IPython users are accustomed to using things like %edit to produce non-
trivial
functions/classes (i.e. something that they don't want to enter
directly on the
interactive prompt, but creating a proper script/module involves too
much
overhead). In ILeo, this task consists just going to the Leo window,
creating a node
and writing the code there, and pressing alt+I (push-to-ipython).

Obviously, you can save the Leo document as usual - this is a great
advantage
of ILeo over using %edit, you can save your experimental scripts all
at one
time, without having to organize them into script/module files (before
you
really want to, of course!)


Installation


You need at least Leo 4.4.7, and the development version of IPython
(ILeo
will be incorporated to IPython 0.8.3).

You can get IPython from Launchpad by installing bzr and doing

bzr branch lp:ipython

and running "setup.py install".

You need to enable the 'ipython.py' plugin in Leo:

- Help -> Open LeoSettings.leo

- Edit @settings-->Plugins-->@enabled-plugins, add/uncomment
'ipython.py'

- Restart Leo. Be sure that you have the console window open (start
leo.py from console, or double-click leo.py on windows)

- Press alt+5 OR alt-x start-ipython to launch IPython in the console
that
started leo. You can start entering IPython commands normally, and Leo
will keep
running at the same time.

Accessing IPython from Leo
==

IPython code


Just enter IPython commands on a Leo node and press alt-I to execute
push-to-ipython to execute the script in IPython. 'commands' is
interpreted
loosely here - you can enter function and class definitions, in
addition to the
things you would usually enter at IPython prompt - calculations,
system commands etc.

Everything that would be legal to enter on IPython prompt is legal to
execute
from ILeo.

Results will be shows in Leo log window for convenience, in addition
to the console.

Suppose that a node had the following contents:
{{{
1+2
print "hello"
3+4

def f(x):
return x.upper()

f('hello world')
}}}

If you press alt+I on that done, you will see the following in Leo log
window (IPython tab):

{{{
In: 1+2
<2> 3
In: 3+4
<4> 7
In: f('hello world')
<6> 'HELLO WORLD'
}}}

(numbers like <6> mean IPython output history indices).


Plain Python code
-

If the headline of the node ends with capital P, alt-I will not run
the code
through IPython translation mechanism but use the direct python 'exec'
statement
(in IPython user namespace) to execute the code. It wont be shown in
IPython
history, and sometimes it is safer (and more efficient) to execute
things as
plain Python statements. Large class definitions are good candidates
for P
nodes.

Accessing Leo nodes from IPython


The real fun starts when you start entering text to leo nodes, and are
using
that as data (input/output) for your IPython work.

Accessing Leo nodes happens through the variable 'wb' (short for
"WorkBook")
that exist in the IPython user namespace. Nodes that are directly
accessible are
the ones that have simple names which could also be Python variable
names;
'foo_1' will be accessible directly from IPython, whereas 'my scripts'
will not.
If you want to access a node with arbitrary headline, add a child node
'@a foo'
(@a stands for 'anchor'). Then, the parent of '@a foo' is accessible
through
'wb.foo'.

You can see what nodes are accessible be entering (in IPython)
wb.. Example:

[C:leo/src]|12> wb.
wb.b   wb.tempfilewb.rfile   wb.NewHeadline
wb.bar wb.Docswb.strlist wb.csvr

Suppose that we had a node with headline 'spam' and body:

['12',+32]

we can access it from IPython (or from scripts entered into other Leo
nodes!) by doing:

C:leo/src]|19> wb.spam.v
  <19> ['12', 2254]

'v' attribute stands for 'value', which means the node contents will
be run
through 'eval' and everything you would be able to enter into IPython
prompt
will be converted to objects. This mechanism can be extended far
beyond direct
evaluation (see '@cl definitions').

'v' attribute also has a setter, i.e. you can do:

wb.spam.v = "mystring"

Which will result in the node 'spam' having the following text:

'mystring'

What assignment to 'v' does can be configured through generic
functions
(simplegeneric module, will be explained later).

Besides v, you can set the body text directly through wb.spam.b =
"some\nstring", headline by wb.spam.

Re: Return value of an assignment statement?

2008-02-22 Thread Jeff Schwab
George Sakkis wrote:
> On Feb 22, 12:26 am, Jeff Schwab <[EMAIL PROTECTED]> wrote:
> 
>>> On the other hand, "a = b" does always the same thing; unlike C++, '='
>>> is not an operator and therefore it cannot be overriden by the class
>>> of 'a'.
>> "Not an operator?"  Then what is it?
> 
> In this context, it's just the token used for the assignment
> statement. In short, if 'a' is an identifier, the statement means
> "bind the name 'a' to the object 'b' (in the local or global
> namespace)". It doesn't say anything about memory allocation,
> initialization or copying. The only case where assigning an identifier
> affects memory is the following [1]:
> 
> """
> The name is rebound if it was already bound. This may cause the
> reference count for the object previously bound to the name to reach
> zero, causing the object to be deallocated and its destructor (if it
> has one) to be called.
> """
> 
> [1] http://docs.python.org/ref/assignment.html

OK, thanks for trying to make it clear.

I'm about through with this discussion, but FWIW, this is a real gotcha 
for me and many others.  This is a case where Python does not do what 
many programmers expect, and it at least takes some getting used-to.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Jeff Schwab
Carl Banks wrote:
> On Feb 22, 12:23 am, Jeff Schwab <[EMAIL PROTECTED]> wrote:
>> Carl Banks wrote:
>>> On Feb 21, 7:17 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:
 Carl Banks wrote:
> On Feb 21, 1:22 pm, Nicola Musatti <[EMAIL PROTECTED]> wrote:
>> There are other downsides to garbage collection, as the fact that it
>> makes it harder to implement the Resource Acquisition Is
>> Initialization idiom, due to the lack of deterministic destruction.
> That's not a downside: it's at least a wash.
> In C++ you manage memory and the language manages resourcewithout bringing
>> anything of particular value to the table.s.  In
> Python you manage resources and the language manages memory.
> RAII is merely one way of minimizing complexity.  Garbage collection
> is another way.
 If you've already got a generic, language-supported way to manage
 resources (like RAII with deterministic destruction), then why bother
 with garbage collection?
>>> Because now you have to manage memory?  Did you read my post?  You
>>> have to manage one thing or the other.
>> Yes, I read your post.  You seem to be saying there's some kind of
>> trade-off between automatic management of dynamically allocated memory,
>> and automated management of other kinds of resources.  I don't
>> understand why you believe that, so I asked.
>>
>> If you have proper RAII and deterministic destruction, the management is
>> of resources is consistent, and mostly automated.
> 
> If you have garbage collection, the management of memory is
> consistent, and mostly automated.
> 
>> Managing memory is
>> just not that difficult,
> 
> Managing resources is just not that difficult,
> 
>> especially if the vast majority of objects are
>> allocated on the stack or in static memory.
> 
> Especially if the there are fewer resources to manage than there would
> have been heap objects
> 
>>  A special language feature
>> for managing dynamically allocated memory robs the programmer of a
>> reliable way to clean up resources automatically,
> 
> A special language feature more managing dynamically allocated robs
> the programmer of a reliable way to free memory automatically,
> 
>> without bringing
>> anything of particular value to the table.
> 
> without bringing
> anything of particular value to the table.
> 
> It cuts both ways, chief.
> 
> You like managing your own memory, be my guest.  But please don't
> imply that you're putting forth less effort because of it.  You're
> just putting forth different effort.

I disagree with you completely.  Your points don't make any sense to me 
at all.  I believe I am putting forth less effort by having a generic 
resource-management infrastructure, rather than a memory-specific 
language feature -- that's not just an implication, it's my honest belief.

But I guess we'll agree to disagree.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Jeff Schwab
Nicola Musatti wrote:

> The real sad thing is that nobody is likely to convince Guido to turn
> CPython into C++Python ;-)

How difficult would that be?  Could it be done in stages?  I would be 
willing to spend some time on that kind of project.  Since I know almost 
nothing about Python internals, though, I'd appreciate it if a 
C++-fluent Python expert could give an estimate in person-months.  Also, 
what would be the general break-down?  Maybe:

(1) Prepare a build environment appropriate for Python, supporting code 
in both C and C++.  Include unit-test targets, and a mechanism for 
module developers to add unit tests to those targets.

(2) Get all the headers C++-clean.

(3) Begin translating one module at a time.  Different people could work 
on different modules, and add their test-cases to the global target.

One potential problem would be linkage.  Would Python-internal C++ 
modules still have to provide C-linkable APIs, so that they could be 
invoked from other parts of Python?  A breakdown of module dependencies 
would help address this issue, so that it would be clear which parts of 
the code-base would be directly affected by translating a given module 
to C++.

At the external API level, would it still be important to support 
C-style linkage, even if the implementation code isn't written in C?  I 
don't know whether it's realistic for people embedding Python in non-C++ 
applications to have to work directly with C++ APIs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter Menu Item Activation

2008-02-22 Thread MartinRinehart


Rob Wolfe wrote:
> But I think that you should read this:
> http://effbot.org/zone/vroom.htm

Rob, may the gods shower you with gold coins!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return value of an assignment statement?

2008-02-22 Thread Aahz
In article <[EMAIL PROTECTED]>,
Jeff Schwab  <[EMAIL PROTECTED]> wrote:
>Aahz wrote:
>> In article <[EMAIL PROTECTED]>,
>> Jeff Schwab  <[EMAIL PROTECTED]> wrote:
>>> [EMAIL PROTECTED] wrote:

 There's nothing like a variable "storing" anything in Python. All you
 have are names to (references to) objects binding in a namespace. Now
 the fact is that some types are mutable and other are not. In your
 above example, the augmented assignment does *not* rebind a, but
 invoke a.extend(). With integers, it would have rebind a. So while
 your observation is exact, your interpretation is wrong !-)
>>> Thank you for the clarification.  For some reason, I had it in my head 
>>> that ints were packed directly into the C structures that represent 
>>> Python variables, in the same (union?) member that otherwise would store 
>>> a pointer.
>> 
>> Notice very very carefully that Bruno is not using "variable".  Many
>> expert Python programmers strongly prefer to talk about "names" instead
>> of "variables" (especially when explaining the Python object model)
>> precisely because using "variable" leads to incorrect expectations.
>> 
>> http://starship.python.net/crew/mwh/hacks/objectthink.html
>
>So what is the "variable?"  Or is Python the first HLL I've ever heard 
>of that didn't have variables?

Whether Python has "variables" depends on your perspective.  Python
certainly does *not* have variables with anything like the semantics of
C/C++ variables.  For that reason, it's often convenient to shift the
vocabulary to avoid misunderstading.  However, the vast majority of
Python programmers do use "variable" in casual conversation (I certainly
do); it's only when trying to discuss the Python object model that
there's a strong tendency to switch to using "names".
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of 
indirection."  --Butler Lampson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 12:54 pm, Paul Rubin  wrote:
> Paul Rubin  writes:
> >     if any(x==element[0] for x in a):
> >       a.append(element)
>
> Should say:
>
>      if any(x[0]==element[0] for x in a):
>         a.append(element)

I think you have this backwards.  Should be:

 if not any(x[0]==element[0] for x in a):
a.append(element)

or

 if all(x[0]!=element[0] for x in a):
a.append(element)

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


Re: Return value of an assignment statement?

2008-02-22 Thread Aahz
In article <[EMAIL PROTECTED]>,
Bruno Desthuilliers  <[EMAIL PROTECTED]> wrote:
>
>FWIW, it's IMHO a real wart - given Python's pretention at readability - 
>that augmented assignement has been implemented that way for lists.

This was debated extensively when augmented assignment was created, and
it was decided that practicality beat purity.  It's just too convenient
to be able to write

L += ['foo']

without rebinding L.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of 
indirection."  --Butler Lampson
-- 
http://mail.python.org/mailman/listinfo/python-list


Pydev, Eclipse

2008-02-22 Thread Boris Ozegovic
Hi

Suppose I have three blocks:
if 1:
if 2:
if 3:
# here I want my cursor go back to second block (if 2:)

What is the standard shortcut for this?  ctrl+arrow keys aren't, arrow keys
alone aren't either.

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


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul Rubin  writes:
> if any(x==element[0] for x in a): 
>   a.append(element)

Should say:

 if any(x[0]==element[0] for x in a): 
a.append(element)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Globals or objects?

2008-02-22 Thread tinnews
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> On Fri, 22 Feb 2008 12:01:20 +, tinnews wrote:
> 
> > Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> >> >> > but you do keep having to use a longer reference to the value
> >> >> >so what have you won?
> >> >> 
> >> >> Clarity, simplicity, robustness
> >> > 
> >> > Clarity - why is it clearer?
> >> 
> >> Consider two function calls:
> >> 
> >> 
> >> x = ham(arg, counter)
> >> y = spam(arg)
> >> 
> >> Both do exactly the same thing: ham() takes an explicit "counter"
> >> argument, while spam() uses a global variable. Which one makes it clear
> >> that it uses a counter, and which does not?
> >> 
> > But you're not comparing what the OP posted.  He was comparing a global
> > with an object with a single variable inside it.  Either would work with
> > the y = spam(arg) example above.
> 
> What do you mean by "an object with a single variable inside it"? I don't 
> understand what that is supposed to mean, or why you think it is the same 
> as a global. Do you mean a Singleton?
> 
> If so, then the answer is simple: using a Singleton argument instead of a 
> global is better, because with a global you are stuck to always using the 
> global (at least until you can re-write the code), but with the Singleton 
> argument, you may be enlightened and *not* use a Singleton.
> 
But if you stop using the Singleton the code no longer does the same
as it would with a global does it?

As I keep saying I agree wholeheartedly with the general idea that
globals are a bad thing.  However wrapping up what is effectively a
global in a different construct doesn't seem to me to be any help at
all.  What you need to do is take a long hard look at the global and
decide if there are better ways of doing it, not just simply wrap it
up in a class that really doesn't help at all.

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


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
rh0dium <[EMAIL PROTECTED]> writes:
> found = False
> for item in a:
>   if item[0] == element[0]
> found = True
> break
> if not found:
>   a.append(element)
> 
> But this is just ugly - Is there a simpler way to interate over all
> items in a without using a found flag?

Untested and I'm not sure I understand the question completely, but
try:

if any(x==element[0] for x in a): 
  a.append(element)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Paul Rubin
Nicola Musatti <[EMAIL PROTECTED]> writes:
> >a = [f(x) + g(y) for x,y in izip(m1, m2) if h(x,y).frob() == 7]
> >
> > Consider how many intermediate objects are being allocated in figuring
> > out that listcomp.  Do you REALLY want to manage all the deallocation
> > with something like RAII?
> 
> What makes you think that a translation of a similar expression would
> involve explicit dynamic allocation at all? Barring bugs, here's an
> equivalent example: ...

There you replace one line of code with 40+ lines to get around the
absence of GC.  Sounds bug-prone among other things.

> int f(int n) { return n * 2; }
> int g(int n) { return ( n * 2 ) + 1; }

That is not a reasonable translation, since you've assumed the output
of f and g are integers that don't need to be dynamically allocated.
Maybe in the Python example, f and g and x and y are all bignums or
matrices or something like that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: n00b with urllib2: How to make it handle cookie automatically?

2008-02-22 Thread Rob Wolfe
est <[EMAIL PROTECTED]> writes:

> Hi all,
>
> I need urllib2 do perform series of HTTP requests with cookie from
> PREVIOUS request(like our browsers usually do ). Many people suggest I
> use some library(e.g. pycURL) instead but I guess it's good practise
> for a python beginner to DIY something rather than use existing tools.
>
> So my problem is how to expand the urllib2 class
>
> from cookielib import CookieJar
> class SmartRequest():
> cj=CookieJar()
> def __init__(self, strUrl, strContent=None):
> self.Request=   urllib2.Request(strUrl, strContent)
> self.cj.add_cookie_header(self.Request)
> self.Response   =   urllib2.urlopen(Request)
> self.cj.extract_cookies(self.Response, self.Request)
> def url
> def read(self, intCount):
> return self.Response.read(intCount)
> def headers(self, strHeaderName):
> return self.Response.headers[strHeaderName]
>
> The code does not work because each time SmartRequest is initiated,
> object 'cj' is cleared. How to avoid that?
> The only stupid solution I figured out is use a global CookieJar
> object. Is there anyway that could handle all this INSIDE the class?
>
> I am totally new to OOP & python programming, so could anyone give me
> some suggestions? Thanks in advance

Google for urllib2.HTTPCookieProcessor.

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Paul Rubin
Nicola Musatti <[EMAIL PROTECTED]> writes:
> > Partial guarantees are like being a little bit pregnant.
> 
> Yes, and I'm sure your tests cover all possible paths through your code.

That is the point of type checking.  With a sound type system, "int x"
makes sure, at compile time, that x stays an integer through every
possible path through the code and never becomes a string or anything
like that.  That's why Pierce's book on type systems describes them as
"lightweight formal methods".

The happening thing in language research these days is designing more
and more powerful type systems so that you can make sure, at compile
time, that any predicate that you can describe mathematically remains
true through all possible paths through the code.  So you can have the
compiler make sure, through every possible path through the code, not
just that x is some integer, but is the actual integer that you want,
e.g. if you want x to be the largest prime number smaller than 3*y,
you can define a type for that, and then if your program to compute x
passes the type checker, it cannot compute the wrong value.

Python is a very pleasant and productive environment for banging out
code quickly that does practical things straightforwardly, but in some
ways it feels almost like assembly language, in that you have to keep
track in your head of what types of values your program is computing,
instead of being able to rely on the compiler to catch errors.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: advanced usage of python threads

2008-02-22 Thread hyperboreean
Well, I will be writing the application server of a three-tier 
architecture system. I will be using Twisted for the communication with 
the client but from there I have to make several calls to a database and 
this asks threading. The tables will be filled by another system that 
gathers some data. I want to implement several threads (workers) that 
takes care of each client and a coordinator thread that takes care of 
all the threads. But I have never worked with threads at this level and 
I feel like I need some coordination. I will be thankful with 
documentation on advanced threads, I probably can apply that in practice.

Thanks to all.

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


bus error/segfault from PyArg_ParseTuple in initproc with incorrect arg number

2008-02-22 Thread Miles Lubin
I am using PyArg_ParseTuple to parse the arguments (ignoring the keyword 
arguments) to my initproc for a type I define.
It seems that something goes wrong inside PyArg_ParseTuple when it gets 
the wrong number of arguments (my format string is "OO");
if the function isn't given exactly two arguments, I get a bus error on 
OS X and a segfault on Linux.
If two arguments are given, the code runs as expected.
This does not occur when using PyArg_ParseTuple in a normal method.
Am I not using PyArg_ParseTuple correctly?

Here's the relevant code:
PyObject *a, *b;
if (!PyArg_ParseTuple(args, "OO", &a, &b))
return -1;

The segfault occurs on this line, not on any line after.

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


Re: Return value of an assignment statement?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 08:19:07 -0800, Carl Banks wrote:

> (The perl example wasn't using an assignment operator.)

Hmmm... I see. Obviously I didn't pretend to understand Perl well enough.

(I assume you're ignoring the assignments $name = chop(\1) etc. Fair 
enough.)


[...]
> I can't help but to think that a lot of people's distaste for this
> natural way to write certain logic is simply defensiveness about one
> minor little thing that Python doesn't support (without workarounds).

But Python certainly does support set-and-test. You just have to separate 
the set from the test with a newline:

m = re.match(r"name=(.*)",line)  # set
if m:  # test
name = m.group(1).strip()


This does the same thing as your proposed syntax

if m where m = re.match(r"name=(.*)",line):
name = m.group(1).strip()

except that it doesn't create a new scope. I'm not sure that the benefit 
of having a new scope is worth the new keyword. Maybe it is, maybe it 
isn't.

I think I have a better idea of what you are trying to say. Despite first 
impressions, you weren't defending the proposed "assign-and-test" idiom 
suggested by Stephen Gross:

pat = re.compile('some pattern')
if m = pat.match(some_string):  # doesn't work in Python
do_something(m)

on account of it needing an assignment expression, which is Bad. But you 
were defending the principle of set-and-test, if we can use something 
other than an assignment expression to do the set.

E.g. Perl's magic syntax "if /pattern/ { }" (everything in Perl is magic 
syntax), or your proposed "if m where m = expression".

Okay, I can agree with that, particularly since Python already supports 
it using the plain old boring, old fashioned idiom of "assign, then test".



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


Re: xml escapedness

2008-02-22 Thread Robin Becker
Steve Holden wrote:
> Robin Becker wrote:
>> Tim van der Leeuw wrote:
>>> On Fri, Feb 22, 2008 at 5:17 PM, Robin Becker <[EMAIL PROTECTED]> wrote:
>>>
 A colleague has decided to keep his django database string values (which
 are xml
 fragments) in an xml escaped form to avoid having the problem of escaping
 them
 when they are used in templates etc etc.

 Unfortunately he found that the normal admin doesn't escape on the way
 through
 so thought of adding a standard mechanism to the save methods. However,
 this
 brings in the possibility of escaping twice ie once in his original
 capture code
 and then in the django save methods.

>>> Well -- you escape them in the save() method only when they contain XML
>>> charachters like <, > ? How about that, wouldn't that work?
>>>
>>> --Tim
>>>
>> ..
>> That might work, but there are all the ampersands etc etc to consider as 
>> well. 
>> So an escaped string could contain &, but so can a raw string.
> 
> by the way, be careful - the Django trunk is already modified to perform 
> escaping by default, so if your colleague is using 0.96 or older he 
> should really look at the implications of that change on his design 
> decision. Storing XML in escaped for is always dodgy, much better to 
> escape when necessary (and when some other tool isn't doing it for you). 
> that is, after all, the canonical form.
> 
> regards
>   Steve

I agree wholeheartedly, I would prefer raw in the db. Since we're scraping for 
some of the content it's hard to eliminate all xml though.
-- 
Robin Becker

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


Re: Tkinter Menu Item Activation

2008-02-22 Thread Rob Wolfe
[EMAIL PROTECTED] writes:

> Tkinter definitely deserves more respect! I'm making rapid progress
> and it looks good.
>
> But am stuck on this: I want the File/Save state to change from
> disabled to enabled, depending on whether or not there is something to
> save (Text modified). Google returns results in every language except
> Python.
> Sub problems: how to change state of menu item? how to detect changes
> in Text widget?
>
> Help appreciated.

State of menu items can be changed like this:


import Tkinter as Tk

def hello():
if mfile.entrycget(2, 'state') == 'disabled':
state = 'normal'
else:
state = 'disabled'
mfile.entryconfigure(2, state=state)

root = Tk.Tk()
menubar = Tk.Menu(root)
mfile = Tk.Menu(menubar)
mfile.add_command(label="Open", command=hello)
mfile.add_command(label="Save", command=hello)
menubar.add_cascade(label='File', menu=mfile)
root.config(menu=menubar)
root.mainloop()


But I think that you should read this:
http://effbot.org/zone/vroom.htm

HTH,
Rob

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Nicola Musatti
On Feb 22, 5:13 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Fri, 22 Feb 2008 04:48:28 -0800, Nicola Musatti wrote:
[...]
> > As you can see the standard library takes care of all memory
> > management.
>
> Aaah, that's much nicer and easier to understand than the list
> comprehension.  After this great example I'll switch to C++.  ;-)

You should. As you can see C++ is way more explicit than
Python ;-)
>
> But somehow you still manage memory by writing in a style that favors
> value types.

Certainly, but this is a natural C++ programming style, at least for
those that aren't too deeply rooted in their C heritage.

Cheers,
Nicola Musatti


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


Re: Getting stdout from other processes

2008-02-22 Thread Miki
Hello Matthias,

> as I found out, it is possible to get the output of other programs
> using os.popen() and read from it. However this method is blocking for
> server processes and programs that don't stop immediately. Has anyone
> an idea how to get the output of such programs?
The current "official" module to use is subprocess (pipe =
Popen([client], stdout=PIPE))
However if the client is sending data, reading from the pipe.stdout
will block the server.
If you *need* to wait, then you can use pipe.wait(), otherwise do as
Diez suggested and have a thread
read the client output and propagate it to the main loop (maybe using
Queue.Queue)

HTH,
--
Miki <[EMAIL PROTECTED]>
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml escapedness

2008-02-22 Thread Steve Holden
Robin Becker wrote:
> Tim van der Leeuw wrote:
>> On Fri, Feb 22, 2008 at 5:17 PM, Robin Becker <[EMAIL PROTECTED]> wrote:
>>
>>> A colleague has decided to keep his django database string values (which
>>> are xml
>>> fragments) in an xml escaped form to avoid having the problem of escaping
>>> them
>>> when they are used in templates etc etc.
>>>
>>> Unfortunately he found that the normal admin doesn't escape on the way
>>> through
>>> so thought of adding a standard mechanism to the save methods. However,
>>> this
>>> brings in the possibility of escaping twice ie once in his original
>>> capture code
>>> and then in the django save methods.
>>>
>> Well -- you escape them in the save() method only when they contain XML
>> charachters like <, > ? How about that, wouldn't that work?
>>
>> --Tim
>>
> ..
> That might work, but there are all the ampersands etc etc to consider as 
> well. 
> So an escaped string could contain &, but so can a raw string.

by the way, be careful - the Django trunk is already modified to perform 
escaping by default, so if your colleague is using 0.96 or older he 
should really look at the implications of that change on his design 
decision. Storing XML in escaped for is always dodgy, much better to 
escape when necessary (and when some other tool isn't doing it for you). 
that is, after all, the canonical form.

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

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


Re: xml escapedness

2008-02-22 Thread Robin Becker
Tim van der Leeuw wrote:
> On Fri, Feb 22, 2008 at 5:17 PM, Robin Becker <[EMAIL PROTECTED]> wrote:
> 
>> A colleague has decided to keep his django database string values (which
>> are xml
>> fragments) in an xml escaped form to avoid having the problem of escaping
>> them
>> when they are used in templates etc etc.
>>
>> Unfortunately he found that the normal admin doesn't escape on the way
>> through
>> so thought of adding a standard mechanism to the save methods. However,
>> this
>> brings in the possibility of escaping twice ie once in his original
>> capture code
>> and then in the django save methods.
>>
> 
> Well -- you escape them in the save() method only when they contain XML
> charachters like <, > ? How about that, wouldn't that work?
> 
> --Tim
> 
..
That might work, but there are all the ampersands etc etc to consider as well. 
So an escaped string could contain &, but so can a raw string.
-- 
Robin Becker

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


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Jason
On Feb 22, 10:20 am, rh0dium <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a simple list to which I want to append another tuple if
> element 0 is not found anywhere in the list.
>
> element =  ('/smsc/chp/aztec/padlib/5VT.Cat',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat', (33060))
>
> element1 =  ('/smsc/chp/aztec/padlib/5VT.Cat2',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat2', (33060))
>
> a =  [ ('/smsc/chp/aztec/padlib/5VT.Cat',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat', (33060)),
>  ('/smsc/chp/aztec/padlib/padlib.TopCat%',
>   '/smsc/chp/aztec/padlib',
>   'padlib.TopCat%', (33204)),
>  ('/smsc/chp/aztec/padlib/Regulators.Cat%',
>   '/smsc/chp/aztec/padlib',
>   'Regulators.Cat%', (33204))]
>
> So my code would look something like this.
>
> found = False
> for item in a:
>   if item[0] == element[0]
> found = True
> break
> if not found:
>   a.append(element)
>
> But this is just ugly - Is there a simpler way to interate over all
> items in a without using a found flag?
>
> Thanks

How-about using a generator expression and Python's built-in "in"
operator:

>>> def example(myData, newData):
...   if newData[0] not in (x[0]  for x in myData):
... myData.append( newData )
...
>>> l = []
>>> example( l, ('a', 'apple', 'aviary') )
>>> l
[('a', 'apple', 'aviary')]
>>> example( l, ('s', 'spam', 'silly') )
>>> l
[('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
>>> example( l, ('s', 'suck-tastic') )
>>> l
[('a', 'apple', 'aviary'), ('s', 'spam', 'silly')]
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 11:20 am, rh0dium <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a simple list to which I want to append another tuple if
> element 0 is not found anywhere in the list.
>
> element =  ('/smsc/chp/aztec/padlib/5VT.Cat',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat', (33060))
>
> element1 =  ('/smsc/chp/aztec/padlib/5VT.Cat2',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat2', (33060))
>
> a =  [ ('/smsc/chp/aztec/padlib/5VT.Cat',
>   '/smsc/chp/aztec/padlib',
>   '5VT.Cat', (33060)),
>  ('/smsc/chp/aztec/padlib/padlib.TopCat%',
>   '/smsc/chp/aztec/padlib',
>   'padlib.TopCat%', (33204)),
>  ('/smsc/chp/aztec/padlib/Regulators.Cat%',
>   '/smsc/chp/aztec/padlib',
>   'Regulators.Cat%', (33204))]
>
> So my code would look something like this.
>
> found = False
> for item in a:
>   if item[0] == element[0]
>     found = True
>     break
> if not found:
>   a.append(element)
>
> But this is just ugly - Is there a simpler way to interate over all
> items in a without using a found flag?
>
> Thanks

Well, that's what I get for typing before thinking...

If the remaining items in each element tuple are the same for any
given element[0], then just use a set.

aset = set(a)
for element in list_of_new_element_tuples:
  aset.add(element)

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


Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 11:20 am, rh0dium <[EMAIL PROTECTED]> wrote:
>
> found = False
> for item in a:
>   if item[0] == element[0]
>     found = True
>     break
> if not found:
>   a.append(element)
>
> But this is just ugly - Is there a simpler way to interate over all
> items in a without using a found flag?
>
> Thanks


for item in a:
  if item[0] == element[0]
break
else: # only called if we never 'break' out of the for loop
  a.append(element)


But what about a dict?

adict = dict((elem[0],elem) for elem in a)

if item[0] not in adict:
  adict[item[0]] = item

# need the final list?
a = adict.values()

No list searching, and will scale well if a gets real long.

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


Simple - looking for a way to do an element exists check..

2008-02-22 Thread rh0dium
Hi all,

I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.

element =  ('/smsc/chp/aztec/padlib/5VT.Cat',
  '/smsc/chp/aztec/padlib',
  '5VT.Cat', (33060))

element1 =  ('/smsc/chp/aztec/padlib/5VT.Cat2',
  '/smsc/chp/aztec/padlib',
  '5VT.Cat2', (33060))

a =  [ ('/smsc/chp/aztec/padlib/5VT.Cat',
  '/smsc/chp/aztec/padlib',
  '5VT.Cat', (33060)),
 ('/smsc/chp/aztec/padlib/padlib.TopCat%',
  '/smsc/chp/aztec/padlib',
  'padlib.TopCat%', (33204)),
 ('/smsc/chp/aztec/padlib/Regulators.Cat%',
  '/smsc/chp/aztec/padlib',
  'Regulators.Cat%', (33204))]

So my code would look something like this.

found = False
for item in a:
  if item[0] == element[0]
found = True
break
if not found:
  a.append(element)

But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?

Thanks


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


Re: Tkinter OSX and "lift"

2008-02-22 Thread Miki
Hello Eric,

> >>> Tk.lift doesn't seem to work on OSX (Python 2.5.1).

> There is a trick that sometimes works even for interpreted application:
>
> import Tkinter as tk
> root = tk.Tk()
> root.withdraw()
> # Code building the window...
> root.lift()
> root.deiconify()
> root.mainloop()
>
> This sometimes forces the window to be top-most. If this doesn't work, you  
> can also try:
>
> import Tkinter as tk
> root = tk.Tk()
> root.withdraw()
> # Code building the window...
> root.lift()
> root.after_idle(root.deiconify)
> root.mainloop()
>
> This was a trick that had to be done on Windows a few years back to force  
> the main window to be created on top of this others. It deosn't seem to be  
> needed anymore now, but maybe the trick can be used on a Mac... Don't know  
> if this will work the same, though...
Sadly, both of them didn't work.

Thanks.
--
Miki <[EMAIL PROTECTED]>
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: advanced usage of python threads

2008-02-22 Thread Christian Heimes
hyperboreean wrote:
> Hi,
> Is there a document where I can find some advanced information about 
> python threads? I know the basic things about them and did some 
> practice, but when I try to advance I don't know where to go or how to go.

What's your application doing? Most people are not aware that threading
isn't the best solution for concurrency. For IO bound applications (like
network applications) async event IO is faster and costs less resources.
For other problems you are better off with forking processes.

This is Python and not Java. This is the 21st century and not the early
90ties of the last century. Java favors threads because back in the
90ties it was design for dumb set-top boxes which neither supported
separate process spaces nor threads.

Christian

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


Re: Any experience with Python on a PDA ?

2008-02-22 Thread Grant Edwards
On 2008-02-22, Stef Mientki <[EMAIL PROTECTED]> wrote:
> hello,
>
> I wonder if anyone has (good ;-) experiences with Python on a PDA ?
> And if so,
> - what OS
> - what GUI

Not a PDA per se, but a target platform very similar to a PDA:
PXA-255 at 200MHz with a 1/4 VGA LCD, and something like 128M
of RAM and 64M of flash.

It's OK, but dependnig on what you're doing, you might have to
put some effort into getting the app to run fast enough to be
usable.

-- 
Grant Edwards   grante Yow! for ARTIFICIAL
  at   FLAVORING!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return value of an assignment statement?

2008-02-22 Thread Steve Holden
Carl Banks wrote:
> On Feb 21, 6:52 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
>> mrstephengross wrote:
 What you can't do (that I really miss) is have a tree of assign-and-test
 expressions:
 import re
 pat = re.compile('some pattern')
 if m = pat.match(some_string):
 do_something(m)
>>> Yep, this is exactly what I am (was) trying to do. Oh well Any
>>> clever ideas on this front?
>> The syntax is the way it is precisely to discourage that kind of clever
>> idea.
> 
> Don't be ridiculous.  Assignment operators are maybe one of the worst
> things in existence, but this particular use case (running a sequence
> of tests like the above) is perfectly useful and good.
> 
> Some Pythonistas will swear to their grave and back that should be
> done by factoring out the tests into a list and iterating over it, and
> NO OTHER WAY WHATSOEVER, but I don't buy it.  That's a lot of
> boilerplate--the very thing Python is normally so good at minimizing--
> when it might not be needed.  It would be the right thing for a
> complex, pluggable, customizable input filter; but is rarely a better
> solution for a simple text processing script.
> 
> Quick, at a glance, which code snippet will you understand faster
> (pretend you know Perl):
> 
> 
> if (/name=(.*)/) {
> $name = chop(\1);
> } elsif (/id=(.*)/) {
> $id = chop(\1);
> } elsif (/phone=(.*)/) {
> $phone = chop(\1);
> }
> 
> 
> vs.
> 
> 
> def set_phone_number(m):
> phone = m.group(1).strip()
> 
> def set_id(m):
> id = m.group(1).strip()
> 
> def set_name(m):
> name = m.group(1).strip()
> 
> _line_tests = [
> (r"phone=(.*)", set_phone_number),
> (r"name=(.*)", set_name),
> (r"id=(.*)", set_id),
> ]
> 
> for pattern,func in _line_tests:
> m = re.match(pattern,line)
> if m:
> func(m)
> 
> 
> At this small scale, and probably at much larger scales too, the Perl
> example blows the Python example out of the water in terms of
> readability.  And that's counting Perl's inherent unreadableness.
> 
I'm supposed to overlook the fact that your example in Python omits the 
"untested" it clearly deserves, I take it? I'm not sure what you are 
trying to do with the assignments inside the function body.

The brevity of the Perl has something to commend it, but I am always 
suspicious about whether algorithms like that should really be data 
driven. It's all too easy to add further tests as new field 
possibilities are added. It's also unpleasant in that it leaves two 
variables in an undetermined state.

Let's assume that your Python functions were correctly assigning to 
attributes of some object that was being passed in or global, at least 
then it would be possible to add an else condition to each iteration to 
set the attribute's default value somehow.

So I think your example is perhaps not the best one you could have 
chosen to make your case.

I will admit that idiomatic usages are acceptable ways to perform common 
tasks, but I still think that Guido's decision to eschew assignments as 
expression terms is a sound one, and one that encourages better program 
construction.

Hey, call me (or my assertions) ridiculous if you want. It remains that 
allowing such terms will inevitably lead to hard-to-debug confusion 
between assignment and equality testing once the difference becomes a 
single equals sign.

> If it were a priority, Python could support this set-and-test idiom,
> and without an assignment operator.  (Notice Perl doesn't use
> assignment operator here.)  For example, a syntax like this (where the
> scope of m is local to the if-condition and the body of the if-
> statement:
> 
> if m where m = re.match(r"name=(.*)",line):
> name = m.group(1).strip()
> elif m where m = re.match(r"id=(.*)",line):
> id = m.group(1).strip()
> elif m where m = re.match(r"phone=(.*)",line):
> phone = m.group(1).strip()
> 
[I'll presume you've already established default values for name, id and 
phone just to quiet the alarms ringing in the background]. Still looks 
to me like it might be better data-driven with a setattr() in there 
somewhere. As far as I can see all this would achieve would be to limit 
the scope of the assignment,  and I don't really see what advantage that 
provides.
> 
> This won't happen because the set-and-test idiom is relatively minor
> and not deemed worthy of syntax support.  But if it were there,
> there'd really be nothing clever about it.
> 
Note also that I'm not saying an experienced programmer can't get these 
things right. But at any given time half the programmers in the world 
are newbies, and Python tries to help them by steering them in safer 
directions.

Maybe we could allow it if you had a digital certificate asserting that 
you'd passed your metaclass abuse test ... <0.75 wink>

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

-- 
http:/

Re: Querying a complex website

2008-02-22 Thread schweet1
On Feb 20, 6:06 pm, 7stud <[EMAIL PROTECTED]> wrote:
> 7stud wrote:
> > schweet1 wrote:
> > > On Feb 19, 4:04�pm, 7stud <[EMAIL PROTECTED]> wrote:
> > > > schweet1 wrote:
> > > > > Greetings,
>
> > > > > I am attempting to use python to submit a query to the following URL:
>
> > > > >https://ramps.uspto.gov/eram/patentMaintFees.do
>
> > > > > The page looks simple enough - it requires submitting a number into 2
> > > > > form boxes and then selecting from the pull down.
>
> > > > > However, my test scripts have been hung up, apparently due to the
> > > > > several buttons on the page having the same name. �Ideally, I would
> > > > > have the script use the "Get Bibligraphic Data" link.
>
> > > > > Any assistance would be appreciated.
>
> > > > > ~Jon
>
> > > > This is the section you are interested in:
>
> > > > -
> > > > 
> > > >  > > > value="Retrieve Fees to Pay"> 
> > > > 
>
> > > > 
> > > >  
> > > > 
>
> > > > 
> > > >  
> > > > 
> > > > 
> > > > 
>
> > > > 1) When you click on a submit button on a web page, a request is sent
> > > > out for the web page listed in the action attribute of the  tag,
> > > > which in this case is:
>
> > > > 
>
> > > > The url specified in the action attribute is a relative url. �The
> > > > current url in the address bar of your browser window is:
>
> > > >https://ramps.uspto.gov/eram/patentMaintFees.do
>
> > > > and if you compare that to the url in the action attribute of the
> > > >  tag:
>
> > > > -https://ramps.uspto.gov/eram/patentMaintFees.do
>
> > > > /eram/getMaintFeesInfo.do;jsessionid=-MCoYNbJsaUCr2VfzZhKILX:
> > > > 11g0uepfb
> > > > -
>
> > > > you can piece them together and get the absolute url:
>
> > > >https://ramps.uspto.gov/eram/getMaintFeesInfo.do;jsessionid=-MCoY...
>
> > > > 2) When you click on a submit button, a request is sent to that url.
> > > > The request will contain all the information you entered into the form
> > > > as name/value pairs. �The name is whatever is specified in the name
> > > > attribute of a tag and the value is whatever is entered into the form.
>
> > > > Because the submit buttons in the form have name attributes, �the name
> > > > and value of the particular submit button that you click will be added
> > > > to the request.
>
> > > > 3) �To programmatically mimic what happens in your browser when you
> > > > click on the submit button of a form, you need to send a request
> > > > directly to the url listed in the action attribute of the .
> > > > Your request will contain the name/value pairs that would have been
> > > > sent to the server if you had actually filled out the form and clicked
> > > > on the 'Get Bibliographic Data' submit button. �The form contains
> > > > these input elements:
>
> > > > 
> > > > 
>
> > > >  > > > value="">
> > > > 
>
> > > > and the submit button you want to click on is this one:
>
> > > > 
>
> > > > So the name value pairs you need to include in your request are:
>
> > > > data = {
> > > > � � 'patentNum':'1234567',
> > > > � � 'applicationNum':'08123456',
> > > > � � 'maintFeeAction':'Get Bibliographic Data'
>
> > > > }
>
> > > > Therefore, try something like this:
>
> > > > import urllib
>
> > > > data = {
> > > > � � 'patentNum':'1234567',
> > > > � � 'applicationNum':'08123456',
> > > > � � 'maintFeeAction':'Get Bibliographic Data'
>
> > > > }
>
> > > > enc_data = urllib.urlencode(data)
> > > > url = 'https://ramps.uspto.gov/eram/
> > > > getMaintFeesInfo.do;jsessionid=-MCoYNbJsaUCr2VfzZhKILX:11g0uepfb'
>
> > > > f = urllib.urlopen(url, enc_data)
>
> > > > print f.read()
> > > > f.close()
>
> > > > If that doesn't work, you may need to deal with cookies that the
> > > > server requires in order to keep track of you as you navigate from
> > > > page to page. �In that case, please post a valid patent number and
> > > > application number, so that I can do some further tests.- Hide quoted 
> > > > text -
>
> > > > - Show quoted text -
>
> > > Thanks all - I think there are cookie issues - here's an example data
> > > pair to play with: 6,725,879 (10/102,919).  I'll post some of the code
> > > i've tried asap.
>
> > Ok.  Here is what your form looks like without all the  and 
> > tags:
>
> > -
> > 
>
> > 
> >  > value="">
>
> >  > value="52371786cafc8b58d140bb03ae5a1210">
> > 
> > 
>
> > 
> > 
> > 
> > 
>
> > for Payment Window:
> > 04 > option>
> >  08
> >  12
> > 
>
> > 
> > 
>
> > First notice that there is a  tag at the bottom that contains
> > some information that would be included in the request if you filled
> > out the form by hand and clicked on the submit button.  As a result,
> > the name/value pair of that  tag needs to be included in your
> > request.  That requires that you add the following data to your
> > request:
>
> > 'maintFeeYear':'04'   #...or whatever you want the value to be
>
> > Also notice that there are 'hidden' form fields in the form.  They
> > look like thi

Re: how to flush child_stdin

2008-02-22 Thread [EMAIL PROTECTED]
On Feb 22, 12:15 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "[EMAIL PROTECTED]"
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>
>
>
> > I'm opening up a subprocess like this where slave.py is a text based
> > app that receives commands and responds with output:
>
> > r, w, e = popen2.popen3('python slave.py')
>
> > I need to send slave.py a command and see the output,
> > so I'll do something like:
>
> > w.write("command here")
> > then i'll try this:
> > w.flush()
>
> > A separate thread is reading from r to retrieve output of slave.py.
>
> > The problem is that slave.py doesn't seem to receive commands unless I
> > also do:
> > w.close()
>
> What happens if you do:
>
> w.write("command here\n")
> w.flush()
>
> Could the slave be blocked waiting for an EOL character before
> processing said command?
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

I don't think that is the problem, I'm feeding it newline characters.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml escapedness

2008-02-22 Thread Tim van der Leeuw
On Fri, Feb 22, 2008 at 5:17 PM, Robin Becker <[EMAIL PROTECTED]> wrote:

> A colleague has decided to keep his django database string values (which
> are xml
> fragments) in an xml escaped form to avoid having the problem of escaping
> them
> when they are used in templates etc etc.
>
> Unfortunately he found that the normal admin doesn't escape on the way
> through
> so thought of adding a standard mechanism to the save methods. However,
> this
> brings in the possibility of escaping twice ie once in his original
> capture code
> and then in the django save methods.
>

Well -- you escape them in the save() method only when they contain XML
charachters like <, > ? How about that, wouldn't that work?

--Tim


>
> I suggested he could use a subclass of str to represent escaped strings
> and an
> escape function which leaves the subclass instances alone so
>
> class xmlstr(str):
>  pass
>
> from xml.sax.saxutils import escape
> def xmlEscape(s):
> if isinstance(s,xmlstr): return s
> return xmlstr(escape(s))
>
> this works up to a point, but anything which modifies the string reverts
> to the
> base class (as it probably should).
>
> type(xmlstr('<') + '') is type(str)
>
> clearly there are a large number of operations which should be overridden
> or
> just hidden to prevent the wrong outcome; has anyone else thought about
> this in
> any detail?
> --
> Robin Becker
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Return value of an assignment statement?

2008-02-22 Thread Steve Holden
Marc 'BlackJack' Rintsch wrote:
> On Fri, 22 Feb 2008 12:32:10 +, Steven D'Aprano wrote:
> 
>> On Fri, 22 Feb 2008 08:12:56 +, Marc 'BlackJack' Rintsch wrote:
>>
>>> A "variable" in programming languages is composed of a name, a memory
>>> location, possibly a type and a value. In C-like languages, where you
>>> put values in named and typed "boxes", the memory location and type are
>>> attached to the name.  In Python both belong to the value.
>> But Python objects don't have names, so by your own definition, they
>> aren't variables.
> 
> Exactly!  Names aren't variables.  The unit of a name, an address, and a
> value are a variable.
> 
>> Names are associated with namespaces, not objects. A name must have one
>> and only one object bound to it at any one time;
> 
> What is a binding when it's not an association between a name and an
> object!?  So names are associated with objects.  There are no names
> without objects in Python.  If a name is not bound to any object, how could
> the name exist?  That would be like a dangling pointer, a beast that
> doesn't exists in Python.
> 
> Okay there are local names that are known and therefore somehow
> "exist" before they get bound, but that's IMHO an implementation
> detail.
> 
>> objects on the other hand can be bound to one name, or no name, or a
>> thousand names. The object itself has no way of knowing what names it is
>> bound to, if any.
>>
>> Or, to put it another way... Python doesn't have variables.
> 
> It has.  You just can't substitute the term "name" with "variable" and
> expect it to behave like in C.  A variable is not just the name but also
> the value and the storage space and how those are connected.
> 
"Does" ... "Doesn't" ... "Does so!".

You guys are merely arguing about what you want to call the Python 
assignment semantics you both understand perfectly well. This isn't 
going to help anyone.

The fact of the matter is that when a Python name is bound to a value 
the value is normally created in heap storage (with a few exceptions 
like the pre-allocated objects such as None and the small integers, but 
*never* directly in the namespace in which the name is being bound), and 
the name is associated with a reference to the value.

I've said before that Python names are very similar to automatically 
dereferenced pointer variables, and I suppose the same old arguments 
will be trotted out against that utterance now I've said it again.

But for the purposes of comprehension, particularly by C and C++ 
programmers who haven't come across this particular semantic before it 
should server to aid comprehension. The fact that objects exist 
independent of the dynamically created scopes of function calls and the 
like is precisely what stops Python from suffering the same out-of-scope 
(dangling) pointer issues that C++ is famous for.

The fact remains that name binding in Python (and binding to container 
items too) doesn't "return a value", and bindings were deliberately not 
allowed as a term in a broader expression to avoid some common 
programming errors.

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

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


Re: Return value of an assignment statement?

2008-02-22 Thread Carl Banks
On Feb 22, 9:58 am, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Fri, 22 Feb 2008 00:45:59 -0800, Carl Banks wrote:
> > On Feb 21, 6:52 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> >> mrstephengross wrote:
> >> >> What you can't do (that I really miss) is have a tree of
> >> >> assign-and-test expressions:
> >> >> import re
> >> >> pat = re.compile('some pattern')
> >> >> if m = pat.match(some_string):
> >> >> do_something(m)
>
> >> > Yep, this is exactly what I am (was) trying to do. Oh well Any
> >> > clever ideas on this front?
>
> >> The syntax is the way it is precisely to discourage that kind of clever
> >> idea.
>
> > Don't be ridiculous.  Assignment operators are maybe one of the worst
> > things in existence, but this particular use case (running a sequence of
> > tests like the above) is perfectly useful and good.
>
> I don't understand your reasoning. If assignment operators are so
> terrible, why do you think the terribleness disappears in this specific
> case?

I don't.

The assignment operator is terrible but the idiom itself isn't.


[snip narrowly applicable counterexamples that don't really mean
anything because they're too narrow]

> Perl's treatment of assignment as an operator tempts the programmer to
> write quick-and-dirty code.

(The perl example wasn't using an assignment operator.)

> Python discourages that sort of behaviour,
> and encourages programmers to factor their code in a structured way.
> That's a feature, not a bug.

The set-and-test idiom is not necessarily quick and dirty, and for
many things it's more readable and a lot easier to follow than any of
indirect methods that are proposed.

The fact that sometimes an assignment operator is used to do this
doesn't change the usefulness of the idiom itself.

It can be done without assignment expressions.  I gave a hypothetical
syntax for how it might be done in Python without them.

I can't help but to think that a lot of people's distaste for this
natural way to write certain logic is simply defensiveness about one
minor little thing that Python doesn't support (without workarounds).


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


xml escapedness

2008-02-22 Thread Robin Becker
A colleague has decided to keep his django database string values (which are 
xml 
fragments) in an xml escaped form to avoid having the problem of escaping them 
when they are used in templates etc etc.

Unfortunately he found that the normal admin doesn't escape on the way through 
so thought of adding a standard mechanism to the save methods. However, this 
brings in the possibility of escaping twice ie once in his original capture 
code 
and then in the django save methods.

I suggested he could use a subclass of str to represent escaped strings and an 
escape function which leaves the subclass instances alone so

class xmlstr(str):
  pass

from xml.sax.saxutils import escape
def xmlEscape(s):
 if isinstance(s,xmlstr): return s
 return xmlstr(escape(s))

this works up to a point, but anything which modifies the string reverts to the 
base class (as it probably should).

type(xmlstr('<') + '') is type(str)

clearly there are a large number of operations which should be overridden or 
just hidden to prevent the wrong outcome; has anyone else thought about this in 
any detail?
-- 
Robin Becker

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Marc 'BlackJack' Rintsch
On Fri, 22 Feb 2008 04:48:28 -0800, Nicola Musatti wrote:

> On Feb 22, 12:07 pm, Paul Rubin  wrote:
>> Nicola Musatti <[EMAIL PROTECTED]> writes:
>> > In C++ memory is just another resource which you can handle just like
>> > any other one, possibly using RAII.
>>
>> Ok, I'll bite.  Here's a straightforward Python expression:
>>
>>a = [f(x) + g(y) for x,y in izip(m1, m2) if h(x,y).frob() == 7]
>>
>> Consider how many intermediate objects are being allocated in figuring
>> out that listcomp.  Do you REALLY want to manage all the deallocation
>> with something like RAII?
> 
> 
> What makes you think that a translation of a similar expression would
> involve explicit dynamic allocation at all? Barring bugs, here's an
> equivalent example:
> 
> #include 
> #include 
> #include 
> 
> int f(int n) { return n * 2; }
> int g(int n) { return ( n * 2 ) + 1; }
> 
> std::map izip(int i, int j) {
>   std::map m;
>   m[i] = j;
>   m[j] = i;
>   return m;
> }
> 
> class A {
>   int i, j;
> public:
>   A(int ii, int jj) : i(ii), j(jj) {}
>   int frob() { return i + j; }
> };
> 
> A h(int i, int j) { return A(i, j); }
> 
> int main() {
>   int m1 = 3;
>   int m2 = 4;
>   std::vector a;
>   std::map m = izip(m1, m2);
>   for ( std::map::iterator i = m.begin(); i != m.end(); ++i )
>   {
>   if ( h(i->first, i->second).frob() == 7 )
>   a.push_back(f(i->first) + g(i->second));
>   }
>   for ( std::vector::iterator i = a.begin(); i != a.end(); ++i )
>   std::cout << *i << '\n';
> }
> 
> As you can see the standard library takes care of all memory
> management.

Aaah, that's much nicer and easier to understand than the list
comprehension.  After this great example I'll switch to C++.  ;-)

But somehow you still manage memory by writing in a style that favors
value types.

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


advanced usage of python threads

2008-02-22 Thread hyperboreean
Hi,
Is there a document where I can find some advanced information about 
python threads? I know the basic things about them and did some 
practice, but when I try to advance I don't know where to go or how to go.
Thanks.

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


Re: Is there a open souce IDE writen by C( C++) or partly writen by C( C++)?

2008-02-22 Thread Preston Landers
On Feb 21, 8:58 pm, zaley <[EMAIL PROTECTED]> wrote:
> Is there a open souce IDE writen by C( C++) or partly writen by C( C+
> +)?

Eclipse is a good open source IDE for many languages including C/C++
and Python.  It includes an interactive debugger.

I believe most of it is written in Java, but I'm not sure why that
would matter.

http://www.eclipse.org/

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Chris Mellon
On Fri, Feb 22, 2008 at 4:56 AM, Nicola Musatti
<[EMAIL PROTECTED]> wrote:
> On Feb 22, 12:24 am, Carl Banks <[EMAIL PROTECTED]> wrote:
>  > On Feb 21, 1:22 pm, Nicola Musatti <[EMAIL PROTECTED]> wrote:
>  >
>  > > There are other downsides to garbage collection, as the fact that it
>  > > makes it harder to implement the Resource Acquisition Is
>  > > Initialization idiom, due to the lack of deterministic destruction.
>  >
>  > That's not a downside: it's at least a wash.
>  >
>  > In C++ you manage memory and the language manages resources.  In
>  > Python you manage resources and the language manages memory.
>  >
>  > RAII is merely one way of minimizing complexity.  Garbage collection
>  > is another way.
>
>  In C++ memory is just another resource which you can handle just like
>  any other one, possibly using RAII. GC deals with memory very
>  reasonably, but makes it more complicate to deal with other resources.
>
There are many different kinds of resource and they have different
optimal handling techniques. Memory in particular is really easily
handled by GC - it's a resource that you can reclaim under pressure,
so deferring it's collection and release makes sense, and it's even
common these days for high quality GC to beat manual management in
performance (never mind correctness). Other kinds of resource are much
more timely, and require tight control over scopes, like mutexes and
other locks. RAII is fantastic for these sort of things. Shared but
limited resources like files work best with refcounting (closing a
file that something else holds a reference to is usually an error).

Optimally, you would have a language that provides all three in easy,
transparent ways. Python only provides two, but the first on is the
least important so Python manages at least a B. C++ provides all 3,
but importantly there's no language level enforcement of use, so you
can (and people do, and this is a common source of bugs) accidentally
bypass the mechanisms.

If I had to use C++ these days, I'd use D instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return value of an assignment statement?

2008-02-22 Thread Marc 'BlackJack' Rintsch
On Fri, 22 Feb 2008 12:32:10 +, Steven D'Aprano wrote:

> On Fri, 22 Feb 2008 08:12:56 +, Marc 'BlackJack' Rintsch wrote:
> 
>> A "variable" in programming languages is composed of a name, a memory
>> location, possibly a type and a value. In C-like languages, where you
>> put values in named and typed "boxes", the memory location and type are
>> attached to the name.  In Python both belong to the value.
> 
> But Python objects don't have names, so by your own definition, they
> aren't variables.

Exactly!  Names aren't variables.  The unit of a name, an address, and a
value are a variable.

> Names are associated with namespaces, not objects. A name must have one
> and only one object bound to it at any one time;

What is a binding when it's not an association between a name and an
object!?  So names are associated with objects.  There are no names
without objects in Python.  If a name is not bound to any object, how could
the name exist?  That would be like a dangling pointer, a beast that
doesn't exists in Python.

Okay there are local names that are known and therefore somehow
"exist" before they get bound, but that's IMHO an implementation
detail.

> objects on the other hand can be bound to one name, or no name, or a
> thousand names. The object itself has no way of knowing what names it is
> bound to, if any.
> 
> Or, to put it another way... Python doesn't have variables.

It has.  You just can't substitute the term "name" with "variable" and
expect it to behave like in C.  A variable is not just the name but also
the value and the storage space and how those are connected.

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


Re: Getting stdout from other processes

2008-02-22 Thread Diez B. Roggisch
Matthias Vogelgesang schrieb:
> Hello,
> as I found out, it is possible to get the output of other programs
> using os.popen() and read from it. However this method is blocking for
> server processes and programs that don't stop immediately. Has anyone
> an idea how to get the output of such programs?

Use either the module select to dispatch in case of arriving input on 
one of several filedescriptors, or a thread to poll the data

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Nicola Musatti
On Feb 22, 3:25 pm, Roy Smith <[EMAIL PROTECTED]> wrote:
> In article
> <[EMAIL PROTECTED]>,
>  Nicola Musatti <[EMAIL PROTECTED]> wrote:
>
> > Yet I'm convinced that even such partial guarantee is worth having.
>
> Partial guarantees are like being a little bit pregnant.

Yes, and I'm sure your tests cover all possible paths through your
code.

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Lou Pecora
In article <[EMAIL PROTECTED]>,
 "Reedick, Andrew" <[EMAIL PROTECTED]> wrote:

> 
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:python-
> > [EMAIL PROTECTED] On Behalf Of Carl Banks
> > Sent: Wednesday, February 20, 2008 8:39 PM
> > To: python-list@python.org
> > Subject: Re: Article of interest: Python pros/cons for the enterprise
> 
> > C++ is a compile-time, type-checked language, which means it is
> > totally safer for newbies than Python.  Yep, your big company is
> > totally safe with newbie C++ programmers.
> 
> Eh, don't laugh too hard.  Since Python code isn't type-checked until
> the actual code block is executed, you have to go through the extra step
> of testing/running  every  line of code before you'll find an error.
> Then there's the problem of how mutable Python objects are.  So even if
> you execute every line of code, you might not have executed the code
> with every possible type of object combination.
> 
> Compared to a statically typed language, it can get very expensive to
> write comprehensive test cases for python scripts.  So I wouldn't be
> quick to dismiss the notion that Java/C#/C++ are more newbie-safe than
> Python. =/
> 
> An amusing case in point was where I had a type-cast error in an
> exception's catch block's print statement.  This simple error caused the
> program to stop with an unhandled exception.  Something that basic would
> have been caught in a statically typed language very early in the dev
> cycle when it's cheaper to fix the problem.  And the idea of
> running/testing exceptions or simple print statements isn't always
> foremost in people's minds.  =P


Well, you're technically right about static typing. That could head off 
some bugs... But my experience over several years has been that this has 
never happened to me.  That is, where I need to test every line of code 
or even half of them.  What usually happens is that I get an error, I 
get the line and module, go there, and realize I tried to use data that 
didn't fit the expression (e.g. an object without the required method).  
Usually, a one-step fix.  What still occasionally gets me is mutable 
objects where I just use "=" to set two things (accidentally) equal to a 
mutable object instead of copying. Then modify one and get and error 
when the other object is used since they were the same object.  I know 
better, but I have a half life on this of about 4 months which means 
that about twice a year this one gets me.

Looking at it the other way having done C++ development, I am  w a y  
more productive in Python and overall spend far less time debugging.  
Just my experience.  YMMV.

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


Re: ANN: NUCULAR B3 Full text indexing (now on Win32 too)

2008-02-22 Thread Aaron Watters
[apologies to the list: I would have done this offline,
but I can't figure out Paul's email address.]

1) Paul please forward your email address

3) Since you seem to know about these things: I was thinking
of adding an optional feature to Nucular which would allow
a look-up like "given a word find all attributes that contain
that word anywhere and give a count of the number of times it
is found in that attribute as well as the entry id for an example
instance (arbitrarily chosen).  I was thinking about calling
this "inverted faceting", but you probably know a
better/standard name, yes?  What is it please?  Thanks!
Answers from anyone else welcomed also.

[Nucular: http://nucular.sourceforge.net/ ]

  -- Aaron Watters

===
There are 3 kinds of people: those who can count, and those who can't.
http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=shit

On 14 Feb, 02:59, Paul Rubin  wrote:
> Jarek Zgoda <[EMAIL PROTECTED]> writes:
> > Did you (or anyone else) compare Nucular with Solr and Sphinx
> > feature-by-feature?
>
> Nucular when I looked at it was in an early alpha release and looked
> interesting and promising, but was nowhere near as built-out as Solr.
> It may be closer now; I haven't yet had a chance to look at the new
> release.
>
> I don't know what Sphinx is.

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


Re: Getting python docstings

2008-02-22 Thread Marc 'BlackJack' Rintsch
On Fri, 22 Feb 2008 03:53:27 -0800, Rufman wrote:

> On Feb 22, 10:36 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
>> Rufman wrote:
>> > Does anyone know how to get docstrings (reStructuredText) out of
>> > python source code using docutils?
>>
>> Depends on what you mean with "get ... out of". There are tools like epydoc
>> that generate source code documentation, maybe that's what you mean?
> 
> Yeah...something like that, but using docutils.

What is "something like that"?  If `epydoc` doesn't do what you want, then
*what* exactly *do* you want?  You know that `epydoc` supports
reStructuredText as markup language!?

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


Re: Acting like button are being pressed (BUT THEY ARE NOT) Please Help

2008-02-22 Thread Marc 'BlackJack' Rintsch
On Fri, 22 Feb 2008 06:48:37 -0800, mcsejung wrote:

> [snipped massive bit of code]

Sorry but dumping about 900 lines of code at people with no real question
in the message body and just sort of a question in the subject won't help
much to get answers.

Just a quick look at the code tells that it could use some loops to
refactor it into a **much** shorter piece of code.

Then get rid of the asterisk import, ``except``\s without a specific
exception to handle, and the``global`` statement before you repost the
problem.

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


Re: Globals or objects?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 12:01:20 +, tinnews wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>> >> > but you do keep having to use a longer reference to the value
>> >> >so what have you won?
>> >> 
>> >> Clarity, simplicity, robustness
>> > 
>> > Clarity - why is it clearer?
>> 
>> Consider two function calls:
>> 
>> 
>> x = ham(arg, counter)
>> y = spam(arg)
>> 
>> Both do exactly the same thing: ham() takes an explicit "counter"
>> argument, while spam() uses a global variable. Which one makes it clear
>> that it uses a counter, and which does not?
>> 
> But you're not comparing what the OP posted.  He was comparing a global
> with an object with a single variable inside it.  Either would work with
> the y = spam(arg) example above.

What do you mean by "an object with a single variable inside it"? I don't 
understand what that is supposed to mean, or why you think it is the same 
as a global. Do you mean a Singleton?

If so, then the answer is simple: using a Singleton argument instead of a 
global is better, because with a global you are stuck to always using the 
global (at least until you can re-write the code), but with the Singleton 
argument, you may be enlightened and *not* use a Singleton.



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


Re: Acting like button are being pressed (BUT THEY ARE NOT) Please Help

2008-02-22 Thread Martin Franklin

> self.entry00.bind('', self.leftClick(self.entry00,
> 0, 0))  # bind left mouse click

Difficult to say for sure due to excessive code wrapping.. ;)
but I would say that these bind methods are calling the left and right
Click methods... rather than bind'ing them to the entry widgets.

there are a few good ways around this, lambda, class with __call__ etc
etc most of these can be found with a little googling (in case this is
homework)


Cheers,
Martin.



-- 
signature file not found, must be something I ate
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a open souce IDE writen by C( C++) or partly writen by C( C++)?

2008-02-22 Thread Jesper
Give PyScripter from http://www.mmm-experts.com/ a try

It is for Windows, though it is written in Delphi and not in C/C++

/Jesper

"zaley" <[EMAIL PROTECTED]> skrev i en meddelelse 
news:[EMAIL PROTECTED]
Of course, python scripts debugger

On 2ÔÂ22ÈÕ, ÏÂÎç3ʱ22·Ö, zaley <[EMAIL PROTECTED]> wrote:
> My project need a simple scripts debugger . I hope I can find
> something instructive
>
> Stefan Behnel дµÀ£º
>
> > zaley wrote:
> > > Is there a open souce IDE writen by C( C++) or partly writen by C( C+
> > > +)?
>
> > Tons of them. What do you want to do with it?
>
> > Stefan


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

Re: Return value of an assignment statement?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 00:45:59 -0800, Carl Banks wrote:

> On Feb 21, 6:52 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
>> mrstephengross wrote:
>> >> What you can't do (that I really miss) is have a tree of
>> >> assign-and-test expressions:
>> >> import re
>> >> pat = re.compile('some pattern')
>> >> if m = pat.match(some_string):
>> >> do_something(m)
>>
>> > Yep, this is exactly what I am (was) trying to do. Oh well Any
>> > clever ideas on this front?
>>
>> The syntax is the way it is precisely to discourage that kind of clever
>> idea.
> 
> Don't be ridiculous.  Assignment operators are maybe one of the worst
> things in existence, but this particular use case (running a sequence of
> tests like the above) is perfectly useful and good.

I don't understand your reasoning. If assignment operators are so 
terrible, why do you think the terribleness disappears in this specific 
case?

The above idiom leads to one of the most common errors in C code: writing 
= when you mean ==. "Running a sequence of tests" isn't immune to that 
problem, it's especially vulnerable to it.

Compare the suggested pseudo-Python code:

pat = re.compile('some pattern')
if m = pat.match(some_string):
do_something(m)


with the actual Python code:

pat = re.compile('some pattern')
m = pat.match(some_string)
if m:
do_something(m)


The difference is exactly one newline plus one extra reference to the 
name "m". And this is a problem?



> Some Pythonistas will swear to their grave and back that should be done
> by factoring out the tests into a list and iterating over it, and NO
> OTHER WAY WHATSOEVER, but I don't buy it.

Well, putting a sequence of tests into a list is the natural way to deal 
with a sequence of tests. What else would you do? 


> That's a lot of boilerplate 

What boilerplate are you talking about?



> --the very thing Python is normally so good at minimizing--
> when it might not be needed.  It would be the right thing for a complex,
> pluggable, customizable input filter; but is rarely a better solution
> for a simple text processing script.

Huh?


> Quick, at a glance, which code snippet will you understand faster
> (pretend you know Perl):
> 
> 
> if (/name=(.*)/) {
> $name = chop(\1);
> } elsif (/id=(.*)/) {
> $id = chop(\1);
> } elsif (/phone=(.*)/) {
> $phone = chop(\1);
> }
> 
> 
> vs.
> 
> 
> def set_phone_number(m):
> phone = m.group(1).strip()
> 
> def set_id(m):
> id = m.group(1).strip()
> 
> def set_name(m):
> name = m.group(1).strip()
> 
> _line_tests = [
> (r"phone=(.*)", set_phone_number),
> (r"name=(.*)", set_name),
> (r"id=(.*)", set_id),
> ]
> 
> for pattern,func in _line_tests:
> m = re.match(pattern,line)
> if m:
> func(m)
> 
> 
> At this small scale, and probably at much larger scales too, the Perl
> example blows the Python example out of the water in terms of
> readability.  And that's counting Perl's inherent unreadableness.


Why would you do that test in such an overblown fashion, then try to 
pretend it is an apples-and-apples comparison with the Perl code? It 
doesn't even work: you have three functions that set a local name, then 
throw it away when they return.

Pretending I understand Perl, here's a fairer, more direct translation of 
the Perl code:


name, id, phone = [None]*3  # Closest thing to an unset variable in Perl.
name = re.match(r"name=(.*)", line)
if name: name = name.group(1).strip()
else:
id = re.match(r"id=(.*)", line)
if id: id = id.group(1).strip()
else:
phone = re.match(r"phone=(.*)", line)
if phone: phone = phone.group(1).strip()

Six lines for Perl against nine for Python, eight if you dump the "unset" 
line. Hardly a big difference.

The main difference is that Python's handling of regexes is a little more 
verbose, and that the indentation is compulsory. But here's a better way 
to do the same test:

tests = [ (r"name=(.*)", 'name'), 
(r"id=(.*)", 'id'), (r"phone=(.*)", 'phone')]
for (test, name) in tests:
m = re.match(t, line)
if m:
globals()[name] = m.group(1).strip()
break

Down to seven lines, or six if the I didn't split the tests over two 
lines.



Here's an even better way:

tests = [ "name", "id", "phone"]
for t in tests:
m = re.match(t + r"=(.*)", line)
if m:
globals()[t] = m.group(1).strip()
break

Six lines for Perl, six for Python, and the Python version is far more 
readable.

Perl's treatment of assignment as an operator tempts the programmer to 
write quick-and-dirty code. Python discourages that sort of behaviour, 
and encourages programmers to factor their code in a structured way. 
That's a feature, not a bug.



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


Acting like button are being pressed (BUT THEY ARE NOT) Please Help

2008-02-22 Thread mcsejung
"""
-
Name:_tkUnderWaterDemolitionRemoval.py
Purpose: The classic Under Water Demolition Removal game

Author:  mcsejung

Created: 2008/02/15
RCS-ID:  $Id: _tkUnderWaterDemolitionRemoval.py $
Copyright:   (c) 2008
Licence: GPL
   This program is free software; you can redistribute it and/or
modify
   it under the terms of the GNU General Public License as published
by
   the Free Software Foundation; either version 2 of the License
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307  USA

Maintenance: 2008/02/15 The classic Under Water Demolition Removal
game
-
"""
from Tkinter import *

class App:
global matrix
def __init__(self, master):
#Define the matrix
self.var2entry = range(8)
for i in range(8):
self.var2entry[i] = range(8)

#Define the outer frame
frame = Frame(master)
frame.grid()

#Define a square
self.var2entry[0][0] = StringVar()
self.entry00 = Button(frame, textvariable=self.var2entry[0]
[0], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry00.bind('', self.leftClick(self.entry00,
0, 0))  # bind left mouse click
self.entry00.bind('', self.rightClick(self.entry00,
0, 0)) # bind right mouse click
self.entry00.grid(row=0, column=0, sticky=W,)

#Define a square
self.var2entry[0][1] = StringVar()
self.entry01 = Button(frame, textvariable=self.var2entry[0]
[1], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry01.bind('', self.leftClick(self.entry01,
0, 1))  # bind left mouse click
self.entry01.bind('', self.rightClick(self.entry01,
0, 1)) # bind right mouse click
self.entry01.grid(row=0, column=1, sticky=W,)

#Define a square
self.var2entry[0][2] = StringVar()
self.entry02 = Button(frame, textvariable=self.var2entry[0]
[2], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry02.bind('', self.leftClick(self.entry02,
0, 2))  # bind left mouse click
self.entry02.bind('', self.rightClick(self.entry02,
0, 2)) # bind right mouse click
self.entry02.grid(row=0, column=2, sticky=W,)

#Define a square
self.var2entry[0][3] = StringVar()
self.entry03 = Button(frame, textvariable=self.var2entry[0]
[3], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry03.bind('', self.leftClick(self.entry03,
0, 3))  # bind left mouse click
self.entry03.bind('', self.rightClick(self.entry03,
0, 3)) # bind right mouse click
self.entry03.grid(row=0, column=3, sticky=W,)

#Define a square
self.var2entry[0][4] = StringVar()
self.entry04 = Button(frame, textvariable=self.var2entry[0]
[4], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry04.bind('', self.leftClick(self.entry04,
0, 4))  # bind left mouse click
self.entry04.bind('', self.rightClick(self.entry04,
0, 4)) # bind right mouse click
self.entry04.grid(row=0, column=4, sticky=W,)

#Define a square
self.var2entry[0][5] = StringVar()
self.entry05 = Button(frame, textvariable=self.var2entry[0]
[5], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry05.bind('', self.leftClick(self.entry05,
0, 5))  # bind left mouse click
self.entry05.bind('', self.rightClick(self.entry05,
0, 5)) # bind right mouse click
self.entry05.grid(row=0, column=5, sticky=W,)

#Define a square
self.var2entry[0][6] = StringVar()
self.entry06 = Button(frame, textvariable=self.var2entry[0]
[6], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry06.bind('', self.leftClick(self.entry06,
0, 6))  # bind left mouse click
self.entry06.bind('', self.rightClick(self.entry06,
0, 6)) # bind right mouse click
self.entry06.grid(row=0, column=6, sticky=W,)

#Define a square
self.var2entry[0][7] = StringVar()
self.entry07 = Button(frame, textvariable=self.var2entry[0]
[7], fg="blue", bg="grey", font=("Verdana", 12), relief=RAISED,
width=2, borderwidth=3)
self.entry07.bind('', self.leftClick(self.ent

Re: ANN: Phatch = PHoto bATCH processor and renamer based on PIL

2008-02-22 Thread Mike Driscoll
On Feb 22, 2:39 am, "SPE - Stani's Python Editor"
<[EMAIL PROTECTED]> wrote:
> On Feb 22, 1:41 am, Mike Driscoll <[EMAIL PROTECTED]> wrote:
>
> > On Feb 20, 4:19 am, Stani <[EMAIL PROTECTED]> wrote:
> > > Even without python-pyexiv2 Phatch features read-only EXIF support thanks 
> > > to
> > > PIL. So you can name your files or write data stamps (date, aperature, 
> > > velocity,
> > > ...) based on EXIF information. If you want to save EXIF and IPTC 
> > > information to
> > > files you needpython-pyexiv2. From its website:
> > > "However, the library and all the tools used are cross-platform, so very 
> > > little
> > > tweaking should be needed to get it to work fine on Windows or MacOS 
> > > X."http://tilloy.net/dev/pyexiv2/developers.htm
>
> > > The exiv2 website says:
> > > "The Windows package only contains the command line utility exiv2.exe
> > > (statically linked), manpage and a sample command file; get the source 
> > > and doc
> > > packages for the library, documentation and other 
> > > tools."http://www.exiv2.org/download.html
>
> > > So maybe someone can compile it.
>
> > I'm confused. What needs to be compiled exactly? Are there any
> > directions? I'm not seeing any at  that website. I can give it a go if
> > someone can give me advice.
>
> You need to compile two things:
> 1. the exiv2 library
> 2. the python-pyexiv2 bindings
>
> For the exiv2 library, you will have to dive into the project:
> The Windows executable provided here was compiled with the MinGW cross
> compiler on an Intel 32 bit machine running Debian.
>
> Maybe the exiv2 Yahoo! group is the right place to be. If you look
> there for Windows threads maybe some might be 
> helpful:http://uk.groups.yahoo.com/group/exiv2/message/956
>
> The author of python-pyexiv2 is active there as well and explains his
> project uses Scons:http://uk.groups.yahoo.com/group/exiv2/message/1002
>
> I am not an expert on compiling extensions. In fact I only know
> Python. A windows version of exiv2 and python-pyexiv2 will only exist
> if some Windows programer(s) take the initiative.


I have Visual Studio 6 and .NET 2003 so I think I'm good for that.
Unfortunately, I am not the best at compiling. Hopefully the links you
provided will have plenty of friendly pros who don't mind sharing
their wisdom.


>
> In case you can't compile, it wouldn't be so hard to write a wrapper
> around the provided windows executable to mimic the API of python-
> pyexiv2.


If it does turn out that they can't help me compile, would you know
how to do this? Are we talking about SWIG here? I've never written a
wrapper.


>
> I am afraid I can't help you more.
>
> Stani

Thanks again,

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


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Roy Smith
In article 
<[EMAIL PROTECTED]>,
 Nicola Musatti <[EMAIL PROTECTED]> wrote:

> Yet I'm convinced that even such partial guarantee is worth having.

Partial guarantees are like being a little bit pregnant.
-- 
http://mail.python.org/mailman/listinfo/python-list


Getting stdout from other processes

2008-02-22 Thread Matthias Vogelgesang
Hello,
as I found out, it is possible to get the output of other programs
using os.popen() and read from it. However this method is blocking for
server processes and programs that don't stop immediately. Has anyone
an idea how to get the output of such programs?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article of interest: Python pros/cons for the enterprise

2008-02-22 Thread Bruno Desthuilliers
Nicola Musatti a écrit :
> On Feb 22, 9:03 am, Bruno Desthuilliers  [EMAIL PROTECTED]> wrote:
>> Nicola Musatti a écrit :
> [...]
>>> So, yes, your big company is
>>> likely to be safer with newbie C++ programmers than with Python newbie
>>> programmers.
>> Sorry but I don't buy your arguments.
> 
> I suspect nobody seriously does, not even in C++ newsgroups ;-)

Mmm... Feel like I've been trolled !-)

>>> Had we been speaking of productivity... but we weren't, were we?
>> Should we ?-)
> 
> Oh, I'm convinced that Python wins in many contexts, but I believe
> that it has more to do with the number of batteries that come with the
> package rather than to its being a dynamically typed language. Is this
> controversial enough? ;-)

Brillant !-)

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


Re: Globals or objects?

2008-02-22 Thread Steven D'Aprano
On Fri, 22 Feb 2008 03:16:12 -0800, MartinRinehart wrote:

> D'Aprano's discussion is persuasive but only in the case where you do
> not want multiple actors updating a single value. In my case multiple
> actors have legitimate interest in updating the value. (Actors within a
> single thread, fortunately.)

You missed the point. It's not whether you only have *one* actor has to 
update the value, or more than one. It's about having loose coupling 
between the actors and the value.

Consider a thought experiment. Suppose somebody wrote a math library that 
worked something like this:

# contents of maths.py
x = 0.0  # default value for global x

def sin():
taylor = x + x**2/2 + x**3/6
# Taylor's expansion of sine. (I think.)
return taylor


and so forth. To use this library, you would have to do this:

savex = maths.x
maths.x = 0.1
y = maths.sin()  # multiple actors with a legitimate need
z = maths.cos()  # to access a single global value
maths.x = savex


The functions are tightly coupled to x, and can't operate on anything 
else other than x, so your program ends up being filled with wasteful 
code storing the value of x, setting it to a value, then restoring it.

I think we will all agree that the above is a ridiculous way to write 
code. But what you're doing differs from it only in degree, not kind: all 
you're doing is replacing x with counter.

In your example, you say you have "multiple actors [with a] legitimate 
interest in updating the [global] value." That's fine. But that *group* 
of actors still behaves as a single entity. What happens when you have 
two *groups*?

savecounter = counter
foo(actor1)  # All these actors are in the same group:
foo(actor3)  # "Odd" actors.
foo(actor5)
print counter
counter = savecounter
# But these are in a different group:
foo(actor2)  # "Even" actors.
foo(actor4)

As soon as you have multiple groups, globals become a millstone around 
your neck because of that tight coupling: your function foo() can't 
operate on any value except counter. That makes testing very hard, 
because your test functions can't provide their own counters.

Now, there are times where using globals is acceptable *in spite of* this 
problem. For example, I will often use globals to write a piece of quick-
and-dirty through-away code. Or as a first draft: write a bit of code 
using a global, get the basic algorithm working, then modify it to work 
with a parameter instead. Or perhaps you've decided that you really 
*want* that tight coupling. And you can't avoid globals altogether, 
because there's always going to be a global scope.


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


  1   2   >