caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
801 stars 185 forks source link

It would be good to de-couple node iterators and node-printing #107

Open GalacticStarfish opened 5 years ago

GalacticStarfish commented 5 years ago

__get_iter is a bit of a mess. It handles both creating a node iterator AND printing functionality.

filter_ tells us to only print nodes meeting some user-specified property. filter_ is a function. You input a node into filter_ and filter returns true (print the node) or false (don't print the node). Do not get me wrong, that's a perfectly reasonable thing to want to do. I only mention what filter_ is so that we can see how to decouple making node iterators and printing.

key and reverse tell us how to order the nodes for printing purposes. reverse is a Boolean, which says to print the nodes in reverse order. key allows more complicated node orderings than a mere reversal.

dt is a container of three elements:

            Root 
            |-- C01
            |    +-- C11
            |         |-- C111
            |         +-- C112
            |-- C02
            |-- C03
            |    +-- C31

            Root 
            line_box     C01
            vline    line_cor     C11
            vline         line_box     C111
            vline         line_cor     C112
            line_box     C02
            line_box     C03
            vline    line_cor     C31

The code would be much cleaner if the back-end function which constructs and returns a node iterator knew nothing about printing, and the printing functionality used node iterators without knowing about filter_ key or reverse

The function with generates a node iterator would use filter_ key and reverse but not dt, dt_vline, dt_line_box, dt_line_cor.

After generating a node-iterator you pass the node-iterator to the printing function. The printing function does not know if the iterator it just received loops over ALL nodes, SOME of the nodes, nodes in forward order, nodes in backward order, etc... The printer doesn't need to know. It just needs a node iterator.