Skip to content

Instantly share code, notes, and snippets.

@tam203
Created February 28, 2019 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tam203/e9b3c2abe1f4ad7b64abd7acad4b1b1e to your computer and use it in GitHub Desktop.
Save tam203/e9b3c2abe1f4ad7b64abd7acad4b1b1e to your computer and use it in GitHub Desktop.
__getitem__ from offsetmap.py for Medium article https://medium.com/p/a394fa48b671/
def __getitem__(self, path):
origpath = path
if '/' in path:
prefix, item = path.rsplit('/',1)
else:
prefix = ''
item = path
if self._chunk_re.match(item):
offset = self._get_offset(prefix)
if offset:
# Apply offset
chunks = [int(i) for i in item.split('.')]
ochunks = [chunk + offset[i] for i,chunk in enumerate(chunks)]
item = '.'.join(str(v) for v in ochunks)
path = prefix + '/' + item if prefix else item
return super().__getitem__(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment