Archive

Archive for January, 2012

Upload your first project to PyPI

January 25, 2012 Leave a comment

Today I uploaded my jabbapylib project to PyPI, available here.

For preparing my first public pypi project, I followed this tutorial. For understanding how to package a Python library, read Mark Pilgrim’s corresponding book chapter.

I only want to add the following remarks:

  1. In setup.py, I import “from setuptools import setup, find_packages“. Notice setuptools instead of distutils.core.
  2. In the prepared package I wanted to include the library jabbapylib/ recursively since all the source codes are there. I could do that with this line: “packages = find_packages(exclude=['demos', 'dist', 'tests'])“, i.e. include all subdirectories with the exception of the ones in the list. What remains in my case is the “jabbapylib” folder.
  3. My library has some dependencies, they are specified here: “install_requires=['html5lib', 'psutil', 'pytest']“. When jabbapylib is installed with pip, pip will install these packages too.
  4. You can create your own MANIFEST.in file to specify what to include and what to exclude. However, if you want to add the directory that contains all the sources, do as indicated in step (2).
  5. For checking, packaging and uploading I made some simple scripts.

Serializations: data <-> XML, data <-> JSON, XML <->JSON

January 20, 2012 Leave a comment

Problems

  1. Python data to XML and back
  2. Python data to JSON and back
  3. XML to JSON and back

Solution

import json
import xmlrpclib
from xml2json import Xml2Json

def data_to_xmlrpc(data):
    # http://www.reddit.com/r/Python/comments/ole01/dictionary_to_xml_in_20_lines/
    """Return value: XML RPC string."""
    return xmlrpclib.dumps((data,)) # arg. is tuple

def xmlrpc_to_data(xml):
    # http://www.reddit.com/r/Python/comments/ole01/dictionary_to_xml_in_20_lines/
    """Return value: python data."""
    return xmlrpclib.loads(xml)[0][0]

def data_to_json(data):
    """Return value: JSON string."""
    data_string = json.dumps(data)
    return data_string

def json_to_data(data_string):
    """Return value: python data."""
    data = json.loads(data_string)
    return data

def xml_to_json(xml):
    """Return value: JSON string."""
    res = Xml2Json(xml).result
    return json.dumps(res)

def json_to_xmlrpc(data_string):
    """Return value: XML RPC string."""
    data = json.loads(data_string)
    return data_to_xmlrpc(data)

def xmlrpc_to_json(xmlrpc):
    """Return value: JSON string."""
    data = xmlrpc_to_data(xmlrpc)
    return data_to_json(data)

The full source code (serialize.py) together with the imported xml2json.py are available here. This work is part of my jabbapylib library.

Examples
Unit tests are here, they show you how to use these functions. Examples with comments are here.

Categories: python Tags: , , ,

Determine which class an object belongs to

January 20, 2012 Leave a comment

Problem
In a unit test, I wanted to verify that a function returns a cookielib.LWPCookieJar object. How to do that? In more general: how to figure out the type of an object?

Solution
First I tried to figure out the object’s type with type(obj) but it was “<type 'instance'>“.

Then, obj.__class__ .__name__ told me that obj is an ‘LWPCookieJar’. Better.

Finally, here is how I did the unit test:

assert isinstance(obj, cookielib.LWPCookieJar)

Note however that explicit typechecking is often discouraged in favor of duck typing.

Credits

Thanks also to Chris on the Python mailing list.

@grammer_man is the best troll

January 11, 2012 Leave a comment

…decided to write a little twitter bot. There he is above. His name is Grammer_Man and he corrects other twitter users’ misspellings, using data scraped from these Wikipedia pages. … You can see who’s responding at the moment by searching for @grammer_man.” (aengus)

It is worth checking out the reactions of the victims. Most of them have no idea that they reply to a bot :))))

Categories: fun, python Tags: , ,

Unit Testing with Python

January 8, 2012 Leave a comment
Categories: python Tags: , ,

The Python Ecosystem

January 7, 2012 Leave a comment

Here is an excellent introductory article to the Python ecosystem including packages, pip, virtualenv (and virtualenvwrapper), and some other important tools (e.g. fabric). I wish I had read it a long time ago.

Categories: python Tags: , , , ,

Python 3 Wall of Shame

January 7, 2012 Leave a comment

At Python 3 Wall of Shame you can monitor the compatibility status of several packages with Python 3.

Categories: python Tags:

2011 in review

January 1, 2012 Leave a comment

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

The concert hall at the Syndey Opera House holds 2,700 people. This blog was viewed about 21,000 times in 2011. If it were a concert at Sydney Opera House, it would take about 8 sold-out performances for that many people to see it.

Click here to see the complete report.

Categories: python Tags: ,