DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dal_helpers.py
Go to the documentation of this file.
1"""
2"""
3
6def get_attribute_info(o: object) -> dict[str, object]:
7 return o.__schema__['attribute']
8
9def get_relation_info(o: object) -> dict[str, object]:
10 return o.__schema__['relation']
11
12def get_attribute_list(o: object) -> list[str]:
13 return list(get_attribute_info(o))
14
15def get_relation_list(o: object) -> list[str]:
16 return list(get_relation_info(o))
17
18def get_superclass_list(o: object) -> list[str]:
19 return o.__schema__['superclass']
20
21def get_subclass_list(o: object) -> list[str]:
22 return o.__schema__['subclass']
23
24def compare_dal_obj(a: object, b: object) -> bool:
25 """Compare two dal objects by content"""
26
27 # TODO: add a check on a and b being dal objects
28 # There is no base class for dal objects in python, but dal objects have _shcema__objects.
29
30
31 if a.className() != b.className():
32 return False
33
34 attrs = get_attribute_list(a)
35 rels = get_relation_list(a)
36
37 a_attrs = {x:getattr(a, x) for x in attrs}
38 b_attrs = {x:getattr(b, x) for x in attrs}
39
40 a_rels = {x:getattr(a, x) for x in rels}
41 b_rels = {x:getattr(b, x) for x in rels}
42
43
44 return (a_attrs == b_attrs) and (a_rels == b_rels)
45
46
47#---------------
48def find_related(dal_obj: object, dal_group: set[object]) -> None:
49
50
51 rels = get_relation_list(dal_obj)
52
53 rel_objs = set()
54 for rel in rels:
55 rel_val = getattr(dal_obj, rel)
56
57 if rel_val is None:
58 continue
59
60 rel_objs.update(rel_val if isinstance(rel_val,list) else [rel_val])
61
62 # Isolate relationship objects that are not in the dal_group yet
63 new_rel_objs = rel_objs - dal_group
64
65 # Safely add the new object to the group
66 dal_group.update(rel_objs)
67 for o in new_rel_objs:
68 if o is None:
69 continue
70
71 find_related(o, dal_group)
72
73from collections.abc import Iterable
74def find_duplicates(collection: Iterable[object]) -> set[object]:
75 """
76 Find duplicated dal objects in a collection by comparing objects attributes and relationships
77 """
78
79 n_items = len(collection)
80 duplicates = set()
81 for i in range(n_items):
82 for j in range(i+1, n_items):
83 if compare_dal_obj(collection[i], collection[j]):
84 duplicates.add(collection[i])
85 duplicates.add(collection[j])
86
87 return duplicates
88
bool compare_dal_obj(object a, object b)
set[object] find_duplicates(Iterable[object] collection)
None find_related(object dal_obj, set[object] dal_group)
dict[str, object] get_relation_info(object o)
Definition dal_helpers.py:9
list[str] get_relation_list(object o)
list[str] get_subclass_list(object o)
list[str] get_superclass_list(object o)
list[str] get_attribute_list(object o)
dict[str, object] get_attribute_info(object o)
Dal helpers.
Definition dal_helpers.py:6