Package: tabular-dict
Tabular DictA Python3 library to flatten and inflate lists and dictionaries:For example, flattening something like this:fromtabular_dictimportflattenflatten({'some_key':{'foo':'bar',},'WithA.Dot':[True,None],})will render you a flat tabular structure like this:{'some_key.foo':'bar','WithA\\.Dot.[0]':True,'WithA\\.Dot.[1]':None}which can be inflated to the original without loss like this:fromtabular_dictimportinflateinflate({'some_key.foo':'bar','WithA\\.Dot.[0]':True,'WithA\\.Dot.[1]':None})How to installDocumentationflatten(data, separator = '.', escape = escape_type.SLASH, mark_arrays = True)Flattens a dict or list into a single-dimension dict, with a path value mapping. Paths are joined consecutive keys bound
by a pre-defined separator. Arrays are specially marked with square brackets.Only valid JSON dictionaries and lists are accepted (for example, dict keys must always be strings)Argumentsdata: Union[dict, list]- Dictionary or list to be flattenedseparator: str = '.'- Separator to be added between different levels on the data, defaults to a dotseparator: escape_type = escape_type.SLASH- Type of escaping, defaults toSLASH. Currently onlySLASHandNO_ESCAPEare supportedmark_arrays: bool = True- If set to True, will mark array index keys with a bracket.Return valueA dict with the flattened structure.Examplesfromtabular_dictimportflattenflatten({'a':{'b':{'c':{'d':'e'}}},'b':[[[[{'foo':'bar'}]]]],})Output:{'a.b.c.d':'e','b.[0].[0].[0].[0].foo':'bar',}inflate(data, separator = '.', escape = escape_type.SLASH, mark_arrays = True)Only valid JSON dictionaries and lists are accepted (for example, dict keys must always be strings)Argumentsflat_data: dict- Flat dictionary, with keys as strings and values as primitives (int, str, None, bool, float)separator: str = '.'- Separator that delimits different levels on the data, defaults to a dotseparator: escape_type = escape_type.SLASH- Type of escaping, defaults toSLASH. Currently onlySLASHandNO_ESCAPEare supportedmark_arrays: bool = True- If set to True, will recover array keys when enclosed in brackets.Return valueA dict or list with the inflated dataExamplesfromtabular_dictimportinflateinflate({'a.b.c.d':'e','b.[0].[0].[0].[0].foo':'bar',})# Also works with roots that are listsinflate({'[0].a':1,'[1]':2,'[2].a.b.c.d.e.f.g':'h',})Output:{'a':{'b':{'c':{'d':'e'}}},'b':[[[[{'foo':'bar'}]]]],}# Also works with roots that are lists[{'a':1},2,{'a':{'b':{'c':{'d':{'e':{'f':{'g':'h'}}}}}}},]escape_type:warning: For now, avoid using anything other than SLASH.There are three accepted escape types:SLASH - Will add a slash before the separator and duplicate slashes. This is the default and suggested typeNO_ESCAPE - Skips escaping altogetherDOUBLE - Duplicates the separator to escape it (Only partial support for now, avoid this)Example:fromtabular_dictimportescape_type,make_path# Gives you "\\some\-example"print(make_path('','\some-example',separator='-',escape=escape_type.SLASH))# Gives you "\some-example"print(make_path('','\some-example',separator='-',escape=escape_type.NO_ESCAPE))# Gives you "\some--example"print(make_path('','\some--example',separator='-',escape=escape_type.DOUBLE))make_path(path, key, separator = '.', escape = escape_type.SLASH)This is mostly an internal function, you will probably not use it.Concatenates a new key into a path, considering the separator and escape rule.Argumentspath: str- Current path, will concatenate new keys into it. Pass empty string if this is the root.key: str- Separator that delimits different levels on the data, defaults to a dotseparator: str = '.'- Separator that delimits different levels on the data, defaults to a dotseparator: escape_type = escape_type.SLASH- Type of escaping, defaults toSLASH.Return valueA string with the concatenated path and keyExamplesfromtabular_dictimportmake_path# Gives you "key"print(make_path('','key'))# Gives you "path,key"print(make_path('path','key',separator=','))# Gives you "a,longer,path,key_with_\,_comma"print(make_path('a,longer,path','key_with_,_comma',separator=','))split_path(path, key, separator = '.', escape = escape_type.SLASH)This is mostly an internal function, you will probably not use it.Concatenates a new key into a path, considering the separator and escape rule.Argumentspath: str- Full path generated by make_path. Will split it into individual keys.separator: str = '.'- Separator that delimits different levels on the data, defaults to a dotseparator: escape_type = escape_type.SLASH- Type of escaping, defaults toSLASH.mark_arrays: bool = True- If set to True, will recover array keys when enclosed in brackets.Return valueA list with the individual keys that make up the pathExamplesfromtabular_dictimportsplit_path# Gives you ["1"]print(split_path('1'))# Gives you [1]print(split_path('[1]'))# Gives you ["one", "tw,o", "three", 4]print(split_path('one,tw\\,o,three,[4]',separator=','))