5 Python Libraries We Love
Tags: engineering, api, python
Published: 2018-12-03 21:21:07

---
title: 5 Python Libraries We Love
published: true
tags: engineering,API,python
canonical_url: https://www.nylas.com/blog/5-python-libraries-we-love
---

[![python_libraries_hero](https://www.nylas.com/hubfs/python_libraries_hero.png) ](https://www.nylas.com/blog/5-python-libraries-we-love)

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.

<p>Open-source code isn’t just developed and left standing; it’s regularly maintained and extended by talented and dedicated Python developers. <a href="https://github.com/nylas/sync-engine">The Nylas Sync Engine</a>, 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.</p>
<p>Here are 5 of our favorite Python libraries, and how we use them:</p>
<h3><strong>1. ptpython for Fast &amp; Easy Internal Tooling</strong></h3>
<p>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.&nbsp;</p>
<p>The <a href="https://github.com/prompt-toolkit/ptpython" target="_blank">ptpython</a> 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.&nbsp;</p>
<p>And just for fun, we made it pretty:</p>
<p>&nbsp;</p>
<div><img src="https://cdn2.hubspot.net/hubfs/3314308/Screen%20Shot%202018-11-28%20at%2011.00.51%20AM.png" alt="Screen Shot 2018-11-28 at 11.00.51 AM"></div>
<p>&nbsp;</p>
<h3><strong>2. exchangelib for Syncing Data from Microsoft</strong></h3>
<p>We work with lots of different email protocols when we’re syncing data. For <a href="/exchange-email-api">Microsoft </a>accounts, we sync data with Exchange ActiveSync (EAS) and Exchange Web Services (EWS), and we use <a href="https://github.com/ecederstrand/exchangelib" target="_blank">exchangelib</a> 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.</p>
<p><img src="https://cdn2.hubspot.net/hubfs/3314308/python_libraries_support.png" alt="python_libraries_support" width="1301" style="width: 1301px;"></p>
<h3><strong>3. tldextract for Parsing URLs</strong></h3>
<p>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 <code>.com</code>, <code>.org</code> and <code>.net</code> top-level domains are easy enough to codify, but these days <code>.limo</code>, <code>.pizza</code> and <code>.duck</code> are all valid suffixes as well. In our code, we need to parse urls to validate emails and server addresses, so we use <a href="https://github.com/john-kurkowski/tldextract" target="_blank" rel=" noopener">tldextract</a> to separate the protocol from the domain, subdomain(s) and suffix in a given url.&nbsp;</p>
<h3><strong>4. vcr for Fast, Reusable Test Fixtures</strong></h3>
<p>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 <a href="https://github.com/kevin1024/vcrpy" target="_blank">vcr</a> 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.</p>
<p>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).&nbsp;</p>
<h3><strong>5. expiringdict for Ordered Caches</strong></h3>
<p>Mailgun’s <a href="https://github.com/mailgun/expiringdict" target="_blank">expiringdict</a> is a great example of a library that does one thing, and does it really well. Their data structure, based off collections’ <code>OrderedDict</code>, creates a dictionary with a max length, and items that “expire” after a certain amount of time, to be used as a cache.&nbsp;</p>
<p>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.</p>
<p>&nbsp;</p>
<p>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 <a href="https://dev.to/nylas/build-a-scheduler-slackbotin-30-minutes-1i6c" target="_blank">side projects</a>, giving talks at <a href="https://us.pycon.org/2019/" target="_blank">PyCon</a> and attending our local meetups. Have a fun Python library you want to share? We’d love to hear about it!&nbsp;</p>
<p>For more information on how to get started with the Nylas APIs, <a href="https://www.nylas.com/products/" target="_blank">Click here</a>.&nbsp;</p>

 ![](https://track.hubspot.com/__ptq.gif?a=3314308&k=14&r=https%3A%2F%2Fwww.nylas.com%2Fblog%2F5-python-libraries-we-love&bu=https%253A%252F%252Fwww.nylas.com%252Fblog&bvt=rss)