Created
July 6, 2020 19:37
-
-
Save dmoisset/23529281c470e7af9ea3207f2f6d6860 to your computer and use it in GitHub Desktop.
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
# Original: https://github.com/python/cpython/blob/46abfc1416ff8e450999611ef8f231ff871ab133/Lib/logging/config.py#L438-L464 | |
def convert(self, value): | |
""" | |
Convert values to an appropriate type. dicts, lists and tuples are | |
replaced by their converting alternatives. Strings are checked to | |
see if they have a conversion format and are converted if they do. | |
""" | |
match value: | |
case ConvertingDict() | ConvertingList() | ConvertingTuple: | |
return value | |
case dict(): | |
value = ConvertingDict(value) | |
value.configurator = self | |
case list(): | |
value = ConvertingList(value) | |
value.configurator = self | |
case tuple() if not hasattr(value, '_fields'): | |
value = ConvertingTuple(value) | |
value.configurator = self | |
case str() | |
m = self.CONVERT_PATTERN.match(value) | |
if m: | |
d = m.groupdict() | |
prefix = d['prefix'] | |
converter = self.value_converters.get(prefix, None) | |
if converter: | |
suffix = d['suffix'] | |
converter = getattr(self, converter) | |
value = converter(suffix) | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment