-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.py
More file actions
25 lines (23 loc) · 995 Bytes
/
tag.py
File metadata and controls
25 lines (23 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from typing import Optional
from utils import flatten, overlap
def list_of(
spec: any,
key: str,
tags: Optional[list[str]] = None,
debounce = True,
) -> list[any]:
if isinstance(spec, list):
return flatten([list_of(item, key, tags, False) for item in spec])
elif isinstance(spec, dict) and key in spec:
if tags != None and 'tag' in spec:
selectors = spec['tag']
if isinstance(selectors, str): selectors = selectors.split()
if not overlap(tags, selectors): return []
return list_of(spec[key], key, tags, False)
else: return ([spec], []) [debounce]
def full_list_of(spec: any, key: str, debounce = True) -> list[any]:
if isinstance(spec, list):
return flatten([full_list_of(item, key, debounce) for item in spec])
elif isinstance(spec, dict) and (debounce or key in spec):
return flatten([full_list_of(spec[k], key, k != key) for k in spec])
else: return ([spec], []) [debounce]