[colug-432] Code check
Tom Hanlon
tom at functionalmedia.com
Sat Aug 3 01:03:01 EDT 2013
Colug,
If I have to write much more python, I will subscribe to the other lists.
But one more question
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]
I am parsing a mbox file, and the above code works.
What goes to stdout is exactly what I want. However I want to capture it to
a variable.
This however does not work.
def showPayload2(msg):
message_content = ""
payload = msg.get_payload()
if msg.is_multipart():
div = ''
for subMsg in payload:
message_content += div
showPayload2(subMsg)
div = '------------------------------'
else:
message_content += msg.get_content_type()
message_content += payload[:200]
return [message_content]
showPayload2 only ever returns..
['------------------------------']
Here is the code with the working function showPayload, and my attempt to
capture to string instead of print to out, in showPayload2.
Am I missing something quick and simple to capture what is emitted to
stdout to a variable ? Or how do I do a recursive function call without
blowing away changes to the string ?
#!/usr/bin/python
import mailbox
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]
def showPayload2(msg):
message_content = ""
payload = msg.get_payload()
if msg.is_multipart():
div = ''
for subMsg in payload:
message_content += div
showPayload2(subMsg)
div = '------------------------------'
else:
message_content += msg.get_content_type()
message_content += payload[:200]
return [message_content]
mbox = mailbox.mbox('201308.mbox')
for message in mbox:
data = {}
data['From'] = message['from']
data['Subject'] = message['subject']
data['Date'] = message['date']
data['Content'] = showPayload2(message)
print data
showPayload(message)
print showPayload2(message)
On Thu, Aug 1, 2013 at 9:13 PM, <jep200404 at columbus.rr.com> wrote:
> On Thu, 1 Aug 2013 15:13:07 -0400, Tom Hanlon <tom at functionalmedia.com>
> wrote:
>
> > That is sweet !!
> >
> > Python is nice,
> >
> > I think what I just did was
> >
> > #!/usr/bin/python
> > from colug import experience
>
> There's a local Python group and mailing list at
> http://cohpy.org/ and
> http://mail.python.org/mailman/listinfo/centraloh
> where other bits of Python niftiness occurs.
>
> http://mail.python.org/pipermail/centraloh/2013-June/001718.html
>
> You missed PyOhio, an annual free Python conference held
> at the Ohio Union on OSU campus, but you can watch the videos.
>
> http://pyvideo.org/category/41/pyohio-2013
>
> You should check out IPython Notebook.
>
>
> http://pyvideo.org/video/1605/science-and-python-retrospective-of-a-mostly-s
> http://pyvideo.org/video/2286/the-ipython-notebook-revolution
> http://mail.python.org/pipermail/centraloh/2013-February/001551.html
>
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.colug.net/pipermail/colug-432/attachments/20130803/deb91a12/attachment.html
More information about the colug-432
mailing list