Skip to content

Commit

Permalink
minor cleanup and do not error on get if key does not exist (tentative)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlizzo committed Sep 4, 2019
1 parent e3a8a90 commit 1f961f6
Showing 1 changed file with 50 additions and 57 deletions.
107 changes: 50 additions & 57 deletions src/hangar/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,58 +57,49 @@ def __init__(self):

def __getitem__(self, index):
# get an arrayset of the dataset (i.e. a "column" of the dataset?)

if isinstance(index, str):
return self.arraysets[index]
elif not isinstance(index, (tuple, list, set)):
raise TypeError(f'Unknown index: {index} type: {type(index)}')
if len(index) > 2:
raise ValueError(f'index of len > 2 not allowed: {index}')

aIdx, sIdx = index

# Arrayset Parsing
if (aIdx is Ellipsis) or (isinstance(aIdx, slice) and (
not any([aIdx.start, aIdx.stop, aIdx.step]))):
asets = list(self.arraysets.values())
elif isinstance(aIdx, str):
asets = [self.arraysets.get(aIdx)]
elif isinstance(aIdx, (tuple, list, set)):
asets = []
for asetIdx in aIdx:
asets.append(self.arraysets.get(asetIdx))
else:
raise TypeError(f'Unknown arraysets idx: {aIdx} type: {type(aIdx)}')

# Sample Parsing
if isinstance(sIdx, (str, int)):
sIdx = [sIdx]
elif not isinstance(sIdx, (tuple, list, set)):
raise TypeError(
f'Unknown samples index value: {sIdx} of type: {type(sIdx)}')

elif isinstance(index, (tuple, list, set)):
if len(index) > 2:
raise ValueError(f'index of len > 2 not allowed: {index}')

asetsIdx, samplesIdx = index

# all asets via Ellipsis -> ``...``
if asetsIdx is Ellipsis:
asets = list(self.arraysets.values())
# all asets via empty slice -> ``:``
elif isinstance(asetsIdx, slice):
if any([asetsIdx.start, asetsIdx.stop, asetsIdx.step]):
raise ValueError('arrayset index cannot be integer slice object')
asets = list(self.arraysets.values())
# single specified aset name
elif isinstance(asetsIdx, str):
asets = [self.arraysets.get(asetsIdx)]
# multiple specified aset names
elif isinstance(asetsIdx, (tuple, list, set)):
asets = []
for asetIdx in asetsIdx:
try:
asets.append(self.arraysets.get(asetIdx))
except KeyError:
# if same with given name does not exist in aset
asets.append(None)

# output is namedtuple with Fields = aset name(s) specified
ArraysetData = namedtuple('ArraysetData', [aset.name for aset in asets])

# multiple specified sample names
if isinstance(samplesIdx, (tuple, list, set)):
out = []
for sampleName in samplesIdx:
res = []
for aset in asets:
res.append(aset.get(sampleName))
out.append(ArraysetData(*res))
return out
# single specified sample names
elif isinstance(samplesIdx, (str, int)):
res = []
for aset in asets:
res.append(aset.get(samplesIdx))
return ArraysetData(*res)
else:
raise TypeError(
f'Unknown samples slice/index value: {samplesIdx} '
f'of type: {type(samplesIdx)}')
# Data Retrieval
out = []
for sampleName in sIdx:
arrays, asetnames = [], []
for aset in asets:
try:
arrays.append(aset.get(sampleName))
asetnames.append(aset.name)
except KeyError:
pass
out.append(namedtuple('ArraysetData', asetnames)(*arrays))

return out

def __setitem__(self, index, value):

Expand All @@ -119,10 +110,10 @@ def __setitem__(self, index, value):
raise ValueError(f'index of len > 2 not allowed: {index}')

asetsIdx, sampleKey = index
# all asets

# Parse Arraysets
if isinstance(asetsIdx, str):
asets = [self.arraysets.get(asetsIdx)]
# specific aset names
elif isinstance(asetsIdx, (tuple, list, set)):
raise ValueError(
f'Cannot set value to multiple arraysets simulatneously in the '
Expand All @@ -132,18 +123,20 @@ def __setitem__(self, index, value):
f'unknown type of arrayset specifying: {asetsIdx} of type: '
f'{type(asetsIdx)} in index: {index}')

# specific sample names
# Parse sample names
if isinstance(sampleKey, (tuple, list, set)):
raise ValueError(
f'batch adding of samples not allowed: {sampleKey}')
if isinstance(sampleKey, (str, int)):
for aset in asets:
aset[sampleKey] = value
return sampleKey
else:
if not isinstance(sampleKey, (str, int)):
raise TypeError(
f'unknown sample value: {sampleKey} of type: {type(sampleKey)}')

# Set Data
for aset in asets:
aset[sampleKey] = value

return sampleKey


class ReaderCheckout(CheckoutIndexer):
"""Checkout the repository as it exists at a particular branch.
Expand Down

0 comments on commit 1f961f6

Please sign in to comment.