Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Implement field setup
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Aug 23, 2024
1 parent 76be49b commit 57516b6
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 100 deletions.
64 changes: 38 additions & 26 deletions new_holders_flat.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from dataclasses import field

import typer

from faebryk.core.module import Module
from faebryk.core.node import d_field, if_list, rt_field
from faebryk.core.util import as_unit
from faebryk.library.can_bridge_defined import can_bridge_defined
from faebryk.library.Electrical import Electrical
from faebryk.library.has_designator_prefix import has_designator_prefix
from faebryk.library.has_designator_prefix_defined import has_designator_prefix_defined
from faebryk.library.has_simple_value_representation_based_on_param import (
has_simple_value_representation_based_on_param,
)
from faebryk.library.TBD import TBD
from faebryk.libs.units import Quantity
from faebryk.libs.util import times
Expand All @@ -27,6 +25,8 @@ class Diode2(Module):
anode: Electrical
cathode: Electrical

XXXXX: Electrical = d_field(Electrical)

# static trait
designator_prefix: has_designator_prefix = d_field(
lambda: has_designator_prefix_defined("D")
Expand All @@ -37,43 +37,55 @@ class Diode2(Module):
def bridge(self):
return can_bridge_defined(self.anode, self.cathode)

def __finit__(self):
print("Called Diode __finit__")
def __preinit__(self):
print("Called Diode __preinit__")

# anonymous dynamic trait
self.add(
has_simple_value_representation_based_on_param(
self.forward_voltage,
lambda p: as_unit(p, "V"),
) # type: ignore
)
# self.add(
# has_simple_value_representation_based_on_param(
# self.forward_voltage,
# lambda p: as_unit(p, "V"),
# ) # type: ignore
# )


class LED2(Diode2):
color: TBD[float]

def __finit__(self):
print("Called LED __finit__")
def __preinit__(self):
print("Called LED __preinit__")


class LED2_NOINT(LED2, init=False):
def __finit__(self):
print("Called LED_NOINT __finit__")
def __preinit__(self):
print("Called LED_NOINT __preinit__")


class LED2_WITHEXTRAT_IFS(LED2):
extra: list[Electrical] = field(default_factory=lambda: times(2, Electrical))
extra2: list[Electrical] = if_list(Electrical, 2)

def __finit__(self):
print("Called LED_WITHEXTRAT_IFS __finit__")
@rt_field
def bridge(self):
return can_bridge_defined(self.extra2[0], self.extra2[1])

def __preinit__(self):
print("Called LED_WITHEXTRAT_IFS __preinit__")


def main():
print("Diode init ----")
_D = Diode2()
print("LED init ----")
_L = LED2()
print("LEDNOINIT init ----")
L2 = LED2_NOINT()
print("LEDEXTRA init ----")
L3 = LED2_WITHEXTRAT_IFS()

L3.cathode.connect(L2.cathode)

assert L3.cathode.is_connected_to(L2.cathode)


print("Diode init ----")
D = Diode2()
print("LED init ----")
L = LED2()
print("LEDNOINIT init ----")
L2 = LED2_NOINT()
print("LEDEXTRA init ----")
L3 = LED2_WITHEXTRAT_IFS()
typer.run(main)
3 changes: 2 additions & 1 deletion src/faebryk/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def get_connections(self) -> list["GraphInterface"]:
class LinkParent(Link):
def __init__(self, interfaces: list["GraphInterface"]) -> None:
super().__init__()
from faebryk.core.graphinterface import GraphInterfaceHierarchical

assert all([isinstance(i, "GraphInterfaceHierarchical") for i in interfaces])
assert all([isinstance(i, GraphInterfaceHierarchical) for i in interfaces])
# TODO rethink invariant
assert len(interfaces) == 2
assert len([i for i in interfaces if i.is_parent]) == 1 # type: ignore
Expand Down
3 changes: 3 additions & 0 deletions src/faebryk/core/moduleinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
_TLinkDirectShallow,
)
from faebryk.core.node import Node
from faebryk.core.trait import Trait
from faebryk.libs.util import print_stack

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -90,6 +91,8 @@ class GraphInterfaceModuleConnection(GraphInterface): ...


class ModuleInterface(Node):
class TraitT(Trait["ModuleInterface"]): ...

specializes: GraphInterface
specialized: GraphInterface
connected: GraphInterfaceModuleConnection
Expand Down
Loading

0 comments on commit 57516b6

Please sign in to comment.