Return a dictionary of Regex Matches

An example of how to make your function more flexible to handle a different number of return values. This means you won’t have to write the same code repeatedly if you are only changing the number of groups that are being returned. def findMatches(string, regex) -> dict: """ This is a generic matching function. Warning! Your regex expression MUST use 'Named Groups' -> (:P<name>) or this function will return an empty dictionary :param string: The text you are searching :type string: str :param regex: The regular expression string you are using to search :type regex: str :returns: A dictionary of named key/value pairs. [Read More]

Decoding Email Header and Contents

Using email.parser and email.policy along with policy.default means that you do not need to worry about character encoding as the modules will take care of it automatically.

I forget where I saw it but I believe that policy.default will become the real default in the future.
Current policy.compat32 is the module default.

Different String Padding Approaches

Padding Numbers

>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
004

Reference: Stack Overflow

Generate QR Codes

I want to try and do something with this eventually. Looks very useful.
Required imports:

pip install pillow
pip install qrcode

Code Sample:

import qrcode
# Link for website
input_data = "https://towardsdatascience.com/face-detection-in-10-lines-for-beginners-1787aa1d9127"
# Creating an instance of qrcode
qr = qrcode.QRCode(
        version=1,
        box_size=10,
        border=5)
qr.add_data(input_data)
qr.make(fit=True)
img = qr.make_image(fill='black', back_color='white')
img.save('qrcode001.png')