Jonathan Bowman

Categories: Python

Two Methods for Testing HTTPS API Calls with Python and pytest and Also Communicating with the in-Laws

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.

Hardening and Simplifying Python's urlopen

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.

HTTP Calls in Python Without Requests or Other External Dependencies

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.

Free Python Courses Online

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.

Videos and Channels for Learning Python

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.

Free Python Tutorials

Books are great. Sometimes, though, I want something less heavy and more casual. Such as a tutorial.

Free Books for Learning Python

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.

Python PDF Generation from HTML with WeasyPrint

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.

Character Encodings and Detection with Python, chardet, and cchardet

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.

Suppressing Exceptions in Python with contextlib.suppress, not try/except/pass

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.

Flexible CSV Handling in Python with DictReader and DictWriter

The code in this article is also available in a public Github repo here.

Modern Python Template Engines

Top 1 Python Web Frameworks to Learn in the 21 th Century

There are many Python web frameworks. Most of them are quite good. You could probably write one yourself, as many have.

Command Line Tools in Python with Typer and pytest: Type Hints Are Useful

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.

Useful Flake8 Plugins for Python Linting

Flake8 is like lint and LEGO® bricks. A great combination.

Easily Load non-Python Data Files from a Python Package with importlib.resources

Do you ever store files alongside your Python files, and want to read them from within a running Python script?

Python Tools for Managing Virtual Environments

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).

How do I install a Python command line tool or script? (hint: pipx)

Using Flake8 and pyproject.toml with FlakeHell

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.

Getting Started with HTTPX, Part 4: pytest-asyncio and pytest_httpx (Asynchronous Version)

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.

Getting Started with HTTPX, Part 3: Building a Python REST Client (Asynchronous Version)

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.

Getting Started with HTTPX, Part 2: pytest and pytest_httpx

In Part 1, we built a simple Wikipedia search tool using Python and HTTPX.

Memoized Functions in Python with functools.lru_cache

“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.

Build and Test a Command Line Interface with Poetry, Python's argparse, and pytest

The Python Standard Library’s own argparse package is the officially recommended way to construct a command line interface (CLI) in Python.

Getting Started with HTTPX, Part 1: Building a Python REST Client (Synchronous Version)

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.

Build and Test a Command Line Interface with Python, Poetry, Click, and pytest

The Click package makes it easy to develop a pretty command line interface (CLI) for your Python project.

Build and Test a Command Line Interface with Poetry, Python Fire, and pytest

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”.

Build Command Line Tools with Python Poetry

Poetry is a robust and convenient tool for building Python projects. The article Getting Started with Python Poetry demonstrated this in simple terms.

Getting Started with Python Poetry

The Poetry packaging and dependency management tool is the somewhat-new hotness for creating and maintaining a Python project.

Write Your Own Python Async ASGI Web App, No Framework Needed

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.

The Three Python ASGI Servers

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.

Python Dev Environment Part 3: dependencies with install_requires and requirements.txt

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.

Python Dev Environment Part 2: Testing with pytest

In Part 1, we built a Python distribution package. It worked. However, we are not done without writing tests.

Python Dev Environment Part 1: setup.py, venv, and pip

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.

Python Module vs. Package

A Python module is a single file, and a package is a folder with potentially multiple files.

Processing Markdown in Python using available CommonMark implementations: cmarkgfm, paka.cmark, and mistletoe

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!)

Case-insensitive string comparison in Python using casefold, not lower

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.