473,394 Members | 1,699 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,394 software developers and data experts.

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 1985
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_args(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_args(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_args(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_args(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_args(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_args(format)]

Keep in mind, though, that it doesn't take '*' into account:
>count_format_args("%*.*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_args("%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
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....
7
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...
51
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...
7
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...
10
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...
3
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...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
10
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...
30
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.