Book Image

Daniel Arbuckle's Mastering Python

By : Daniel Arbuckle
Book Image

Daniel Arbuckle's Mastering Python

By: Daniel Arbuckle

Overview of this book

Daniel Arbuckle's Mastering Python covers the basics of operating in a Python development environment, before moving on to more advanced topics. Daniel presents you with real-world solutions to Python 3.6 and advanced-level concepts, such as reactive programming, microservices, ctypes, and Cython tools. You don't need to be familiar with the Python language to use this book, as Daniel starts with a Python primer. Throughout, Daniel highlights the major aspects of managing your Python development environment, shows you how to handle parallel computation, and helps you to master asynchronous I/O with Python 3.6 to improve performance. Finally, Daniel will teach you the secrets of metaprogramming and unit testing in Python, helping you acquire the perfect skillset to be a Python expert. Daniel will get you up to speed on everything from basic programming practices to high-end tools and techniques, things that will help set you apart as a successful Python programmer.
Table of Contents (13 chapters)

The standard library

The library of code that comes pre-installed with Python is extensive, so we're not going into the details. The goal here is to come away with an understanding of the breadth of quality tools we have available, so if we need them in the future then we know where to look. Thus, we're going to just briefly touch on many useful things. You can find the official documentation on the standard library at https://docs.python.org/3/library/index.html.

Different types of packages

The index page contains a list of the different packages available to you in Python's standard library. Let's briefly run through them in order.

First of all, there is the Collections package, which contains even more data structures: https://docs.python.org/3/library/collections.html.

The Collections package contains the defaultdict class that we spoke about in the previous section. The Collections package also contains an OrderedDict parameter that remembers the order in which the items were inserted and gives them back in the same order when we iterate over it. A deque class is a variation on tuples that uses names to access the element and a PseudoDict parameter that provides a composite view of several other dictionaries.

There are a few other data structures in there as well. One common data structure missing from the collections package is a PriorityQueue parameter, but that's just because it has its own package called heapq:

https://docs.python.org/3/library/heapq.html

Python's PriorityQueue operations are implemented as functions that work with built-in lists to add and remove items according to the heap property.

Storing and retrieving data is an extremely common need for programs and the pickle package makes it easy:

https://docs.python.org/3/library/pickle.html

Inside the pickle package are classes and functions that facilitate transforming arbitrary Python data into a sequence of bytes that can be stored in a file, sent across the network, or whatever you need. The pickle package also has the tools to reverse the process and transform those bytes back into fully-fledged Python data objects.

Also, in the vein of storing data, the sqlite3 package provides complete access to the SQLite database manager, allowing us to take advantage of a complete transactional relational database:

https://docs.python.org/3/library/sqlite.html

Third-party packages to access other database systems follow pretty much the same interface, so it's easy to make the switch to a different database later, if needed.

The json package is also relevant to data handling. It parses or generates the de facto standard Internet Data Exchange (IDX) format:

https://docs.python.org/3/library/json.html

The json package is smart, so it handles JSON (JavaScript Object Notation) objects, arrays, strings, numbers, null values, and so on, in a sensible way.

Mapping them onto the proper Python datatypes, the base64 package encodes bytes into base64, or decodes base64 into bytes:

https://docs.python.org/3/library/base64.html

There are several other similar packages for binhex, uu code, and so on, as well.

The html and xml packages provide all sorts of utilities for dealing with the major internet markup languages, including parsers and the document object model:

https://docs.python.org/3/library/html.html

The urllib package provides us with convenient ways to retrieve data from URLs or to send data to them:

https://docs.python.org/3/library/urllib.html

In particular, the urllib.request.url open function is extremely useful.

The itertools and functools packages provide an assortment of utilities having to do with functional programming paradigms:

https://docs.python.org/3/library/itertools.html

In particular, the functools package allows for us to create partially applied functions and the itertools package lets us concatenate iterators.

The enum package contains support for creating and using named enumerations:

https://docs.python.org/3/library/enum.html

Each enumeration is a distinct data type, like a class.

The pathlib package contains classes and functions that provide a cross-platform abstraction of file and file path operations:

https://docs.python.org/3/library/pathlib.html

The inspect package is interesting and quite useful. It provides us with functions that can be used to gather information about data objects, particularly about functions and classes. If we want to know the names of functions, parameters, or we want to access an object's documentation, or any number of things along those lines, the inspect package will get us there:

https://docs.python.org/3/library/inspect.html

The packages we just looked at are by no means the complete list of what's available in the standard library, but hopefully they give some idea of the depth and breadth of what we get just by installing Python. Taking a look through the library documentation on https://www.python.org/ is highly recommended for people who want to get the most out of Python. There are some particularly useful packages that we didn't mention at all. That's because there are whole other sections devoted to them later in the book.

So, that brings us to the end of our overview of the standard library.