Tags: python
API endpoints and web URLs are, thankfully, more secure than ever, usually requiring encrypted HTTPS. Python works well as an HTTPS client, and pytest simplifies testing Python-based tools or libraries. Tools like VCR.py or the combination of pytest-httpserver and trustme provide an additional testing layer that is fast and convenient, and well-suited for HTTPS work. This will help with family gatherings. Let me show you.
I recently wrote an article detailing the use of Python’s urlopen() for performing HTTP calls. While researching and writing, I learned of the OpenerDirector class. This class offers the opportunity to streamline urlopen(), make it more secure, and provide custom error handling.
In addition to great Python HTTP client tools such as Requests and HTTPX, the standard library itself supplies the necessary ingredients to make a working HTTP client for API calls. This tutorial shares how to construct and customize such a tool for your own scripts.
I think the Python ecosystem is a place where the self-taught programmer can thrive. I know I have certainly made use of the wealth of free books and free tutorials available online. I have also been impressed by the range of free video resources and Youtube channels.
With such an abundance of freely available online resources, it is no wonder Python attracts self-taught programmers. Many well-written tutorials and even books are able to be accessed without a fee.
Books are great. Sometimes, though, I want something less heavy and more casual. Such as a tutorial.
Python tutorials abound. Sometimes, though, I want to feel like I am reading a consolidated and comprehensive volume of information about Python. In other words, a book.
While there are numerous ways to handle PDF documents with Python, I find generating or editing HTML far easier and more reliable than trying to figure out the intricacies of the PDF format. Sure, there is the venerable ReportLab, and if HTML is not your cup of tea, I encourage you to look into that option. There is also PyPDF2. Or maybe PyPDF3? No, perhaps PyPDF4! Hmmm… see the problem? My best guess is PyPDF3, for what that is worth.
If your name is José, you are in good company. José is a very common name. Yet, when dealing with text files, sometimes José will appear as José, or other mangled array of symbols and letters. Or, in some cases, Python will fail to convert the file to text at all, complaining with a UnicodeDecodeError.
When programming, it is common to perform an operation that is expected to fail at times, but should not throw an error. In Python, try/except clauses are frequent. In human terms, try/except means, “try this operation, and if it fails for this reason, then do this other thing.” For instance, you can try the following out in your console.
The code in this article is also available in a public Github repo here.
There are many Python web frameworks. Most of them are quite good. You could probably write one yourself, as many have.
With Typer, you can write command line tools in Python, intuitively and easily. At the same time, Typer is flexible enough to handle the complexity thrown at it.
Flake8 is like lint and LEGO® bricks. A great combination.
Do you ever store files alongside your Python files, and want to read them from within a running Python script?
A Python virtual environment is “a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages” (quote from the official docs).
More and more, I am using Python tools like Poetry and Black that use pyproject.toml as a central configuration file for packaging and tools. The finalized PEP 518 defined the specification for pyproject.toml, and many tools have adopted it.
In Part 2, we explored how to write tests and mocks using pytest and pytest_httpx for the synchronous version of the client, which was built in Part 1.
HTTPX is a modern HTTP client library for Python. Its interface is similar to the old standby Requests, but it supports asynchronous HTTP requests, using Python’s asyncio library (or trio). In other words, while your program is waiting for an HTTP request to finish, other work does not need to be blocked.
In Part 1, we built a simple Wikipedia search tool using Python and HTTPX.
“A memoized function ‘remembers’ the results corresponding to some set of specific inputs.” (From Wikipedia’s Memoization article). I think of memoization as an internal smart cache. A memoized function caches the results dependent on the arguments.
The Python Standard Library’s own argparse package is the officially recommended way to construct a command line interface (CLI) in Python.
HTTPX is a modern HTTP client library for Python. Its interface is similar to the old standby Requests, but it supports asynchronous HTTP requests, using Python’s asyncio library (or trio). In other words, while your program is waiting for an HTTP request to finish, other work does not need to be blocked.
The Click package makes it easy to develop a pretty command line interface (CLI) for your Python project.
The Python Fire package provides a way to develop a command line interface (CLI) in Python, automatically, with very minimal extra code. The package comes from Google but is “not an official Google product”.
Poetry is a robust and convenient tool for building Python projects. The article Getting Started with Python Poetry demonstrated this in simple terms.
The Poetry packaging and dependency management tool is the somewhat-new hotness for creating and maintaining a Python project.
ASGI is the new WSGI, with an asynchronous flair. ASGI is emerging as a new standard for asynchronous Python web apps. Because web applications spend a lot of time waiting, async web apps make a lot of sense, and have a significant performance boost over synchronous apps. Speedy.
Three prominent ASGI servers are all good options for testing and running your ASGI app: Uvicorn, Hypercorn, and Daphne. This article gives a brief synopsis of each, with examples for command-line invocation.
In Part 1 of this series, we created a simple Python distribution package called “pygreet.” In Part 2, we wrote tests and ran them using pytest.
In Part 1, we built a Python distribution package. It worked. However, we are not done without writing tests.
While Poetry and similar tools are being utilized more and more, I sometimes find that my Python development needs are satisfied by built-in Python features.
A Python module is a single file, and a package is a folder with potentially multiple files.
Markdown is “just enough markup” to write most of the documents I write, and is very readable for humans. (Aside: it is good to have a cheatsheet handy while typing!)
Here is a discipline I am trying to adopt in my Python programs: use "My string".casefold() instead of "My string".lower() when comparing strings irrespective of case.