pantable.ast module

class pantable.ast.Align(aligns: np.ndarray[np.int8])[source]

Bases: object

Alignment class

ALIGN: ClassVar = array(['AlignDefault', 'AlignLeft', 'AlignRight', 'AlignCenter'],       dtype='<U12')
aligns: np.ndarray[np.int8]
property aligns_char
property aligns_idx: np.ndarray[np.int8]

this is designed such that aligns_text below works

the last % 4 is to gunrantee garbage input still falls inside the idx range of ALIGN

property aligns_string: str

the aligns string used in pantable codeblock

such as LDRC…

property aligns_text: np.ndarray[np.str_]
classmethod default(shape: Union[Tuple[int], Tuple[int, int]] = (1,)) Align[source]
classmethod from_aligns_char(aligns_char: np.ndarray[np.dtype('S1')]) Align[source]
classmethod from_aligns_string(alignment: str) pantable.ast.Align[source]

create Align from aligns_string

used in __repr__

should not be used by data created by users

should satisfies Align.from_aligns_string(align.aligns_string) == align

classmethod from_aligns_string_1d(alignment: str, size: int) pantable.ast.Align[source]

create Align from aligns_string, 1-dimensional

should be used by data created by users

classmethod from_aligns_string_2d(alignment_cells: str, shape: Tuple[int, int]) Align[source]

create Align from aligns_string, 2-dimensional

should be used by data created by users

classmethod from_aligns_text(aligns_text: np.ndarray[Optional[np.str_]]) Align[source]
class pantable.ast.Ica(identifier: str = '', classes: List[str] = <factory>, attributes: Dict[str, str] = <factory>)[source]

Bases: object

a class of identifier, classes, and attributes

attributes: Dict[str, str]
classes: List[str]
classmethod from_panflute_ast(elem: ListContainer[Block]) Ica[source]
identifier: str = ''
to_panflute_ast() ListContainer[Plain][source]

to panflute AST element

we choose a ListContainer-Plain-Span here as it is simplest to capture the Ica

class pantable.ast.PanCodeBlock(data: str = '', options: pantable.ast.PanTableOption = <factory>, ica: pantable.ast.Ica = <factory>)[source]

Bases: object

A PanTable representation of CodeBlock

it handles the transition between panflute CodeBlock and PanTable

It can convert to and from panflute CodeBlock, and to and from PanTable

there’s no from_panflute_ast method, as we expect the args in the __init__ to be from panflute.yaml_filter directly.

c.f. .util.parse_markdown_codeblock for testing purposes

data: str = ''
classmethod from_data_format(data: np.ndarray[np.str_], options: Optional[PanTableOption] = None, ica: Optional[Ica] = None) PanCodeBlock[source]

construct from different data formats

TODO: should io be done by PanCodeBLock.to_panflute_ast or other places? seems wrong to be here

but it may actually belongs to here because where else for binary data?

classmethod from_yaml_filter(data: str = '', options: Optional[dict] = None, element: Optional[CodeBlock] = None, doc: Optional[Doc] = None) PanCodeBlock[source]

these args are those passed from within yaml_filter

ica: pantable.ast.Ica
options: pantable.ast.PanTableOption
static parse_data_markdown(str_array: np.ndarray[np.str_], fancy_table: bool = False, ica_cell_pat=re.compile('^(\\([0-9, ]+\\))?({[^{}]*})?$'), fancy_table_pat=re.compile('^({[^{}]*})?? ?(---|===|___)? ?({[^{}]*})?$')) Tuple[Optional[np.ndarray[np.int64]], Optional[np.ndarray[np.str_]], np.ndarray[np.str_], np.ndarray[np.str_], TableArray][source]

parse markdown in string array

c.f. PanTableMarkdown.to_str_array

parse_options(shape: Tuple[int, int]) Tuple[str, str, Spec, Align, Optional[np.ndarray[np.int64]], Optional[np.ndarray[np.int64]]][source]

parsing PanTableOption to whatever PanTableStr.__init__ needed

