def test_getslice(self): a = range(10) self.failUnlessRaises(TypeError, operator.getslice) self.failUnlessRaises(TypeError, operator.getslice, a, None, None) self.failUnless(operator.getslice(a, 4, 6) == [4, 5]) b = operator.getslice(a, 0, test_support.MAX_Py_ssize_t) self.assert_(b == a)
def _get_subfields(self, data): """Private function to extract subfields. Returns a dictionary (dict) containing the subfield identifier (key) and the subfield's data (value). """ self.order = [] # parse subfields # This can be memoized in the future for optimization purposes. subfields = dict() if not data.startswith(self.delimiter): # single value == single anonymous subfield data = self.delimiter + " " + data pairs = [(i[0].lower().strip(), i[1:])\ for i in data.split(self.delimiter) if i] for key, values in groupby(pairs, lambda x: getslice(x, 0, 1)): subkey = key[0] # preserve order of subfields self.order.append(subkey) value = list(imap(lambda x: getslice(x,1,2)[0], values)) if len(value)==1: # single values are not wrapped in lists value = value[0] subfields[subkey] = value # support ^* == first subfield subfields['*'] = subfields[self.order[0]] return subfields
def test_getslice(self): a = list(range(10)) self.assertRaises(TypeError, operator.getslice) self.assertRaises(TypeError, operator.getslice, a, None, None) self.assertTrue(operator.getslice(a, 4, 6) == [4, 5]) b = operator.getslice(a, 0, support.MAX_Py_ssize_t) self.assertTrue(b == a)
def __getitem__(self, i): if type(i) == types.SliceType: return self.clone( operator.getslice(self._value, i.start, i.stop) ) else: return self._value[i]
def __init__(self, payload, nextLayer): self.type = operator.getslice(payload, *TCodeData.TypeRange) self.payload = payload self.nextLayer = nextLayer if self.type == TCodeData.RelayType: self.parseRelayType() elif self.type == TCodeData.TerminalType: self.parseTerminalType()
def assemble_record(pairs): encoding = safe_encoding(self.mst) for key, values in groupby(pairs, lambda x: getslice(x, 0, 1)): key = key[0] fields = [MasterField(tag, data, config=self.config) for tag, data in values] if len(fields) == 1: # single values are not wrapped in lists new_field = fields[0] else: new_field = MasterContainerField(key, sequence=fields, config=self.config) dict.__setitem__(self, key, new_field)
def __getitem__(self, key): if isinstance(key, slice): if isinstance(self._wrapped, (list, tuple)): return self._wrapped[key] start, stop = key.start, key.stop if start is None: start = 0 if start < 0: start += len(self._wrapped) if stop is None: stop = sys.maxint if stop < 0: stop += len(self._wrapped) return operator.getslice(self._wrapped, start, stop) return self._wrapped[key]
def __getitem__(self, key): if isinstance(key, slice) and hasattr(operator, 'getslice'): # Only on Python 2 # XXX: This is probably not proxying correctly, but the existing # tests pass with this behaviour return operator.getslice(self._obj, key.start if key.start is not None else 0, key.stop if key.stop is not None else sys.maxint) aq_self = self._obj try: getter = type(aq_self).__getitem__ except AttributeError: raise AttributeError("__getitem__") # doctests care about the name else: return getter(self, key)
def __getitem__ (self,key): if isinstance(key,tuple): if len(key) != 1: raise TypeError("Index must be one-dimensional") key, = key if isinstance(key,slice): try: val = operator.getitem(self._datap,key) except TypeError: if key.step: raise TypeError("Key cannot have a step size other than 1") val = operator.getslice(self._datap,key.start,key.stop) value = (self.dtype * len(val))() value[:] = val return array(value) else: return self._backcast(self._datap[key])
def __getslice__(self, *args, **kw): from operator import getslice return getslice(self.val, *args, **kw)
def getslice(a, b, c): return operator.getslice(a, b, c)
def test_getslice(self): a = range(10) self.failUnlessRaises(TypeError, operator.getslice) self.failUnlessRaises(TypeError, operator.getslice, a, None, None) self.failUnless(operator.getslice(a, 4, 6) == [4, 5])
def __getitem__(self, idx): if type(idx) == SliceType: return apply(self.__class__, (self.__baseTag,) + getslice(self.__superTags, idx.start, idx.stop)) return self.__superTags[idx]
def __getitem__(self, idx): if type(idx) == SliceType: return apply(self.__class__, (self.__baseTag, ) + getslice(self.__superTags, idx.start, idx.stop)) return self.__superTags[idx]
print "16 >> 1 = operator.rshift(16, 1) = ", operator.rshift(16, 1) # 連續型態值乘法運算(tuple或list) print "'-' * 10 = operator.repeat('-', 10) = ", operator.repeat('-', 10) # 以切片指派值 seq = [1, 2, 3] seq[2:3] = [4] print "seq[2:3] = [4] = operator.setslice(seq,2,3,[4]) = ", seq # 以切片刪除值 operator.delslice(seq, 1, 2) print "del seq[1:2] = operator.delslice(seq,1,2) = ", seq # 取得切片值 print "seq[1:2] = operator.getslice(seq,1,2) = ", operator.getslice(seq, 1, 2) # 餘數運算 print "10 % 3 = operator.mod(10, 3) = ", operator.mod(10, 3) # 減法運算 print "4 - 2 = operator.sub(4, 2) = ", operator.sub(4, 2) # 真值運算(True或False) print "1 == True, 0 == False = operator.truth(1), operator.truth(0) = ", operator.truth( 1), operator.truth(0) # 比較運算 print "1 < 11 = operator.lt(1,11) = ", operator.lt(1, 11) # 比較運算
def __getitem__(self, i): if type(i) == types.SliceType: return self.clone(operator.getslice(self._value, i.start, i.stop)) else: return self._value[i]
import operator