Three Useful Python Libraries for Startups

1. Whitenoise

With a couple of lines of config, WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service.

Whitenoise handles Gzipping your content and setting far-future cache headers on content. With a trivial amount of work, you can configure your app to automatically append a hash to each of your static files every time you deploy changes, so that you can set the cache headers as so.

A WSGI compliant app literally requires a few lines of code changes to deploy Whitenoise:

from whitenoise import WhiteNoise

from my_project import MyWSGIApp

application = MyWSGIApp()
application = WhiteNoise(application, root='/path/to/static/files')
application.add_files('/path/to/more/static/files', prefix='more-files/')

Why is this important? Using Gzip significantly reduces file sizes / page load for static files. Search engines also detect Gzip compression, penalizing websites that don’t implement it. Being discovered is critical for any early-stage company, and enabling Gzip is low hanging fruit to enable this (not to mention, also providing a better user experience). Thanks to my friend Peter for showing this to me.

You can find Whitenoise here.

2. Phonenumbers (lite)

Validating phone numbers is not easy, and there are tons of valid formats that make using regular expressions impossible. Moreover, even if a number passes a regular expression for formatting, it may still not be valid.

Invalid: 222-222-2222 (but it would pass common regular expression tests)
Valid: 313-442-1231 ext. 901

Relying solely on a regular expression would mean you reject valid phone numbers and accept invalid ones!

Solution: use python-phonenumbers, a port of Google’s libphonenumber.

We chose to use phonenumberslite because it does not include geocoding, carrier, and time zone metadata and is significantly smaller.

This library makes it trivial to validate phone numbers. Using this library ensures that you accept a variety of formats, with various formats / schemes and ensure they are valid.

3. Pdfkit

Pdfkit makes it simple to generate PDFs from html. Why would one use this? Let’s say you have an invoice page in your app – you can use the same code to render that page to render a downloadable PDF for customers or your own records. Here’s how simple it is:

import pdfkit

pdfkit.from_file('test.html', 'out.pdf')

# Generating PDFs from strings and web-pages is equally easy:

pdfkit.from_string('Hello!', 'out.pdf')
pdfkit.from_url('http://google.com', 'out.pdf')

Another perk of this library is that it is a binding to the extremely resilient and maintained library wkhtmltopdf.

You can find pdfkit here.

Tons of APIs exist for creating PDFs – however, we’re fans of this library because it’s actually easier than connecting to an API, and totally free.

Honorable Mentions

These are 2 libraries we love, but they are so common that they don’t merit their own section. If you haven’t used them, they are certainly worth a look:

Requests: requests is an elegant HTTP library.

Python-dateutil: Numerous date utilities for calculating differences, etc. The most useful of these is a resilient date parser:

import dateutil.parser

>>> dateutil.parser.parse("May 4th, 2012") 
datetime.datetime(2012, 5, 4, 0, 0) 

>>> dateutil.parser.parse("5-4-2012") 
datetime.datetime(2012, 5, 4, 0, 0) 

>>> dateutil.parser.parse("5.4.2012") 
datetime.datetime(2012, 5, 4, 0, 0) 

>>> dateutil.parser.parse("4th May 2012") 
datetime.datetime(2012, 5, 4, 0, 0)

These are a few libraries we found extremely useful. What other libraries do you think merit consideration?

Zain Allarakhia is a Co-Founder and CTO of Instavest, a collaborative investment network.

 
798
Kudos
 
798
Kudos

Now read this

Three Stock Market Investing Truths I Used to Hold

Over the last few months, I’ve had the opportunity to observe seasoned investors up close. This has significantly changed the way I view investing in the stock market. Here’s what I used to hold true, what I now believe and why my... Continue →