A follow on from my previous “cool things about Python”, there’s another cool thing you might not have known about. Almost all python packages are available at the Cheeseshop in Python EGG files (geddit?), and the easiest way to install them is using setuptools’s ez_setup.py bootstrap script.
This little 8K Python file is a little package manager a little like Red Hat’s yum tool, or Debian’s apt-get tool — it will download, install and configure each python package you need.
To use it, save the ez_setup.py file somewhere, then run it with python, and the package name. So, for example, to install the Python Time Zone (pytz) package, run:
python ez_setup.py pytz
Within a very short amount of time you’ll have pytz ready and available to import and use.
Incidentally, pytz is a pretty damn cool little timezone library — if you’ve ever had to do any manipulation of dates and times across daylight saving times or time zone boundaries in Python, I recommend you take a look.
Quick caveat on using it though — to create a local time from a “non-localised”
time, don’t be tempted to just construct a default datetime(blahblah, tzinfo=myTimeZone)
. Instead
make sure you use the localize()
method of the timezone. This will then take into account daylight
saving time boundaries appropriately. I use something like:
myTimeZone = pytz.timezone('Europe/London')
nonLocal = datetime(2007, 10, 1, 1, 18, 0)
local = myTimeZone.localize(nonLocal, is_dst=None)
The is_dst=None
part means in the ambiguous hour as the clocks go back, raise an exception. The other
choices are True
or False
to break the ambiguity by assuming it was DST or not.
Matt Godbolt is a C++ developer living in Chicago. Follow him on Mastodon or Bluesky.