Archive
Logging with Python (Part 1)
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.
pudb: a simple and intuitive debugger
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
- pudb HQ (@pypi)
- PuDB, a better Python debugger (Part 1)
- Hacking PuDB: Now an even better Python debugger (Part 2)
- Introduction to the PuDB Python Debugging Tool (by Prof. Norm Matloff )
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
Python keylogger
Imbox – Python IMAP for Humans
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
Remove suffix from the right side of a text
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'
Log in to 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
