本文整理汇总了Python中grid.Grid.height方法的典型用法代码示例。如果您正苦于以下问题:Python Grid.height方法的具体用法?Python Grid.height怎么用?Python Grid.height使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grid.Grid
的用法示例。
在下文中一共展示了Grid.height方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Box
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import height [as 别名]
class Box(object):
"""
Manages the drawing of boxes.
"""
def __init__(self, screen, rows, columns, rows_offset=0, columns_offset=0,
command=None, interval=None):
# Set box colors.
foreground = curses.COLOR_BLACK
background = curses.COLOR_WHITE
curses.init_pair(1, foreground, background)
# Set attributes.
self.screen = screen
self.grid = Grid(screen)
self.rows = rows
self.columns = columns
self.rows_offset = rows_offset
self.columns_offset = columns_offset
self.command = command
self.interval = interval
# Create an empty placeholder box and set it to be refreshed
# immediately.
self.box = curses.newpad(1, 1)
self.next_refresh = datetime.now()
def redraw_if_changed(self):
"""
Redraws the box if it has changed.
"""
# Re-calculate the box's size and position.
height = self.grid.height(self.rows)
width = self.grid.width(self.columns)
x_offset = self.grid.x_offset(self.columns_offset)
y_offset = self.grid.y_offset(self.rows_offset)
# Only refresh the contents after waiting for the specified interval.
if self.next_refresh <= datetime.now():
try:
self.contents = subprocess.check_output(
self.command,
shell=True
)
except:
self.contents = '\n There was a problem running this ' + \
'window\'s command.'
# Calculate the height the pad needs to be to fit the contents.
lines = self.contents.splitlines()
if len(lines) >= height:
# Without adding 1 to height here the program crashes, but I'm
# not sure why. It doesn't seem like it should be necessary.
self.pad_height = len(lines) + 1
else:
self.pad_height = height
# Calculate the width the pad needs to be to fit the contents.
self.pad_width = width
for line in lines:
# Add 1 to the string length to allow for end-of-line
# characters.
if len(line) >= self.pad_width:
self.pad_width = len(line) + 1
# Set the time of the next refresh.
self.next_refresh += timedelta(seconds=self.interval)
# Recreate the box and set its colors.
self.box = curses.newpad(self.pad_height, self.pad_width)
self.box.bkgdset(ord(' '), curses.color_pair(1))
self.box.erase()
self.box.addstr(self.contents)
# Only refresh the box if it has changed since the last refresh.
if self.box.is_wintouched():
# Subtract 1 from height and width to allow for column and row
# numbering starting at 0.
self.box.refresh(
0,
0,
y_offset,
x_offset,
y_offset + (height - 1),
x_offset + (width - 1)
)
示例2: TestGrid
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import height [as 别名]
class TestGrid(unittest.TestCase):
def setUp(self):
self.grid = Grid([
[0 , 1 , 2 , 3 , 4 ],
[0.5 , 1.4 , 2.3 , 3.2 , 4.1 ],
['a' , 'b' , 'c' , 'd' , 'e' ],
[None, [] , () , {} , '' ],
[None, [1] , '23', 4 , 5 ],
[5 , 6 , 7 , 8 , 9 ],
])
def test1_1(self):
"""testing the width() method"""
self.assertEqual(self.grid.width(), 5)
def test1_2(self):
"""testing the height() method"""
self.assertEqual(self.grid.height(), 6)
def test2_1(self):
"""testing list features: item access"""
self.assertEqual(self.grid[4][2], '23')
self.assertEqual(self.grid[3][0], None)
self.assertEqual(self.grid[0][4], 4 )
self.assertEqual(self.grid[5] , [5, 6, 7, 8, 9])
def test2_2(self):
"""testing list features: slicing ..."""
self.assertEqual(self.grid[3:5],
[
[None, [] , () , {} , '' ],
[None, [1] , '23', 4 , 5 ],
])
self.assertEqual(self.grid[2::2],
[
['a' , 'b' , 'c' , 'd' , 'e' ],
[None, [1] , '23', 4 , 5 ],
])
self.assertEqual(self.grid[0][::2], [0, 2, 4])
self.assertEqual(self.grid[:], self.grid )
def test3_1(self):
"""testing the get_subgrid() method: single cell ..."""
self.assertEqual(self.grid.get_subgrid(1, 2), [['b']])
self.assertEqual(self.grid.get_subgrid(4, 1), [[4.1]])
self.assertEqual(self.grid.get_subgrid(0, 5), [[ 5 ]])
def test3_2(self):
"""testing the get_subgrid() method: single row.. ..."""
self.assertEqual(self.grid.get_subgrid(1, 4, 4),
[[[1], '23', 4, 5]])
def test3_3(self):
"""testing the get_subgrid() method: single column..."""
self.assertEqual(self.grid.get_subgrid(1, 1, 1, 5),
[
[1.4],
['b'],
[[ ]],
[[1]],
[ 6 ],
])
def test3_4(self):
"""testing the get_subgrid() method: rectangular area"""
self.assertEqual(self.grid.get_subgrid(1, 2, 3, 4),
[
['b' , 'c' , 'd' ],
[[] , () , {} ],
[[1] , '23', 4 ],
[6 , 7 , 8 ],
])
def test4(self):
"""testing the __str__() method"""
self.assertEqual(str(self.grid),
"""[
[0, 1, 2, 3, 4]
[0.5, 1.4, 2.3, 3.2, 4.1]
['a', 'b', 'c', 'd', 'e']
[None, [], (), {}, '']
[None, [1], '23', 4, 5]
[5, 6, 7, 8, 9]
]"""
)