This is the point where correctness is checked most aggressively. Here we assumed the types are already correct, so we are checking things beyond types such as Optional, shape, positivity, etc.

to_panflute_ast() panflute.elements.CodeBlock[source]

return a panflute AST representation

TODO: handle differently if include exists and writable need to be able to configure pantable2csv on write location

to_pantablestr() pantable.ast.PanTableStr[source]

parse data and return a PanTableStr

Exceptions might be raised here

c.f. to_pancodeblock

class pantable.ast.PanTable(cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]], caption: ListContainer[Block] = <factory>, icas_rowblock: Optional[np.ndarray[Ica]] = None, icas_row: Optional[np.ndarray[Ica]] = None, icas: Optional[np.ndarray[Ica]] = None, short_caption: Optional[ListContainer[Inline]] = None, ica_table: Ica = <factory>, spec: Optional[Spec] = None, aligns: Optional[Align] = None, ms: Optional[np.ndarray[np.int64]] = None, ns_head: Optional[np.ndarray[np.int64]] = None)[source]

Bases: pantable.ast.PanTableAbstract

a representation of panflute Table

TableArray should have content type as ListContainer although not strictly enforced here

caption: ListContainer[Block]
classmethod from_panflute_ast(table: panflute.table_elements.Table) pantable.ast.PanTable[source]
icas: Optional[np.ndarray[Ica]] = None
icas_row: Optional[np.ndarray[Ica]] = None
icas_rowblock: Optional[np.ndarray[Ica]] = None
static iter_tablerows(icas_row: np.ndarray[Ica], pf_cells: np.ndarray[TableCell]) Iterator[TableRow][source]
property panflute_tablecells: np.ndarray[TableCell]
short_caption: Optional[ListContainer[Inline]] = None
to_panflute_ast() panflute.table_elements.Table[source]
to_pantablemarkdown() pantable.ast.PanTableMarkdown[source]

return a PanTableMarkdown representation of self

to_pantablestr() pantable.ast.PanTableStr[source]

return a PanTableStr representation of self

All contents are stringified so it is lossy.

class pantable.ast.PanTableAbstract(cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]], caption: Union[ListContainer[Block], str], icas_rowblock: np.ndarray, icas_row: np.ndarray, icas: np.ndarray, short_caption: Optional[Union[ListContainer[Inline], str]] = None, ica_table: Ica = <factory>, spec: Optional[Spec] = None, aligns: Optional[Align] = None, ms: Optional[np.ndarray[np.int64]] = None, ns_head: Optional[np.ndarray[np.int64]] = None)[source]

Bases: object

an abstract class of PanTables

aligns: Optional[Align] = None
property body_idxs_row: np.ndarray[np.int64]

calculate the i-th body that each row belongs to

negative values means the row is not in a body

caption: Union[ListContainer[Block], str]
cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]]
property contents: np.ndarray[Union[ListContainer, str]]
classmethod default(shape: Tuple[int, int], has_geometries=False)[source]

return a default object given shape, etc

This won’t work in PanTableAbstract itself but all derived classes including PanTableStr, PanTableMarkdown, PanTableText

property ica_foot: pantable.ast.Ica
property ica_head: pantable.ast.Ica
ica_table: Ica
icas: np.ndarray
property icas_body: np.ndarray[Ica]
icas_row: np.ndarray
icas_rowblock: np.ndarray
property icas_rowblock_idxs_row: np.ndarray[np.int64]

calculate the i-th row-block attrs that each row belongs to

property is_body_bodies: np.ndarray[np.bool_]
property is_body_heads: np.ndarray[np.bool_]
property is_foots: np.ndarray[np.bool_]
property is_heads: np.ndarray[np.bool_]
iter_rowblocks(array: numpy.ndarray) List[numpy.ndarray][source]

break array into list of head, bodies, foot

assume array is iterables of rows

property last_row_of_rowblock_idxs: Set[np.int64]

return a set of the indices of the last row per row-block excluding foot

property m: int
property m_bodies: int
property m_icas_rowblock: int

only one ica per body

