def __getitem__(self, key): if np.isscalar(key) and resolve_scalar_dtype(key) == 'int64': orig_key = key if key < 0: # Interpret negative key as offset from end of array key += self.size if (key >= 0 and key < self.size): cmd = "segmentedIndex" args = " {} {} {} {} {}".format('intIndex', self.objtype, self.offsets.name, self.bytes.name, key) repMsg = generic_msg(cmd=cmd, args=args) _, value = repMsg.split(maxsplit=1) return parse_single_value(value) else: raise IndexError("[int] {} is out of bounds with size {}".\ format(orig_key,self.size)) elif isinstance(key, slice): (start, stop, stride) = key.indices(self.size) self.logger.debug('start: {}; stop: {}; stride: {}'.format( start, stop, stride)) cmd = "segmentedIndex" args = " {} {} {} {} {} {} {}".format('sliceIndex', self.objtype, self.offsets.name, self.bytes.name, start, stop, stride) repMsg = generic_msg(cmd=cmd, args=args) offsets, values = repMsg.split('+') return Strings(offsets, values) elif isinstance(key, pdarray): kind, _ = translate_np_dtype(key.dtype) if kind not in ("bool", "int"): raise TypeError("unsupported pdarray index type {}".format( key.dtype)) if kind == "bool" and self.size != key.size: raise ValueError("size mismatch {} {}".format( self.size, key.size)) cmd = "segmentedIndex" args = "{} {} {} {} {}".format('pdarrayIndex', self.objtype, self.offsets.name, self.bytes.name, key.name) repMsg = generic_msg(cmd, args) offsets, values = repMsg.split('+') return Strings(offsets, values) else: raise TypeError("unsupported pdarray index type {}".format( key.__class__.__name__))
def __getitem__(self, key): if np.isscalar(key) and resolve_scalar_dtype(key) == 'int64': if (key >= 0 and key < self.size): msg = "segmentedIndex {} {} {} {} {}".format( 'intIndex', self.objtype, self.offsets.name, self.bytes.name, key) repMsg = generic_msg(msg) fields = repMsg.split() # value = fields[2] return parse_single_value(' '.join(fields[1:])) else: raise IndexError( "[int] {} is out of bounds with size {}".format( key, self.size)) elif isinstance(key, slice): (start, stop, stride) = key.indices(self.size) if verbose: print(start, stop, stride) msg = "segmentedIndex {} {} {} {} {} {} {}".format( 'sliceIndex', self.objtype, self.offsets.name, self.bytes.name, start, stop, stride) repMsg = generic_msg(msg) offsets, values = repMsg.split('+') return Strings(offsets, values) elif isinstance(key, pdarray): kind, itemsize = translate_np_dtype(key.dtype) if kind not in ("bool", "int"): raise TypeError("unsupported pdarray index type {}".format( key.dtype)) if kind == "bool" and self.size != key.size: raise ValueError("size mismatch {} {}".format( self.size, key.size)) msg = "segmentedIndex {} {} {} {} {}".format( 'pdarrayIndex', self.objtype, self.offsets.name, self.bytes.name, key.name) repMsg = generic_msg(msg) offsets, values = repMsg.split('+') return Strings(offsets, values) else: raise TypeError("unsupported pdarray index type {}".format( type(key)))