Five Neat Little Python Libraries2014-07-23
Here are some great little Python libraries that have made my life (well, at least the coding part) a little bit nicer and easier. They mostly add neat syntax and a few things that you always wanted to do, but never knew.
EDIT: I missed out on
pathlib, which joined the
standard library in Python 3.4, and provides the same object-oriented niceness
for paths as
Unipath. You should use that - there’s a
backport for Python 2.x as well.
Unipath is just a has-been contender now I guess.
It’s an “object-oriented alternative to os/os.path/shutil”.
From the README:
Basically, if you’re fiddling around a lot with
os.path and other
file-manipulation functions, you’re missing out on this much nicer, arguably
more pythonic, way of doing things.
Unipath, but for replacing
subprocess. Call any program as if it
were a function, and get return values in a sane manner.
Using a bit of python magic, the module lets you import any program as if it were a function:
Argument passing can also be done with keyword-args:
From that you might surmise that the return of any shell command is a
object - but it’s not! It’s a
sh.RunningCommand that wraps around the
result to expose the
stdout by default, but lets you get at other attributes
I’m sure you can see how much easier this makes subprocess interaction. Maybe you’ll find yourself forgoing bash for Python soon :)
A mini-library with easy syntax for scheduling recurring jobs and running a job loop that executes them. For when cron isn’t enough (environment issues, logging, etc.), but anything much bigger is overkill.
From the README:
It’s inspired by a Ruby module, as you might be able to tell from the DSL syntax (don’t let anyone ever tell you again that Python can’t do DSLs!). If you do use it to replace cron, use something like supervisord to keep your schedule program running in case of failure.
pip install schedule
- Readme says “(coming soon)” - but the FAQ explains nearly everything
- Source on Github dbader/schedule
“Time Travel Made Easy.” Simplifies a lot of
datetime interactions that
become painful when you’re doing a lot of them.
Delorean wraps a couple of useful date and time libraries -
dateutil - to provide this functionality. And it has an awesome name.
“Schema validation just got Pythonic.”
Brilliant for validating API responses, user input, JSON stuffed in a database, or whatever. Also great for unittests, where validating all the values output by the code might be impossible, but you can at least check for their type and existence.
6. Bonus!! django-unchained
We’ve all heard of django for web app development; now there’s the awesome
Django Unchained library to take it to the next level. You’ll have to
pip install django-unchained to find out what it’s all about.
…and that’s the end of the presentation for today. Thanks for reading!
Are your Django project's tests slow? Read Speed Up Your Django Tests now!
One summary email a week, no spam, I pinky promise.
© 2020 All rights reserved.