'Can I pre-compile regex for PyInstaller?

I've written a script in which some regular expressions are used repeatedly. So, I stored them to a constant like this:

from re import compile as compile_regex

VALIDATE_REGEX = {
    # match for a list of comma separated values. each value must be/contain digits, letters, and/or hyphens. spaces are allowed between values and commas. any number of values are allowed, but at least one must be present.
    'CSVs': compile_regex(r'^[-\w]+( *, *[-\w]+)*( *,)?$'),

    # match for a single number. must be one or two digits
    'number': compile_regex(r'^[\d]{1,2}?$'),

    # match for a range of numbers. each number must be one or two digits. numbers are separated by a hyphen. spaces are allowed between numbers and the hyphen.
    'range': compile_regex(r'^\d{1,2} *- *\d{1,2}$'),

    # [more]
}

When I want to use them, I do something like VALIDATE_REGEX['number'].match(user_input).

Now, I'm exporting this script as a one-file stand-alone with PyInstaller. If I understand correctly, re.compile() would produce some kind of an object. Is there a way I can compile this in advance and pass it to PyInstaller instead of compiling each time the executable runs?

EDIT: This is on Python 3.10.4 (latest stable release at the time of writing), if that's relevant. Application will ultimately be converted to executables for MacOS, Windows, and Linux on CI/CD.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source