code: def _set_level_depth(self, optobj):
          
          has_relationship = optobj is not None and 'relationship' in optobj.optional_attrs
  
          def _init_level(rec):
              if rec.level is None:
                  if rec.parents:
                      rec.level = min(_init_level(rec) for rec in rec.parents) + 1
                  else:
                      rec.level = 0
              return rec.level
  
          def _init_depth(rec):
              if rec.depth is None:
                  if rec.parents:
                      rec.depth = max(_init_depth(rec) for rec in rec.parents) + 1
                  else:
                      rec.depth = 0
              return rec.depth
  
          def _init_reldepth(rec):
              if not hasattr(rec, 'reldepth'):
                  up_terms = rec.get_goterms_upper()
                  if up_terms:
                      rec.reldepth = max(_init_reldepth(rec) for rec in up_terms) + 1
                  else:
                      rec.reldepth = 0
              return rec.reldepth
  
          for rec in self.values():
  
              # Add invert relationships
              if has_relationship:
                  if rec.depth is None:
                      _init_reldepth(rec)
  
                  # print("BBBBBBBBBBB1", rec.item_id, rec.relationship)
                  #for (typedef, terms) in rec.relationship.items():
                  #    invert_typedef = self.typedefs[typedef].inverse_of
                  #    # print("BBBBBBBBBBB2 {} ({}) ({}) ({})".format(
                  #    #    rec.item_id, rec.relationship, typedef, invert_typedef))
                  #    if invert_typedef:
                  #        # Add inverted relationship
                  #        for term in terms:
                  #            if not hasattr(term, 'relationship'):
                  #                term.relationship = defaultdict(set)
                  #            term.relationship[invert_typedef].add(rec)
                  # print("BBBBBBBBBBB3", rec.item_id, rec.relationship)
  
              if rec.level is None:
                  _init_level(rec)
  
              if rec.depth is None:
                  _init_depth(rec)
docs: Set level, depth and add inverted relationships.