-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeneric_grid_view_adapter.py
463 lines (403 loc) · 16.9 KB
/
generic_grid_view_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# -*- coding: utf-8 -*-
r"""
Generic Grid View Adapter
**Grid View operations:**
.. csv-table::
:class: contentstable
:widths: 30, 70
:delim: |
:meth:`~GridViewAdapter.cell_to_display` | Static method for typecasting cell content to widget display value
:meth:`~GridViewAdapter.display_to_cell` | Instance method for typecasting widget display value to cell content
:meth:`~GridViewAdapter.compute_cells` | Compute object cells as a dictionary { coordinate pair : integer }
:meth:`~GridViewAdapter.from_cells` | Create a new Sage object from a cells dictionary
:meth:`~GridViewAdapter._validate` | Validate a new object
:meth:`~GridViewAdapter.get_cell` | Get the object cell content
:meth:`~GridViewAdapter.set_cell` | Set the object cell content
:meth:`~GridViewAdapter.addable_cells` | List addable cells
:meth:`~GridViewAdapter.removable_cells` | List removable cells
:meth:`~GridViewAdapter.add_cell` | Add a cell at given position
:meth:`~GridViewAdapter.remove_cell` | Remove a cell from given position
:meth:`~GridViewAdapter.append_row` | Append a row
:meth:`~GridViewAdapter.insert_row` | Insert a row at given index
:meth:`~GridViewAdapter.remove_row` | Remove a row at given index
:meth:`~GridViewAdapter.append_column` | Append a column
:meth:`~GridViewAdapter.insert_column` | Insert a column at given index
:meth:`~GridViewAdapter.remove_column` | Remove a column at given index
AUTHORS ::
Odile Bénassy, Nicolas Thiéry
"""
import traitlets, sage.all
from sage.all import SageObject
from sage.misc.abstract_method import abstract_method
from six import text_type
import __main__
def eval_in_main(s):
"""
Evaluate the expression `s` in the global scope
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import eval_in_main
sage: from sage.combinat.tableau import Tableaux
sage: eval_in_main("Tableaux")
<class 'sage.combinat.tableau.Tableaux'>
"""
try:
return eval(s, sage.all.__dict__)
except:
return eval(s, __main__.__dict__)
class GridViewAdapter(object):
r"""
A generic grid view adapter.
ATTRIBUTES::
* ``objclass`` -- object class for this adapter
* ``constructorname`` -- name of the constructor that builds a math object from a list
* ``traitclass`` -- cells trait class
* ``celltype`` -- cell content object type (to be defined in subclasses)
* ``cellzero`` -- cell content zero (to be defined in subclasses)
* ``addablecelltype`` -- addable cell content zero (to be defined in subclasses) -- by default = celltype
* ``addablecellzero`` -- addable cell content zero (to be defined in subclasses) -- by default == cellzero
"""
objclass = SageObject
constructorname = None
traitclass = traitlets.Instance
constructorname = None
addablecelltype = None
addablecellzero = None
@staticmethod
def cell_to_display(cell_content, display_type=text_type):
r"""
From a cell value `cell_content`,
return widget display value.
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: from six import text_type
sage: GridViewAdapter.cell_to_display(1, text_type)
'1'
sage: GridViewAdapter.cell_to_display(True, bool)
True
"""
if display_type == text_type:
return str(cell_content)
return cell_content
def display_to_cell(self, display_value, display_type=text_type):
r"""
From an unicode string `s`,
return matching cell value.
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: from six import text_type
sage: a.display_to_cell('1', text_type)
Traceback (most recent call last):
...
AttributeError: 'GridViewAdapter' object has no attribute 'celltype'
"""
if display_value:
return self.celltype(display_value)
return self.cellzero
@staticmethod
@abstract_method
def compute_cells(obj):
r"""
From an object `obj`,
return a dictionary { coordinates pair : integer }
"""
@classmethod
def _validate(cls, obj, constructorname=''):
r"""
From an object `obj`,
Try to build an object of type `cls`.
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: assert issubclass(GridViewAdapter._validate(pi).__class__, SageObject)
sage: from sage.matrix.constructor import matrix
sage: assert issubclass(GridViewAdapter._validate(pi, constructorname='matrix').__class__, BaseException)
"""
try:
if constructorname:
return eval_in_main(constructorname)(obj)
if cls.constructorname:
return eval_in_main(cls.constructorname)(obj)
return cls.objclass(obj)
except Exception as e:
return e
@classmethod
@abstract_method
def from_cells(cls, cells={}):
r"""
From a dictionary { coordinates pair : integer },
return a Sage object.
"""
@staticmethod
def get_cell(obj, pos):
r"""
From an object and a tuple `pos`,
return the object cell value at position `pos`.
TESTS ::
sage: from sage.matrix.constructor import Matrix
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: m = Matrix(QQ, 3, 3, range(9))/2
sage: GridViewAdapter.get_cell(m, (1,2))
5/2
sage: from sage.combinat.tableau import Tableau
sage: t = Tableau([[1, 2, 5, 6], [3, 7], [4]])
sage: GridViewAdapter.get_cell(t, (1,1))
7
sage: GridViewAdapter.get_cell(t, (1,6))
Traceback (most recent call last):
...
ValueError: Cell '(1, 6)' does not exist!
sage: from sage.combinat.skew_tableau import SkewTableau
sage: st = SkewTableau([[None,1,2],[3,4,5],[6]])
sage: GridViewAdapter.get_cell(st, (0,0))
sage: GridViewAdapter.get_cell(st, (1,1))
4
"""
try:
return obj[pos[0]][pos[1]]
except:
pass
try:
l = [list(x) for x in obj]
except:
raise NotImplementedError("Adapter class method 'get_cell(obj, pos)' is not implemented.")
try:
return l[pos[0]][pos[1]]
except:
raise ValueError("Cell '%s' does not exist!" % str(pos))
def make_dirty(self, l, dirty={}):
r"""
Append 'dirty' values to list 'l'.
Return a list with no empty values.
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: from sage.combinat.tableau import Tableau
sage: t = Tableau([[1, 2, 5, 6], [3, 7], [4]])
sage: ga = GridViewAdapter()
sage: ga.cellzero = 0
sage: ga.make_dirty(t.to_list(), {(1,2):42})
[[1, 2, 5, 6], [3, 7, 42], [4]]
sage: ga.make_dirty(t.to_list(), {(2,0):0})
[[1, 2, 5, 6], [3, 7]]
sage: ga.make_dirty(t.to_list(), {(0,2):0, (0,3):0})
[[1, 2], [3, 7], [4]]
"""
for p in dirty:
if p[0] < len(l):
if p[1] < len(l[p[0]]):
l[p[0]][p[1]] = dirty[p]
elif len(l[p[0]]) <= p[1] and dirty[p] != self.cellzero:
# padding with cellzero
l[p[0]] += [self.cellzero] * (p[1] - len(l[p[0]]))
l[p[0]].append(dirty[p])
else:
for i in range(p[0] - len(l)):
l.append([])
l.append([cellzero] * p[1] + [dirty[p]])
l = [[ val for val in row if val != self.cellzero ] for row in l]
return [val for val in l if val]
def set_cell(self, obj, pos, val, dirty={}, constructorname=''):
r"""
From a Sage object, a position (pair of coordinates) `pos` and a value `val`,
return a new Sage object.
with a modified cell at position `pos`.
TESTS ::
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: from sage.combinat.tableau import Tableau
sage: t = Tableau([[1, 2, 5, 6], [3, 7], [4]])
sage: ga = GridViewAdapter()
sage: ga.cellzero = None
sage: ga.set_cell(t, (1,1), 8, constructorname='Tableau')
[[1, 2, 5, 6], [3, 8], [4]]
sage: from sage.matrix.constructor import Matrix, matrix
sage: m = Matrix(QQ, 3, 3, range(9))/2
sage: ga.set_cell(m, (0,1), 2/3, constructorname='matrix')
[ 0 2/3 1]
[3/2 2 5/2]
[ 3 7/2 4]
sage: ga.set_cell(m, (4,2), 1/2, constructorname='matrix')
Traceback (most recent call last):
...
ValueError: Position '(4, 2)' does not exist
"""
try:
l = [list(x) for x in obj]
except:
raise NotImplementedError("Adapter method 'set_cell(obj, pos, val)' is not implemented.")
l = self.make_dirty(l, dirty)
try:
l[pos[0]][pos[1]] = val
except:
raise ValueError("Position '%s' does not exist" % str(pos))
return self._validate(l, constructorname)
@staticmethod
@abstract_method(optional = True)
def addable_cells(obj):
r"""
For Sage object `obj`,
list those cell positions where a user could want to add a cell,
and get a still valid Sage object for this adapter.
"""
@staticmethod
@abstract_method(optional = True)
def removable_cells(obj):
r"""
For Sage object `obj`,
list those cell positions where a user could want to remove a cell,
and get a still valid Sage object for this adapter.
"""
@abstract_method(optional = True)
def add_cell(self, obj, pos, val, dirty={}):
r"""
This method should try to add a cell to object `obj`
at position `pos` and with value `val`.
"""
@abstract_method(optional = True)
def remove_cell(self, obj, pos, dirty={}):
r"""
This method should try to remove a cell from object `obj`
at position `pos`.
"""
@abstract_method(optional = True)
def append_row(self, obj, r=None):
r"""
This method should try to append a row to object `obj`
with values from list `r`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.append_row(S, (1,2,3)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
@abstract_method(optional = True)
def insert_row(self, obj, index, r=None):
r"""
This method should try to insert a row to object `obj`
at index `index`, with values from list `r`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.insert_row(S, 1, (1,2,3)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
def add_row(self, obj, index=None, r=None):
r"""
An alias for appending/inserting a row.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.add_row(S, 1, (1,2,3,4))
Traceback (most recent call last):
...
NotImplementedError: Method 'insert_row' is not implemented.
"""
if index:
try:
return self.insert_row(obj, index, r)
except:
raise NotImplementedError("Method 'insert_row' is not implemented.")
else:
try:
return self.append_row(obj, r)
except:
raise NotImplementedError("Method 'append_row' is not implemented.")
@abstract_method(optional = True)
def remove_row(self, obj, index=None):
r"""
This method should try to remove a row from object `obj`
at index `index`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.remove_row(S, 1) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
@abstract_method(optional = True)
def append_column(self, obj, r=None):
r"""
This method should try to append a column to object `obj`
with values from list `r`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.append_column(S, (1,2,3,4)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
@abstract_method(optional = True)
def insert_column(self, obj, index, r=None):
r"""
This method should try to insert a column to object `obj`
at index `index`, with values from list `r`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.insert_column(S, 1, (1,2,3,4)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
@abstract_method(optional = True)
def remove_column(self, obj, index=None):
r"""
This method should try to remove a column from object `obj`
at index `index`.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.remove_column(S, 2) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: 'AbstractMethod' object is not callable
"""
def add_column(self, obj, index=None, r=None):
r"""
An alias for appending/inserting a column.
TESTS ::
sage: from sage.matrix.matrix_space import MatrixSpace
sage: S = MatrixSpace(ZZ, 4,3)
sage: m = S.matrix([1,7,1,0,0,3,0,-1,2,1,0,-3])
sage: from sage_widget_adapters.generic_grid_view_adapter import GridViewAdapter
sage: a = GridViewAdapter()
sage: a.add_column(S, 1, (1,2,3))
Traceback (most recent call last):
...
NotImplementedError: Method 'insert_column' is not implemented.
"""
if index:
try:
return self.insert_column(obj, index, r)
except:
raise NotImplementedError("Method 'insert_column' is not implemented.")
else:
try:
return self.append_column(obj, r)
except:
raise NotImplementedError("Method 'append_column' is not implemented.")