Datasets:

ArXiv:
Elron commited on
Commit
9697a55
1 Parent(s): 950b2da

Upload dict_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. dict_utils.py +26 -19
dict_utils.py CHANGED
@@ -30,13 +30,11 @@ def dpath_set_one(dic, query_path, value):
30
 
31
 
32
  def dict_delete(dic, query_path):
33
- n = dpath.delete(dic, query_path)
34
 
35
 
36
  def dict_creator(current, segments, i, hints=()):
37
- """
38
- Create missing path components. If the segment is an int, then it will
39
- create a list. Otherwise a dictionary is created.
40
 
41
  set(obj, segments, value) -> obj
42
  """
@@ -60,7 +58,9 @@ def dict_creator(current, segments, i, hints=()):
60
  else:
61
  segment_next = None
62
 
63
- if isinstance(segment_next, int) or (isinstance(segment_next, str) and segment_next.isdecimal()):
 
 
64
  current[segment] = []
65
  else:
66
  current[segment] = {}
@@ -72,7 +72,9 @@ def dpath_set(dic, query_path, value, not_exist_ok=True):
72
  dpath.new(dic, query_path, value, creator=dict_creator)
73
  else:
74
  if len(paths) != 1:
75
- raise ValueError(f'query "{query_path}" matched {len(paths)} items in dict: {dic}. should match only one.')
 
 
76
  for path in paths:
77
  dpath_set_one(dic, path, value)
78
 
@@ -81,15 +83,17 @@ def dpath_set_multiple(dic, query_path, values, not_exist_ok=True):
81
  paths = [p for p, _ in dpath.search(dic, query_path, yielded=True)]
82
  if len(paths) == 0:
83
  if not_exist_ok:
84
- raise ValueError(f"Cannot set multiple values to non-existing path: {query_path}")
85
- raise ValueError(f'query "{query_path}" did not match any item in dict: {dic}')
86
- else:
87
- if len(paths) != len(values):
88
  raise ValueError(
89
- f'query "{query_path}" matched {len(paths)} items in dict: {dic} but {len(values)} values are provided. should match only one.'
90
  )
91
- for path, value in zip(paths, values):
92
- dpath_set_one(dic, path, value)
 
 
 
 
 
 
93
 
94
 
95
  def dict_get(dic, query, use_dpath=True, not_exist_ok=False, default=None):
@@ -97,18 +101,21 @@ def dict_get(dic, query, use_dpath=True, not_exist_ok=False, default=None):
97
  values = dpath_get(dic, query)
98
  if len(values) == 0 and not_exist_ok:
99
  return default
100
- elif len(values) == 0:
101
  raise ValueError(f'query "{query}" did not match any item in dict: {dic}')
102
 
103
  if len(values) == 1 and "*" not in query and "," not in query:
104
  return values[0]
105
 
106
  return values
107
- else:
108
- if not_exist_ok:
109
- return dic.get(query, default)
110
- else:
111
- return dic[query]
 
 
 
112
 
113
 
114
  def dict_set(dic, query, value, use_dpath=True, not_exist_ok=True, set_multiple=False):
 
30
 
31
 
32
  def dict_delete(dic, query_path):
33
+ dpath.delete(dic, query_path)
34
 
35
 
36
  def dict_creator(current, segments, i, hints=()):
37
+ """Create missing path components. If the segment is an int, then it will create a list. Otherwise a dictionary is created.
 
 
38
 
39
  set(obj, segments, value) -> obj
40
  """
 
58
  else:
59
  segment_next = None
60
 
61
+ if isinstance(segment_next, int) or (
62
+ isinstance(segment_next, str) and segment_next.isdecimal()
63
+ ):
64
  current[segment] = []
65
  else:
66
  current[segment] = {}
 
72
  dpath.new(dic, query_path, value, creator=dict_creator)
73
  else:
74
  if len(paths) != 1:
75
+ raise ValueError(
76
+ f'query "{query_path}" matched {len(paths)} items in dict: {dic}. should match only one.'
77
+ )
78
  for path in paths:
79
  dpath_set_one(dic, path, value)
80
 
 
83
  paths = [p for p, _ in dpath.search(dic, query_path, yielded=True)]
84
  if len(paths) == 0:
85
  if not_exist_ok:
 
 
 
 
86
  raise ValueError(
87
+ f"Cannot set multiple values to non-existing path: {query_path}"
88
  )
89
+ raise ValueError(f'query "{query_path}" did not match any item in dict: {dic}')
90
+
91
+ if len(paths) != len(values):
92
+ raise ValueError(
93
+ f'query "{query_path}" matched {len(paths)} items in dict: {dic} but {len(values)} values are provided. should match only one.'
94
+ )
95
+ for path, value in zip(paths, values):
96
+ dpath_set_one(dic, path, value)
97
 
98
 
99
  def dict_get(dic, query, use_dpath=True, not_exist_ok=False, default=None):
 
101
  values = dpath_get(dic, query)
102
  if len(values) == 0 and not_exist_ok:
103
  return default
104
+ if len(values) == 0:
105
  raise ValueError(f'query "{query}" did not match any item in dict: {dic}')
106
 
107
  if len(values) == 1 and "*" not in query and "," not in query:
108
  return values[0]
109
 
110
  return values
111
+
112
+ if not_exist_ok:
113
+ return dic.get(query, default)
114
+
115
+ if query in dic:
116
+ return dic[query]
117
+
118
+ raise ValueError(f'query "{query}" did not match any item in dict: {dic}')
119
 
120
 
121
  def dict_set(dic, query, value, use_dpath=True, not_exist_ok=True, set_multiple=False):