1 | LIB "tst.lib"; |
---|
2 | tst_init(); |
---|
3 | |
---|
4 | // Assignment of string literals and integers |
---|
5 | pyobject pystr = "a string literal"; |
---|
6 | pyobject pyone = 1; |
---|
7 | |
---|
8 | |
---|
9 | pystr; |
---|
10 | pyone; |
---|
11 | |
---|
12 | |
---|
13 | // Checking python errors |
---|
14 | pystr + pyone; |
---|
15 | pystr(); |
---|
16 | pystr(1, pyone); |
---|
17 | |
---|
18 | // Compute something in python |
---|
19 | def result = python_eval("4+5"); |
---|
20 | result; |
---|
21 | |
---|
22 | int(result); |
---|
23 | |
---|
24 | // Execute python commands from Singular |
---|
25 | python_run("def myprint(*args): print args"); |
---|
26 | |
---|
27 | myprint("Seventeen: ", 17); |
---|
28 | myprint(); |
---|
29 | |
---|
30 | // Get type |
---|
31 | typeof(myprint); |
---|
32 | |
---|
33 | // Get String |
---|
34 | string(python_eval("'pystring'")); |
---|
35 | |
---|
36 | // Get attribute |
---|
37 | attrib(myprint, "func_name"); |
---|
38 | |
---|
39 | // Check initialization |
---|
40 | pyobject empty; |
---|
41 | empty; |
---|
42 | typeof(empty); |
---|
43 | |
---|
44 | |
---|
45 | // python list from Singular list and access item |
---|
46 | pyobject ll = list(1, 2, 3); |
---|
47 | ll; |
---|
48 | ll[1]; |
---|
49 | proc(attrib(ll,"__getitem__"))(2); |
---|
50 | |
---|
51 | |
---|
52 | //// Works, is PolyBoRi is also delivered |
---|
53 | // python_import("polybori"); |
---|
54 | // declare_ring(list(Block("x", 10), Block("y", 10))); |
---|
55 | // list polybori_ideal = (x(1)+x(2),x(2)+y(1)); |
---|
56 | // def result1 = groebner_basis(polybori_ideal); |
---|
57 | // result1; |
---|
58 | // typeof(result1); |
---|
59 | // result1[1]; |
---|
60 | |
---|
61 | |
---|
62 | // Check whether already defined objects will be overwritten |
---|
63 | def already_defined = list(1); |
---|
64 | already_defined; |
---|
65 | |
---|
66 | python_run("def already_defined(): return 17"); |
---|
67 | already_defined(); |
---|
68 | |
---|
69 | def already_defined = "overwritten"; |
---|
70 | already_defined; |
---|
71 | |
---|
72 | python_run("defined_by_python = range(10)"); |
---|
73 | |
---|
74 | defined_by_python; |
---|
75 | python_run("del defined_by_python"); |
---|
76 | |
---|
77 | // Should rise error message now: |
---|
78 | defined_by_python; |
---|
79 | |
---|
80 | // Test python import |
---|
81 | python_import("os"); |
---|
82 | name; |
---|
83 | sep; |
---|
84 | linesep; |
---|
85 | |
---|
86 | // Numerical operations |
---|
87 | pyobject two = 2; |
---|
88 | pyobject three = 3; |
---|
89 | |
---|
90 | two + three; |
---|
91 | two - three; |
---|
92 | two * three; |
---|
93 | two / three; |
---|
94 | two ^ three; |
---|
95 | two ** three; |
---|
96 | |
---|
97 | three < two; |
---|
98 | two < three; |
---|
99 | three <= two; |
---|
100 | two <= three; |
---|
101 | two == three; |
---|
102 | two == two; |
---|
103 | three > two; |
---|
104 | two > three; |
---|
105 | three >= two; |
---|
106 | two >= three; |
---|
107 | two != two; |
---|
108 | two != three; |
---|
109 | |
---|
110 | pyobject pystr = "Hello"; |
---|
111 | pystr + " World!"; |
---|
112 | pystr * 3; |
---|
113 | |
---|
114 | // Test attrib command |
---|
115 | attrib(myprint); |
---|
116 | attrib(myprint, "func_name"); |
---|
117 | attrib(myprint, "func_name", "byAnyOtherName"); |
---|
118 | attrib(myprint, "func_name"); |
---|
119 | |
---|
120 | pyobject pystr2 = "pypypypyp"; |
---|
121 | proc(attrib(pystr2, "count"))("y"); |
---|
122 | pystr2::"count"("p"); // simplified notation |
---|
123 | pystr2::count("y"); // even more simplified |
---|
124 | pystr2.count("y"); // simplest |
---|
125 | |
---|
126 | // testing member function call |
---|
127 | python_run("def new_pyobj(): pass"); |
---|
128 | attrib(new_pyobj, "new_attr", "something"); |
---|
129 | attrib(new_pyobj, "new_attr"); |
---|
130 | attrib(new_pyobj); |
---|
131 | |
---|
132 | killattrib(new_pyobj, "new_attr"); |
---|
133 | attrib(new_pyobj); |
---|
134 | |
---|
135 | killattrib(new_pyobj, "new_attr"); // -> error message |
---|
136 | killattrib(new_pyobj, 17); // -> error message |
---|
137 | |
---|
138 | attrib(new_pyobj, "new_attr"); // -> error message |
---|
139 | |
---|
140 | // python list from empty Singular list (bug fixed?) |
---|
141 | pyobject ll_empty = list(); |
---|
142 | ll_empty; |
---|
143 | |
---|
144 | // converting to and from intvec |
---|
145 | intvec v0; |
---|
146 | v0; |
---|
147 | pyobject obj0 = v0; |
---|
148 | obj0; |
---|
149 | |
---|
150 | intvec v=(1,2,3); |
---|
151 | pyobject obj = v; |
---|
152 | obj; |
---|
153 | intvec(obj); |
---|
154 | |
---|
155 | intvec(python_eval("[1,2,9999999999]")); // -> error |
---|
156 | intvec(python_eval("[1,2,list()]")); // -> error |
---|
157 | |
---|
158 | ring r=0,x,lp; |
---|
159 | def rl = ringlist(r); |
---|
160 | |
---|
161 | python_eval("tuple")(list(rl[1..3])); |
---|
162 | |
---|
163 | // interaction with newstruct'ed type |
---|
164 | newstruct("wrapping","pyobject p"); |
---|
165 | wrapping wrapped; |
---|
166 | |
---|
167 | pyobject seventeen = 17; |
---|
168 | wrapped.p = seventeen; |
---|
169 | wrapped; |
---|
170 | |
---|
171 | proc unwrap(wrapping arg) { return (arg.p); } |
---|
172 | system("install", "wrapping", "pyobject", unwrap, 1); |
---|
173 | |
---|
174 | pyobject released = wrapped; |
---|
175 | released; |
---|
176 | |
---|
177 | proc wrap(pyobject arg) { wrapping res; res.p = arg; return (res); } |
---|
178 | system("install", "pyobject", "wrapping", wrap, 1); |
---|
179 | |
---|
180 | pyobject nineteen = 19; |
---|
181 | wrapped = nineteen; |
---|
182 | wrapped; |
---|
183 | |
---|
184 | |
---|
185 | // interaction with built-ins |
---|
186 | proc to_poly(pyobject arg) { return (poly(23)); } |
---|
187 | system("install", "pyobject", "poly", to_poly, 1); |
---|
188 | |
---|
189 | pyobject for_poly="4poly"; |
---|
190 | poly(for_poly); |
---|
191 | |
---|
192 | proc from_poly(poly arg) { pyobject res = string(arg); return (res); } |
---|
193 | system("install", "pyobject", "=", from_poly, 1); |
---|
194 | |
---|
195 | poly p = x+1; |
---|
196 | for_poly = p; |
---|
197 | for_poly; |
---|
198 | |
---|
199 | |
---|
200 | // interaction with both built-ins and newstructs |
---|
201 | newstruct("another","pyobject p"); |
---|
202 | another other; |
---|
203 | other.p = seventeen; |
---|
204 | other; |
---|
205 | |
---|
206 | released = other; // -> error |
---|
207 | released; |
---|
208 | |
---|
209 | proc from_types(def arg) { |
---|
210 | if (typeof(arg) == "poly") { return (from_poly(arg)); } |
---|
211 | if (typeof(arg) == "another") { return (arg.p); } |
---|
212 | } |
---|
213 | system("install", "pyobject", "=", from_types, 1); |
---|
214 | |
---|
215 | released = other; |
---|
216 | released; |
---|
217 | released = p; |
---|
218 | released; |
---|
219 | |
---|
220 | |
---|
221 | tst_status(1); |
---|
222 | $ |
---|