Skip to content

Instantly share code, notes, and snippets.

@dmoisset
Created July 6, 2020 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmoisset/23529281c470e7af9ea3207f2f6d6860 to your computer and use it in GitHub Desktop.
Save dmoisset/23529281c470e7af9ea3207f2f6d6860 to your computer and use it in GitHub Desktop.
# 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