Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
LhaID.py
Go to the documentation of this file.
1"""Typed LHA entry identifiers."""
2
3from pyhyperiso.phyperiso.pyhyperiso import common
4from typing import List, Union
5
6
7class LhaID:
8 """Python wrapper around the C++ ``LhaID`` identifier.
9
10 ``LhaID`` represents an identifier as an *ordered* sequence of integer parts.
11 This allows representing both:
12 - simple IDs (single part), e.g. a PDG-like code, and
13 - multi-part IDs (several parts), e.g. (block, row, column).
14
15 The canonical string representation joins the integer parts with underscores:
16
17 - ``[511]`` -> ``"511"``
18 - ``[5, 2]`` -> ``"5_2"``
19 - ``[321, 1, 3]`` -> ``"321_1_3"``
20
21 Notes:
22 - Hashing and comparisons are delegated to the underlying C++ object.
23 - Converting to ``int`` keeps **only the first part** (potentially losing
24 information for multi-part IDs), mirroring the C++ behavior.
25 """
26
27 def __init__(self, *args: Union[int, str, List[int], common.LhaID]):
28 """Create a ``LhaID`` from common Python representations.
29
30 This wrapper mirrors the C++ constructors which accept:
31 - a variadic list of integral sub-IDs,
32 - an underscore-separated string,
33 - a vector/list of integers,
34 - or an existing ``LhaID`` object.
35
36 Args:
37 *args: One of the following forms:
38
39 - ``LhaID(cpp_id)``
40 Where ``cpp_id`` is an existing ``common.LhaID`` instance.
41
42 - ``LhaID(n)``
43 Where ``n`` is a single integer sub-ID.
44
45 - ``LhaID("1_2_3")``
46 Underscore-separated sub-IDs.
47
48 - ``LhaID([1, 2, 3])``
49 List of sub-IDs.
50
51 - ``LhaID(1, 2, 3)``
52 Multiple positional integer sub-IDs.
53
54 Raises:
55 TypeError: If the provided argument types are not supported.
56
57 Examples:
58 >>> LhaID(511).to_string()
59 '511'
60 >>> LhaID("5_2").get_parts()
61 [5, 2]
62 >>> LhaID(321, 1, 3).to_string()
63 '321_1_3'
64 """
65 if len(args) == 1:
66 arg = args[0]
67 if isinstance(arg, common.LhaID):
68 self._cpp_obj = arg
69 elif isinstance(arg, int):
70 self._cpp_obj = common.LhaID(arg)
71 elif isinstance(arg, str):
72 self._cpp_obj = common.LhaID(arg)
73 elif isinstance(arg, list):
74 self._cpp_obj = common.LhaID(arg)
75 else:
76 raise TypeError("Unsupported argument for LhaID")
77 else:
78 self._cpp_obj = common.LhaID(*args)
79
80 def to_cpp(self):
81 """Return the underlying bound C++ object.
82
83 Returns:
84 common.LhaID: The wrapped pybind11 C++ instance.
85 """
86 return self._cpp_obj
87
88 def to_string(self):
89 """Return the canonical underscore-joined string representation.
90
91 Returns:
92 str: Canonical string form (e.g. ``"1_2_3"``). If the identifier has
93 no parts, the C++ implementation typically returns an empty string.
94 """
95 return self._cpp_obj.to_string()
96
97 def get_parts(self):
98 """Return the ordered list of integer parts.
99
100 Returns:
101 List[int]: The sub-IDs making up this identifier.
102 """
103 return self._cpp_obj.get_parts()
104
105 def __int__(self):
106 """Convert the identifier to a Python ``int``.
107
108 This is intended for *trivial* (single-part) IDs. If the underlying ID
109 contains multiple parts, only the **first** part is returned (the C++
110 implementation may emit a warning).
111
112 Returns:
113 int: The first part of the identifier.
114
115 Raises:
116 ValueError: If the identifier has no parts (depends on the C++ binding).
117 """
118 return int(self._cpp_obj)
119
120 def __eq__(self, other):
121 """Check equality with another ``LhaID``.
122
123 Args:
124 other (LhaID): Another wrapped identifier.
125
126 Returns:
127 bool: ``True`` if both wrap equal C++ ``LhaID`` values.
128 """
129 return isinstance(other, LhaID) and self._cpp_obj == other._cpp_obj
130
131 def __repr__(self):
132 """Return a debug representation.
133
134 Returns:
135 str: Debug-style string such as ``"LhaID(1_2_3)"``.
136 """
137 return f"LhaID({self.to_string()})"
138
139 def __hash__(self):
140 """Make the object usable as a dict key / set element.
141
142 Returns:
143 int: Hash based on the identifier parts.
144 """
145 return hash(tuple(self.get_parts()))
__init__(self, *Union[int, str, List[int], common.LhaID] args)
Definition LhaID.py:27