1 /* Copyright 2000 by Jeffrey Chang. All rights reserved.
2 * This code is part of the Biopython distribution and governed by its
3 * license. Please see the LICENSE file that should have been included
4 * as part of this package.
16 /************************************** Exported Functions ***********/
18 static char clistfns_count__doc__[] =
19 "count(items) -> dict of counts of each item\n\
21 Count the number of times each item appears in a list of data.\n\
25 static PyObject *clistfns_count(PyObject *self, PyObject *args)
28 PyObject *items, *counts;
29 PyObject *item, *count, *newcount;
32 if(!PyArg_ParseTuple(args, "O", &items))
34 if(!PySequence_Check(items)) {
35 PyErr_SetString(PyExc_TypeError, "expected sequence type");
39 if(!(counts = PyDict_New()))
42 /* Go through the loop, counting how often each item appears. */
45 if(!(item = PySequence_GetItem(items, i))) {
46 PyErr_Clear(); /* clear the exception set by PySequence_GetItem */
47 break; /* no more numbers */
50 if(!(count = PyDict_GetItem(counts, item))) {
51 newcount = PyInt_FromLong(1); /* New item, set count to 1 */
54 current = PyInt_AsLong(count);
55 newcount = PyInt_FromLong(current+1);
58 PyDict_SetItem(counts, item, newcount);
71 static char clistfns_contents__doc__[] =
72 "contents(items) -> dict of item -> percentage\n\
74 Summarize the contents of the list in terms of the percentages of each\n\
75 item. For example, if an item appears 3 times in a list with 10 items,\n\
76 it is in 0.3 of the list\n\
80 static PyObject *clistfns_contents(PyObject *self, PyObject *args)
83 PyObject *items, *counts, *percentages;
84 PyObject *countitems, *countitem;
85 PyObject *key, *count, *perc;
89 if(!PyArg_ParseTuple(args, "O", &items))
91 if(!PySequence_Check(items)) {
92 PyErr_SetString(PyExc_TypeError, "expected mapping type");
95 if((total = PySequence_Length(items)) == -1) {
96 PyErr_SetString(PyExc_ValueError, "I couldn't get length of item.");
100 counts = clistfns_count(self, args);
101 if(!counts || PyErr_Occurred())
104 if(!(percentages = PyDict_New())) {
109 /* Loop through every element in counts, calculating the probabilities. */
110 if(!(countitems = PyMapping_Items(counts))) {
112 Py_DECREF(percentages);
116 /* Go through the loop, counting how often each item appears. */
119 if(!(countitem = PyList_GetItem(countitems, i))) {
120 PyErr_Clear(); /* clear the exception set by PyList_GetItem */
121 break; /* no more numbers */
123 key = PyTuple_GetItem(countitem, 0);
124 count = PyTuple_GetItem(countitem, 1);
125 c = PyInt_AsLong(count);
126 perc = PyFloat_FromDouble((double)c / total);
127 PyDict_SetItem(percentages, key, perc);
129 if(PyErr_Occurred()) /* PyDict_SetItem failed */
133 if(PyErr_Occurred()) {
134 Py_DECREF(percentages);
137 Py_DECREF(countitems);
144 /************************************** Module definition stuff ******/
146 static PyMethodDef clistfnsMethods[] = {
147 {"count", clistfns_count, METH_VARARGS, clistfns_count__doc__},
148 {"contents", clistfns_contents, METH_VARARGS, clistfns_contents__doc__},
152 static char clistfns__doc__[] =
153 "This provides helper functions for the listfns module.\n\
154 You should never import this module on its own.\n\
158 void initclistfns(void)
160 (void) Py_InitModule3("clistfns", clistfnsMethods, clistfns__doc__);