Skip to content

Commit

Permalink
Merge pull request #483 from SpiNNakerManchester/neo_database
Browse files Browse the repository at this point in the history
Ability to create a slice for String
  • Loading branch information
rowleya committed Dec 8, 2022
2 parents 763ab87 + 58c7460 commit d27f8b4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
23 changes: 22 additions & 1 deletion pacman/model/graphs/common/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,25 @@ def __str__(self):
value = ""
for slice in self.slices:
value += f"({slice.start}:{slice.stop})"
return f"[{value}]"
return f"{self.lo_atom}{value}"

@classmethod
def from_string(cls, str):
if str[0] == "(":
parts = str[1:-1].split(":")
lo_atom = int(parts[0])
hi_atom = int(parts[1])
return Slice(lo_atom, hi_atom)
parts = str.split("(")
lo_atom = int(parts[0])
shape = []
start = []
size = 1
for part in parts[1:]:
subs = part.split(":")
begin = int(subs[0])
atoms = int(subs[1][:-1]) - begin
size *= atoms
shape.append(atoms)
start.append(begin)
return Slice(lo_atom, lo_atom + size - 1, tuple(shape), tuple(start))
26 changes: 24 additions & 2 deletions unittests/model_tests/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def test_basic(self):
assert s.hi_atom == 10 # As specified
assert s.as_slice == slice(0, 11) # Slice object supported by arrays
self.assertEqual("(0:10)", str(s))
s2 = Slice.from_string(str(s))
self.assertEqual(s, s2)

def test_check_lo_atom_sanity(self):
# Check for value sanity
Expand Down Expand Up @@ -80,5 +82,25 @@ def test_immutability_as_slice(self):
s.as_slice = slice(2, 10)

def test_2d(self):
s = Slice(0, 10, (3, 3), (0, 6))
self.assertEqual("[(0:3)(6:9)]", str(s))
s = Slice(0, 8, (3, 3), (0, 0))
self.assertEqual("0(0:3)(0:3)", str(s))
s2 = Slice.from_string(str(s))
self.assertEqual(s, s2)

def test_2a(self):
s = Slice(36, 44, (3, 3), (0, 6))
self.assertEqual("36(0:3)(6:9)", str(s))
s2 = Slice.from_string(str(s))
self.assertEqual(s, s2)

def test_2b(self):
s = Slice(9, 17, (3, 3), (3, 0))
self.assertEqual("9(3:6)(0:3)", str(s))
s2 = Slice.from_string(str(s))
self.assertEqual(s, s2)

def test_3b(self):
s = Slice(432, 455, (2, 3, 4), (6, 9, 16))
self.assertEqual("432(6:8)(9:12)(16:20)", str(s))
s2 = Slice.from_string(str(s))
self.assertEqual(s, s2)

0 comments on commit d27f8b4

Please sign in to comment.