MCPcopy
hub / github.com/pydata/xarray / relative_to

Method relative_to

xarray/core/treenode.py:742–761  ·  view source on GitHub ↗

Compute the relative path from this node to node `other`. If other is not in this tree, or it's otherwise impossible, raise a ValueError.

(self, other: Self)

Source from the content-addressed store, hash-verified

740 return "/" + "/".join(names) # type: ignore[arg-type]
741
742 def relative_to(self, other: Self) -> str:
743 """
744 Compute the relative path from this node to node `other`.
745
746 If other is not in this tree, or it's otherwise impossible, raise a ValueError.
747 """
748 if not self.same_tree(other):
749 raise NotFoundInTreeError(
750 "Cannot find relative path because nodes do not lie within the same tree"
751 )
752
753 this_path = NodePath(self.path)
754 if any(other.path == parent.path for parent in (self, *self.parents)):
755 return str(this_path.relative_to(other.path))
756 else:
757 common_ancestor = self.find_common_ancestor(other)
758 path_to_common_ancestor = other._path_to_ancestor(common_ancestor)
759 return str(
760 path_to_common_ancestor / this_path.relative_to(common_ancestor.path)
761 )
762
763 def find_common_ancestor(self, other: Self) -> Self:
764 """

Callers 7

to_dictMethod · 0.80
filter_likeMethod · 0.80
open_groups_as_dictMethod · 0.80
open_groups_as_dictMethod · 0.80
open_groups_as_dictMethod · 0.80
open_groups_as_dictMethod · 0.80
test_relative_pathsMethod · 0.80

Calls 5

find_common_ancestorMethod · 0.95
NotFoundInTreeErrorClass · 0.85
NodePathClass · 0.85
same_treeMethod · 0.80
_path_to_ancestorMethod · 0.80

Tested by 1

test_relative_pathsMethod · 0.64