Archive

Archive for the ‘python’ Category

Roman numbers to decimal

November 8, 2012 Leave a comment

Problem
You want to convert Roman numbers to decimal and decimal numbers to Roman numbers.

Solution
I found a module for this task called romanclass (http://pypi.python.org/pypi/romanclass).

I wrote a simple wrapper around it, available here.

Categories: python Tags: , ,

Check iO: A game where you code in Python

October 30, 2012 Leave a comment

Check iO is a browser-based game where you need to solve problems in Python in order to advance.

@reddit

Update (20121109)
Guido found it too :)

Categories: python Tags: , ,

Determine the name of the current function

October 24, 2012 Leave a comment

Problem
You want to determine the name of the current function.

Solution

import sys

def whoami():
    print '# this function is:', sys._getframe().f_code.co_name
    print '# this line number is:', sys._getframe().f_lineno
    print '# this file\'s name is:', sys._getframe().f_code.co_filename

whoami()

Output:

# this function is: whoami
# this line number is: 5
# this file's name is: na.py

Also, by calling sys._getframe(1), you can get this information for the *caller* of the current function. So you can package this functionality up into your own handy functions:

import sys

def get_function_name():
    return sys._getframe(1).f_code.co_name

def whoami():
    func_name = get_function_name()
    print '# current function\'s name:', func_name

whoami()

Output:

# current function's name: whoami

This tip is from here.

Building Skills in Python

October 8, 2012 Leave a comment

I found a freely available Python book at Linuxtopia called “Building Skills in Python“.

This Linuxtopia site is great! Check it out!

Categories: python, ubuntu Tags:

How to Write a Spelling Corrector

October 8, 2012 Leave a comment

See Peter Norvig’s post.

You can also check out his site.

BeautifulSoup: _detectEncoding error

September 30, 2012 Leave a comment

Problem
While parsing an HTML page with BeautifulSoup, I got a similar error message:

File ".../BeautifulSoup.py", line 1915, in _detectEncoding
    '^<\?.*encoding=[\'"](.*?)[\'"].*\?>').match(xml_data)
TypeError: expected string or buffer

In the code I had this:

text = get_page(url)
soup = BeautifulSoup(text)

Solution

text = get_page(url)
text = str(text)    # here is the trick
soup = BeautifulSoup(text)

Tip from here.

Categories: python Tags: ,

Check Gmail for new messages

September 8, 2012 2 comments

Problem
I want to check my new Gmail messages periodically. When I get a message from a specific sender (with a specific Subject), I want to trigger some action. How to do that?

Solution
Fortunately, there is an atom feed of unread Gmail messages at https://mail.google.com/mail/feed/atom. All you have to do it is visit this page, send your login credentials, fetch the feed and process it.

import urllib2

FEED_URL = 'https://mail.google.com/mail/feed/atom'

def get_unread_msgs(user, passwd):
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(
        realm='New mail feed',
        uri='https://mail.google.com',
        user='{user}@gmail.com'.format(user=user),
        passwd=passwd
    )
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    feed = urllib2.urlopen(FEED_URL)
    return feed.read()

##########

if __name__ == "__main__":
    import getpass

    user = raw_input('Username: ')
    passwd = getpass.getpass('Password: ')
    print get_unread_msgs(user, passwd)

For reading XML I use the untangle module:

import untangle    # sudo pip install untangle

xml = get_unread_msgs(USER, PASSWORD)
o = untangle.parse(xml)
try:
    for e in o.feed.entry:
        title = e.title.cdata
        print title
except IndexError:
    pass    # no new mail

Links

Categories: python Tags: , , , ,

Obfuscated Python

September 7, 2012 Leave a comment
Categories: python Tags:

The best free Python resources

September 2, 2012 Leave a comment
Categories: python Tags: ,

Print unicode text to the terminal

September 2, 2012 2 comments

Problem
I wrote a script in Eclipse-PyDev that prints some text with accented characters to the standard output. It runs fine in the IDE but it breaks in the console:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 11: ordinal not in range(128)

This thing bugged me for a long time but now I found a working solution.

Solution
Insert the following in your source code:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

I found this trick here. “This allows you to switch from the default ASCII to other encodings such as UTF-8, which the Python runtime will use whenever it has to decode a string buffer to unicode.”

Related

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