Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.82 KB

python.rst

File metadata and controls

46 lines (37 loc) · 1.82 KB

Type Hint: Python Code

You can specify type hints to a writer via :py~AbstractTableWriter.type_hints.

Sample Code
from datetime import datetime
import pytablewriter as ptw

writer = ptw.PythonCodeTableWriter()
writer.value_matrix = [
    [-1.1, float("inf"), "2017-01-02 03:04:05", datetime(2017, 1, 2, 3, 4, 5)],
    [0.12, float("nan"), "2017-02-03 04:05:06", datetime(2017, 2, 3, 4, 5, 6)],
]

# column data types detected automatically by default
writer.table_name = "python variable without type hints"
writer.headers = ["float", "infnan", "string", "datetime"]
writer.write_table()

# set type hints
writer.table_name = "python variable with type hints"
writer.headers = ["hint_int", "hint_str", "hint_datetime", "hint_str"]
writer.type_hints = [ptw.Integer, ptw.String, ptw.DateTime, ptw.String]
writer.write_table()
Output
python_variable_without_type_hints = [
    ["float", "infnan", "string", "datetime"],
    [-1.1, float("inf"), "2017-01-02 03:04:05", dateutil.parser.parse("2017-01-02T03:04:05")],
    [0.12, float("nan"), "2017-02-03 04:05:06", dateutil.parser.parse("2017-02-03T04:05:06")],
]

python_variable_with_type_hints = [
    ["hint_int", "hint_str", "hint_datetime", "hint_str"],
    [-1, "inf", dateutil.parser.parse("2017-01-02T03:04:05"), "2017-01-02 03:04:05"],
    [0, "nan", dateutil.parser.parse("2017-02-03T04:05:06"), "2017-02-03 04:05:06"],
]