property m_rowblocks: int

2 rowblocks per body

property ms: np.ndarray[np.int64]

setter and getter of ms

quirks of dataclass with property see https://stackoverflow.com/a/61480946/5769446

property n: int
ns_head: Optional[np.ndarray[np.int64]] = None
property rowblock_idxs_row: np.ndarray[np.int64]

reverse lookup the index of rowblocks per row

property rowblock_splitting_idxs: np.ndarray[np.int64]

applying np.split(array_of_rows, rowblock_splitting_idxs) would break it back into list of head, bodies, foot

property shape: Tuple[int, int]
short_caption: Optional[Union[ListContainer[Inline], str]] = None
spec: Optional[Spec] = None
class pantable.ast.PanTableMarkdown(cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]], caption: str = '', icas_rowblock: Optional[np.ndarray[np.str_]] = None, icas_row: Optional[np.ndarray[np.str_]] = None, icas: Optional[np.ndarray[np.str_]] = None, short_caption: Optional[str] = None, ica_table: Ica = <factory>, spec: Optional[Spec] = None, aligns: Optional[Align] = None, ms: Optional[np.ndarray[np.int64]] = None, ns_head: Optional[np.ndarray[np.int64]] = None, table_width: Optional[float] = None)[source]

Bases: pantable.ast.PanTableStr

similar to PanTableStr, but with all str assumed to be in markdown

to_pancodeblock(format: str = 'csv', fancy_table: bool = False, include: str = '', csv_kwargs: Optional[dict] = None) pantable.ast.PanCodeBlock[source]

to PanCodeBlock object

This is lossy as there’s no way to encode the geometries of self.cells in PanCodeBlock. Use PanTableMarkdown instead if you want to preserve that info.

to_pantable() pantable.ast.PanTable[source]

return a PanTable representation of self

to_str_array(fancy_table: bool = False) np.ndarray[np.str_][source]

construct a table with both content and ica together

class pantable.ast.PanTableOption(short_caption: str = '', caption: str = '', alignment: str = '', alignment_cells: str = '', width: typing.Optional[typing.List[typing.Union[float, str]]] = None, table_width: typing.Optional[float] = None, header: bool = True, ms: typing.Optional[typing.List[int]] = None, ns_head: typing.Optional[typing.List[int]] = None, markdown: bool = False, fancy_table: bool = False, include: str = '', include_encoding: str = '', format: str = 'csv', csv_kwargs: dict = <factory>)[source]

Bases: object

options in CodeBlock table

remember that the keys in YAML sometimes uses hyphen/underscore and here uses underscore

alignment: str = ''
alignment_cells: str = ''
caption: str = ''
csv_kwargs: dict
fancy_table: bool = False
format: str = 'csv'
classmethod from_kwargs(**kwargs) pantable.ast.PanTableOption[source]
header: bool = True
include: str = ''
include_encoding: str = ''
property kwargs: dict

to dict without the defaults

expect self.from_kwargs(**self.kwargs) == self

markdown: bool = False
ms: Optional[List[int]] = None
normalize(shape: Tuple[int, int])[source]

assume the types are correct. Normalize what’s beyond type-correctness.

e.g. from PanCodeBlock to PanTableStr should uses this

ns_head: Optional[List[int]] = None
short_caption: str = ''
simplify()[source]

Reduced equivalent attrs to simplest form

e.g. from PanTableStr to PanCodeBlock should uses this

table_width: Optional[float] = None
to_spec(size: int) pantable.ast.Spec[source]

to Spec

assume normalized self.

width: Optional[List[Union[float, str]]] = None
class pantable.ast.PanTableStr(cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]], caption: str = '', icas_rowblock: Optional[np.ndarray[np.str_]] = None, icas_row: Optional[np.ndarray[np.str_]] = None, icas: Optional[np.ndarray[np.str_]] = None, short_caption: Optional[str] = None, ica_table: Ica = <factory>, spec: Optional[Spec] = None, aligns: Optional[Align] = None, ms: Optional[np.ndarray[np.int64]] = None, ns_head: Optional[np.ndarray[np.int64]] = None, table_width: Optional[float] = None)[source]

