Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Creating custom formatter function

Thread Tools

Creating custom formatter function

Garrett Cooper
Posts: n/a
Hello Python folks,
I have a function where I'd like to prefix a format string via a
`prefix' string. The definition of the base method is as follows:

def print_message(prefix, out_stream, fmt, *args, **kwargs):
""" Print out [prefix]: [message] """

message = fmt

if 0 < len(kwargs.keys()):
message = message % kwargs

if 0 < len(args):
message = message % args

out_stream.write(message + "\n")

My python 2.4.5 interpreter fails at `message % args' claiming the

File "", line 10, in print_message
message = message % (args)
TypeError: not all arguments converted during string formatting

Thus I was wondering what the proper means was for formatting
strings. I'm new to this portion of Python, so I obviously didn't
apply the right syntax.
Reply With Quote
Alan G Isaac
Posts: n/a
On 2/16/2009 6:50 AM Garrett Cooper apparently wrote:
> I was wondering what the proper means was for formatting
> strings.

As for the last: "values must be a tuple with exactly the number
of items specified by the format string, or a single mapping
object (for example, a dictionary)" unless you need only a
single argument in your format string.

Alan Isaac
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Enterprise Library Jan 2006 - custom exception formatter fails unless running debugger David Herbst ASP .Net 1 01-29-2006 04:02 PM
Logging: Formatter: name of the function Gregor Horvath Python 2 12-23-2005 09:34 PM
Switching from XML formatter to Binary Formatter A.M-SG ASP .Net Web Services 1 11-22-2005 08:33 AM
JSP formatter Timo Nentwig Java 4 12-31-2004 06:58 AM
About ViewState and LOS formatter Edward Yang ASP .Net 1 11-22-2004 07:07 PM