-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
113 lines (92 loc) · 3.46 KB
/
plot.py
File metadata and controls
113 lines (92 loc) · 3.46 KB
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
# The MIT License (MIT)
#
# Copyright (c) 2013 cpelley
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from abc import ABCMeta, abstractmethod
import curses
import time
class Plot(object):
"""Abstract class for a GOL simulation graphical backend."""
__metaclass__ = ABCMeta
@abstractmethod
def __enter__(self):
raise NotImplementedError()
@abstractmethod
def update(self):
raise NotImplementedError()
@abstractmethod
def __exit__(self):
raise NotImplementedError()
class CursePlot(Plot):
"""Curses context manager for fast plotting an ndarray."""
def __init__(self, grid, window=None, fps=None):
"""
Curses context manager for fast plotting an ndarray.
Args:
* grid (2darray):
Grid to plot.
Kwargs:
* window (ndarray):
ndarray view of the grid (see :func:`window_views`).
* fps (int):
~ Numbers of frames per second.
Returns:
None
"""
self.fps = fps
if window:
self.window = window
else:
self.window = curses.initscr()
self.grid = grid
# No echo of input characters
curses.noecho()
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_WHITE)
# Visibility of cursor
curses.curs_set(0)
def __enter__(self):
ymax, xmax = self.window.getmaxyx()
if self.grid.shape[0]-2 > ymax or self.grid.shape[1] > xmax:
msg = ('Grid size {} exceeds screen character resolution '
'{}'.format((self.grid.shape[0]-2, self.grid.shape[1]-2),
(ymax, xmax)))
raise RuntimeError(msg)
return self
def update(self):
"""
Update the curses window with the current content of our grid, ignoring
the grid border.
"""
for ind, line in enumerate(self.grid):
if 0 < ind < self.grid.shape[0]-1:
pout = ''.join(line.astype('|S1')).replace('0', ' ')
self.window.addstr(ind, 0, pout[1:-1])
self.window.refresh()
if self.fps:
time.sleep(1. / self.fps)
def __exit__(self, type, value, traceback):
print 'I did it'
# Close curses.
curses.nocbreak()
self.window.keypad(0)
curses.echo()
curses.endwin()