Upload your first project to PyPI
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:
- In
setup.py
, I import “from
setuptools
import setup, find_packages
“. Noticesetuptools
instead ofdistutils.core
. - 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. - 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. - 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). - For checking, packaging and uploading I made some simple scripts.
Serializations: data <-> XML, data <-> JSON, XML <->JSON
Problems
- Python data to XML and back
- Python data to JSON and back
- 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.
Determine which class an object belongs to
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
“…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 :))))
Unit Testing with Python
IBM developerWorks series
- Python testing frameworks : Make your life easy with a Python testing framework
- Python testing frameworks: Finding modules to test
- Python testing frameworks: Selecting and running tests
py.test
- Python unit testing part 3: the py.test tool and library (a very nice intro)
- py.test HQ
nose
The Python Ecosystem
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.
Python 3 Wall of Shame
At Python 3 Wall of Shame you can monitor the compatibility status of several packages with Python 3.
2011 in review
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.