Upload utils.py with huggingface_hub
Browse files
utils.py
CHANGED
|
@@ -1,7 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
class Singleton(type):
|
| 3 |
_instances = {}
|
| 4 |
def __call__(cls, *args, **kwargs):
|
| 5 |
if cls not in cls._instances:
|
| 6 |
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
| 7 |
-
return cls._instances[cls]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| 21 |
+
return cls._instances[cls]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def flatten_dict(d: Dict[str, Any], parent_key: str = "", sep: str = "_") -> Dict[str, Any]:
|
| 25 |
+
items = []
|
| 26 |
+
for k, v in d.items():
|
| 27 |
+
new_key = parent_key + sep + k if parent_key else k
|
| 28 |
+
if isinstance(v, dict):
|
| 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 |
+
d, q, p = tasks.get()
|
| 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]
|