Created
December 15, 2023 21:37
-
-
Save Mroik/30e2af06e5ab874d5f51b8563ea02998 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import repeat | |
from functools import reduce | |
def get_dir_nodes(root): | |
match root["type"]: | |
case "anyof" | "oneof": | |
return [root["name"]] | |
case "dir": | |
return reduce(lambda a, b: a + b, map(get_dir_nodes, root["children"]), [root["name"]]) | |
def is_valid(root, leafs): | |
old_leafs = list(leafs) | |
leafs = get_dir_nodes(root) + old_leafs | |
def loop(root, leafs): | |
match root["type"], root["name"], root["children"]: | |
case ("anyof", _, []) | ("oneof", _, []): | |
return False | |
case "dir", s, []: | |
return s in leafs | |
case "dir", _, v: | |
return reduce(lambda a, b: a and b, map(loop, v, repeat(leafs)), True) | |
case "anyof", _, v: | |
ris = False | |
for k, x in zip(v, map(loop, v, repeat(leafs))): | |
if k["name"] in old_leafs: | |
should = loop(k, leafs) | |
if should is False: | |
raise Exception(f"{root['name']} as explicit tag makes collisions") | |
ris = ris or x | |
return ris | |
case "oneof", s, v: | |
a = map(loop, v, repeat(leafs)) | |
a = filter(lambda x: x, a) | |
a = len(list(a)) | |
if a > 1: | |
raise Exception(root["name"]) | |
elif a == 1: | |
return True | |
else: | |
return False | |
return loop(root, leafs) | |
root = { | |
"type": "dir", | |
"name": "Tampone_Antigenico", | |
"children": [ | |
{ | |
"type": "dir", | |
"name": "Senza autorizzazione", | |
"children": [] | |
}, | |
{ | |
"type": "anyof", | |
"name": "PPIP with optional result", | |
"children": [ | |
{ | |
"type": "oneof", | |
"name": "PPIP", | |
"children": [ | |
{ | |
"type": "dir", | |
"name": "Senza autorizzazione", | |
"children": [] | |
}, | |
{ | |
"type": "dir", | |
"name": "Con autorizzazione", | |
"children": [] | |
}, | |
] | |
}, | |
{ | |
"type": "dir", | |
"name": "ppip_with_result", | |
"children": [ | |
{ | |
"type": "oneof", | |
"name": "PPIP", | |
"children": [ | |
{ | |
"type": "dir", | |
"name": "Senza autorizzazione", | |
"children": [] | |
}, | |
{ | |
"type": "dir", | |
"name": "Con autorizzazione", | |
"children": [] | |
}, | |
] | |
}, | |
{ | |
"type": "oneof", | |
"name": "BooleanResult", | |
"children": [ | |
{ | |
"type": "dir", | |
"name": "positive", | |
"children": [] | |
}, | |
{ | |
"type": "dir", | |
"name": "negative", | |
"children": [] | |
}, | |
] | |
}, | |
] | |
}, | |
] | |
}, | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment