我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用operator.countOf()。
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 test_countOf(self): self.assertRaises(TypeError, operator.countOf) self.assertRaises(TypeError, operator.countOf, None, None) self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1) self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)
def test_in_and_not_in(self): for sc5 in IteratingSequenceClass(5), SequenceClass(5): for i in range(5): self.assertIn(i, sc5) for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: self.assertNotIn(i, sc5) self.assertRaises(TypeError, lambda: 3 in 12) self.assertRaises(TypeError, lambda: 3 not in map) d = {"one": 1, "two": 2, "three": 3, 1j: 2j} for k in d: self.assertIn(k, d) self.assertNotIn(k, d.values()) for v in d.values(): self.assertIn(v, d.values()) self.assertNotIn(v, d) for k, v in d.items(): self.assertIn((k, v), d.items()) self.assertNotIn((v, k), d.items()) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n") finally: f.close() f = open(TESTFN, "r") try: for chunk in "abc": f.seek(0, 0) self.assertNotIn(chunk, f) f.seek(0, 0) self.assertIn((chunk + "\n"), f) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test iterators with operator.countOf (PySequence_Count).
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_in_and_not_in(self): for sc5 in IteratingSequenceClass(5), SequenceClass(5): for i in range(5): self.assertIn(i, sc5) for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: self.assertNotIn(i, sc5) self.assertRaises(TypeError, lambda: 3 in 12) self.assertRaises(TypeError, lambda: 3 not in map) d = {"one": 1, "two": 2, "three": 3, 1j: 2j} for k in d: self.assertIn(k, d) self.assertNotIn(k, d.itervalues()) for v in d.values(): self.assertIn(v, d.itervalues()) self.assertNotIn(v, d) for k, v in d.iteritems(): self.assertIn((k, v), d.iteritems()) self.assertNotIn((v, k), d.iteritems()) f = open(TESTFN, "w") try: f.write("a\n" "b\n" "c\n") finally: f.close() f = open(TESTFN, "r") try: for chunk in "abc": f.seek(0, 0) self.assertNotIn(chunk, f) f.seek(0, 0) self.assertIn((chunk + "\n"), f) finally: f.close() try: unlink(TESTFN) except OSError: pass # Test iterators with operator.countOf (PySequence_Count).
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 lst_ope(): lst = [1, 2, 3] print operator.indexOf(lst, 2) # 1 lst1 = [1, 2, 3, 2] print operator.countOf(lst1, 2) # 2