From 9a0664f6c2fd1d3376c027eaf87592242c806e12 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Wed, 11 Mar 2020 23:07:58 +0100 Subject: [PATCH] =?UTF-8?q?TreeLib:=20Bezpe=C4=8Dn=C3=A9=20varianty=20.pre?= =?UTF-8?q?d=20a=20.father=5Fof=5Ffirst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/treelib.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/seminar/treelib.py b/seminar/treelib.py index ce956219..b2b106b2 100644 --- a/seminar/treelib.py +++ b/seminar/treelib.py @@ -10,23 +10,26 @@ def print_tree(node,indent=0): if node.succ: print_tree(node.succ, indent=indent) +# Django je trošku hloupé, takže node.prev nevrací None, ale hází django.core.exceptions.ObjectDoesNotExist +def safe_pred(node): + try: + return node.prev + except ObjectDoesNotExist: + return None + +# A to samé pro .father_of_first +def safe_father_of_first(node): + return node.prev + except ObjectDoesNotExist: + return None + ## Rodinné vztahy def get_parent(node): - pass # Nejdřív získáme prvního potomka... - # Django je trošku hloupé, takže node.prev nevrací None, ale hází django.core.exceptions.ObjectDoesNotExist - while True: - try: - node = node.prev - except ObjectDoesNotExist: - # We rely here on the fact that the assignment will not happen if RHS throws an exception - break + while safe_pred(node) is not None: + node = safe_pred(node) # ... a z prvního potomka umíme najít rodiče - # Django je pořád hloupé... - try: - return node.father_of_first - except ObjectDoesNotExist: - return None + return safe_father_of_first(node) # Obecný next: další Node v "the-right-order" pořadí (já, pak potomci, pak sousedé) def general_next(node):