bigraph-schema 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ # from bigraph_schema.registry import (
2
+ # deep_merge, validate_merge, default, Registry, hierarchy_depth, is_schema_key, establish_path,
3
+ # strip_schema_keys, type_parameter_key, non_schema_keys, set_path, transform_path)
4
+
5
+ # from bigraph_schema.utilities import get_path, visit_method
6
+
7
+ # from bigraph_schema.type_system import TypeSystem
8
+ # from bigraph_schema.type_functions import FUNCTION_TYPE, METHOD_TYPE, type_schema_keys, resolve_path
9
+
10
+
11
+ from bigraph_schema.edge import Edge
12
+ from bigraph_schema.protocols import local_lookup_module
13
+
14
+ from bigraph_schema.schema import BASE_TYPES, resolve_path, deep_merge, make_default
15
+ from bigraph_schema.core import Core, allocate_core
16
+
17
+ import bigraph_schema.methods
18
+
19
+
20
+ def get_path(tree, path):
21
+ """
22
+ Given a tree and a path, find the subtree at that path
23
+
24
+ Args:
25
+ - tree: the tree we are looking in (a nested dict)
26
+ - path: a list/tuple of keys we follow down the tree to find the subtree we are looking for
27
+
28
+ Returns:
29
+ - subtree: the subtree found by following the list of keys down the tree
30
+ """
31
+
32
+ if len(path) == 0:
33
+ return tree
34
+ else:
35
+ head = path[0]
36
+ if not tree or head not in tree:
37
+ return None
38
+ else:
39
+ return get_path(tree[head], path[1:])
40
+
41
+ def establish_path(tree, path, top=None, cursor=()):
42
+ """
43
+ Create or traverse a path in a nested dictionary (tree structure).
44
+ """
45
+ if tree is None:
46
+ tree = {}
47
+ if top is None:
48
+ top = tree
49
+ if path is None or len(path) == 0:
50
+ return tree
51
+ if isinstance(path, str):
52
+ path = (path,)
53
+ head = path[0]
54
+ if head == '..':
55
+ if len(cursor) == 0:
56
+ raise Exception(f'trying to travel above the top of the tree: {path}')
57
+ return establish_path(top, cursor[:-1])
58
+ if head not in tree:
59
+ tree[head] = {}
60
+ return establish_path(tree[head], path[1:], top=top, cursor=cursor + (head,))
61
+
62
+ def set_path(tree, path, value, top=None, cursor=None):
63
+ """
64
+ Set `value` at the given `path` in a tree-like dictionary.
65
+ """
66
+ if value is None:
67
+ return None
68
+ if len(path) == 0:
69
+ return value
70
+ final = path[-1]
71
+ destination = establish_path(tree, path[:-1])
72
+ destination[final] = value
73
+ return tree
74
+
75
+ def transform_path(tree, path, transform):
76
+ """
77
+ Apply a transformation function to a value at a specific path in a tree.
78
+ """
79
+ before = establish_path(tree, path)
80
+ after = transform(before)
81
+ return set_path(tree, path, after)
82
+
83
+ def hierarchy_depth(hierarchy, path=()):
84
+ """
85
+ Recursively collect all node paths in a hierarchy.
86
+ """
87
+ base = {}
88
+ for key, inner in hierarchy.items():
89
+ down = path + (key,)
90
+ if is_schema_key(key):
91
+ base[path] = inner
92
+ elif isinstance(inner, dict) and 'instance' not in inner:
93
+ base.update(hierarchy_depth(inner, down))
94
+ else:
95
+ base[down] = inner
96
+ return base
97
+
98
+ def is_schema_key(key):
99
+ """Check if a key is a schema key (starts with underscore)."""
100
+ return isinstance(key, str) and key.startswith('_')
101
+
102
+ def strip_schema_keys(state):
103
+ """
104
+ Recursively remove all schema keys from a dictionary.
105
+ """
106
+ if isinstance(state, dict):
107
+ return {
108
+ k: strip_schema_keys(v)
109
+ for k, v in state.items()
110
+ if not is_schema_key(k)}
111
+
112
+ return state