-
Notifications
You must be signed in to change notification settings - Fork 58
/
ipythonblocks.py
376 lines (281 loc) · 9.65 KB
/
ipythonblocks.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
"""
ipythonblocks provides a BlockGrid class that displays a colored grid in the
IPython Notebook. The colors can be manipulated, making it useful for
practicing control flow stuctures and quickly seeing the results.
"""
# This file is copyright 2013 by Matt Davis and covered by the license at
# https://github.com/jiffyclub/ipythonblocks/blob/master/LICENSE.txt
import copy
import itertools
import numbers
import os
from operator import iadd
from IPython.display import HTML, display
import sys
if sys.version_info[0] >= 3:
xrange = range
from functools import reduce
__all__ = ('Block', 'BlockGrid', 'InvalidColorSpec', '__version__')
__version__ = '1.1.1'
_TABLE = '<table><tbody>{0}</tbody></table>'
_TR = '<tr>{0}</tr>'
_TD = ('<td style="width: {0}px; height: {0}px; padding: 0px;'
'border: 1px solid white; background-color: {1};"></td>')
_RGB = 'rgb({0}, {1}, {2})'
_SINGLE_ITEM = 'single item'
_SINGLE_ROW = 'single row'
_ROW_SLICE = 'row slice'
_DOUBLE_SLICE = 'double slice'
_SMALLEST_BLOCK = 1
class InvalidColorSpec(Exception):
"""
Error for a color value that is not a number.
"""
pass
class Block(object):
"""
A colored square.
Parameters
----------
red, green, blue : int
Integers on the range [0 - 255].
row, col : int, optional
The zero-based grid position of this `Block`.
size : int, optional
Length of the sides of this block in pixels. One is the lower limit.
Attributes
----------
red, green, blue : int
The color values for this `Block`. The color of the `Block` can be
updated by assigning new values to these attributes.
row, col : int
The zero-based grid position of this `Block`.
size : int
Length of the sides of this block in pixels. The block size can be
changed by modifying this attribute. Note that one is the lower limit.
"""
def __init__(self, red, green, blue, row=None, col=None, size=20):
self.red = red
self.green = green
self.blue = blue
self._row = row
self._col = col
self.size = size
@staticmethod
def _check_value(value):
"""
Check that a value is a number and constrain it to [0 - 255].
"""
if not isinstance(value, numbers.Number):
s = 'value must be a number. got {0}.'.format(value)
raise InvalidColorSpec(s)
return int(round(min(255, max(0, value))))
@property
def red(self):
return self._red
@red.setter
def red(self, value):
value = self._check_value(value)
self._red = value
@property
def green(self):
return self._green
@green.setter
def green(self, value):
value = self._check_value(value)
self._green = value
@property
def blue(self):
return self._blue
@blue.setter
def blue(self, value):
value = self._check_value(value)
self._blue = value
@property
def row(self):
return self._row
@property
def col(self):
return self._col
@property
def size(self):
return self._size
@size.setter
def size(self, size):
self._size = max(_SMALLEST_BLOCK, size)
def set_colors(self, red, green, blue):
"""
Updated block colors.
Parameters
----------
red, green, blue : int
Integers on the range [0 - 255].
"""
self.red = red
self.green = green
self.blue = blue
@property
def td(self):
"""
The HTML for a table cell with the background color of this Block.
"""
rgb = _RGB.format(self._red, self._green, self._blue)
return _TD.format(self._size, rgb)
def _repr_html_(self):
return _TABLE.format(_TR.format(self.td))
def show(self):
display(HTML(self._repr_html_()))
def __str__(self):
s = ['Block',
'Color: ({0}, {1}, {2})'.format(self._red,
self._green,
self._blue)]
# add position information if we have it
if self._row is not None:
s[0] += ' ({0}, {1})'.format(self._row, self._col)
return os.linesep.join(s)
class BlockGrid(object):
"""
A grid of blocks whose colors can be individually controlled.
Individual blocks have a width and height of 10 screen pixels.
To get the second Block in the third row use block = grid[1, 2].
Parameters
----------
width : int
Number of blocks wide to make the grid.
height : int
Number of blocks high to make the grid.
fill : tuple of int, optional
An optional initial color for the grid, defaults to black.
Specified as a tuple of (red, green, blue). E.g.: (10, 234, 198)
block_size : int, optional
Length of the sides of grid blocks in pixels. One is the lower limit.
Attributes
----------
width : int
Number of blocks along the width of the grid.
height : int
Number of blocks along the height of the grid.
shape : tuple of int
A tuple of (width, height).
block_size : int
Length of the sides of grid blocks in pixels. The block size can be
changed by modifying this attribute. Note that one is the lower limit.
"""
def __init__(self, width, height, fill=(0, 0, 0), block_size=20):
self._width = width
self._height = height
self._block_size = block_size
self._initialize_grid(fill)
def _initialize_grid(self, fill):
grid = [[Block(*fill, row=row, col=col, size=self._block_size)
for col in xrange(self.width)]
for row in xrange(self.height)]
self._grid = grid
@property
def width(self):
return self._width
@property
def height(self):
return self._height
@property
def shape(self):
return (self._width, self._height)
@property
def block_size(self):
return self._block_size
@block_size.setter
def block_size(self, size):
self._block_size = size
for block in self:
block.size = size
@classmethod
def _view_from_grid(cls, grid):
"""
Make a new BlockGrid from a list of lists of Block objects.
"""
new_width = len(grid[0])
new_height = len(grid)
new_BG = cls(new_width, new_height)
new_BG._grid = grid
return new_BG
@staticmethod
def _categorize_index(index):
"""
Used by __getitem__ and __setitem__ to determine whether the user
is asking for a single item, single row, or some kind of slice.
"""
if isinstance(index, int):
return _SINGLE_ROW
elif isinstance(index, slice):
return _ROW_SLICE
elif isinstance(index, tuple):
if len(index) not in (1, 2):
s = 'Invalid index, too many dimensions.'
raise IndexError(s)
if isinstance(index[0], slice):
if isinstance(index[1], (int, slice)):
return _DOUBLE_SLICE
if isinstance(index[1], slice):
if isinstance(index[0], (int, slice)):
return _DOUBLE_SLICE
elif isinstance(index[0], int) and isinstance(index[0], int):
return _SINGLE_ITEM
raise IndexError('Invalid index.')
def __getitem__(self, index):
ind_cat = self._categorize_index(index)
if ind_cat == _SINGLE_ROW:
return self._grid[index]
elif ind_cat == _SINGLE_ITEM:
return self._grid[index[0]][index[1]]
elif ind_cat == _ROW_SLICE:
return BlockGrid._view_from_grid(self._grid[index])
elif ind_cat == _DOUBLE_SLICE:
new_grid = self._get_double_slice(index)
return BlockGrid._view_from_grid(new_grid)
def __setitem__(self, index, value):
if len(value) != 3:
s = 'Assigned value must have three integers. got {0}.'
raise ValueError(s.format(value))
ind_cat = self._categorize_index(index)
if ind_cat == _SINGLE_ROW:
map(lambda b: b.set_colors(*value), self._grid[index])
elif ind_cat == _SINGLE_ITEM:
self._grid[index[0]][index[1]].set_colors(*value)
else:
if ind_cat == _ROW_SLICE:
sub_grid = self._grid[index]
elif ind_cat == _DOUBLE_SLICE:
sub_grid = self._get_double_slice(index)
map(lambda b: b.set_colors(*value), itertools.chain(*sub_grid))
def _get_double_slice(self, index):
sl_height = index[0]
sl_width = index[1]
if isinstance(sl_width, int):
sl_width = slice(sl_width, sl_width + 1)
if isinstance(sl_height, int):
sl_height = slice(sl_height, sl_height + 1)
rows = self._grid[sl_height]
grid = [r[sl_width] for r in rows]
return grid
def __iter__(self):
return itertools.chain(*self._grid)
def _repr_html_(self):
html = reduce(iadd,
(_TR.format(reduce(iadd, (block.td for block in row)))
for row in self._grid))
return _TABLE.format(html)
def __str__(self):
s = ['BlockGrid',
'Shape: {0}'.format(self.shape)]
return os.linesep.join(s)
def copy(self):
"""
Returns an independent copy of this BlockGrid.
"""
return copy.deepcopy(self)
def show(self):
"""
Display colored grid as an HTML table.
"""
display(HTML(self._repr_html_()))