Skip to content

Commit

Permalink
in_ and not_in
Browse files Browse the repository at this point in the history
  • Loading branch information
dg-pb committed Jun 12, 2024
1 parent 34e4d32 commit 218f05e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Doc/library/operator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ Operations which work with sequences (some of them with mappings too) include:
Return ``a + b`` for *a* and *b* sequences.


.. function:: in_(a, b)

Return the outcome of the test ``a in b``.


.. function:: not_in(a, b)

Return the outcome of the test ``a not in b``.


.. function:: contains(a, b)
__contains__(a, b)

Expand Down
15 changes: 14 additions & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
This is the pure Python implementation of the module.
"""

__all__ = ['abs', 'add', 'and_', 'attrgetter', 'call', 'concat', 'contains', 'countOf',
__all__ = ['abs', 'add', 'and_', 'attrgetter', 'call', 'concat',
'in_', 'not_in', 'contains', 'countOf',
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
Expand Down Expand Up @@ -150,10 +151,22 @@ def concat(a, b):
raise TypeError(msg)
return a + b


def in_(a, b):
"Same as a in b."
return a in b


def not_in(a, b):
"Same as a not in b."
return a not in b


def contains(a, b):
"Same as b in a (note reversed operands)."
return b in a


def countOf(a, b):
"Return the number of items in a which are, or which equal, b."
count = 0
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,22 @@ def test_rshift(self):
self.assertEqual(operator.rshift(5, 0), 5)
self.assertRaises(ValueError, operator.rshift, 2, -1)

def test_in(self):
operator = self.module
self.assertRaises(TypeError, operator.in_)
self.assertRaises(TypeError, operator.in_, None, None)
self.assertRaises(ZeroDivisionError, operator.in_, 1, BadIterable())
self.assertTrue(operator.in_(2, range(4)))
self.assertFalse(operator.in_(5, range(4)))

def test_not_in(self):
operator = self.module
self.assertRaises(TypeError, operator.not_in)
self.assertRaises(TypeError, operator.not_in, None, None)
self.assertRaises(ZeroDivisionError, operator.not_in, 1, BadIterable())
self.assertFalse(operator.not_in(2, range(4)))
self.assertTrue(operator.not_in(5, range(4)))

def test_contains(self):
operator = self.module
self.assertRaises(TypeError, operator.contains)
Expand Down
40 changes: 40 additions & 0 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,44 @@ _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b)
return PySequence_InPlaceConcat(a, b);
}

/*[clinic input]
_operator.in_ -> bool
a: object
b: object
/
Same as a in b.
[clinic start generated code]*/

static int
_operator_in__impl(PyObject *module, PyObject *a, PyObject *b)
/*[clinic end generated code: output=cd4eca456096fa9a input=8bd20ace23fe9808]*/
{
return PySequence_Contains(b, a);
}

/*[clinic input]
_operator.not_in -> bool
a: object
b: object
/
Same as a not in b.
[clinic start generated code]*/

static int
_operator_not_in_impl(PyObject *module, PyObject *a, PyObject *b)
/*[clinic end generated code: output=93a4db26908835bd input=203499d558a29ee3]*/
{
int result = PySequence_Contains(b, a);
if (result != -1) {
result = 1 - result;
}
return result;
}

/*[clinic input]
_operator.contains -> bool
Expand Down Expand Up @@ -910,6 +948,8 @@ _operator_call(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObje
static struct PyMethodDef operator_methods[] = {

_OPERATOR_TRUTH_METHODDEF
_OPERATOR_IN__METHODDEF
_OPERATOR_NOT_IN_METHODDEF
_OPERATOR_CONTAINS_METHODDEF
_OPERATOR_INDEXOF_METHODDEF
_OPERATOR_COUNTOF_METHODDEF
Expand Down
72 changes: 71 additions & 1 deletion Modules/clinic/_operator.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 218f05e

Please sign in to comment.