GitLab Repo

amachine.am_transition

 1from dataclasses import dataclass, replace, asdict
 2from fractions import Fraction
 3from typing import Self, Any
 4import copy
 5
 6@dataclass(frozen=True)
 7class Transition :
 8
 9    origin_state_idx : int
10    target_state_idx : int
11    prob : float
12    symbol_idx : int 
13    pq : Fraction | None = None
14    composition_depth : int = 0
15
16    def deepcopy ( self ) -> Self:
17         # All fields are immutable, so shallow copy is equivalent to deep copy
18        return copy.copy(self)
19
20    def modified_deep_copy(self, **changes) -> Self:
21         # All fields are immutable, so shallow copy is equivalent to deep copy
22        return replace ( self, **changes )
23
24    @classmethod
25    def from_json_dict(cls, data: dict) -> Self:
26
27        raw_pq = data.get("pq")
28        pq_field = Fraction(raw_pq) if raw_pq is not None else None
29        
30        return cls(
31            origin_state_idx=data["origin_state_idx"],
32            target_state_idx=data["target_state_idx"],
33            prob=data["prob"],
34            symbol_idx=data["symbol_idx"],
35            pq=pq_field,
36            composition_depth=data.get( "composition_depth", 0 )
37        )
38
39    def to_json_dict( self ) -> dict[str,Any]:
40        
41        d = asdict( self )
42
43        if d[ "pq" ] is not None :
44            d[ "pq" ] = str( self.pq )
45
46        d["symbol_idx"] = int(d["symbol_idx"])
47
48        return d 
@dataclass(frozen=True)
class Transition:
 7@dataclass(frozen=True)
 8class Transition :
 9
10    origin_state_idx : int
11    target_state_idx : int
12    prob : float
13    symbol_idx : int 
14    pq : Fraction | None = None
15    composition_depth : int = 0
16
17    def deepcopy ( self ) -> Self:
18         # All fields are immutable, so shallow copy is equivalent to deep copy
19        return copy.copy(self)
20
21    def modified_deep_copy(self, **changes) -> Self:
22         # All fields are immutable, so shallow copy is equivalent to deep copy
23        return replace ( self, **changes )
24
25    @classmethod
26    def from_json_dict(cls, data: dict) -> Self:
27
28        raw_pq = data.get("pq")
29        pq_field = Fraction(raw_pq) if raw_pq is not None else None
30        
31        return cls(
32            origin_state_idx=data["origin_state_idx"],
33            target_state_idx=data["target_state_idx"],
34            prob=data["prob"],
35            symbol_idx=data["symbol_idx"],
36            pq=pq_field,
37            composition_depth=data.get( "composition_depth", 0 )
38        )
39
40    def to_json_dict( self ) -> dict[str,Any]:
41        
42        d = asdict( self )
43
44        if d[ "pq" ] is not None :
45            d[ "pq" ] = str( self.pq )
46
47        d["symbol_idx"] = int(d["symbol_idx"])
48
49        return d 
Transition( origin_state_idx: int, target_state_idx: int, prob: float, symbol_idx: int, pq: fractions.Fraction | None = None, composition_depth: int = 0)
origin_state_idx: int
target_state_idx: int
prob: float
symbol_idx: int
pq: fractions.Fraction | None = None
composition_depth: int = 0
def deepcopy(self) -> Self:
17    def deepcopy ( self ) -> Self:
18         # All fields are immutable, so shallow copy is equivalent to deep copy
19        return copy.copy(self)
def modified_deep_copy(self, **changes) -> Self:
21    def modified_deep_copy(self, **changes) -> Self:
22         # All fields are immutable, so shallow copy is equivalent to deep copy
23        return replace ( self, **changes )
@classmethod
def from_json_dict(cls, data: dict) -> Self:
25    @classmethod
26    def from_json_dict(cls, data: dict) -> Self:
27
28        raw_pq = data.get("pq")
29        pq_field = Fraction(raw_pq) if raw_pq is not None else None
30        
31        return cls(
32            origin_state_idx=data["origin_state_idx"],
33            target_state_idx=data["target_state_idx"],
34            prob=data["prob"],
35            symbol_idx=data["symbol_idx"],
36            pq=pq_field,
37            composition_depth=data.get( "composition_depth", 0 )
38        )
def to_json_dict(self) -> dict[str, typing.Any]:
40    def to_json_dict( self ) -> dict[str,Any]:
41        
42        d = asdict( self )
43
44        if d[ "pq" ] is not None :
45            d[ "pq" ] = str( self.pq )
46
47        d["symbol_idx"] = int(d["symbol_idx"])
48
49        return d