Posted on January 12, 2021
| 1 minutes
| 151 words
| Bas-Man
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.
deffindMatches(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. The key value is derived \
from (:P<name>)
:returns: None is returned if No match is found.
:rtype: dict
:rtype: None
"""matcher=re.compile(regex,re.UNICODE)match=matcher.match(string)ifmatch:matches=dict()forkeyinmatch.groupdict():matches[key]=match.group(key)returnmatches# No MatchesreturnNone
Posted on October 15, 2020
(Last modified on November 2, 2020)
| 1 minutes
| 52 words
| Bas-Man
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.