Skip to content

Commit a54ef58

Browse files
dbiebercopybara-github
authored andcommitted
Adds BinaryCanvas as test component.
PiperOrigin-RevId: 260185289 Change-Id: I8c8354359486b9f0c63a7c524eb9a904de6c3249
1 parent d774539 commit a54ef58

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

fire/test_components.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,36 @@ def fn_with_code_in_docstring():
465465
True.
466466
"""
467467
return True
468+
469+
470+
class BinaryCanvas(object):
471+
"""A canvas with which to make binary art, one bit at a time."""
472+
473+
def __init__(self, size=10):
474+
self.pixels = [[0] * size for _ in range(size)]
475+
self._size = size
476+
self._row = 0 # The row of the cursor.
477+
self._col = 0 # The column of the cursor.
478+
479+
def __str__(self):
480+
return '\n'.join(
481+
' '.join(str(pixel) for pixel in row) for row in self.pixels)
482+
483+
def show(self):
484+
print(self)
485+
return self
486+
487+
def move(self, row, col):
488+
self._row = row % self._size
489+
self._col = col % self._size
490+
return self
491+
492+
def on(self):
493+
return self.set(1)
494+
495+
def off(self):
496+
return self.set(0)
497+
498+
def set(self, value):
499+
self.pixels[self._row][self._col] = value
500+
return self

0 commit comments

Comments
 (0)