Skip to content

Instantly share code, notes, and snippets.

@cdunklau
Last active August 29, 2015 13:55
Show Gist options
  • Save cdunklau/90e27ffbfc30d497dceb to your computer and use it in GitHub Desktop.
Save cdunklau/90e27ffbfc30d497dceb to your computer and use it in GitHub Desktop.
class IndexProxy(object):
"""
Proxies access to a multidimensional sequence.
When the constructor is provided with a sequence of dimension N,
item access with iterables of length <= N will retrieve the subsequence
or item at the depth corresponding to the distance. For example:
ip = IndexProxy(multiseq)
ip[1,4] == multiseq[1][4]
ip[1,5,23] == multiseq[1][5][23]
"""
def __init__(self, multisequence):
self.store = multisequence
def __getitem__(self, indicies):
item = self.store
for i in indicies:
item = item[i]
return item
multilist = [
[['000', '001'],
['010', '011']],
[['100', '101'],
['110', '111']],
]
ip = IndexProxy(multilist)
assert ip[0,1,0] == '010'
assert ip[1,1,1] == '111'
# similar thing as a function. This is probably better
def sequential_access(multiseq, *indicies):
item = multiseq
for i in indicies:
item = item[i]
return item
assert sequential_access(multilist, 0, 1, 0) == '010'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment