NOTE: It was brought to my attention that my last e-mail message arrived
completely garbled. Here it is again, as I meant to send it. My Apologies to
everyone.
> >
> > How about this?
> >
> >>>> from itertools import *
> >>>> def fixlen(iterable, len, item=None):
> > ... return islice(chain(iterable, repeat(item)), len)
> > ...
> >>>> list(fixlen([1, 2, 3], 5))
> > [1, 2, 3, None, None]
> >
>
> I like this. Maybe an addition to itertools?
>
> +1
+1 from me too.
Way better than the generator I proposed. Having this as a function also
makes it applicable to much more than just one use case.
If you see the following code:
a,b,c,d = fixlen(str.split(),4)
You immediately know it's not a regular unpacking operation; it will be the
first place you look if something goes wrong.
Vitor
Just realized i clicked "reply" instead of 'reply to all"
---------- Forwarded message ----------
From: Guillaume Chereau <charlie137(a)gmail.com>
Date: Wed, Jan 21, 2009 at 12:00 PM
Subject: Re: [Python-ideas] standard library proposal for "tasklets"
using generators
To: Guido van Rossum <guido(a)python.org>
Thanks for the answer,
I understand the concern here.
I still think having such a library, if not as a standard python lib,
but at least as an independent one, that could then be used by all the
project using such pattern (twisted, kiwi, gobject, etc.) would be a
good idea.
I will try to contact those different projects for suggestions.
I also have to say one of my secret hope was that the pypy interpreter
could eventually understand this kind of 'tasklet' generators and
generate C or other language code out of it, since pypy already has
support for tasklets, but using a syntax that can't be interpreted by
cpython.
- Guillaume
On Wed, Jan 21, 2009 at 10:56 AM, Guido van Rossum <guido(a)python.org> wrote:
> The pattern of invoking all sorts of stuff that may wait indefinitely
> using yield has been proposed before, but I find it creates rather
> awkward code compared to using plain threads. Not something I'd like
> to encourage by having standard decorators and other APIs around this
> idea. I believe Twisted has something like this, and that's probably a
> good place to isolate such patterns; in their case they really
> want/need this, so they put up with the awkwardness. Putting the
> necessary decorators/APIs in the language doesn't do much about the
> awkwardness, so I don't think we should do this.
>
> On Tue, Jan 20, 2009 at 6:36 PM, Guillaume Chereau <charlie137(a)gmail.com> wrote:
>> Hello,
>> Thanks to the introduction of new generator features, as described in
>> PEP 0342, it is possible to write what I would call "tasklet" (I am no
>> sure what should be the term exactly).
>> tasklet are a way to write callback based code that looks like thread. Example :
>>
>> (assuming we have a function sleep(sec, callback) that will call the
>> callback after `sec` seconds)
>>
>> @tasklet
>> task1():
>> print "sleep"
>> yield Wait(sleep, 5)
>> print "called after sleep"
>> yield 10 # returned value
>>
>> @tasklet
>> task2():
>> for i in range(10):
>> value = yield task1()
>>
>> (the same thing using callback only would be quite difficult to read)
>>
>> The library should provide -the names are not very important- a
>> Tasklet class, plus a tasklet decorator to turn any generator into a
>> tasklet, plus a Wait function to turn a callback function into a
>> tasklet, plus maybe a few other functions.
>>
>> In many cases this kind of library would be very useful. I am
>> currently using something very similar for a project I am working on
>> [0]. the kiwi project also have a similar library [1] (but the syntax
>> is not really nice because they don't take advantage of the PEP 0342
>> features.)
>>
>> I guess more and more people may try to use similar library in the
>> future, but the problem is that the implementation of it is really not
>> trivial, so I would like to suggest having such a library in the
>> standard set of python libraries.
>>
>> Do you think this is a good idea ?
>>
>> cheers,
>>
>> Guillaume
>>
>> [0] http://git.openmoko.org/?p=tichy.git;a=blob;f=tichy/tasklet.py
>> [1] http://www.async.com.br/projects/kiwi/api/kiwi.tasklet.html
>>
>> --
>> http://charlie137.blogspot.com/
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas(a)python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
>
>
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/)
>
--
http://charlie137.blogspot.com/
--
http://charlie137.blogspot.com/
Hello,
Thanks to the introduction of new generator features, as described in
PEP 0342, it is possible to write what I would call "tasklet" (I am no
sure what should be the term exactly).
tasklet are a way to write callback based code that looks like thread. Example :
(assuming we have a function sleep(sec, callback) that will call the
callback after `sec` seconds)
@tasklet
task1():
print "sleep"
yield Wait(sleep, 5)
print "called after sleep"
yield 10 # returned value
@tasklet
task2():
for i in range(10):
value = yield task1()
(the same thing using callback only would be quite difficult to read)
The library should provide -the names are not very important- a
Tasklet class, plus a tasklet decorator to turn any generator into a
tasklet, plus a Wait function to turn a callback function into a
tasklet, plus maybe a few other functions.
In many cases this kind of library would be very useful. I am
currently using something very similar for a project I am working on
[0]. the kiwi project also have a similar library [1] (but the syntax
is not really nice because they don't take advantage of the PEP 0342
features.)
I guess more and more people may try to use similar library in the
future, but the problem is that the implementation of it is really not
trivial, so I would like to suggest having such a library in the
standard set of python libraries.
Do you think this is a good idea ?
cheers,
Guillaume
[0] http://git.openmoko.org/?p=tichy.git;a=blob;f=tichy/tasklet.py
[1] http://www.async.com.br/projects/kiwi/api/kiwi.tasklet.html
--
http://charlie137.blogspot.com/
From the department of doomed ideas:
data = f.read() with open("file.txt") as f
Has the idea of a with-expression already been preemptively shot down?
I guess the objection is that the above could easily be rewritten as
data = read_and_close("file.txt")
if one defines the appropriate function. The counter-objection is that
why do I need to write a micro-function when the with-statement
already does what I want, except it takes up two lines when it would
still be clear as one line. But then the counter-counter-objection is
that you can write
with open("file.txt") as f: data = f.read()
today. To which the counter-counter-counter-objection is, yeah, but
that looks cluttered and ugly in a way that the first example
doesn't... Except the first example is sort of cluttered.
I dunno, what do other people think about this? Doomed or super-doomed?
-- Carl
Hello,
I have entered this yesterday : http://bugs.python.org/issue4972.
about the fact that ftplib.FTP could be
used in a "with" loop.
But I spotted that imaplib.IMAP4/IMAP4_SSL and smtplib.SMTP could be
equipped as well,
so an __exit__ call takes care of closing or quiting.
This change could be done together with some examples in the documentation.
If this sounds like a good idea, I can start a patch in #4972,
Regards
Tarek
--
Tarek Ziadé | Association AfPy | www.afpy.org
Blog FR | http://programmation-python.org
Blog EN | http://tarekziade.wordpress.com/
I think defaultdict would be more useful if one could initialize it
with the option to have the key that is being looked up passed to
default_factory.
--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games
Sometimes multiple arguments in a with statement would be convinient.
with open('in.txt','rt'), \
open('out.txt','wt'),\
open('err.txt','wt') \
as fstdin, fstdout, fstderr:
But today this has to be written as:
with open('in.txt','rt') as fstdin:
with open('out.txt','wt') as fstdout:
with open('err.txt','wt') as fstderr:
with the effect of causing multiple redundant levels of intendation.
Regards,
Sturla Molden
Hello,
What about having an option in getpass to store and reuse passwords in
system keyrings ?
getpass(prompt[, stream])
would become:
getpass(prompt[, stream, keyring])
where keyring would be a callable that can be use to retrieve the
password from a keyring system
and store it the first time.
The getpass module could provide some keyring support for:
- ssh-agent under Linux
- keychain under Mac OS X
- ...
And let the developers use their own keyring system by providing a callable.
Regards
Tarek
--
Tarek Ziadé | Association AfPy | www.afpy.org
Blog FR | http://programmation-python.org
Blog EN | http://tarekziade.wordpress.com/
I've been using Python generators for a while now. e.g.
a=(i for i in range(10))
a.next()
a.next()
...etc.
I also find the "if" clause handy:
a = (i for i in range(10) if i%2==0)
(I know that range(0,12,2) will do the same thing, but it's the idea I
like, especially for more complex predicates.)
I would like to know if anyone has thought of adding a "while" clause
as well, like this:
a = (i for i in range(100) while i <=50)
(Again, this could be done with range(0,51) but then the predicate can
be more complicated.)
Why would this be helpful? Consider the "in ..." part of the
generator. You could be referring to something that is ordered --
sorted names for example. Then you might want to stop your iterator
when you reach "Morgan" so you would like to write:
name = (n for n in names while n <= "Morgan")
name.next()
...etc...
Of course,
name = (n for n in names if n <= "Morgan")
will work, but it will look at every item in "names." Since "names"
is sorted, this is a waste of time. Imagine you want to stop at
"Baker". Your "if" clause will look at and discard most names in the
list, assuming a normal distribution of English names.
Now, you could do the same thing with a generator function:
def leMorgan(names):
i = 0
while names[i] <= "Morgan":
yield names[i]
i+=1
and use it like this:
name=leMorgan(names)
name.next()
...etc...
but I think that adding a while clause to the generator expression is
simpler and clearer (and it keeps it all in one place!)
I know that this is functionally equivalent to the takewhile function
in itertools and my motivation is the same. I just think that this
could be done nicely within the context of the existing syntax. This
is also convenient when the "in ..." clause refers to another
(possibly infinite) generator. For a simple example, suppose I want
to run through some natural numbers. I can write an infinte generator
function like this:
def genN(n=0):
while 1:
yield n
n+=1
Then, I might use this in another generator:
p = (n for n in genN() if prime(n) while n <= 100)
to get the prime numbers under 100 (assuming I have a predicate
"prime" that works as one would hope). Of course you could do this
with range(101) instead of genN, but this is just an example to
demonstrate the idea.
Without the "while" clause, this will not work:
p = (n for n in genN() if prime(n) if n <= 100)
This will actually NEVER terminate, since EVERY item in genN() (which
is an infinite generator) will be tested for <= 100.
So...What do others think? Is this a loony idea? Is there a better
way? Also, can anyone think of a similarly syntax to replicate the
dropwhile function?