The back-end private functions should not have both reverse and key as input parameters. It is fine if the front-end allows users to specify reverse and key, the back-end functions are simpler if we use key only.
key and reverse tell us how to order the nodes for printing purposes.
reverse is a Boolean, which if set to true, indicates that we should print the nodes in reverse order. If reverse is false, we are supposed to print the nodes in their natural order.
key allows more complicated node orderings than a mere reversal. keyis also sufficiently general to define reverse node ordering.
Below, I have extracted behavior of functions only in-terms of input arguments key and reverse
def show(self, key=None, reverse=False):
self.__print_backend(self, key, reverse)
def __str__(self, key=None, reverse=False):
self.__print_backend(self, key, reverse)
save2file(self, key=None, reverse=False):
self.__print_backend(self, key, reverse)
def __print_backend(self, key=None, reverse=False):
# `key` will passed as input to the built-in python function `sorted`
if key is None:
def key(node):
# does **NOT** actually accept a node as input
# accepts a node identifier
# if `n` is a node, then `n._identifier` is the input to the key function.
return node
__get_iter(key, reverse)
def __get_iter(self, key, reverse):
if key:
children.sort(key=key, reverse=reverse)
# `children` is a list of node identifiers (not the nodes themselves)
# We are calling the list.sort() method
# children = [self[i] for i in node.fpointer if filter_(self[i])]
# node.fpointer returns node.fpointer
# node.fpointer returns node.fpointer
else: # key != none
# This almost never happens, because if `key` was originally `None`
# then `key` has long since been replaced by a function which simply returns
# the node.
if reverse: #
children = reversed(children)
def expand_tree(self, key=None, reverse=False):
queue.sort(key, reverse)
expansion.sort(key=key, reverse=reverse)
If a user passes reverse = true into one of the front-end functions, we need only generate a key-function which reverses the keys.
if reverse:
def bck_key(node):
return (-1) * frnt_key(node)
# key functions do **NOT** actually accept a nodes as input
# they accept node identifiers
# if `n` is a node, then `n._identifier` is the input to the key function.
From then on, all back-end functions can use key only, instead of accepting both key and reversed as input arguments.
The back-end private functions should not have both
reverse
andkey
as input parameters. It is fine if the front-end allows users to specifyreverse
andkey
, the back-end functions are simpler if we usekey
only.key
andreverse
tell us how to order the nodes for printing purposes.reverse
is a Boolean, which if set totrue
, indicates that we should print the nodes in reverse order. Ifreverse
is false, we are supposed to print the nodes in their natural order.key
allows more complicated node orderings than a mere reversal.key
is also sufficiently general to define reverse node ordering.Below, I have extracted behavior of functions only in-terms of input arguments
key
andreverse
If a user passes
reverse = true
into one of the front-end functions, we need only generate a key-function which reverses the keys.From then on, all back-end functions can use
key
only, instead of accepting bothkey
andreversed
as input arguments.