I only want to add the following remarks:
setup.py, I import “
import setup, find_packages“. Notice
- 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 “
- 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.infile 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.
- Python data to XML and back
- Python data to JSON and back
- XML to JSON and back
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) 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)
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?
First I tried to figure out the object’s type with
type(obj) but it was “
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.
Thanks also to Chris on the Python mailing list.
“…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 :))))
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
- Python unit testing part 3: the py.test tool and library (a very nice intro)
- py.test HQ
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.