Nylas is a Python shop, and while we use a lot of the Python libraries you’d expect to see in an email API codebase, like datetime, imaplib, and xml parsers, we also use a ton of open-source Python tools outside the standard library. These crucial libraries have saved us from reinventing the wheel time and time again, and we’ve gotten a lot of mileage out of them.
Open-source code isn’t just developed and left standing; it’s regularly maintained and extended by talented and dedicated Python developers. The Nylas Sync Engine, our core service for syncing data from Gmail, Microsoft, and IMAP email providers, started as an open-source project, which gives us a unique appreciation for the Python maintainers who are responding to issues, merging pull requests, and iterating on the open-source tools we use every day.
Here are 5 of our favorite Python libraries, and how we use them:
1. ptpython for Fast & Easy Internal Tooling
When you’re running as many services as we do (a sync service, an API, a developer dashboard, and a webhooks service, to name a few!), with hundreds of customers all using your platform in unique ways, and a database distributed across 200 shards, iterating on interconnected systems and debugging issues becomes a complex problem.
The ptpython REPL brings autocomplete, syntax highlighting and multiline editing to the Python shell, creating a robust and customizable Python environment. We use it to enhance the Python console with pre-loaded text, helper functions and objects for our developers and our developer success engineers to use. If an error pops up in our logs, we can quickly load the buggy object in a Python REPL, debug the root cause, and experiment with solutions directly in the Python shell.
And just for fun, we made it pretty:
2. exchangelib for Syncing Data from Microsoft
We work with lots of different email protocols when we’re syncing data. For Microsoft accounts, we sync data with Exchange ActiveSync (EAS) and Exchange Web Services (EWS), and we use exchangelib in our EWS integrations. Microsoft documentation is hard to find via Google search, and not intuitively organized, and exchangelib is especially good about commenting code to include links back to the relevant documentation, so it’s easy for us to track down descriptions of more obscure attributes.
3. tldextract for Parsing URLs
If you’ve ever tried to write a regular expression to define a valid url, you know it’s not as trivial as it sounds. The normal
.net top-level domains are easy enough to codify, but these days
.duck are all valid suffixes as well. In our code, we need to parse urls to validate emails and server addresses, so we use tldextract to separate the protocol from the domain, subdomain(s) and suffix in a given url.
4. vcr for Fast, Reusable Test Fixtures
At Nylas, we work with a lot of APIs, which means we make a lot of HTTP requests. To simplify and speed up testing, we use vcr to record the responses from the HTTP interactions in our test code and save them to a flat file in our codebase, called a cassette.
After the initial response has been recorded, it is “replayed” by later tests, so we don’t have to make live requests to external APIs in our test code. This makes our tests fast (no real HTTP requests anymore), deterministic (the test will continue to pass, even if you are offline) and accurate (the response will contain the same headers and body you get from a real request).
5. expiringdict for Ordered Caches
Mailgun’s expiringdict is a great example of a library that does one thing, and does it really well. Their data structure, based off collections’
OrderedDict, creates a dictionary with a max length, and items that “expire” after a certain amount of time, to be used as a cache.
We have a ton of data in our databases, and caching the results of queries we know are executed frequently lets us serve up faster results, with less load on the database. To prevent the cache from getting too big, which impacts performance, we like to cap the size, and it’s convenient to set an expiration date on the data, so that our cache doesn’t get stale.
Python is at the heart of what we do, and our love for Python isn’t limited to just our work; you can also find us building Python side projects, giving talks at PyCon and attending our local meetups. Have a fun Python library you want to share? We’d love to hear about it!
For more information on how to get started with the Nylas APIs, Click here.