You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I find metadict very useful but find a lot of times dicts have keys that are not valid python names so not useable with auto completion using dot notation.
I used the following workaround and wondering if a proper implementation of the following idea could be incorporated as an option to enhance MetaDict class so it can support optional conversion to valid names for keys so all dict keys are accessible using dot notation.
def convert_keys_to_valid_names(dict_in, prefix = "N_",strip_char=''):
""" convert keys to valid strings for use in MetaDict which support dot notation access for dictionaries - tab completion
dict keys can be integers but integers are not supported in dot access so convert to strings with prefix.
"""
dictionary = dict(dict_in) # in case iterable is passed, first convert to dictionary
return {(prefix+str(key) if isinstance(key, int) else prefix + re.sub(r'\W+', '', key).strip(strip_char) if key[0].isnumeric()
else re.sub(r'\W+', '_', key).strip(strip_char)): value for key, value in dictionary.items()}
def myMetaDict(dict_in,nested_assignment=False,strip_char='_'):
""" dot notation dict by using MetaDict with python valid transformed keys"""
return ( MetaDict(convert_keys_to_valid_names(dict_in,strip_char=strip_char),nested_assignment=nested_assignment))
The text was updated successfully, but these errors were encountered:
I find metadict very useful but find a lot of times dicts have keys that are not valid python names so not useable with auto completion using dot notation.
I used the following workaround and wondering if a proper implementation of the following idea could be incorporated as an option to enhance MetaDict class so it can support optional conversion to valid names for keys so all dict keys are accessible using dot notation.
def convert_keys_to_valid_names(dict_in, prefix = "N_",strip_char=''):
""" convert keys to valid strings for use in MetaDict which support dot notation access for dictionaries - tab completion
dict keys can be integers but integers are not supported in dot access so convert to strings with prefix.
"""
dictionary = dict(dict_in) # in case iterable is passed, first convert to dictionary
return {(prefix+str(key) if isinstance(key, int) else prefix + re.sub(r'\W+', '', key).strip(strip_char) if key[0].isnumeric()
else re.sub(r'\W+', '_', key).strip(strip_char)): value for key, value in dictionary.items()}
def myMetaDict(dict_in,nested_assignment=False,strip_char='_'):
""" dot notation dict by using MetaDict with python valid transformed keys"""
return ( MetaDict(convert_keys_to_valid_names(dict_in,strip_char=strip_char),nested_assignment=nested_assignment))
The text was updated successfully, but these errors were encountered: