PsiQ / bartiq

Bartiq
https://psiq.github.io/bartiq/
Apache License 2.0
31 stars 9 forks source link

Improve `repr` methods #29

Closed mstechly closed 4 months ago

mstechly commented 6 months ago

Currently the way various objects are printed out is a bit too verbose and hard to read, for example printing out Port object gives:

name='in' parent=<Routine name="my_algorithm"> direction='input' size='N' meta={}

But it could be much more concise, e.g.:

my_algorithm.#in, input, size: "N"

It would be good to review repr methods of all the classes we have and update them.

This will be helpful for both tutorials and debugging.

bdg221 commented 5 months ago

Here are the current __repr__ implementations:

src/bartiq/_routine.py class Routine -

def __repr__(self):
        return f'<{self.__class__.__name__} name="{self.name}">'

class Resource -

def __repr__(self):
        return f'<{self.__class__.__name__} name="{self.name}" value="{self.value}">'

src/bartiq/compilation/_symbolic_function.py class SymbolicFunction -

def __repr__(self) -> str:
        inputs = list(self._inputs.values())
        outputs = list(self._outputs.values())
        return f"SymbolicFunction(inputs={inputs}, outputs={outputs})"

src/bartiq/symbolics/variables.py class Independent Variable -

    def __repr__(self) -> str:
        attrs = ["value", "description"]
        kwargs_strs = _compile_kwargs_strs(self, attrs)
        args_str = ", ".join([self.symbol, *kwargs_strs])
        return f"IndependentVariable({args_str})"

class DependentVariable -

    def __repr__(self) -> str:
        attrs = ["value", "description"]
        kwargs_strs = _compile_kwargs_strs(self, attrs)
        args_str = ", ".join(map(str, [self.symbol, self.expression, *kwargs_strs]))
        return f"DependentVariable({args_str})"

For duplicating this behavior, I am using the 01_basic_example.ipynb which includes outputs for some objects, like resources and ports.

@mstechly If you would to provide a list of objects, I can create a list of the outputs. This list could then be reviewed and more concise output versions can be determined. Once that has been confirmed, then I would look to either update or create repr in the appropriate classes.

mstechly commented 5 months ago

Thanks @bdg221!

I think repr for these classes look fine. The one for Routine needs some more love, but I don't know what it should be, so let's leave it as it is.

For sure it would be good to get some more custom repr methods for Connection and Port. Connection could look sth like: routine_a.#port_0 -> routine_b.#port_1 For Port maybe what I suggested above, but if you have other suggestions, I'm open. Maybe sth like this will look slightly better: my_algorithm.#in, size: "N" (input) ?