Bases: pantable.ast.PanTableAbstract

similar to PanTable, but with panflute ASTs as str

TableArray should have content type as str although not strictly enforced here

TODO: check that icas* are always empty and remove them

TODO: implement auto_width

auto_width(override_width: bool = False, cell_width_func: Optional[Callable[[str], int]] = <function cell_width_func>)[source]

calculate column widths

assume a normalized table

caption: str = ''
icas: Optional[np.ndarray[np.str_]] = None
icas_row: Optional[np.ndarray[np.str_]] = None
icas_rowblock: Optional[np.ndarray[np.str_]] = None
short_caption: Optional[str] = None
table_width: Optional[float] = None
to_pancodeblock(format: str = 'csv', include: str = '', csv_kwargs: Optional[dict] = None) pantable.ast.PanCodeBlock[source]

to PanCodeBlock object

This is lossy as there’s no way to encode the geometries of self.cells in PanCodeBlock. Use PanTableMarkdown instead if you want to preserve that info.

to_pantable() pantable.ast.PanTable[source]

return a PanTable representation of self

to_pantableoption(format: str = 'csv', fancy_table: bool = False, include: str = '', csv_kwargs: Optional[dict] = None) pantable.ast.PanTableOption[source]
class pantable.ast.PanTableText(cells: Union[TableArray, np.ndarray[Union[ListContainer, str]]], caption: str = '', short_caption: Optional[str] = None, ica_table: Ica = <factory>, spec: Optional[Spec] = None, aligns: Optional[Align] = None, ms: Optional[np.ndarray[np.int64]] = None, ns_head: Optional[np.ndarray[np.int64]] = None, table_width: Optional[float] = None)[source]

Bases: pantable.ast.PanTableStr

a quick and dirty PanTableStr without Ica

Except for ica_table, If you try to access icas* and any methods that use them, it will errs.

icas: ClassVar = None
icas_row: ClassVar = None
icas_rowblock: ClassVar = None
class pantable.ast.Spec(aligns: Align, col_widths: Optional[np.ndarray[np.float64]] = None)[source]

Bases: object

a class of spec of PanTable

aligns: Align
col_widths: Optional[np.ndarray[np.float64]] = None
classmethod default(n_col: int = 1) pantable.ast.Spec[source]
classmethod from_panflute_ast(table: panflute.table_elements.Table) pantable.ast.Spec[source]
property size: int
to_panflute_ast() List[Tuple][source]
class pantable.ast.TableArray(contents: 'np.ndarray[Union[ListContainer, str]]', geometries: 'Optional[np.ndarray[np.int64]]' = None)[source]

Bases: object

property cannonical: pantable.ast.TableArray

return a cell array where spanned cells appeared in cannonical location only

top-left corner of the grid is the cannonical location of a spanned cell

contents: np.ndarray[Union[ListContainer, str]]
classmethod default(shape: Tuple[int, int], has_geometries=False) TableArray[source]
geometries: Optional[np.ndarray[np.int64]] = None
is_at(i: int, j: int) bool[source]
is_block(i: int, j: int) bool[source]
put(content: Union[panflute.containers.ListContainer, str], row_span: int, col_span: int, i: int, j: int, overwrite: bool = False)[source]

put content in self

property shape: Tuple[int, int]
shape_at(i: int, j: int) Tuple[int, int][source]
stringified(width: int = 15, cannonical=True) pantable.ast.TableArray[source]

return stringified TableArray

Parameters

width (int) – width per column

pantable.ast.cell_width_func(string: str, offset: int = 3) int[source]

return max no. of characters +3 among lines in the cell

The +3 match the way pandoc handle width, see jgm/pandoc commit 0dfceda

pantable.ast.single_para_to_plain(elem: panflute.containers.ListContainer) panflute.containers.ListContainer[source]

convert single element to Plain

if elem is a ListContainer of a single Para, then convert it to a ListContainer of Plain and return that. Else return elem.