<s> """<STR_LIT>""" <EOL> from operator import attrgetter <EOL> from . exceptions import InvalidMoveError <EOL> from . manager import TreeClassManager , TreeInstanceManager , TreeManager <EOL> from . options import TreeOptions <EOL> from . orm import TreeMapperExtension , TreeSessionExtension , DeclarativeMeta <EOL> from . types import TreeDepthType , TreeEndpointType , TreeIdType , TreeIntegerType , TreeLeftType , TreeRightType <EOL> from . import tests <EOL> _nonexistent = object ( ) <EOL> def _iter_current_next ( sequence ) : <EOL> """<STR_LIT>""" <EOL> iterator = iter ( sequence ) <EOL> current_item = next ( iterator ) <EOL> while current_item != _nonexistent : <EOL> try : <EOL> next_item = next ( iterator ) <EOL> except StopIteration : <EOL> next_item = _nonexistent <EOL> yield ( current_item , next_item ) <EOL> current_item = next_item <EOL> def _recursive_iterator ( sequence , is_child_func ) : <EOL> """<STR_LIT>""" <EOL> current_next_iterator = _iter_current_next ( sequence ) <EOL> item = { } <EOL> is_parent_of_next = lambda node : item [ '<STR_LIT>' ] is not _nonexistent and is_child_func ( node , item [ '<STR_LIT>' ] ) <EOL> def step ( ) : <EOL> item [ '<STR_LIT>' ] , item [ '<STR_LIT>' ] = next ( current_next_iterator ) <EOL> if is_parent_of_next ( item [ '<STR_LIT>' ] ) : <EOL> return ( item [ '<STR_LIT>' ] , children_generator ( item [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> return ( item [ '<STR_LIT>' ] , tuple ( ) ) <EOL> def children_generator ( parent_node ) : <EOL> while True : <EOL> yield step ( ) <EOL> if not is_parent_of_next ( parent_node ) : <EOL> break <EOL> while True : <EOL> yield step ( ) <EOL> def tree_recursive_iterator ( flat_tree , class_manager ) : <EOL> """<STR_LIT>""" <EOL> opts = class_manager . _tree_options <EOL> tree_id = attrgetter ( opts . tree_id_field . name ) <EOL> depth = attrgetter ( opts . depth_field . name ) <EOL> def is_child ( parent , child ) : <EOL> return tree_id ( parent ) == tree_id ( child ) and depth ( child ) == depth ( parent ) + <NUM_LIT:1> <EOL> return _recursive_iterator ( flat_tree , is_child ) </s>
