Re: static variables in Python?

2008-07-29 Thread Bruce Frederiksen
On Tue, 29 Jul 2008 21:31:01 +, kj wrote:

> In <[EMAIL PROTECTED]> Larry Bates <[EMAIL PROTECTED]> writes:
>
> [snip]
> 
> Maybe it's easier to see what I mean with JavaScript:
> 
> function foo() {
>   if (foo.x === undefined) foo.x = expensive_call();
>   return do_stuff_with(foo.x);
> }

def foo():
if not hasattr(foo, 'x'): foo.x = expensive_call()
return do_stuff_with(foo.x)

or, maybe just define foo in two steps:

def foo():
return do_stuff_with(foo.x)

foo.x = expensive_call()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Defunct when using subprocess.Popen

2008-07-30 Thread Bruce Frederiksen
On Wed, 30 Jul 2008 01:56:28 -0300, Gabriel Genellina wrote:

> You should call os.waitpid() after killing the child process, to let the  
> OS free the resources allocated to it.
>

The subprocess.Popen object supports a 'wait' method directly.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Non Continuous Subsequences

2008-07-31 Thread Bruce Frederiksen
On Wed, 30 Jul 2008 09:32:25 -0700, bearophileHUGS wrote:

> This post is not about practical stuff, so if you have little time,
> you may ignore it.
> 
> This is a task of the rosettacode.org site:
> http://www.rosettacode.org/wiki/Non_Continuous_Subsequences
> 
> A subsequence contains some subset of the elements of this sequence,
> in the same order. A continuous subsequence is one in which no
> elements are missing between the first and last elements of the
> subsequence. The task is to enumerate all non-continuous subsequences
> for a given sequence.
> 
> Translating the Scheme code to Python was easy (and I think this is
> quite more readable than the Scheme version):
> 
> def ncsub(seq, s=0):
> if seq:
> x = seq[:1]
> xs = seq[1:]
> p2 = s % 2
> p1 = not p2
> return [x + ys for ys in ncsub(xs, s + p1)] + ncsub(xs, s +
> p2)
> else:
> return [[]] if s >= 3 else []
> 
> Output:
> 
 ncsub(range(1, 4))
> [[1, 3]]
 ncsub(range(1, 5))
> [[1, 2, 4], [1, 3, 4], [1, 3], [1, 4], [2, 4]]
 ncsub(range(1, 6))
> [[1, 2, 3, 5], [1, 2, 4, 5], [1, 2, 4], [1, 2, 5], [1, 3, 4, 5], [1,
> 3, 4], [1, 3, 5], [1, 3], [1, 4, 5], [1, 4], [1, 5], [2, 3, 5], [2, 4,
> 5], [2, 4], [2, 5], [3, 5]]
> 
> 
> 
> But in many cases you can create a lazy code in Python too, even if
> that may be harder. So I have tried to create a lazy version for
> Python, hoping to speed up the code avoiding the creation and joining
> of most/all sublists, but I have failed so far (once I have created a
> lazy Python version, I can probably create a short lazy version in D
> too, my D libs contain most of itertools module too).
> 
> In my failed attempts I have used chain() to join the sublists,
> islice() to slice their items, and iter() to make the management more
> uniform when the input seq is a Python list instead of an xrange, etc.

Try this:

import itertools

def ncsub(seq, s = 0):
if seq:
x = seq[:1]
xs = seq[1:]
p2 = s % 2
p1 = 1 - p2
return itertools.chain(
   itertools.imap(lambda ys: x + ys, ncsub(xs, s + p1)),
   ncsub(xs, s + p2))
else:
return [[]] if s >= 3 else []

>>> ncsub(range(1, 4))

