nd2py.core.nettype package#
Submodules#
nd2py.core.nettype.inter_nettype module#
nd2py.core.nettype.nettype_mixin module#
- class nd2py.core.nettype.nettype_mixin.NetTypeMixin(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#
Bases:
ABCMixin that manages nettype candidates and constraint propagation.
Symbols can carry a network type (nettype) describing their role in a computational graph, such as
"node","edge"or"scalar". This mixin tracks:User-assigned nettype anchors (hard constraints).
The set of possible nettypes each symbol can take.
Propagation of constraints across the expression tree.
The host class (typically
Symbol) is expected to provide:self.parent: Reference to the parent symbol in the tree.self.operands: List of child symbols.self.map_nettype(*children_nettypes: NetType) -> Optional[NetType]: Class method that maps child nettypes to a result nettype for the operator.
- abstractmethod classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#
Map child nettypes to the operator’s resulting nettype.
This method defines the nettype semantics of a symbol class. Concrete subclasses must implement their own mapping logic.
- Parameters:
*children_nettypes (NetType) – Nettypes of each operand, in order.
- Returns:
Inferred nettype of the operator, or
Noneif the combination is invalid or cannot be resolved.- Return type:
Optional[NetType]
- __init__(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#
Initialize nettype state for a symbol-like object.
The initial nettype can be provided as a single value or a set of allowed values. When specified, it acts as a hard constraint (anchor) that guides later nettype inference.
- Parameters:
nettype (Optional[NetType | Set[NetType]]) – Initial nettype constraint. If a string in
ALL_NETTYPES, it is converted to a singleton set; ifNone, all nettypes are initially allowed.
- property possible_nettypes: Set[Literal['node', 'edge', 'scalar']]#
Return the set of possible nettypes for this object.
- Returns:
Current candidate nettypes that are consistent with all known constraints.
- Return type:
Set[NetType]
- property nettype: Literal['node', 'edge', 'scalar'] | None#
Return the resolved nettype if unique.
When the candidate set contains exactly one element, this property returns that nettype. Otherwise it returns
Noneto indicate that the nettype is still ambiguous.- Returns:
Unique nettype if determined, otherwise
None.- Return type:
Optional[NetType]
- infer_nettype()[source]#
Infer possible nettypes for the entire expression tree.
This method uses the currently assigned nettype constraints as anchors and propagates them through the symbol tree to update
_possible_nettypesof all involved symbols.
- classmethod get_nettype_range() Set[Literal['node', 'edge', 'scalar']][source]#
Compute and cache the full nettype range for this operator class.
The nettype range is the set of all possible result nettypes that can be produced by this operator, over all combinations of valid child nettypes. The result is cached on the class to avoid recomputation.
- Returns:
Set of all nettypes that this operator can yield.
- Return type:
Set[NetType]
- nettype_range#
自定义的类属性装饰器。 允许通过 ClassName.property_name 直接获取动态计算的类属性, 同时也支持 instance.property_name。