Python Developers: PyPI Makes Life Easier

shutterstock_307703714

Python’s major strength lies in its ecosystem. There’s the community, the quality of documentation, the PEPs (Python Enhancement Proposals, or white papers that suggest improvements), and PyPI, or the Python Package Index. PyPI contains almost 80,000 packages, meaning that everyone who works with Python will find something useful here.

As with most open-source repositories, PyPI offers quite a few gems, as well as packages designed to tackle highly specific tasks. (If you’re exploring PyPI for the first time, be aware that, while some packages receive updates, many are abandoned.)

Here are five packages that I’ve found in PyPi that are worth your consideration: DebtCollector, DE422, phonenumbers, geocoder and genaa. In order to find these (or any package, for that matter), just type in the name after the last “/”:

https://pypi.python.org/pypi/

Like so:

https://pypi.python.org/pypi/genaa 

DebtCollector

So you have a large codebase and want to improve it by eliminating some of the technical debt, which is inevitable on projects involving many different developers over a long period of time. In order to reduce the debt, developers need a “flag” that indicates when a function or class is deprecated, so they can avoid it.

DebtCollector accomplishes that end by allowing developers to rename or move functions, read-only properties, classes, and other elements so that a “This function is deprecated” message appears whenever they’re called. It’s such a neat idea, I wish it could be implemented in other programming languages.

DE422

This package is half a gigabyte in size, making it one of the larger ones on PyPI, but it’s mostly data, not code.

Have you ever wondered how astronomers calculate where celestial bodies are located on a given date, so that spacecraft can navigate accurately? That’s the purpose of an ephemeris, which is a record or model of astronomical objects. The Jet Propulsion Lab (JPL) Development Ephemeris, for example, models the solar system for spaceship navigation over a given period. And now the JPL DE is available as a Python package.

The data in the JPL’s DE422 package provides calculation of the positions, velocities and accelerations of objects (planets and asteroids) over the period -3000 to 3000. There’s a smaller shorter-period ephemeris 423, covering 1800 to 2200. You might not be launching a mission to Mars anytime soon, but this is nonetheless a fun package to tinker with.

phonenumbers

Ever had to check if international phone numbers are in a correct format? If I did, I would trust Google to get it right. This library is a Python-based translation of Google’s libphone number. Once a phone number is validated, it can be formatted in a standard mode; or you can have the code search a block of text to extract phone numbers. It includes over 100 MB of geocoding data.

geocoder

Speaking of geocoding: if you use a geocoder, you know it can prove a bit… fiddly at moments. Geocoding info for an address takes a lot of work, and there’s a lot of boilerplate in the code:

>>> import requests
>>> url = 'https://maps.googleapis.com/maps/api/geocode/json'
>>> params = {'sensor': 'false', 'address': 'Mountain View, CA'}
>>> r = requests.get(url, params=params)
>>> results = r.json()['results']
>>> location = results[0]['geometry']['location']
>>> location['lat'], location['lng']

But if you import this geocoder package, you can reduce the above to just three lines:

>>> import geocoder

>>> g = geocoder.google('Mountain View, CA')

>>> g.latlng

I tried it on my old street where I lived in London until last year. It worked like a dream:

>>> g = geocoder.google('Dawlish Road,Leyton, E10 6QB, United Kingdom')

>>> g.latlng

[51.5648904, -0.009021]

Try it yourself.

genaa

Genaa is short for “generate ascii art,” but really it just puts text in boxes. There are other styles, allowing # and adjustable width and height, as well as text alignment.

echo -en 'Hello world!\nThis is genaa' | genaa box

+---------------+

| Hello world! |

| This is genaa |

+---------------+

Conclusion

PyPI has a lot of packages. It can prove confusing at some moments, but persevere: there’s some very good stuff in there that will simplify your programming life.

Image Credit: RAYphotographer/Shutterstock.com

Post a Comment

Your email address will not be published.