Creating custom formatter function

Discussion in 'Python' started by Garrett Cooper, Feb 16, 2009.

  1. 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
    Garrett Cooper, Feb 16, 2009
    #1
    1. Advertising

  2. Garrett Cooper

    Alan G Isaac Guest

    On 2/16/2009 6:50 AM Garrett Cooper apparently wrote:
    > 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
    Alan G Isaac, Feb 16, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Yang

    About ViewState and LOS formatter

    Edward Yang, Nov 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    876
    bruce barker
    Nov 22, 2004
  2. David Herbst
    Replies:
    1
    Views:
    3,229
    David Herbst
    Jan 29, 2006
  3. Timo Nentwig

    JSP formatter

    Timo Nentwig, Aug 1, 2004, in forum: Java
    Replies:
    4
    Views:
    1,130
  4. Gregor Horvath

    Logging: Formatter: name of the function

    Gregor Horvath, Dec 23, 2005, in forum: Python
    Replies:
    2
    Views:
    363
    Sylvain Defresne
    Dec 23, 2005
  5. A.M-SG

    Switching from XML formatter to Binary Formatter

    A.M-SG, Nov 21, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    321
    Steven Cheng[MSFT]
    Nov 22, 2005
Loading...

Share This Page