Creating custom formatter function

G

Garrett Cooper

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:

#START CODE
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")
#END CODE

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

File "logging.py", 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.
TIA!
-Garrett
 
A

Alan G Isaac

I was wondering what the proper means was for formatting
strings.

http://docs.python.org/library/string.html#string-formatting

http://docs.python.org/library/string.html#template-strings

http://docs.python.org/library/stdtypes.html#string-formatting

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.

hth,
Alan Isaac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top