473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String formatters with variable argument length

I've been trying to get the string formatting operator (%) to work with
more arguments than the format string requires, but I can find no way to
do that. For example:
>>"%i" % 10
'10'
>>"i" % 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: not all arguments converted during string formatting

The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.

Could it be as I fear, that it is impossible?

Fredrik Tolf
Nov 30 '06 #1
8 1991
How do you know which ones to use? Your best bet is to write a handler
for the TypeError and give a meaningful error message.

Fredrik Tolf wrote:
I've been trying to get the string formatting operator (%) to work with
more arguments than the format string requires, but I can find no way to
do that. For example:
>"%i" % 10
'10'
>"i" % 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: not all arguments converted during string formatting

The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.

Could it be as I fear, that it is impossible?

Fredrik Tolf
Dec 1 '06 #2
Fredrik Tolf wrote:
I've been trying to get the string formatting operator (%) to work with
more arguments than the format string requires, but I can find no way to
do that. For example:
>"%i" % 10
'10'
>"i" % 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: not all arguments converted during string formatting

The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.

Could it be as I fear, that it is impossible?
Three approaches spring to mind. In descending order of my preference:

(a) don't do that

(b) parse the format string, counting the number of args required. If
the user has supplied more, throw them away.

(c) wrap your execution of format_string % args in a try/except
bracket. If you get a TypeError with that message [not guaranteed to
remain constant in the future], throw away the last arg and go around
again.

As a matter of curiosity, why don't you want the user to consume all
the arguments? Don't they get even a teensy-weensy warning message? Are
you writing a Perl interpreter in Python?

Cheers,
John

Dec 1 '06 #3

John Machin wrote:
Fredrik Tolf wrote:
I've been trying to get the string formatting operator (%) to work with
more arguments than the format string requires, but I can find no way to
do that. For example:
>>"%i" % 10
'10'
>>"i" % 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: not all arguments converted during string formatting

The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.

Could it be as I fear, that it is impossible?

Three approaches spring to mind. In descending order of my preference:

(a) don't do that

(b) parse the format string, counting the number of args required. If
the user has supplied more, throw them away.

(c) wrap your execution of format_string % args in a try/except
bracket. If you get a TypeError with that message [not guaranteed to
remain constant in the future], throw away the last arg and go around
again.

As a matter of curiosity, why don't you want the user to consume all
the arguments? Don't they get even a teensy-weensy warning message? Are
you writing a Perl interpreter in Python?
Another approach: instead of the "%s %.2f %.5f%%" style, give the users
a list of names of each possible arg so that they can do e.g.

"The rate for %(name)s is %(rate).5f%%"
or
"Amount borrowed: $%(amount).2f; borrower: %(name)s"

HTH,
John

Dec 1 '06 #4
On Thu, 2006-11-30 at 16:26 -0800, John Machin wrote:
Fredrik Tolf wrote:
[...]
The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.

Three approaches spring to mind. In descending order of my preference:

(a) don't do that
It would be a possibility, since all current uses actually do have the
right number of parameters. I would just like to keep the option
available.
(b) parse the format string, counting the number of args required. If
the user has supplied more, throw them away.
I was thinking of that, but it just seems far too ugly.
(c) wrap your execution of format_string % args in a try/except
bracket. If you get a TypeError with that message [not guaranteed to
remain constant in the future], throw away the last arg and go around
again.
That might be a good possibility. Thanks for the idea! I do consider it
quite a bit ugly, but that often happens when languages try to police
programmers... :P
As a matter of curiosity, why don't you want the user to consume all
the arguments? Don't they get even a teensy-weensy warning message? Are
you writing a Perl interpreter in Python?
Well basically, I'm rewriting a autodownloader for a file-sharing
network in Python (previously written as a bash script, using the printf
command), and I have a number of files scattered over my hard drive
specifying search expressions, into which a potentially optional episode
number can be inserted using sprintf-like arguments (using
fsexpr="`printf "$sexpr" "$curep"`" in bash). I would like to keep it as
a printf parameter, in order to be able to write e.g. %02i, and I would
like to keep it optional, for downloading non-episoded stuff.

I couldn't help noticing that the named variant of the % operator (using
a dict, that is) doesn't require all its arguments to be consumed. Using
that would require me to rewrite *all* the existing files, though.

Anyway, thanks!

Fredrik Tolf
Dec 1 '06 #5
Could it be as I fear, that it is impossible?

p'shaw, you underestimate the power of the Python...

