Building a Console Utility with Commis

Applications like Git or Django’s management utility provide a rich interaction between a software library and their users by exposing many subcommands from a single root command. This style of what is essentially better argument parsing simplifies the user experience by only forcing them to remember one primary command, and allows the exploration of the utility hierarchy by using --help and other visibility mechanisms. Moreover, it allows the utility writer to decouple different commands or actions from each other. ...

January 23, 2016 · 9 min · 1753 words · Benjamin Bengfort

Freezing Package Requirements

I have a minor issue with freezing requirements, and so I put together a very complex solution. One that is documented here. Not 100% sure why this week is all about packaging, but there you go. First up, what is a requirement file? Basically they are a list of items that can be installed with pip using the following command: $ pip install -r requirements.txt The file therefore mostly serves as a list of arguments to the pip install command. The requirements file itself has a very specific format and can be created by hand, but generally the pip freeze command is used to dump out the requirements as follows: ...

January 21, 2016 · 3 min · 465 words · Benjamin Bengfort

Packaging Python Libraries with PyPI

Package deployment is something that is so completely necessary, but such a pain in the butt that I avoid it a little bit. However to reuse code in Python and to do awesome things like pip install mycode, you need to package it up and stick it on to PyPI (pronounced /pīˈpēˈī/ according to one site I read, though I still prefer /pīˈpī/). This process should be easy, but it’s detail oriented and there are only two good walk throughs (see links below). ...

January 20, 2016 · 8 min · 1599 words · Benjamin Bengfort

Better JSON Encoding

The topic of the day is a simple one: JSON serialization. Here is my question, if you have a data structure like this: import json import datetime data = { "now": datetime.datetime.now(), "range": xrange(42), } Why can’t you do something as simple as: print json.dumps(data)? These are simple Python datetypes from the standard library. Granted serializing a datetime might have some complications, but JSON does have a datetime specification. Moreover, a generator is just an iterable, which can be put into memory as a list, which is exactly the kind of thing that JSON likes to serialize. It feels like this should just work. Luckily, there is a solution to the problem as shown in the Gist below: ...

January 19, 2016 · 2 min · 363 words · Benjamin Bengfort

Simple SQL Query Wrapper

Programming with databases is a fact of life for any seasoned programmer (read, “worth their salt”). From embedded databases like SQLite and LevelDB to server databases like PostgreSQL, data management is a fundamental part of any significant project. The first thing I should say here is skip the ORM and learn SQL. SQL is such a powerful tool to query and manage a database, and is far more performant thanks to 40 years of research and development. ...

January 18, 2016 · 4 min · 647 words · Benjamin Bengfort

The codetime and clock Commands

If you’ve pair programmed with me, you might have seen me type something to the following effect on my terminal, particularly if I have just created a new file: $ codetime Then somehow I can magically paste a formatted timestamp into the file! Well it’s not a mystery, in fact, it’s just a simple alias: alias codetime="clock.py code | pbcopy" Oh, well that’s easy — why the blog post? Hey, what’s clock.py? A great question! This Python script is the dumbest thing that I have ever written, that has become the most useful tool that I use on a daily basis. Whenever there is a dumb to useful ratio like that, it’s blogging time. Here is clock.py: ...

January 12, 2016 · 1 min · 202 words · Benjamin Bengfort

Wrapping the Logging Module

The standard library logging module is excellent. It is also quite tedious if you want to use it in a production system. In particular you have to figure out the following: configuration of the formatters, handlers, and loggers object management throughout the script (e.g. the logging.getLogger function) adding extra context to log messages for more complex formatters handling and logging warnings (and to a lesser extent, exceptions) The logging module actually does all of these things. The problem is that it doesn’t do them all at once for you, or with one single API. Therefore we typically go the route that we want to wrap the logging module so that we can provide extra context on demand, as well as handle warnings with ease. Moreover, once we have a wrapped logger, we can do fun things like create mixins to put together classes that have loggers inside of them. ...

January 11, 2016 · 2 min · 341 words · Benjamin Bengfort