Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from reducers import Content
- from reducers import random_length
- import random
- class ListOffsetArray(Content):
- def __init__(self, offsets, content):
- assert isinstance(offsets, list)
- assert isinstance(content, Content)
- assert len(offsets) != 0
- for i in range(len(offsets) - 1):
- start = offsets[i]
- stop = offsets[i + 1]
- assert isinstance(start, int)
- assert isinstance(stop, int)
- if start != stop:
- assert start < stop # i.e. start <= stop
- assert start >= 0
- assert stop <= len(content)
- self.offsets = offsets
- self.content = content
- @staticmethod
- def random(minlen, choices):
- counts = [random_length(minlen) for i in range(random_length(minlen))]
- offsets = [random_length()]
- for x in counts:
- offsets.append(offsets[-1] + x)
- return ListOffsetArray(offsets, random.choice(choices).random(offsets[-1], choices))
- def __len__(self):
- return len(self.offsets) - 1
- def __getitem__(self, where):
- if isinstance(where, int):
- assert 0 <= where < len(self)
- return self.content[self.offsets[where]:self.offsets[where + 1]]
- elif isinstance(where, slice) and where.step is None:
- offsets = self.offsets[where.start : where.stop + 1]
- if len(offsets) == 0:
- offsets = [0]
- return ListOffsetArray(offsets, self.content)
- elif isinstance(where, str):
- return ListOffsetArray(self.offsets, self.content[where])
- else:
- raise AssertionError(where)
- def __repr__(self):
- return "ListOffsetArray(" + repr(self.offsets) + ", " + repr(self.content) + ")"
- def xml(self, indent="", pre="", post=""):
- out = indent + pre + "<ListOffsetArray>\n"
- out += indent + " <offsets>" + " ".join(str(x) for x in self.offsets)
- out += "</offsets>\n"
- out += self.content.xml(indent + " ", "<content>", "</content>\n")
- out += indent + "</ListOffsetArray>" + post
- return out
- if __name__ == "__main__":
- ListOffsetArray.random(4, [0, 1, 5, 4, 3, 2])
- Traceback (most recent call last):
- File "ListOffsetArraygen.py", line 60, in <module>
- ListOffsetArray.random(4, [0, 1, 5, 4, 3, 2])
- File "ListOffsetArraygen.py", line 29, in random
- return ListOffsetArray(offsets, random.choice(choices).random(offsets[-1], choices))
- AttributeError: 'int' object has no attribute 'random'
Add Comment
Please, Sign In to add comment