I think this is a poor solution (I would go with the solution in John
Machin's second post) but here it is anyway since it does essentially
what you asked for:

Expand|Select|Wrap|Line Numbers
  1. def print_data(fmtstr):
  2. data = (10,11,12,13)
  3. for i in xrange(len(data)):
  4. try:
  5. print fmtstr %data[:i+1]
  6. break
  7. except:
  8. pass
  9.  
Dec 1 '06 #6
Fredrik Tolf wrote:
On Thu, 2006-11-30 at 16:26 -0800, John Machin wrote:
Fredrik Tolf wrote:
[...]
The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments. docs.python.org
doesn't seem to have any clues on how to achieve this, and I can't think
of what to google for.
Three approaches spring to mind. In descending order of my preference:

(a) don't do that

It would be a possibility, since all current uses actually do have the
right number of parameters. I would just like to keep the option
available.
(b) parse the format string, counting the number of args required. If
the user has supplied more, throw them away.

I was thinking of that, but it just seems far too ugly.
what's ugly about this:
[untested]:

def count_format_ar gs(s):
pending = False
count = 0
for c in s:
if c == "%":
# doubled % chars aren't counted
pending = not pending
elif pending:
count += 1
pending = False
return count

output = format % arglist[:count_format_a rgs(format)]

>
(c) wrap your execution of format_string % args in a try/except
bracket. If you get a TypeError with that message [not guaranteed to
remain constant in the future], throw away the last arg and go around
again.

That might be a good possibility. Thanks for the idea! I do consider it
quite a bit ugly, but that often happens when languages try to police
programmers... :P
As a matter of curiosity, why don't you want the user to consume all
the arguments? Don't they get even a teensy-weensy warning message? Are
you writing a Perl interpreter in Python?

Well basically, I'm rewriting a autodownloader for a file-sharing
network in Python (previously written as a bash script, using the printf
command), and I have a number of files scattered over my hard drive
specifying search expressions, into which a potentially optional episode
number can be inserted using sprintf-like arguments (using
fsexpr="`printf "$sexpr" "$curep"`" in bash). I would like to keep it as
a printf parameter, in order to be able to write e.g. %02i, and I would
like to keep it optional, for downloading non-episoded stuff.

I couldn't help noticing that the named variant of the % operator (using
a dict, that is) doesn't require all its arguments to be consumed. Using
that would require me to rewrite *all* the existing files, though.
So offer the named variant as an option for new users or new uses by
old users.

Cheers,
John

Dec 1 '06 #7
John Machin wrote:
Fredrik Tolf wrote:
The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments.
what's ugly about this:
[untested]:

def count_format_ar gs(s):
pending = False
count = 0
for c in s:
if c == "%":
# doubled % chars aren't counted
pending = not pending
elif pending:
count += 1
pending = False
return count

output = format % arglist[:count_format_a rgs(format)]
Keep in mind, though, that it doesn't take '*' into account:
>>count_format_ args("%*.*f")
1
>>"%*.*f" % (3,2,1)
'1.00'

And just because I don't think I've seen it before:
>>count_format_ args("%42%")
1
>>"%42%" % ()
' %'

Peter

Dec 1 '06 #8

Peter Otten wrote:
John Machin wrote:
Fredrik Tolf wrote:
The thing is, I want to get format strings from the user, and I don't
want to require the user to consume all the arguments.
what's ugly about this:
[untested]:

def count_format_ar gs(s):
pending = False
count = 0
for c in s:
if c == "%":
# doubled % chars aren't counted
pending = not pending
elif pending:
count += 1
pending = False
return count

output = format % arglist[:count_format_a rgs(format)]

Keep in mind, though, that it doesn't take '*' into account:
>count_format_a rgs("%*.*f")
1
>"%*.*f" % (3,2,1)
'1.00'
A good point. Adding checking for "*" would make it rather ugly, as "*"
is special only inside a conversion specifier.
>
And just because I don't think I've seen it before:
>count_format_a rgs("%42%")
1
>"%42%" % ()
' %'
Hmmm ... I hadn't seen that before either. I would have said if shown
that input that I could have put in an error check that pending was not
true at the end of the loop, but was relying instead on an exception
from the formatting operator.

Even better: >>"%-42%" % ()
'% '

:-)
Before gentle readers consider nominating the Python core dev team to
thedailyWTF.com , they might wish to:
(1) read the Python documentation
(http://docs.python.org/lib/typesseq-strings.html)
[it is not a simple escape mechanism; the second % is a "conversion
type"]
(2) compare those docs carefully with K&R v2 section B1.2 Formatted
Output (pp 243-245)
(3) note that not everything that emanated from Murray Hill NJ obeyed
the Law of Least Astonishment
:-)

Cheers,
John

Dec 1 '06 #9

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

Similar topics

5
3278
by: nboutelier | last post by:
Scenario: you enter "foo bar" into a text field... Is it possible through javascript to select/highlight just "foo"? formObject.select() selects all. I need to select only part of the string. -Nick
7
4349
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
51
8252
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
7
2278
by: BluDog | last post by:
Hi I am trying to store an image in an xml file, i vcan store it fine but the retreval is a bit of a problem: To String: Dim ms As New IO.MemoryStream Dim arrImage() As Byte Me.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
10
4568
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which Product Is Null. SELECT COUNT(*) AS CountofNullProdcut
3
8701
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned string length is larger than 16. An ArgumentOutOfRangeExceptoin is raised. However, if string is used, no exception. I searched this topic. It is said that StringBuilder is preferred in this case, because the performance is better. I don't think so....
7
2701
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
10
9072
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is Unicode, UCS-2LE(BMP) in Win32, and UCS4 in Linux?). I have 2 ways to do the job:
30
2719
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.