Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
BlockName.py
Go to the documentation of this file.
1"""Typed names for LHA and internal parameter blocks."""
2
3from pyhyperiso.phyperiso.pyhyperiso import common
4from typing import List, Union, Set
5
6
8 """Python wrapper around the C++ ``BlockName`` with alias semantics.
9
10 A ``BlockName`` is not just one string: it is a *set of aliases* that all
11 refer to the same logical block (useful for legacy naming conventions or
12 capitalization differences).
13
14 Important semantic detail (from C++):
15 Two block names can be considered equal if they **share at least one**
16 alias (depending on the C++ operator== implementation).
17
18 Notes:
19 - ``to_string()`` returns a representative string for the block. If
20 multiple aliases exist, which one is returned may be unspecified.
21 Prefer ``get_alias()`` when you need all aliases.
22 - ``add_alias()`` and ``to_upper()`` are chainable and return ``self``.
23 """
24
25 def __init__(self, names: Union[str, List[str], Set[str], common.BlockName]):
26 """Create a ``PyBlockName`` from common Python representations.
27
28 Args:
29 names: One of:
30 - ``common.BlockName``: an existing bound C++ instance.
31 - ``str``: a single alias (some C++ implementations may also
32 parse slash-separated aliases like ``"A/B/C"``).
33 - ``list[str]`` or ``set[str]``: multiple aliases.
34
35 Raises:
36 TypeError: If ``names`` has an unsupported type.
37 """
38 if isinstance(names, common.BlockName):
39 self._cpp_obj = names
40 elif isinstance(names, str):
41 self._cpp_obj = common.BlockName(names)
42 elif isinstance(names, (list, set)):
43 str_set = {str(name) for name in names}
44 self._cpp_obj = common.BlockName(str_set)
45 else:
46 raise TypeError(f"Unsupported type for BlockName init: {type(names)}")
47
48 def to_string(self) -> str:
49 """Return a representative string for this block.
50
51 Returns:
52 str: A string alias for this block. If several aliases are present,
53 the chosen alias may be unspecified (mirrors C++).
54 """
55 return self._cpp_obj.to_string()
56
57 def get_alias(self) -> Set[str]:
58 """Return the full alias set.
59
60 Returns:
61 Set[str]: All aliases associated with this block name.
62 """
63 return set(self._cpp_obj.get_alias())
64
65 def has_alias(self, alias: str) -> bool:
66 """Check whether an alias is registered.
67
68 Args:
69 alias (str): Alias string to test.
70
71 Returns:
72 bool: ``True`` if ``alias`` is present in the alias set.
73 """
74 return self._cpp_obj.has_alias(alias)
75
76 def add_alias(self, alias: str):
77 """Add a new alias to this block name.
78
79 Args:
80 alias (str): Alias to add.
81
82 Returns:
83 PyBlockName: ``self`` (chainable).
84 """
85 self._cpp_obj.add_alias(alias)
86 return self # chainable
87
88 def to_upper(self):
89 """Normalize aliases to upper case.
90
91 Returns:
92 PyBlockName: ``self`` (chainable).
93 """
94 self._cpp_obj.to_upper()
95 return self # chainable
96
97 def __eq__(self, other):
98 """Equality comparison.
99
100 Args:
101 other (Union[PyBlockName, str]): Another block name wrapper or a string.
102
103 Returns:
104 bool: Result of the underlying C++ equality semantics. When comparing
105 to a string, the binding typically checks against the alias set.
106 """
107 if isinstance(other, BlockName):
108 return self._cpp_obj == other._cpp_obj
109 elif isinstance(other, str):
110 return self._cpp_obj == other
111 return False
112
113 def __ne__(self, other):
114 """Negated equality."""
115 return not self.__eq__(other)
116
117 def __lt__(self, other):
118 """Strict ordering.
119
120 Args:
121 other (PyBlockName): Another block name wrapper.
122
123 Returns:
124 bool: Result of the underlying C++ ordering (if provided).
125
126 Raises:
127 TypeError: If ``other`` is not a ``PyBlockName``.
128 """
129 if isinstance(other, BlockName):
130 return self._cpp_obj < other._cpp_obj
131 raise TypeError(f"Cannot compare BlockName with {type(other)}")
132
133 def __hash__(self):
134 """Hash for set/dict usage."""
135 return hash(self._cpp_obj)
136
137 def __str__(self):
138 """String conversion."""
139 return self.to_string()
140
141 def __repr__(self):
142 """Debug representation."""
143 return f"BlockName({self.get_alias()})"
__init__(self, Union[str, List[str], Set[str], common.BlockName] names)
Definition BlockName.py:25