473,408 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

using generators with format strings

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 =---
Jul 18 '05 #1
7 1937
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 =---


--
Jul 18 '05 #2

"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

Jul 18 '05 #3
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).

Jul 18 '05 #4
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
Jul 18 '05 #5
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.

Jul 18 '05 #6
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
Jul 18 '05 #7
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
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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... ...
3
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...
3
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...
11
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...
5
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...
3
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...
3
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...
4
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)....
13
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.