source: git/Tst/Short/pyobject.tst @ 3877fe

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