I have a weird request.
I want to be able to say
def myvalues():
while True:
# stuff that determines a new somevalue
yield somevalue
x = "Hello, %s, this is a %s with %s and %s on top of %s" % myvalues()
y = "Yes it's true that %s has way too many %s's" % myvalues()
I was hoping that myvalues() would be iterated over, but instead the
interpreter gives me a "TypeError: not enough arguments for format string"
error. I tried tuple(myvalues()) and I think that kinda works but of
course myvalues goes into an infinite loop. myvalues will not know before
hand how many times it will be called.
Is there actually a simple way of doing this that I'm overlooking?
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =--- 7 1932
The generators are not list-type objects, but iterators. Because the %
operator does not operate on iterators directly (because, presumably, you
may be wanting to print the iterator itself, not the items it iterates
over), you must construct a list out of it, which can be done very easily,
as you can see.
x = "Hello, %s, this is a %s with %s and %s on top of %s" % [ i for i in
myvalues()]
y = "Yes it's true that %s has way too many %s's" % [i for i in myvalues()]
marduk wrote: I have a weird request.
I want to be able to say
def myvalues(): while True: # stuff that determines a new somevalue yield somevalue
x = "Hello, %s, this is a %s with %s and %s on top of %s" % myvalues() y = "Yes it's true that %s has way too many %s's" % myvalues()
I was hoping that myvalues() would be iterated over, but instead the interpreter gives me a "TypeError: not enough arguments for format string" error. I tried tuple(myvalues()) and I think that kinda works but of course myvalues goes into an infinite loop. myvalues will not know before hand how many times it will be called.
Is there actually a simple way of doing this that I'm overlooking? ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World!100,000 Newsgroups ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
--
"Calvin Spealman" <ca****@ironfroggy.com> wrote in message
news:11****************@ironfroggy.com... The generators are not list-type objects, but iterators. Because the % operator does not operate on iterators directly (because, presumably, you may be wanting to print the iterator itself, not the items it iterates over), you must construct a list out of it, which can be done very
easily, as you can see.
x = "Hello, %s, this is a %s with %s and %s on top of %s" % [ i for i in myvalues()] y = "Yes it's true that %s has way too many %s's" % [i for i in
myvalues()]
list(myvalues()) is even more concise and more direct than the list comp
tjr
On Mon, 26 Jul 2004, Calvin Spealman wrote: The generators are not list-type objects, but iterators. Because the % operator does not operate on iterators directly (because, presumably, you may be wanting to print the iterator itself, not the items it iterates over), you must construct a list out of it, which can be done very easily, as you can see.
Not quite, because for the same reason as generators, lists are passed to
% as a single argument. The arguments must be contained in a tuple
(tuple(myvalues()) does nicely).
On Tue, 27 Jul 2004 09:15:57 -0400, Christopher T King <sq******@wpi.edu> wrote: On Mon, 26 Jul 2004, Calvin Spealman wrote:
The generators are not list-type objects, but iterators. Because the % operator does not operate on iterators directly (because, presumably, you may be wanting to print the iterator itself, not the items it iterates over), you must construct a list out of it, which can be done very easily, as you can see.
Not quite, because for the same reason as generators, lists are passed to % as a single argument. The arguments must be contained in a tuple (tuple(myvalues()) does nicely).
does anyone else get the feeling that (str %) should have a third
behaviour wrt generators? By this I mean that str % seq is one
behaviour, str % dict is another, and there should be a str % iter,
probably consuming items from the iterator up to the number of
arguments of the format string?
--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
On Tue, 27 Jul 2004, John Lenton wrote: does anyone else get the feeling that (str %) should have a third behaviour wrt generators? By this I mean that str % seq is one behaviour, str % dict is another, and there should be a str % iter, probably consuming items from the iterator up to the number of arguments of the format string?
The only argument I see against this is that it could break existing code
of the form '%r' % some_object, where some_object could easily be an
iterable object (say, a numarray array). Of course, limiting it to only
generators rather than iterators in general would fix this, but then the
benefit gained seems too small to justify the cost of implementing another
exception to the rule.
On Wed, 21 Jul 2004 15:21:50 -0500, marduk <ma****@python.net> wrote: I have a weird request.
I want to be able to say
def myvalues(): while True: # stuff that determines a new somevalue yield somevalue
x = "Hello, %s, this is a %s with %s and %s on top of %s" % myvalues() y = "Yes it's true that %s has way too many %s's" % myvalues()
I was hoping that myvalues() would be iterated over, but instead the interpreter gives me a "TypeError: not enough arguments for format string" error. I tried tuple(myvalues()) and I think that kinda works but of course myvalues goes into an infinite loop. myvalues will not know before hand how many times it will be called.
Is there actually a simple way of doing this that I'm overlooking?
If you are willing to modify your format strings so they call on a
mapping (with ignored key '' in this case), you can supply a mapping that will do the job: def myvalues():
... values = 'one two three'.split() + [4,5,6]
... while True:
... # stuff that determines somevalue
... for somevalue in values:
... yield somevalue
... mapping = type('',(),{'__getitem__':lambda s,k,g=myvalues().next:g()})() x = "Hello, %s, this is a %s with %s and %s on top of %s".replace('%','%()') % mapping y = "Yes it's true that %s has way too many %s's".replace('%','%()') % mapping x
'Hello, one, this is a two with three and 4 on top of 5' y
"Yes it's true that 6 has way too many one's"
You can obviously define mapping as an instance of a more conventionally defined class also,
and pass the generator to its constructor. Maybe even differentiate among multiple keyword-named
generators if you want to feed in several value streams and not ignore keys from the format.
You don't want to use .replace('%','%()') if your format string already has some '%(...) or
'%%' instances in it, of course.
Hm, maybe if '%s' without mapping names were interpreted as a mapping with an integer key
whose value was the position of the %s in the format, then a mapping could (if it had the
integer key in question, otherwise its __str__ method would be called) be used with ordinary
formats as well. E.g.,
'%s %04x' % mapping
would get mapping[0] and mapping[1] as the values to convert per the format. Note that
that's not equivalent to '%(0)s %(1)04x' which would use keys '0' and '1' instead of 0 and 1.
You could of course make a mapping that would ignore the keys for some purpose, as I did above.
Just a wild idea, don't take too seriously ;-)
Regards,
Bengt Richter
On Tue, 27 Jul 2004 11:28:29 -0400, Christopher T King <sq******@WPI.EDU> wrote: On Tue, 27 Jul 2004, John Lenton wrote:
does anyone else get the feeling that (str %) should have a third behaviour wrt generators? By this I mean that str % seq is one behaviour, str % dict is another, and there should be a str % iter, probably consuming items from the iterator up to the number of arguments of the format string?
The only argument I see against this is that it could break existing code of the form '%r' % some_object, where some_object could easily be an iterable object (say, a numarray array). Of course, limiting it to only generators rather than iterators in general would fix this, but then the benefit gained seems too small to justify the cost of implementing another exception to the rule.
You could subtype str and override __mod__ to do it (which I'll leave
as an exercise ;-) e.g.,
class MYFMT(str):
def __mod__(self, args):
...
and then use it like
MYFMT('formatted string args from generator: %s %r %(mapping_key)s') % generator
Then you could make it do anything you like without changing current python, even
mixing in mapping usage if you wanted to.
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: The_Incubator |
last post by:
As the subject suggests, I am interested in using Python as a scripting
language for a game that is primarily implemented in C++, and I am also
interested in using generators in those scripts...
...
|
by: Carlos Ribeiro |
last post by:
As a side track of my latest investigations, I began to rely heavily
on generators for some stuff where I would previsouly use a more
conventional approach. Whenever I need to process a list, I'm...
|
by: Michael Sparks |
last post by:
Hi,
I'm posting a link to this since I hope it's of interest to people here :)
I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework,
and it's been published as a BBC...
|
by: Grasshopper |
last post by:
Hi,
I am automating Access reports to PDF using PDF Writer 6.0. I've
created a DTS package to run the reports and schedule a job to run this
DTS package. If I PC Anywhere into the server on...
|
by: Just Me |
last post by:
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:
string1,string2,string3
Then read them later.
Suppose I want to write...
|
by: Klint Gore |
last post by:
Does anyone know of a mailing list for application developers using
postgres? It'd probably be more relevant than pgsql-general for my
question.
Failing that, what do people use to generate...
|
by: walterbyrd |
last post by:
I would like to put together a very simple inventory program. When I
ship an item, and edit the quantity; I would like the quantity _on_hand
to auto-decrement, and the quantity_to_reorder to...
|
by: VMI |
last post by:
In the next few weeks, we'll be discussing what standards will be used
for our web development, and one of the suggestions was to use a code
generator (in our case, the first version of LLBLGen)....
|
by: Martin Sand Christensen |
last post by:
Hi!
First a bit of context.
Yesterday I spent a lot of time debugging the following method in a
rather slim database abstraction layer we've developed:
,----
| def selectColumn(self,...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |