def test_json_data_to_pdtable():
    """ ensure dict-obj to pdtable conversion
        compare to target created w. make_table(List[List]])
    """
    # Make a table using the cell grid parser
    lines_target = [
        ["**farm_types1"],
        ["your_farm my_farm farms_galore"],
        ["species", "num", "flt", "log"],
        ["text", "-", "kg", "onoff"],
        ["chicken", 2, 3, 1],
        ["pig", 4, 39, 0],
        ["goat", 4, None, 1],
        ["zybra", 4, None, 0],
        ["cow", None, 200, 1],
        ["goose", 2, 9, 0],
    ]

    table_from_cell_grid = make_table(lines_target, fixer=custom_test_fixer)

    # Make an identical table, but starting from JSON

    # fmt: off
    table_json_data = {
        "name": "farm_types1",
        "columns": {
            "species": {
                "unit": "text",
                "values": ["chicken", "pig", "goat", "zybra", "cow", "goose"]
            },
            "num": {
                "unit": "-",
                "values": [2.0, 4.0, 4.0, 4.0, None, 2.0]
            },
            "flt": {
                "unit": "kg",
                "values": [3.0, 39.0, None, None, 200.0, 9.0]
            },
            "log": {
                "unit": "onoff",
                "values": [True, False, True, False, True, False]
            }
        },
        "destinations": {
            "your_farm": None,
            "my_farm": None,
            "farms_galore": None
        }
    }
    # fmt: on

    table_from_json = json_data_to_table(table_json_data,
                                         fixer=custom_test_fixer)
    assert table_from_cell_grid.equals(table_from_json)

    # Round trip
    table_json_data_back = table_to_json_data(table_from_json)
    table_from_json_round_trip = json_data_to_table(table_json_data_back,
                                                    fixer=custom_test_fixer)
    assert table_from_cell_grid.equals(table_from_json_round_trip)
def test_preserve_column_order():
    """ Unit test
        Verify that column order is preserved when translating btw. jsondata
        and pdtable.Table
    """
    # fmt: off
    lines_target = [
        ["**col_order"],
        ["dst2 dst2 dst2"],
        ["species", "a3", "a2", "a1", "a4"],
        ["text", "-", "-", "-", "-"],
        ["chicken", 1, 2, 3, 4],
        ["pig", 1, 2, 3, 4],
        ["goat", 1, 2, 3, 4],
        ["zybra", 1, 2, 3, 4],
        ["cow", 1, 2, 3, 4],
        ["goose", 1, 2, 3, 4],
    ]
    # fmt: on

    pandas_pdtab = make_table(lines_target)
    js_obj = table_to_json_data(pandas_pdtab)
    pdtab = json_data_to_table(js_obj)
    assert pdtab.df.iloc[0][3] == 3
    assert pdtab.df.iloc[1][1] == 1
    assert pdtab.df.iloc[2][4] == 4
    assert pandas_pdtab.equals(pdtab)

    # now verify from json-string
    jstr = json.dumps(js_obj)
    js_obj_from_json = json.loads(jstr)
    pdtab_from_json = json_data_to_table(js_obj_from_json)

    assert pandas_pdtab.equals(pdtab_from_json)
def test_converter():
    """ Unit test
        Verify that misc. float input are handled consistently
    """
    # fmt: off
    table_lines = [
        ["**flt_errors"],
        ["dst1"],
        [ "a1"  , "a2"  , "a3"  , "a4"  ],
        [ "-"   , "-"   , "-"   , "-"   ],
        [ "NaN" , "nan" , "Nine", "Ten" ],
        [ 1     , 2     , 3     , 3.14  ],
    ]
    # fmt: on
    cf = custom_test_fixer()
    pandas_pdtab = make_table(table_lines, fixer=cf)
    js_obj = table_to_json_data(pandas_pdtab)
    assert js_obj["columns"]["a3"]["values"][0] is None
    assert js_obj["columns"]["a4"]["values"][1] == 3.14

    assert cf.fixes == 2  # Nine and Ten
Beispiel #4
0
import json

table_json = json.dumps(json_ready_tables[0])
print(table_json)

# %% [markdown]
# There are also utilities to convert back and forth between `JsonData` and `Table`.

# %%
from pdtable.io import json_data_to_table, table_to_json_data

t = json_data_to_table(json_ready_tables[0])
t  # It's now a Table

# %%
table_to_json_data(t)  # Now it's back to JsonData

# %% [markdown]
#

# %% [markdown]
# ### Handling directives
# StarTable Directive blocks are intended to be interpreted and handled at read time, and then discarded. The client code (i.e. you) is responsible for doing this. Handling directives is done outside of the `pdtable` framework. This would typically done by a function that consumes a `BlockGenerator` (the output of the reader functions), processes the directive blocks encountered therein, and in turn yields a processed `BlockGenerator`, as in:
#
# ```
# def handle_some_directive(bg: BlockGenerator, *args, **kwargs) -> BlockGenerator:
#     ...
# ```
#
# Usage would then be:
# ```