001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.modules;
003:
004: import org.python.core.*;
005:
006: class OperatorFunctions extends PyBuiltinFunctionSet {
007: public OperatorFunctions(String name, int index, int argcount) {
008: this (name, index, argcount, argcount);
009: }
010:
011: public OperatorFunctions(String name, int index, int minargs,
012: int maxargs) {
013: super (name, index, minargs, maxargs);
014: }
015:
016: public PyObject __call__(PyObject arg1) {
017: switch (index) {
018: case 10:
019: return arg1.__abs__();
020: case 11:
021: return arg1.__invert__();
022: case 12:
023: return arg1.__neg__();
024: case 13:
025: return arg1.__not__();
026: case 14:
027: return arg1.__pos__();
028: case 15:
029: return Py.newBoolean(arg1.__nonzero__());
030: case 16:
031: return Py.newBoolean(arg1.isCallable());
032: case 17:
033: return Py.newBoolean(arg1.isMappingType());
034: case 18:
035: return Py.newBoolean(arg1.isNumberType());
036: case 19:
037: return Py.newBoolean(arg1.isSequenceType());
038: case 32:
039: return arg1.__invert__();
040: default:
041: throw info.unexpectedCall(1, false);
042: }
043: }
044:
045: public PyObject __call__(PyObject arg1, PyObject arg2) {
046: switch (index) {
047: case 0:
048: return arg1._add(arg2);
049: case 1:
050: return arg1._and(arg2);
051: case 2:
052: return arg1._div(arg2);
053: case 3:
054: return arg1._lshift(arg2);
055: case 4:
056: return arg1._mod(arg2);
057: case 5:
058: return arg1._mul(arg2);
059: case 6:
060: return arg1._or(arg2);
061: case 7:
062: return arg1._rshift(arg2);
063: case 8:
064: return arg1._sub(arg2);
065: case 9:
066: return arg1._xor(arg2);
067: case 20:
068: return Py.newBoolean(arg1.__contains__(arg2));
069: case 21:
070: arg1.__delitem__(arg2);
071: return Py.None;
072: case 23:
073: return arg1.__getitem__(arg2);
074: case 27:
075: return arg1._ge(arg2);
076: case 28:
077: return arg1._le(arg2);
078: case 29:
079: return arg1._eq(arg2);
080: case 30:
081: return arg1._floordiv(arg2);
082: case 31:
083: return arg1._gt(arg2);
084: case 33:
085: return arg1._lt(arg2);
086: case 34:
087: return arg1._ne(arg2);
088: case 35:
089: return arg1._truediv(arg2);
090: default:
091: throw info.unexpectedCall(2, false);
092: }
093: }
094:
095: public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
096: switch (index) {
097: case 22:
098: arg1.__delslice__(arg2, arg3);
099: return Py.None;
100: case 24:
101: return arg1.__getslice__(arg2, arg3);
102: case 25:
103: arg1.__setitem__(arg2, arg3);
104: return Py.None;
105: default:
106: throw info.unexpectedCall(3, false);
107: }
108: }
109:
110: public PyObject __call__(PyObject arg1, PyObject arg2,
111: PyObject arg3, PyObject arg4) {
112: switch (index) {
113: case 26:
114: arg1.__setslice__(arg2, arg3, arg4);
115: return Py.None;
116: default:
117: throw info.unexpectedCall(4, false);
118: }
119: }
120: }
121:
122: public class operator implements ClassDictInit {
123: public static PyString __doc__ = new PyString(
124: "Operator interface.\n"
125: + "\n"
126: + "This module exports a set of functions implemented in C "
127: + "corresponding\n"
128: + "to the intrinsic operators of Python. For example, "
129: + "operator.add(x, y)\n"
130: + "is equivalent to the expression x+y. The function names "
131: + "are those\n"
132: + "used for special class methods; variants without leading "
133: + "and trailing\n"
134: + "'__' are also provided for convenience.\n");
135:
136: public static void classDictInit(PyObject dict)
137: throws PyIgnoreMethodTag {
138: dict.__setitem__("__add__", new OperatorFunctions("__add__", 0,
139: 2));
140: dict.__setitem__("add", new OperatorFunctions("add", 0, 2));
141: dict.__setitem__("__concat__", new OperatorFunctions(
142: "__concat__", 0, 2));
143: dict.__setitem__("concat",
144: new OperatorFunctions("concat", 0, 2));
145: dict.__setitem__("__and__", new OperatorFunctions("__and__", 1,
146: 2));
147: dict.__setitem__("and_", new OperatorFunctions("and_", 1, 2));
148: dict.__setitem__("__div__", new OperatorFunctions("__div__", 2,
149: 2));
150: dict.__setitem__("div", new OperatorFunctions("div", 2, 2));
151: dict.__setitem__("__lshift__", new OperatorFunctions(
152: "__lshift__", 3, 2));
153: dict.__setitem__("lshift",
154: new OperatorFunctions("lshift", 3, 2));
155: dict.__setitem__("__mod__", new OperatorFunctions("__mod__", 4,
156: 2));
157: dict.__setitem__("mod", new OperatorFunctions("mod", 4, 2));
158: dict.__setitem__("__mul__", new OperatorFunctions("__mul__", 5,
159: 2));
160: dict.__setitem__("mul", new OperatorFunctions("mul", 5, 2));
161: dict.__setitem__("__repeat__", new OperatorFunctions(
162: "__repeat__", 5, 2));
163: dict.__setitem__("repeat",
164: new OperatorFunctions("repeat", 5, 2));
165: dict.__setitem__("__or__",
166: new OperatorFunctions("__or__", 6, 2));
167: dict.__setitem__("or_", new OperatorFunctions("or_", 6, 2));
168: dict.__setitem__("__rshift__", new OperatorFunctions(
169: "__rshift__", 7, 2));
170: dict.__setitem__("rshift",
171: new OperatorFunctions("rshift", 7, 2));
172: dict.__setitem__("__sub__", new OperatorFunctions("__sub__", 8,
173: 2));
174: dict.__setitem__("sub", new OperatorFunctions("sub", 8, 2));
175: dict.__setitem__("__xor__", new OperatorFunctions("__xor__", 9,
176: 2));
177: dict.__setitem__("xor", new OperatorFunctions("xor", 9, 2));
178: dict.__setitem__("__abs__", new OperatorFunctions("__abs__",
179: 10, 1));
180: dict.__setitem__("abs", new OperatorFunctions("abs", 10, 1));
181: dict.__setitem__("__inv__", new OperatorFunctions("__inv__",
182: 11, 1));
183: dict.__setitem__("inv", new OperatorFunctions("inv", 11, 1));
184: dict.__setitem__("__neg__", new OperatorFunctions("__neg__",
185: 12, 1));
186: dict.__setitem__("neg", new OperatorFunctions("neg", 12, 1));
187: dict.__setitem__("__not__", new OperatorFunctions("__not__",
188: 13, 1));
189: dict.__setitem__("not_", new OperatorFunctions("not_", 13, 1));
190: dict.__setitem__("__pos__", new OperatorFunctions("__pos__",
191: 14, 1));
192: dict.__setitem__("pos", new OperatorFunctions("pos", 14, 1));
193: dict
194: .__setitem__("truth", new OperatorFunctions("truth",
195: 15, 1));
196: dict.__setitem__("isCallable", new OperatorFunctions(
197: "isCallable", 16, 1));
198: dict.__setitem__("isMappingType", new OperatorFunctions(
199: "isMappingType", 17, 1));
200: dict.__setitem__("isNumberType", new OperatorFunctions(
201: "isNumberType", 18, 1));
202: dict.__setitem__("isSequenceType", new OperatorFunctions(
203: "isSequenceType", 19, 1));
204: dict.__setitem__("contains", new OperatorFunctions("contains",
205: 20, 2));
206: dict.__setitem__("__contains__", new OperatorFunctions(
207: "__contains__", 20, 2));
208: dict.__setitem__("sequenceIncludes", new OperatorFunctions(
209: "sequenceIncludes", 20, 2));
210: dict.__setitem__("__delitem__", new OperatorFunctions(
211: "__delitem__", 21, 2));
212: dict.__setitem__("delitem", new OperatorFunctions("delitem",
213: 21, 2));
214: dict.__setitem__("__delslice__", new OperatorFunctions(
215: "__delslice__", 22, 3));
216: dict.__setitem__("delslice", new OperatorFunctions("delslice",
217: 22, 3));
218: dict.__setitem__("__getitem__", new OperatorFunctions(
219: "__getitem__", 23, 2));
220: dict.__setitem__("getitem", new OperatorFunctions("getitem",
221: 23, 2));
222: dict.__setitem__("__getslice__", new OperatorFunctions(
223: "__getslice__", 24, 3));
224: dict.__setitem__("getslice", new OperatorFunctions("getslice",
225: 24, 3));
226: dict.__setitem__("__setitem__", new OperatorFunctions(
227: "__setitem__", 25, 3));
228: dict.__setitem__("setitem", new OperatorFunctions("setitem",
229: 25, 3));
230: dict.__setitem__("__setslice__", new OperatorFunctions(
231: "__setslice__", 26, 4));
232: dict.__setitem__("setslice", new OperatorFunctions("setslice",
233: 26, 4));
234: dict.__setitem__("ge", new OperatorFunctions("ge", 27, 2));
235: dict.__setitem__("__ge__", new OperatorFunctions("__ge__", 27,
236: 2));
237: dict.__setitem__("le", new OperatorFunctions("le", 28, 2));
238: dict.__setitem__("__le__", new OperatorFunctions("__le__", 28,
239: 2));
240: dict.__setitem__("eq", new OperatorFunctions("eq", 29, 2));
241: dict.__setitem__("__eq__", new OperatorFunctions("__eq__", 29,
242: 2));
243: dict.__setitem__("floordiv", new OperatorFunctions("floordiv",
244: 30, 2));
245: dict.__setitem__("__floordiv__", new OperatorFunctions(
246: "__floordiv__", 30, 2));
247: dict.__setitem__("gt", new OperatorFunctions("gt", 31, 2));
248: dict.__setitem__("__gt__", new OperatorFunctions("__gt__", 31,
249: 2));
250: dict.__setitem__("invert", new OperatorFunctions("invert", 32,
251: 1));
252: dict.__setitem__("__invert__", new OperatorFunctions(
253: "__invert__", 32, 1));
254: dict.__setitem__("lt", new OperatorFunctions("lt", 33, 2));
255: dict.__setitem__("__lt__", new OperatorFunctions("__lt__", 33,
256: 2));
257: dict.__setitem__("ne", new OperatorFunctions("ne", 34, 2));
258: dict.__setitem__("__ne__", new OperatorFunctions("__ne__", 34,
259: 2));
260: dict.__setitem__("truediv", new OperatorFunctions("truediv",
261: 35, 2));
262: dict.__setitem__("__truediv__", new OperatorFunctions(
263: "__truediv__", 35, 2));
264: }
265:
266: public static int countOf(PyObject seq, PyObject item) {
267: int count = 0;
268:
269: PyObject iter = seq.__iter__();
270: for (PyObject tmp = null; (tmp = iter.__iternext__()) != null;) {
271: if (item._eq(tmp).__nonzero__())
272: count++;
273: }
274: return count;
275: }
276:
277: public static int indexOf(PyObject seq, PyObject item) {
278: int i = 0;
279: PyObject iter = seq.__iter__();
280: for (PyObject tmp = null; (tmp = iter.__iternext__()) != null; i++) {
281: if (item._eq(tmp).__nonzero__())
282: return i;
283: }
284: throw Py.ValueError("sequence.index(x): x not in list");
285: }
286: }
|