Code Monkey home page Code Monkey logo

Comments (1)

PythonFZ avatar PythonFZ commented on August 30, 2024

First tests show that one could use:

class NodeBaseMixin:
    def __getattribute__(self, item):
        value = super().__getattribute__(item)
        if NodeBaseMixin._graph_ is not None:
            if item not in ["_graph_"] and not item.startswith("__"):
                connector = Connection(instance=self, attribute=item)
                return connector
        return value

    def __setattr__(self, item, value) -> None:
        if isinstance(value, Connection):
            assert id(self) in self._graph_
            assert id(value.instance) in self._graph_
            self._graph_.add_edge(
                id(value.instance), id(self), i_attr=value.attribute, j_attr=item
            )
        super().__setattr__(item, value)

with

@dataclasses.dataclass(frozen=True)
class Connection:
    """A Connector for Nodes.
    instance: either a Node or FucntionFuture
    attribute:
        Node.attribute
        or FunctionFuture.result
        or None if the class is passed and not an attribute
    """

    instance: any
    attribute: any

it is REALLY important to mention, that within the DiGraph something like the following does not work within the context manager. This affects also all calls inside the __init__ or generally everything that is not specifically excluded inside __getattribute__.

@dataclasses.dataclass()
class Node(NodeBaseMixin):
    input: int
    output: int = None
    
    def __post_init__(self):
        self.output = self.input * 2  # this can not work, because self.input is Connection and not int

from znflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.