Upload utils.py with huggingface_hub
Browse files
utils.py
CHANGED
@@ -1,20 +1,10 @@
|
|
1 |
-
from typing import Dict, Any
|
2 |
from queue import Queue
|
|
|
3 |
|
4 |
|
5 |
-
def is_done(query):
|
6 |
-
return query is None or len(query) == 0 or query == '/'
|
7 |
-
|
8 |
-
|
9 |
-
def is_wildcard(query):
|
10 |
-
return query == '*'
|
11 |
-
|
12 |
-
|
13 |
-
def is_int(query):
|
14 |
-
return query.isdigit()
|
15 |
-
|
16 |
class Singleton(type):
|
17 |
_instances = {}
|
|
|
18 |
def __call__(cls, *args, **kwargs):
|
19 |
if cls not in cls._instances:
|
20 |
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
@@ -29,45 +19,5 @@ def flatten_dict(d: Dict[str, Any], parent_key: str = "", sep: str = "_") -> Dic
|
|
29 |
items.extend(flatten_dict(v, new_key, sep=sep).items())
|
30 |
else:
|
31 |
items.append((new_key, v))
|
32 |
-
return dict(items)
|
33 |
-
|
34 |
-
|
35 |
-
def dict_query(dic, query_path):
|
36 |
-
results = []
|
37 |
-
tasks = Queue()
|
38 |
-
|
39 |
-
tasks.put((dic, query_path, ''))
|
40 |
-
|
41 |
-
while not tasks.empty():
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
if is_done(q):
|
46 |
-
results.append(d)
|
47 |
-
else:
|
48 |
-
|
49 |
-
parts = q.split('/', maxsplit=1)
|
50 |
-
|
51 |
-
current = parts[0]
|
52 |
-
nexts = parts[1] if len(parts) > 1 else None
|
53 |
-
|
54 |
-
if is_wildcard(current):
|
55 |
-
assert isinstance(d, list), f'cannot query wildcard "*" on non-list object: {d} (in path: {p})'
|
56 |
-
for item in d:
|
57 |
-
tasks.put((item, nexts, p + f'/{item}'))
|
58 |
-
|
59 |
-
elif is_int(current):
|
60 |
-
assert isinstance(d, list), f'cannot query integer "{current}" on non-list object: {d} (in path: {p})'
|
61 |
-
tasks.put((d[int(current)], nexts, p + f'/{current}'))
|
62 |
-
|
63 |
-
elif not isinstance(d, dict):
|
64 |
-
raise ValueError(f'cannot query "{current}" on non-dict object: {d} (in path: {p})')
|
65 |
-
else:
|
66 |
-
if current not in d:
|
67 |
-
raise ValueError(f'"{current}" not in dict object: {d} (in path: {p})')
|
68 |
-
tasks.put((d[current], nexts, p + f'/{current}'))
|
69 |
-
|
70 |
-
if len(results) == 0:
|
71 |
-
raise ValueError(f'query "{query_path}" did not match any item in dict: {dic}')
|
72 |
-
|
73 |
-
return results if len(results) > 1 else results[0]
|
|
|
|
|
1 |
from queue import Queue
|
2 |
+
from typing import Any, Dict
|
3 |
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
class Singleton(type):
|
6 |
_instances = {}
|
7 |
+
|
8 |
def __call__(cls, *args, **kwargs):
|
9 |
if cls not in cls._instances:
|
10 |
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
|
19 |
items.extend(flatten_dict(v, new_key, sep=sep).items())
|
20 |
else:
|
21 |
items.append((new_key, v))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
return dict(items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|