Creating custom formatter function

Terry Reedy tjreedy at udel.edu
Mon Feb 16 21:42:22 CET 2009


Garrett Cooper wrote:
> 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):

To clarify error message, "print fmt, message, args" here

>         message = message % args

This line

> 
>     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)

does not quite match this one, so one is not copy/pasted.

> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list