BinPy/BinPy — pull request #156 by petermlm (2014-03-21 02:09:27+00:00)
Title: Outputs

I've been working on what I proposed for issue #140. So I implemented the \_\_str\_\_() and \_\_repr\_\_() functions in the classes defined in gates, connectors and combinational.

These two classes allows us to have things like this:

```
>>> g1 = AND(True, False)
>>> print g1
AND Gate; Output: 0; Inputs: [True, False];
```

So, it is a bit easier to do prints.

Extending to that, the backtrack function was implemented. It's use is defined in the doc string:

```
This function returns a tree representation of a digital element, such as a
gate, and its inputs. The second argument specifies the depth to which the
representation shows digital elements instead of inputs. To ilustrate a use
case, suppose the following code:

>>> g1 = AND(True, False)
>>> g2 = AND(True, False)
>>> g3 = AND(g1, g2)
>>> tree = backtrack(g3, 2)

The 'tree' variable would have the following content:

>>> (g3, [(g1, [True, False]), (g2, [True, False])])

This tree representation is made using tuples, where the first element of
tuple is the node (gate, connector, mux, etc.), and the second is a list
of inputs. In that list each element will also be a tuple with the same
representation. In some cases instead of a tuple there will be a boolean
or int value, meaning the maximum depth as been reached.

Note that by doing 'print tree', the first element of the tuples will be
the __str__() output of the digital element.
```

After this there is the function printBacktrackTree(), which prints the tree defined by the previous function in a friendly way (at least I hope it's friendly). It's doc string:

```
This function takes a tree made by the backtrack functions and prints
that tree in a readable way. The way a gate, or a mux or any other
digital element gets represented depends on it's __str__()
implementation.

>>> g1 = AND(True, False)
>>> g2 = AND(True, False)
>>> g3 = AND(g1, g2)
>>> tree = backtrack(g3, 2)
>>> printBacktrackTree(tree)
|- AND Gate; Output: 0; Inputs: [0, 0];
|- AND Gate; Output: 0; Inputs: [True, False];
|- True
|- False
|- AND Gate; Output: 0; Inputs: [True, False];
|- True
|- False
```

So, I believe all this makes a few better outputs. What do you think?