>>> list(ncsub(range(1, 4)))
[[1, 3]]
>>> list(ncsub(range(1, 5)))
[[1, 2, 4], [1, 3, 4], [1, 3], [1, 4], [2, 4]]
>>> list(ncsub(range(1, 6)))
[[1, 2, 3, 5], [1, 2, 4, 5], [1, 2, 4], [1, 2, 5], [1, 3, 4, 5], [1, 
3, 4], [1, 3, 5], [1, 3], [1, 4, 5], [1, 4], [1, 5], [2, 3, 5], [2, 4, 
5], [2, 4], [2, 5], [3, 5]]
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem when reading file

2008-07-31 Thread Bruce Frederiksen
On Thu, 31 Jul 2008 23:44:33 +1000, shrimpy wrote:

> hi every one, i am new to python,
> and coz i want to write a handy command for my linux machine, to find
> a word in all the files which are under the current folder.
> 
> the code is half done, but when i run it, it complain, and i don`t know why???
> 
> can anyone help me have a look at it?
> 
> 
> here is the message it complain
> ===
> file is : qt_plugins_3.3rc
> 
> Traceback (most recent call last):
>   File "./python_script/svn_grep.py", line 34, in 
> searPatten(file,'are','no')
>   File "./python_script/svn_grep.py", line 8, in searPatten
> openFile = open(file, 'r')
> IOError: [Errno 2] No such file or directory: 'qt_plugins_3.3rc'
> 
> 
> here are the code i wrote
> ---code begin
> 
> 
> for root, dirs, files in os.walk(os.getcwd()):
>   for file in files:
>   print "==="
>   print "file is : " + file + "\n"
>   searPatten(file,'are','no')
> 
> ---code end

Try changing that last line to:

 searPatten(os.path.join(root, file), 'are', 'no')


You'll have to import os.path too...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Agnostic fetching

2008-08-01 Thread Bruce Frederiksen
On Fri, 01 Aug 2008 17:05:00 -0700, jorpheus wrote:

> OK, that sounds stupid. Anyway, I've been learning Python for some
> time now, and am currently having fun with the urllib and urllib2
> modules, but have run into a problem(?) - is there any way to fetch
> (urllib.retrieve) files from a server without knowing the filenames?
> For instance, there is smth like folder/spam.egg, folder/
> unpredictable.egg and so on. If not, perhaps some kind of glob to
> create a list of existing files? I'd really appreciate some help,
> since I'm really out of my (newb) depth here.

You might try the os.path module and/or the glob module in the standard
python library.
--
http://mail.python.org/mailman/listinfo/python-list


Micro-threading PEP proposal announcement

2008-08-25 Thread Bruce Frederiksen
I wanted to make everybody aware that I've posted a (rather long and
involved) PEP proposal for adding micro-threading to Python on
python-ideas for feedback and review.

In a nutshell, this proposal implements the Twisted Deferred/Reactor at
the C level so that the Python programmer gets the advantages that Twisted
offers without having to write their code in an event driven style.  Thus,
legacy Python code not written in the Twisted style (Django, TurboGears,
WSGI apps) will gain the benefits of Twisted with almost no additional
work.

This PEP provides similar benefits to GUI toolkits where, again, Python
programmers have been faced with event driven programming.  So using this
PEP, GUI toolkits could hide this event driven programming from the Python
programmer, making GUI programming much easier.  For example, you would no
longer have to use a modal dialog just to make the programming easier.

The C-level Deferreds and Reactor are not made visible to the Python
programmer (as they are in Twisted).

Rather, what is visible is a very simple micro-thread that allows for
"start_and_forget" threads, "parallel" threads (where you're only
interested in the final return value) and fully cooperative threads
communicating over micro-pipes (which, BTW, gives us a new way to write
generators that allows one generator to simply call another one to have
the second generator's output included with its own output without having
to capture and pass back values).

As there is a great deal of traffic on comp.lang.python, I don't expect to
be able to keep up with the posts here and would prefer to discuss this
on python-ideas...

If there is any interest in this, please let me know!  If I don't see any
interest, I'll assume that it's not solving a real problem and will let it
quietly die on the vine...

Thank you for your attention!

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