KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > modules > operator


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.modules;
3
4 import org.python.core.*;
5
6 class OperatorFunctions extends PyBuiltinFunctionSet
7 {
8     public OperatorFunctions(String JavaDoc name, int index, int argcount) {
9         super(name, index, argcount, argcount, false, null);
10     }
11
12     public OperatorFunctions(String JavaDoc name, int index, int minargs, int maxargs)
13     {
14         super(name, index, minargs, maxargs, false, null);
15     }
16
17     public PyObject __call__(PyObject arg1) {
18         switch (index) {
19         case 10: return arg1.__abs__();
20         case 11: return arg1.__invert__();
21         case 12: return arg1.__neg__();
22         case 13: return arg1.__not__();
23         case 14: return arg1.__pos__();
24         case 15: return Py.newBoolean(arg1.__nonzero__());
25         case 16: return Py.newBoolean(arg1.isCallable());
26         case 17: return Py.newBoolean(arg1.isMappingType());
27         case 18: return Py.newBoolean(arg1.isNumberType());
28         case 19: return Py.newBoolean(arg1.isSequenceType());
29         case 32: return arg1.__invert__();
30         default:
31             throw argCountError(1);
32         }
33     }
34
35     public PyObject __call__(PyObject arg1, PyObject arg2) {
36         switch (index) {
37         case 0: return arg1._add(arg2);
38         case 1: return arg1._and(arg2);
39         case 2: return arg1._div(arg2);
40         case 3: return arg1._lshift(arg2);
41         case 4: return arg1._mod(arg2);
42         case 5: return arg1._mul(arg2);
43         case 6: return arg1._or(arg2);
44         case 7: return arg1._rshift(arg2);
45         case 8: return arg1._sub(arg2);
46         case 9: return arg1._xor(arg2);
47         case 20: return Py.newBoolean(arg1.__contains__(arg2));
48         case 21:
49             arg1.__delitem__(arg2);
50             return Py.None;
51         case 23: return arg1.__getitem__(arg2);
52         case 27: return arg1._ge(arg2);
53         case 28: return arg1._le(arg2);
54         case 29: return arg1._eq(arg2);
55         case 30: return arg1._floordiv(arg2);
56         case 31: return arg1._gt(arg2);
57         case 33: return arg1._lt(arg2);
58         case 34: return arg1._ne(arg2);
59         case 35: return arg1._truediv(arg2);
60         default:
61             throw argCountError(2);
62         }
63     }
64
65     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
66         switch (index) {
67         case 22: arg1.__delslice__(arg2, arg3); return Py.None;
68         case 24: return arg1.__getslice__(arg2, arg3);
69         case 25: arg1.__setitem__(arg2, arg3); return Py.None;
70         default:
71             throw argCountError(3);
72         }
73     }
74
75     public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3,
76                              PyObject arg4)
77     {
78         switch (index) {
79         case 26:
80             arg1.__setslice__(arg2, arg3, arg4);
81             return Py.None;
82         default:
83             throw argCountError(4);
84         }
85     }
86 }
87
88 public class operator implements ClassDictInit
89 {
90     public static PyString __doc__ = new PyString(
91         "Operator interface.\n"+
92         "\n"+
93         "This module exports a set of functions implemented in C "+
94                 "corresponding\n"+
95         "to the intrinsic operators of Python. For example, "+
96                 "operator.add(x, y)\n"+
97         "is equivalent to the expression x+y. The function names "+
98                 "are those\n"+
99         "used for special class methods; variants without leading "+
100                 "and trailing\n"+
101         "'__' are also provided for convenience.\n"
102     );
103
104     public static void classDictInit(PyObject dict) throws PyIgnoreMethodTag {
105         dict.__setitem__("__add__", new OperatorFunctions("__add__", 0, 2));
106         dict.__setitem__("add", new OperatorFunctions("add", 0, 2));
107         dict.__setitem__("__concat__",
108                          new OperatorFunctions("__concat__", 0, 2));
109         dict.__setitem__("concat", new OperatorFunctions("concat", 0, 2));
110         dict.__setitem__("__and__", new OperatorFunctions("__and__", 1, 2));
111         dict.__setitem__("and_", new OperatorFunctions("and_", 1, 2));
112         dict.__setitem__("__div__", new OperatorFunctions("__div__", 2, 2));
113         dict.__setitem__("div", new OperatorFunctions("div", 2, 2));
114         dict.__setitem__("__lshift__",
115                          new OperatorFunctions("__lshift__", 3, 2));
116         dict.__setitem__("lshift", new OperatorFunctions("lshift", 3, 2));
117         dict.__setitem__("__mod__", new OperatorFunctions("__mod__", 4, 2));
118         dict.__setitem__("mod", new OperatorFunctions("mod", 4, 2));
119         dict.__setitem__("__mul__", new OperatorFunctions("__mul__", 5, 2));
120         dict.__setitem__("mul", new OperatorFunctions("mul", 5, 2));
121         dict.__setitem__("__repeat__",
122                          new OperatorFunctions("__repeat__", 5, 2));
123         dict.__setitem__("repeat", new OperatorFunctions("repeat", 5, 2));
124         dict.__setitem__("__or__", new OperatorFunctions("__or__", 6, 2));
125         dict.__setitem__("or_", new OperatorFunctions("or_", 6, 2));
126         dict.__setitem__("__rshift__",
127                          new OperatorFunctions("__rshift__", 7, 2));
128         dict.__setitem__("rshift", new OperatorFunctions("rshift", 7, 2));
129         dict.__setitem__("__sub__", new OperatorFunctions("__sub__", 8, 2));
130         dict.__setitem__("sub", new OperatorFunctions("sub", 8, 2));
131         dict.__setitem__("__xor__", new OperatorFunctions("__xor__", 9, 2));
132         dict.__setitem__("xor", new OperatorFunctions("xor", 9, 2));
133         dict.__setitem__("__abs__", new OperatorFunctions("__abs__", 10, 1));
134         dict.__setitem__("abs", new OperatorFunctions("abs", 10, 1));
135         dict.__setitem__("__inv__", new OperatorFunctions("__inv__", 11, 1));
136         dict.__setitem__("inv", new OperatorFunctions("inv", 11, 1));
137         dict.__setitem__("__neg__", new OperatorFunctions("__neg__", 12, 1));
138         dict.__setitem__("neg", new OperatorFunctions("neg", 12, 1));
139         dict.__setitem__("__not__", new OperatorFunctions("__not__", 13, 1));
140         dict.__setitem__("not_", new OperatorFunctions("not_", 13, 1));
141         dict.__setitem__("__pos__", new OperatorFunctions("__pos__", 14, 1));
142         dict.__setitem__("pos", new OperatorFunctions("pos", 14, 1));
143         dict.__setitem__("truth", new OperatorFunctions("truth", 15, 1));
144         dict.__setitem__("isCallable",
145                          new OperatorFunctions("isCallable", 16, 1));
146         dict.__setitem__("isMappingType",
147                          new OperatorFunctions("isMappingType", 17, 1));
148         dict.__setitem__("isNumberType",
149                          new OperatorFunctions("isNumberType", 18, 1));
150         dict.__setitem__("isSequenceType",
151                          new OperatorFunctions("isSequenceType", 19, 1));
152         dict.__setitem__("contains",
153                          new OperatorFunctions("contains", 20, 2));
154         dict.__setitem__("__contains__",
155                          new OperatorFunctions("__contains__", 20, 2));
156         dict.__setitem__("sequenceIncludes",
157                          new OperatorFunctions("sequenceIncludes", 20, 2));
158         dict.__setitem__("__delitem__",
159                          new OperatorFunctions("__delitem__", 21, 2));
160         dict.__setitem__("delitem", new OperatorFunctions("delitem", 21, 2));
161         dict.__setitem__("__delslice__",
162                          new OperatorFunctions("__delslice__", 22, 3));
163         dict.__setitem__("delslice",
164                          new OperatorFunctions("delslice", 22, 3));
165         dict.__setitem__("__getitem__",
166                          new OperatorFunctions("__getitem__", 23, 2));
167         dict.__setitem__("getitem", new OperatorFunctions("getitem", 23, 2));
168         dict.__setitem__("__getslice__",
169                          new OperatorFunctions("__getslice__", 24, 3));
170         dict.__setitem__("getslice",
171                          new OperatorFunctions("getslice", 24, 3));
172         dict.__setitem__("__setitem__",
173                          new OperatorFunctions("__setitem__", 25, 3));
174         dict.__setitem__("setitem", new OperatorFunctions("setitem", 25, 3));
175         dict.__setitem__("__setslice__",
176                          new OperatorFunctions("__setslice__", 26, 4));
177         dict.__setitem__("setslice",
178                          new OperatorFunctions("setslice", 26, 4));
179         dict.__setitem__("ge", new OperatorFunctions("ge", 27, 2));
180         dict.__setitem__("__ge__", new OperatorFunctions("__ge__", 27, 2));
181         dict.__setitem__("le", new OperatorFunctions("le", 28, 2));
182         dict.__setitem__("__le__", new OperatorFunctions("__le__", 28, 2));
183         dict.__setitem__("eq", new OperatorFunctions("eq", 29, 2));
184         dict.__setitem__("__eq__", new OperatorFunctions("__eq__", 29, 2));
185         dict.__setitem__("floordiv",
186                         new OperatorFunctions("floordiv", 30, 2));
187         dict.__setitem__("__floordiv__",
188                         new OperatorFunctions("__floordiv__", 30, 2));
189         dict.__setitem__("gt", new OperatorFunctions("gt", 31, 2));
190         dict.__setitem__("__gt__", new OperatorFunctions("__gt__", 31, 2));
191         dict.__setitem__("invert", new OperatorFunctions("invert", 32, 1));
192         dict.__setitem__("__invert__",
193                         new OperatorFunctions("__invert__", 32, 1));
194         dict.__setitem__("lt", new OperatorFunctions("lt", 33, 2));
195         dict.__setitem__("__lt__", new OperatorFunctions("__lt__", 33, 2));
196         dict.__setitem__("ne", new OperatorFunctions("ne", 34, 2));
197         dict.__setitem__("__ne__", new OperatorFunctions("__ne__", 34, 2));
198         dict.__setitem__("truediv", new OperatorFunctions("truediv", 35, 2));
199         dict.__setitem__("__truediv__",
200                         new OperatorFunctions("__truediv__", 35, 2));
201     }
202
203     public static int countOf(PyObject seq, PyObject item) {
204         int count = 0;
205
206         PyObject iter = seq.__iter__();
207         for (PyObject tmp = null; (tmp = iter.__iternext__()) != null; ) {
208             if (item._eq(tmp).__nonzero__())
209                 count++;
210         }
211         return count;
212     }
213
214     public static int indexOf(PyObject seq, PyObject item) {
215         int i = 0;
216         PyObject iter = seq.__iter__();
217         for (PyObject tmp = null; (tmp = iter.__iternext__()) != null; i++) {
218             if (item._eq(tmp).__nonzero__())
219                 return i;
220         }
221         throw Py.ValueError("sequence.index(x): x not in list");
222     }
223 }
224
Popular Tags