[colug-432] Code check

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Sat Aug 3 02:28:36 EDT 2013


On Sat, 3 Aug 2013 01:03:01 -0400, Tom Hanlon <tom at functionalmedia.com> wrote:

> I have this snippet of borrowed code.
> 
> def showPayload(msg):
>     payload = msg.get_payload()
> 
>     if msg.is_multipart():
>         div = ''
>         for subMsg in payload:
>             print div
>             showPayload(subMsg)
>             div = '------------------------------'
>     else:
>         print msg.get_content_type()
>         print payload[:200]

By the way, a Pythonic way of doing the following: 

    div = '------------------------------'  # How many characters are there? 

is:

    div = 30 * '-'

> ... the above code works.
> 
> What goes to stdout is exactly what I want. However I want to capture it to
> a variable.

Python has a nifty idea called "file-like objects". 
If an object has a write method, Voila! you can write to that object. 
So make an object that has a write method, 
which appends to some variable. 
Study the tee[1] stuff. 

Try the following untested code: 

import sys

class Buffer:
    def __init__(self):
        self.buffer = ''

    def write(self, s):
        self.buffer += s

    def read(self):
        s = self.buffer
        self.buffer = ''
        return s

def show_payload(msg):
    b = Buffer()

    payload = msg.get_payload()

    if msg.is_multipart():
        div = ''
        for subMsg in payload:
            print >>b, div
            # Printing show_payload(subMsg) is likely what you need. 
            print >>b, show_payload(subMsg)
            div = '------------------------------'
    else:
        print >>b, msg.get_content_type()
        print >>b, payload[:200]

    return b.read()[:]  # I forget if copying with [:] is necessary. 

I leave the debugging to you. 

But the I/O redirection magic is icky. 
Maybe it's better to rewrite to avoid prints. 

def show_payload(msg):
    payload = msg.get_payload()

    if msg.is_multipart():
        if False:
            # Too hard to read. 
            return '\n' + (30*'-' + '\n').join([show_payload(sub) for sub in payload])
        else:
            shown_sub_payloads = [show_payload(sub) for sub in payload]
            s = ''
            s += '\n'
            s += (30*'-' + '\n').join(shown_sub_payloads)
            return s
    else:
        return '%s\n%s\n' % (msg.get_content_type(), payload[:200])

but that has a different ickiness. At which point I would want 
to review what you are trying to accomplish. 
I forget if I need some str() calls around things. 
I leave that to you to figure out. 

> This however does not work.
> 
> def showPayload2(msg):
>     message_content = ""

Choose a shorter name. 

>     payload = msg.get_payload()
> 
>     if msg.is_multipart():
>         div = ''
>         for subMsg in payload:
>             message_content += div

maybe bug #3: what about newlines? 

>             showPayload2(subMsg)

bug #1: you ignored return value

>             div = '------------------------------'

Ick. I hate counting those. 

>     else:
>         message_content +=  msg.get_content_type()

maybe bug #3: what about newlines? 

>         message_content += payload[:200]

maybe bug #3: what about newlines? 

> 
>     return [message_content]

maybe bug #2: why [] around return value? 

Try the following. Probably two significant things are: 
    s += show_payload3(subMsg) 
    return s  # not return [s]

def show_payload3(msg):
    s = ''
    payload = msg.get_payload()

    if msg.is_multipart():
        div = ''
        for subMsg in payload:
            s += div
            s += show_payload3(subMsg)
            div = 30 * '-'
    else:
        s += msg.get_content_type()
        s += payload[:200]

    return s

But what about newlines? 
I forget if I need some str() calls around things. 

[1] http://mail.python.org/pipermail/centraloh/2013-August/001781.html



More information about the colug-432 mailing list