Commit 1c75398c authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Check for circularity in hierarchies

parent af1b3166
......@@ -30,6 +30,13 @@ import pandas as pd
import numpy as np
class CircularError(Exception):
"""Error raised by the :meth:`Hierarchy.parents` method in the event
that a circular relationship is detected in a hierarchy.
"""
pass
def getHierarchyFilePath(dtable, vid):
"""Return a path to the file containing hierarchy information for the
specified variable. Pass the path to the :func:`loadHierarchyFile`
......@@ -132,8 +139,12 @@ class Hierarchy(object):
parents = []
cidx = self.index(coding)
pidx = self.__parents[cidx]
seen = set()
while pidx >= 0:
if pidx in seen:
raise CircularError(pidx + 1, str(self.coding(pidx)))
seen.add(pidx)
parents.append(self.coding(pidx))
pidx = self.__parents[pidx]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment