7 Essential Pypl Libraries PDF
7 Essential Pypl Libraries PDF
com
About Opensource.com
What is Opensource.com?
2 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ABOUT THE AUTHOR
MOSHE ZADKA
Twitter: https://twitter.com/moshezadka
7 ESSENTIAL PYPI LIBRARIES AND HOW TO USE THEM . CC BY-SA 4.0 . OPENSOURCE.COM 3
Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction
Introduction 5
Chapters
Write for Us 15
4 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction
Introduction
Python is one of the most popular programming languages in use
today—and for good reasons: it's open source, it has
a wide range of uses (such as web programming, business applications,
games, scientific programming, and much more), and it has a vibrant
and dedicated community supporting it. This community is the reason we
have such a large, diverse range of software packages available in the
Python Package Index (PyPI) to extend and improve Python and solve
the inevitable glitches that crop up.
In this series, we'll look at seven PyPI libraries that can help you solve
common Python problems.
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 5
Write faster C extensions for Python with Cython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Format Python however you like with Black
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 7
Say goodbye to boilerplate in Python with attrs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
If you have been using Python for any length of time, you
are probably used to writing code like:
ISBNs have a specific format. What if we want to enforce
that format?
Next, you write a nice docstring documenting the expected The attrs library also has great support for immutability-style
types. But you notice you forgot to add the edition and programming [2]. Changing the first line to @attr.s(auto_at-
published_year attributes, so you have to modify them in tribs=True, frozen=True) means that Book is now im-
five places. mutable: trying to modify an attribute will raise an exception.
What if you didn’t have to? Instead, we can get a new instance with modification using
attr.evolve(old_book, published_year=old_book.pub-
@attr.s(auto_attribs=True) lished_year+1), for example, if we need to push publication
class Book(object): forward by a year.
isbn: str
name: str Links
author: str [1] https://pypi.org/project/attrs/
published_year: int [2] https://opensource.com/article/18/10/functional-
edition: int programming-python-immutable-data-structures
Annotating the attributes with types using the new type anno-
tation syntax, attrs [1] detects the annotations and creates a
class.
8 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . Add methods retroactively in Python with singledispatch
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 9
Automate your Python code tests with tox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Automate your Python code tests with tox
It is also a good idea to run a linter in tox. For example, By default, tox will run all test environments. But you can run
running Black [3] will do the right thing. just one environment; for example, if you only want to run
Black, run tox -e py36-black.
[tox] If you have a Python library you care about, add tox.ini to
envlist = py36,py37,py36-black your workflow to keep its quality high.
[testenv]
deps = Links
pytest [1] https://opensource.com/article/19/5/python-tox
commands = [2] http://www.sphinx-doc.org/en/master/
pytest mylibrary [3] https://opensource.com/article/19/5/python-black
[testenv:py36-black]
deps =
black
commands =
black --check --diff mylibrary
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 11
Ensure consistency in your Python code with flake8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Check type annotations in Python with mypy
Calling print_seven raises a TypeError informing us we If we run mypy on this, there will not be any complaints; we
cannot add a string and a number: we cannot add “5” fixed the bug. This also results, happily, in working code.
and 1. The Python type system can get pretty deep, of course. It
However, we cannot know this until we run the code. Run- is not uncommon to encounter signatures like:
ning the code, if it were correct, would have produced a print- from typing import Dict, List, Mapping, Sequence
out to the screen: a side-effect. A relatively harmless one, as
side-effects go, but still, a side-effect. Is it possible to do it def unify_results(
without risking any side-effects? results1: Mapping[str, Sequence[int]],
We just have to let the robots know what to expect. results2: Mapping[str, Sequence[int]]
) -> Dict[str, List[int]]:
def add_one(input: int) -> int: pass
return input + 1
In those cases, remember that everything is an object: yes,
def print_seven() -> None: even types.
five = "5"
seven = add_one(add_one(five)) ResultsType = Mapping[str, Sequence[int]]
print(seven) ConcreteResultsType = Dict[str, List[int]]
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 13
Check type annotations in Python with mypy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
def unify_results(
results1: ResultsType, results2: ResultsType) guarantee less in order to allow future changes to change the
-> ConcreteResultsType: return type.
pass MyPy allows progressive annotation: not everything has to
be annotated at once. Functions without any annotations will
We defined the input types as abstract types (using Mapping not be type-checked.
and Sequence). This allows sending in, say, a defaultdict, Go forth and annotate!
which maps strings to tuples. This is usually the right choice.
We also chose to guarantee concrete return types in the sig- Links
nature. This is more controversial: sometimes it is useful to [1] https://pypi.org/project/mypy/
14 7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Write for Us
Write for Us
In 2010, Red Hat CEO Jim Whitehurst announced the launch of Opensource.com
in a post titled Welcome to the conversation on Opensource.com. He explained,
“This site is one of the ways in which Red Hat gives something back to the open
source community. Our desire is to create a connection point for conversations
about the broader impact that open source can have—and is having—even beyond
the software world.” he wrote, adding, “All ideas are welcome, and all participants
are welcome. This will not be a site for Red Hat, about Red Hat. Instead, this will be
a site for open source, about the future.”
More than 60% of our content is contributed by members of open source communities,
and additional articles are written by the editorial team and other Red Hat contributors.
A small, international team of staff editors and Community Moderators work closely
with contributors to curate, polish, publish, and promote open source stories from
around the world.
Would you like to write for us? Send pitches and inquiries to [email protected].
7 essential PyPI libraries and how to use them . CC BY-SA 4.0 . Opensource.com 15