Grid Cell¶
A Cell
object stores the content of a Grid
cell.
A cell can have styles, a dictionary of key-value properties attached to the cell.
A cell has a nature to distinguish between header, body and footer cells.
The default nature is None
, but you can also use “body”, “header”, “footer” or whatever…
A cell has top-left coordinates: x and y. The default coordinates is (1, 1): this is the top-left coordinate of the cell box. The coordinates x and y cannot be null: grid coordinates are 1-indexed.
A cell has a size: width and height. The default size is (1, 1), you can increase them to represent horizontal or vertical spanning. The width and the height cannot be null.
To instantiate a Cell
, you can do:
>>> c1 = Cell("c1")
>>> c2 = Cell("c2", styles={'color': 'red'})
>>> c3 = Cell("c3", nature="footer")
>>> c4 = Cell("c4", width=2)
>>> c5 = Cell("c5", height=2)
The string representation of a cell is the string representation of it’s content:
>>> for cell in c1, c2, c3, c4, c5:
... print(cell)
c1
c2
c3
c4
c5
On initialization, the cell min position is always (1, 1), a.k.a. the top-left.
>>> c1 = Cell("c1")
>>> c1.min
Coord(x=1, y=1)
>>> c1.size
Size(width=1, height=1)
>>> c1.box
Box(min=Coord(x=1, y=1), max=Coord(x=1, y=1))
A cell can be moved to another position:
>>> c1 = Cell("c1", width=3, height=2)
>>> c2 = c1.move_to(Coord(5, 3))
>>> c2.min
Coord(x=5, y=3)
>>> c2.size
Size(width=3, height=2)
>>> c2.box
Box(min=Coord(x=5, y=3), max=Coord(x=7, y=4))
You can check if a coord is inside the box:
>>> c1 = Cell("c1", width=3, height=2)
>>> c2 = c1.move_to(Coord(5, 3))
>>> (7, 4) in c2
True
>>> Coord(6, 3) in c2
True
>>> Box(6, 3, 7, 4) in c2
True
- class benker.cell.Cell(content, styles=None, nature=None, x=1, y=1, width=1, height=1)¶
Bases:
benker.styled.Styled
Cell of a grid.
- Variables
content –
user-defined cell content. It can be of any type:
None
,str
,int
,float
, a container (list
), a XML element, etc. The same content can be shared by several cells, it’s your own responsibility to handle the copy (or deep copy) of the content reference when needed.styles –
user-defined cell styles: a dictionary of key-value pairs. This values are useful to store some HTML-like styles (border-style, border-width, border-color, vertical-align, text-align, etc.). Of course, we are not tied to the HTML-like styles, you can use your own list of styles.
Note
The style dictionary is always copied: in other words, key-value pairs are copied but a shallow copy is done for the values (in general, it is not a problem if you use non-mutable values like
str
).nature –
nature: a way to distinguish the body cells, from the header and the footer. The default value is
None
, but you can use “body”, “header”, “footer” or whatever is suitable for your needs.
Changed in version 0.4.2: The default value of nature is
None
(instead of “body”).- property box¶
Bounding box of the cell.
- content¶
- property height¶
Height of the cell – rows spanning.
- property max¶
Maximum coordinates of the cell – bottom-right coordinates.
- property min¶
Minimum coordinates of the cell – top-left coordinates.
- move_to(coord)¶
Move the cell to the given coordinates.
See:
transform()
.- Parameters
coord (tuple[int, int] or benker.coord.Coord) – new top-left coordinates of the cell, by default it is unchanged.
- Return type
- Returns
a copy of this cell after transformation.
- resize(size)¶
Resize the cell to the given size.
See:
transform()
.- Parameters
size (tuple[int, int] or benker.size.Size) – new size of the cell, by default it is unchanged.
- Return type
- Returns
a copy of this cell after transformation.
- property size¶
Size of the cell – (with, height).
- transform(coord=None, size=None)¶
Transform the bounding box of the cell by making a move and a resize.
- Parameters
coord (tuple[int, int] or benker.coord.Coord) – new top-left coordinates of the cell, by default it is unchanged.
size (tuple[int, int] or benker.size.Size) – new size of the cell, by default it is unchanged.
- Return type
- Returns
a copy of this cell after transformation.
- property width¶
Width of the cell – columns spanning.
- benker.cell.get_content_text(content)¶
Try hard to extract a good string representation of the cell content.
>>> from benker.cell import get_content_text
>>> get_content_text(None) == '' True >>> get_content_text('') == '' True >>> print(get_content_text('Hi')) Hi >>> print(get_content_text(True)) True >>> print(get_content_text(123)) 123 >>> print(get_content_text(3.14)) 3.14 >>> get_content_text([None, None]) == '' True >>> print(get_content_text(["hello", " ", "world!"])) hello world!
- Parameters
content – Cell content.
- Returns
the cell text:
if the content is
None
: returns “”,if the content is a string: return the string unchanged,
if the content is a number: return the string representation of the number,
if the content is a list of strings, return the concatenated strings (
None
is ignored),if the content is a list of XML nodes, return the concatenated strings of the elements (the processing-instruction and the comments are ignored),
else: return a concatenation of the string representation of the content items.
New in version 0.4.1.