Created
February 28, 2019 11:15
-
-
Save tam203/e9b3c2abe1f4ad7b64abd7acad4b1b1e to your computer and use it in GitHub Desktop.
__getitem__ from offsetmap.py for Medium article https://medium.com/p/a394fa48b671/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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