11"""Module providing tooling to elaborate a dictionary object."""
2+
23from typing import Dict , Set , Any
34
45
5- def dict_of_dict ( D : Dict ) -> bool :
6+ def is_dict_of_dict ( dictionary : Dict ) -> bool :
67 """Return whether given dictionary contains only dictionaries.
78
89 Parameters
910 -------------------------------
10- D : Dict
11+ dictionary : Dict
1112 Dictionary of which to determine if contains only dictionaries.
1213
1314 Returns
1415 -------------------------------
1516 Boolean representing whether given dictionary contains only dictionaries.
1617 """
17- return all (isinstance (d , dict ) for d in D .values ())
18+ return all (isinstance (value , dict ) for value in dictionary .values ())
1819
1920
20- def min_depth (D : Dict ) -> int :
21+ def min_depth (dictionary : Dict ) -> int :
2122 """Return minimum depth of given dictionary.
2223
2324 Parameters
2425 -------------------------------
25- D : Dict
26+ dictionary : Dict
2627 Dictionary of which to determine the width
2728
2829 Returns
2930 -------------------------------
3031 Minimum depth of dictionary.
3132 """
32- return 1 + min (
33- min_depth (d ) for d in D .values ()
34- ) if dict_of_dict (D ) else 0
33+ return (
34+ 1 + min (min_depth (value ) for value in dictionary .values ())
35+ if is_dict_of_dict (dictionary )
36+ else 0
37+ )
3538
3639
37- def axis_keys (D : Dict , axis : int ) -> Set [Any ]:
40+ def axis_keys (dictionary : Dict , axis : int ) -> Set [Any ]:
3841 """Return set of keys at given axis.
3942
4043 Parameters
4144 -------------------------------
42- D : Dict
45+ dictionary : Dict
4346 Dictionary to determine keys of.
4447 Axis:int
4548 Depth of keys.
@@ -48,17 +51,19 @@ def axis_keys(D: Dict, axis: int) -> Set[Any]:
4851 -------------------------------
4952 The set of keys at given axis
5053 """
51- return set .union (* [
52- axis_keys (d , axis - 1 ) for d in D .values ()
53- ]) if axis else set (D .keys ())
54+ return (
55+ set .union (* [axis_keys (value , axis - 1 ) for value in dictionary .values ()])
56+ if axis
57+ else set (dictionary .keys ())
58+ )
5459
5560
56- def reindex_key (D : Dict , key : Any , axis : int ) -> Dict :
61+ def reindex_key (dictionary : Dict , key : Any , axis : int ) -> Dict :
5762 """Return reindex dictionary to given key.
5863
5964 Parameters
6065 -------------------------------
61- D : Dict
66+ dictionary : Dict
6267 Dictionary to reindex.
6368 key: Any
6469 Key to reindex.
@@ -70,34 +75,30 @@ def reindex_key(D: Dict, key: Any, axis: int) -> Dict:
7075 Reindexed dictionary.
7176 """
7277 if axis == 0 :
73- return D .get (key )
78+ return dictionary .get (key )
7479 return {
7580 subkey : value
76- for subkey , value in
77- (
81+ for subkey , value in (
7882 (
7983 subkey ,
80- value
81- if not isinstance (value , dict )
82- else
83- reindex_key (
84- value ,
85- key ,
86- axis - 1
87- )
84+ (
85+ value
86+ if not isinstance (value , dict )
87+ else reindex_key (value , key , axis - 1 )
88+ ),
8889 )
89- for subkey , value in D .items ()
90+ for subkey , value in dictionary .items ()
9091 )
9192 if value is not None and (not isinstance (value , dict ) or len (value ) != 0 )
9293 }
9394
9495
95- def transpose_dict (D : Dict , axis : int ) -> Dict :
96+ def transpose_dict (dictionary : Dict , axis : int ) -> Dict :
9697 """Transpose given dictionary on given axis.
9798
9899 Parameters
99100 -------------------------------
100- D : Dict
101+ dictionary : Dict
101102 Dictionary to transpose.
102103 axis: int
103104 Axis to traspose.
@@ -107,6 +108,5 @@ def transpose_dict(D: Dict, axis: int) -> Dict:
107108 Transposed dictionary.
108109 """
109110 return {
110- key : reindex_key (D , key , axis )
111- for key in axis_keys (D , axis )
111+ key : reindex_key (dictionary , key , axis ) for key in axis_keys (dictionary , axis )
112112 }
0 commit comments