Creating custom formatter function

Garrett Cooper yanegomi at gmail.com
Mon Feb 16 06:50:22 EST 2009


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



More information about the Python-list mailing list