我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用operator.indexOf()。
def number_of_args(fn): """Return the number of positional arguments for a function, or None if the number is variable. Looks inside any decorated functions.""" try: if hasattr(fn, '__wrapped__'): return number_of_args(fn.__wrapped__) if any(p.kind == p.VAR_POSITIONAL for p in signature(fn).parameters.values()): return None else: return sum(p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD) for p in signature(fn).parameters.values()) except ValueError: # signatures don't work for built-in operators, so check for a few explicitly UNARY_OPS = [len, op.not_, op.truth, op.abs, op.index, op.inv, op.invert, op.neg, op.pos] BINARY_OPS = [op.lt, op.le, op.gt, op.ge, op.eq, op.ne, op.is_, op.is_not, op.add, op.and_, op.floordiv, op.lshift, op.mod, op.mul, op.or_, op.pow, op.rshift, op.sub, op.truediv, op.xor, op.concat, op.contains, op.countOf, op.delitem, op.getitem, op.indexOf] TERNARY_OPS = [op.setitem] if fn in UNARY_OPS: return 1 elif fn in BINARY_OPS: return 2 elif fn in TERNARY_OPS: return 3 else: raise NotImplementedError("Bult-in operator {} not supported".format(fn))
def merge_practices(*args, **kwargs): """Merge two or more dictionaries, preferring values in increasing order of index in `order`. Treats practices with no `status` as 'na'. """ order = kwargs.pop('order', ['unknown', 'na', 'nudge', 'warn', 'ok']) if len(kwargs) > 0: raise TypeError("merge_practices() got unexpected keyword argument(s) {:s}" "".format(', '.join("'{:s}'".format(kw) for kw in six.iterkeys(kwargs)))) practices = dict() for practice in set(itertools.chain.from_iterable(arg.keys() for arg in args)): practices[practice] = max( (arg.get(practice, {'status': 'unknown'}) for arg in args), key=lambda _practice: operator.indexOf(order, _practice.get('status', 'unknown')) ) return practices
def test_indexOf(self): self.assertRaises(TypeError, operator.indexOf) self.assertRaises(TypeError, operator.indexOf, None, None) self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1) self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
def test_countOf(self): from operator import countOf self.assertEqual(countOf([1,2,2,3,2,5], 2), 3) self.assertEqual(countOf((1,2,2,3,2,5), 2), 3) self.assertEqual(countOf("122325", "2"), 3) self.assertEqual(countOf("122325", "6"), 0) self.assertRaises(TypeError, countOf, 42, 1) self.assertRaises(TypeError, countOf, countOf, countOf) d = {"one": 3, "two": 3, "three": 3, 1j: 2j} for k in d: self.assertEqual(countOf(d, k), 1) self.assertEqual(countOf(d.values(), 3), 3) self.assertEqual(countOf(d.values(), 2j), 1) self.assertEqual(countOf(d.values(), 1j), 0) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n" "b\n") finally: f.close() f = open(TESTFN, "r") try: for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0): f.seek(0, 0) self.assertEqual(countOf(f, letter + "\n"), count) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test iterators with operator.indexOf (PySequence_Index).
def test_indexOf(self): from operator import indexOf self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0) self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1) self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3) self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5) self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0) self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6) self.assertEqual(indexOf("122325", "2"), 1) self.assertEqual(indexOf("122325", "5"), 5) self.assertRaises(ValueError, indexOf, "122325", "6") self.assertRaises(TypeError, indexOf, 42, 1) self.assertRaises(TypeError, indexOf, indexOf, indexOf) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n" "d\n" "e\n") finally: f.close() f = open(TESTFN, "r") try: fiter = iter(f) self.assertEqual(indexOf(fiter, "b\n"), 1) self.assertEqual(indexOf(fiter, "d\n"), 1) self.assertEqual(indexOf(fiter, "e\n"), 0) self.assertRaises(ValueError, indexOf, fiter, "a\n") finally: f.close() try: unlink(TESTFN) except OSError: pass iclass = IteratingSequenceClass(3) for i in range(3): self.assertEqual(indexOf(iclass, i), i) self.assertRaises(ValueError, indexOf, iclass, -1) # Test iterators with file.writelines().
def test_countOf(self): from operator import countOf self.assertEqual(countOf([1,2,2,3,2,5], 2), 3) self.assertEqual(countOf((1,2,2,3,2,5), 2), 3) self.assertEqual(countOf("122325", "2"), 3) self.assertEqual(countOf("122325", "6"), 0) self.assertRaises(TypeError, countOf, 42, 1) self.assertRaises(TypeError, countOf, countOf, countOf) d = {"one": 3, "two": 3, "three": 3, 1j: 2j} for k in d: self.assertEqual(countOf(d, k), 1) self.assertEqual(countOf(d.itervalues(), 3), 3) self.assertEqual(countOf(d.itervalues(), 2j), 1) self.assertEqual(countOf(d.itervalues(), 1j), 0) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n" "b\n") finally: f.close() f = open(TESTFN, "r") try: for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0): f.seek(0, 0) self.assertEqual(countOf(f, letter + "\n"), count) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test iterators with operator.indexOf (PySequence_Index).
def reshape(self, *newShape): if len(newShape)==1 and not type(newShape[0]) in _numberTypes: newShape = tuple(newShape[0]) if not _all2_(newShape, _isNumber): raise TypeError('the parameters to reshape don\'t look like a valid shape') if -1 in newShape: if _prodT(newShape)==0: raise ValueError("-1 as a parameter to reshape is not allowed if one of the other parameters is zero.") newShape = _modifyT(newShape, operator.indexOf(newShape, -1), self.size//-_prodT(newShape)) if _prodT(newShape) != self.size: raise ValueError('the total number of items cannot be changed in a reshape') return garray(self._base, newShape, self)
def _scheduler_remove(value): try: del _squeue[operator.indexOf(_squeue, value)] except ValueError: pass
def _scheduler_contains(value): try: operator.indexOf(_squeue, value) return True except ValueError: return False
def lst_ope(): lst = [1, 2, 3] print operator.indexOf(lst, 2) # 1 lst1 = [1, 2, 3, 2] print operator.countOf(lst1, 2) # 2