Archive

Archive for August, 2013

Logging with Python (Part 1)

August 5, 2013 Leave a comment

Problem
You have used print statements in your programs to print debug information, but you would like to start using the logging module too. You want to log to the stdout, you want to log to a file, or you want to log to BOTH places (stdout and file).

Solution
The following entry is based on this post.

Our customized logging module (mylogging.py):

import logging
import sys

DEBUG_LOG_FILENAME = "jabba.log"

# set up formatting
formatter = logging.Formatter("%(levelname)-5s %(asctime)s %(module)s.%(funcName)s() [%(lineno)d]: %(message)s", "%Y-%m-%d %H:%M:%S")

# set up logging to STDOUT for all levels DEBUG and higher
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.DEBUG)
sh.setFormatter(formatter)

# set up logging to a file for all levels DEBUG and higher
fh = logging.FileHandler(DEBUG_LOG_FILENAME)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)

# create Logger object
mylogger = logging.getLogger('MyLogger')
mylogger.setLevel(logging.DEBUG)
mylogger.addHandler(sh)    # enabled: stdout
mylogger.addHandler(fh)    # enabled: file

# create shortcut functions
debug = mylogger.debug
info = mylogger.info
warning = mylogger.warning
error = mylogger.error
critical = mylogger.critical

To enable/disable logging to stdout / file, just comment/uncomment these two lines:

mylogger.addHandler(sh)    # enabled: stdout
mylogger.addHandler(fh)    # enabled: file

And now a test file to demonstrate its usage:

#!/usr/bin/env python

from mylogging import debug, info, warning

def main():
    for i in xrange(100):
        if i % 5 == 0:
            info("i is {i}".format(i=i))
        if i % 50 == 0:
            debug("i is {i}".format(i=i))
        if i % 99 == 0:
            warning("i is {i}".format(i=i))

#############################################################################

if __name__ == "__main__":
    main()

Credits
Thanks to SaltyCrane for his excellent blog post on the topic.

Categories: python Tags:

pudb: a simple and intuitive debugger

August 5, 2013 Leave a comment

Problem
You want to debug a Python source file. You have heard about pdb, but in the good old days you used Borland Pascal, Borland C, and you want something similar.

Solution
If you develop in the konsole, you must try pudb. “PuDB is a full-screen, console-based visual debugger for Python. Its goal is to provide all the niceties of modern GUI-based debuggers in a more lightweight and keyboard-friendly package. PuDB allows you to debug code right where you write and test it–in a terminal. If you’ve worked with the excellent (but nowadays ancient) DOS-based Turbo Pascal or C tools, PuDB’s UI might look familiar.” (source)

Links

For more alternatives, see this discussion about debugging (on reddit).

Usage tip
Add this line to your ~/.bashrc file:

alias pudb='python -m pudb'

Then you can start debugging with pudb like this:

$ pudb problem.py
Categories: python Tags: ,

Python keylogger

August 2, 2013 2 comments

See PyKeylogger. Could be useful one day :)

Here is a simple Windows solution too.

Found here.

Categories: python Tags: ,

Imbox – Python IMAP for Humans

August 2, 2013 Leave a comment

Imbox (https://github.com/martinrusev/imbox) is a “Python library for reading IMAP mailboxes and converting email content to machine readable data“.

Every message is an object with the following keys:

    message.sent_from
    message.sent_to
    message.subject
    message.headers
    message.message-id
    message.date
    message.body.plain
    message.body.html
    message.attachments
Categories: python Tags: ,

Remove suffix from the right side of a text

August 2, 2013 Leave a comment

Problem
You have a string and you want to remove a substring from its right side. Your first idea is “rstrip“, but when you try it out, you get a strange result:

>>> "python.json".rstrip(".json")
'pyth'

Well, it’s not surprising if you know how “rstrip” works. Here the string “.json” is a set of characters, and these characters are removed from the right side of the original string. It goes from right to left: “n” is in the set, remove; remove “o“, remove “s“, remove “j“, remove “.“, then go on: “n” is in the set, remove; “o” is in the set, remove; “h” is not in the set, stop.

Solution
If you want to remove a suffix from a string, try this:

# tip from http://stackoverflow.com/questions/1038824
def strip_right(text, suffix):
    if not text.endswith(suffix):
        return text
    # else
    return text[:len(text)-len(suffix)]

Note that you cannot simply write “return text[:-len(suffix)]” because the suffix may be empty too.

How to remove a prefix:

def strip_left(text, prefix):
    if not text.startswith(prefix):
        return text
    # else
    return text[len(prefix):]

Usage:

>>> strip_right("python.json", ".json")
'python'

>>> strip_left("data/text/vmi.txt", "data/")
'text/vmi.txt'
Categories: python Tags: ,

Log in to comment

August 2, 2013 Leave a comment

Dear Readers,

The spam filter on wordpress.com has not been very efficient recently. Too many spam messages get the “pending” status that I need to review manually. I can’t imagine why they aren’t flagged automatically as spam…

I got tired of these spam messages, thus from now on you must be logged in if you want to leave a comment. Thank you for your understanding. I hope you will still leave comments, I love receiving feedbacks.

Best wishes,

Jabba Laci

Categories: python Tags: , ,
Design a site like this with WordPress.com
Get started