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
= "https://github.com/strickvl/some-repo/"
url
"https://github.com/{owner}/{repo}/", url).named
parse(
# 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.