Skip to content

Commit

Permalink
feat: Paths sort by their path tree
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Oct 15, 2019
1 parent abdfc78 commit 0921994
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mutapath/immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def __eq__(self, other):
return str(self) == str(other)
return super(Path, self).__eq__(other)

def __lt__(self, other):
if isinstance(other, Path):
return self.splitall() < other.splitall()
left = str(self).replace("\\\\", "\\").replace("\\", "/")
right = str(other).replace("\\\\", "\\").replace("\\", "/")
return left < right

def __add__(self, other):
return self._contained.__add__(Path(other)._contained)

Expand Down
23 changes: 23 additions & 0 deletions tests/test_immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,26 @@ def test_hash(self):
expected = hash(Path("/A") / "B")
actual = hash(Path("/A/B/"))
self.assertEqual(expected, actual)

def test_lt_last(self):
lesser = Path("/A/B/")
greater = Path("/A/C")
self.assertLess(lesser, greater)

def test_lt_first(self):
lesser = Path("/A/D")
greater = Path("/B/C")
self.assertLess(lesser, greater)

def test_sort(self):
first = Path("/A/B/C")
second = Path("/A/C")
third = Path("/B/A/A")
expected = [first, second, third]
actual = sorted([third, first, second])
self.assertEqual(expected, actual)

def test_lt_str(self):
lesser = Path("/A/B/")
greater = "/A/C"
self.assertLess(lesser, greater)

0 comments on commit 0921994

Please sign in to comment.