Skip to content

Commit 66a64a3

Browse files
committed
Rename CyclicInt to SymmetricInt
1 parent 317e3cd commit 66a64a3

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

dpath/segments.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dpath import options
66
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
7-
from dpath.types import PathSegment, Creator, Hints, Glob, Path, CyclicInt
7+
from dpath.types import PathSegment, Creator, Hints, Glob, Path, SymmetricInt
88

99

1010
def make_walkable(node) -> Iterator[Tuple[PathSegment, Any]]:
@@ -22,8 +22,8 @@ def make_walkable(node) -> Iterator[Tuple[PathSegment, Any]]:
2222
except AttributeError:
2323
try:
2424
indices = range(len(node))
25-
# Make all list indices cyclic so negative (wraparound) indexes are supported
26-
indices = map(lambda i: CyclicInt(i, len(node)), indices)
25+
# Convert all list indices to object so negative indexes are supported.
26+
indices = map(lambda i: SymmetricInt(i, len(node)), indices)
2727
return zip(indices, node)
2828
except TypeError:
2929
# This can happen in cases where the node isn't leaf(node) == True,
@@ -217,7 +217,6 @@ def match(segments: Path, glob: Glob):
217217
# If we were successful in matching up the lengths, then we can
218218
# compare them using fnmatch.
219219
if path_len == len(ss_glob):
220-
# TODO: Delete if not needed (previous code) - i = zip(map(int_str, segments), map(int_str, ss_glob))
221220
i = zip(segments, ss_glob)
222221
for s, g in i:
223222
# Match the stars we added to the glob to the type of the

dpath/types.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
from typing import Union, Any, Callable, Sequence, Tuple, List, Optional, MutableMapping
33

44

5-
class CyclicInt(int):
5+
class SymmetricInt(int):
66
"""Same as a normal int but mimicks the behavior of list indexes (can be compared to a negative number)."""
77

88
def __new__(cls, value: int, max_value: int, *args, **kwargs):
99
if value >= max_value:
1010
raise TypeError(
11-
f"Tried to initiate a CyclicInt with a value ({value}) "
11+
f"Tried to initiate a {cls.__name__} with a value ({value}) "
1212
f"greater than the provided max value ({max_value})"
1313
)
1414

@@ -21,10 +21,13 @@ def __eq__(self, other):
2121
if not isinstance(other, int):
2222
return False
2323

24+
if other >= self.max_value or other >= -self.max_value:
25+
return False
26+
2427
return int(self) == (self.max_value + other) % self.max_value
2528

2629
def __repr__(self):
27-
return f"<CyclicInt {int(self)}%{self.max_value}>"
30+
return f"<{self.__class__.__name__} {int(self)}%{self.max_value}>"
2831

2932
def __str__(self):
3033
return str(int(self))

0 commit comments

Comments
 (0)