Three Python Helpers for Parsing Inputs

The parse, yarl and datefinder packages are all ways in Python to help parse input data of different formats and types. Nothing essential here, but useful nonetheless.
python
tools
Author

Alex Strick van Linschoten

Published

February 27, 2022

I continue to slowly work my way through the calmcode back catalogue. This week I learned about three tiny utility packages that make certain parsing tasks less painful.

parse (introduced here) is a way of turning simple text patterns into restructured data. Take the following example as an illustration:

from parse import parse

url = "https://github.com/strickvl/some-repo/"

parse("https://github.com/{owner}/{repo}/", url).named

# returns {'owner': 'strickvl', 'repo': 'some-repo'}

As Vincent explains, it’s sort of the inverse or opposite operation to what happens with an f-string.

For URLs of various kinds that you want to decompose easily, yarl (introduced here) is a great way to approach that in Python.

For dates stored in some kind of a string format, you might want to try datefinder (introduced here), an elegant if not always perfect way for converting date strings into datetime.datetime objects.