[Python-checkins] python/nondist/sandbox/setobj setobject.c, 1.1,
1.2 test_set.py, 1.1, 1.2
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Wed Nov 12 17:46:48 EST 2003
Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv28486
Modified Files:
setobject.c test_set.py
Log Message:
Add __len__, __contains__, copy, and union.
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** setobject.c 12 Nov 2003 20:06:24 -0000 1.1
--- setobject.c 12 Nov 2003 22:46:45 -0000 1.2
***************
*** 94,97 ****
--- 94,184 ----
}
+ static int
+ set_len(setobject *so)
+ {
+ return PyDict_Size(so->data);
+ }
+
+ static int
+ set_contains(setobject *so, PyObject *key)
+ {
+ return so->data->ob_type->tp_as_sequence->sq_contains(so->data, key);
+ }
+
+ static PyObject *
+ set_copy(setobject *so)
+ {
+ PyObject *data;
+
+ data = PyDict_Copy(so->data);
+ if (data == NULL)
+ return NULL;
+
+ so = (setobject *)PyObject_GC_New(setobject, &set_type);
+ if (so == NULL) {
+ Py_DECREF(data);
+ return NULL;
+ }
+ so->data = data;
+ return (PyObject *)so;
+ }
+
+ PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
+
+ static PyObject *
+ set_union(setobject *so, PyObject *other)
+ {
+ setobject *result;
+ PyObject *item, *data, *it;
+
+ result = (setobject *)set_copy(so);
+ it = PyObject_GetIter(other);
+ if (it == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ data = result->data;
+ while (1) {
+ item = PyIter_Next(it);
+ if (item == NULL)
+ break; // XXX add error checking
+ if (PyDict_SetItem(data, item, Py_None) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(result);
+ PyErr_SetString(PyExc_ValueError,
+ "all set entries must be immutable");
+ return NULL;
+ }
+ Py_DECREF(item);
+ }
+ return (PyObject *)result;
+ }
+
+ PyDoc_STRVAR(union_doc,
+ """Return the union of two sets as a new set.\n\
+ \n\
+ (i.e. all elements that are in either set.)\n");
+
+ static PySequenceMethods set_as_sequence = {
+ (inquiry)set_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)set_contains, /* sq_contains */
+ };
+
+ static PyMethodDef set_methods[] = {
+ {"copy", (PyCFunction)set_copy, METH_NOARGS,
+ copy_doc},
+ {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
+ copy_doc},
+ {"union", (PyCFunction)set_union, METH_O,
+ union_doc},
+ {NULL, NULL} /* sentinel */
+ };
+
PyDoc_STRVAR(set_doc,
"set(iterable) --> set object\n\
***************
*** 102,106 ****
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
! "set.set", /* tp_name */
sizeof(setobject), /* tp_basicsize */
0, /* tp_itemsize */
--- 189,193 ----
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
! "set", /* tp_name */
sizeof(setobject), /* tp_basicsize */
0, /* tp_itemsize */
***************
*** 113,117 ****
0, /* tp_repr */
0, /* tp_as_number */
! 0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
--- 200,204 ----
0, /* tp_repr */
0, /* tp_as_number */
! &set_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
***************
*** 130,134 ****
(getiterfunc)set_iter, /* tp_iter */
0, /* tp_iternext */
! 0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
--- 217,221 ----
(getiterfunc)set_iter, /* tp_iter */
0, /* tp_iternext */
! set_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/test_set.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** test_set.py 12 Nov 2003 20:06:24 -0000 1.1
--- test_set.py 12 Nov 2003 22:46:45 -0000 1.2
***************
*** 5,12 ****
class TestBasicOps(unittest.TestCase):
def test_uniquification(self):
! s = set('abracadabra')
! result = list.sorted(s)
! self.assertEqual(result, list('abcdr'))
--- 5,31 ----
class TestBasicOps(unittest.TestCase):
+ def setUp(self):
+ word = 'abracadabra'
+ self.otherword = 'alacazam'
+ self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ self.s = set(word)
+ self.d = dict.fromkeys(word)
+
def test_uniquification(self):
! actual = list.sorted(self.s)
! expected = list.sorted(self.d)
! self.assertEqual(actual, expected)
!
! def test_len(self):
! self.assertEqual(len(self.s), len(self.d))
!
! def test_contains(self):
! for c in self.letters:
! self.assertEqual(c in self.s, c in self.d)
!
! def test_union(self):
! u = self.s.union(self.otherword)
! for c in self.letters:
! self.assertEqual(c in u, c in self.d or c in self.otherword)
More information about the Python-checkins
mailing list