예제 #1
0
def test_unknown():
    a = ak.from_json("[[], [], []]", highlevel=False)
    assert ak.to_list(a) == [[], [], []]
    assert str(ak.type(a)) == "var * unknown"
    assert ak.type(a) == ak.types.ListType(ak.types.UnknownType())
    assert not ak.type(a) == ak.types.PrimitiveType("float64")

    a = ak.from_json("[[], [[], []], [[], [], []]]", highlevel=False)
    assert ak.to_list(a) == [[], [[], []], [[], [], []]]
    assert str(ak.type(a)) == "var * var * unknown"
    assert ak.type(a) == ak.types.ListType(
        ak.types.ListType(ak.types.UnknownType()))

    a = ak.layout.ArrayBuilder()
    a.beginlist()
    a.endlist()
    a.beginlist()
    a.endlist()
    a.beginlist()
    a.endlist()
    assert ak.to_list(a) == [[], [], []]
    assert str(ak.type(a)) == "var * unknown"
    assert ak.type(a) == ak.types.ListType(ak.types.UnknownType())
    assert not ak.type(a) == ak.types.PrimitiveType("float64")

    a = a.snapshot()
    assert ak.to_list(a) == [[], [], []]
    assert str(ak.type(a)) == "var * unknown"
    assert ak.type(a) == ak.types.ListType(ak.types.UnknownType())
    assert not ak.type(a) == ak.types.PrimitiveType("float64")
예제 #2
0
def test_getitem():
    a = ak.from_json("[]")
    a = ak.from_json("[[], [[], []], [[], [], []]]")
    assert ak.to_list(a[2]) == [[], [], []]

    assert ak.to_list(a[2, 1]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1, 0]
    assert " attempting to get 0, index out of range" in str(excinfo.value)
    assert ak.to_list(a[2, 1][()]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][0]
    assert " attempting to get 0, index out of range" in str(excinfo.value)
    assert ak.to_list(a[2, 1][100:200]) == []
    assert ak.to_list(a[2, 1, 100:200]) == []
    assert ak.to_list(a[2, 1][np.array([], dtype=int)]) == []
    assert ak.to_list(a[2, 1, np.array([], dtype=int)]) == []
    with pytest.raises(ValueError) as excinfo:
        a[2, 1, np.array([0], dtype=int)]
    assert " attempting to get 0, index out of range" in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, 0]
    assert ", too many dimensions in slice" in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, 200:300]
    assert ", too many dimensions in slice" in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        a[2, 1][100:200, np.array([], dtype=int)]
    assert ", too many dimensions in slice" in str(excinfo.value)

    assert ak.to_list(a[1:, 1:]) == [[[]], [[], []]]
    with pytest.raises(ValueError) as excinfo:
        a[1:, 1:, 0]
    assert " attempting to get 0, index out of range" in str(excinfo.value)
def test_blanc_lines():
    str = """{"one": 1, "two": 2.2}

    {"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{"one": 1, "two": 2.2}, {"one": 10, "two": 22.0}]

    str = """{"one": 1, "two": 2.2}

    {"one": 10, "two": 22}
    """
    array = ak.from_json(str)
    assert ak.to_list(array) == [{"one": 1, "two": 2.2}, {"one": 10, "two": 22.0}]

    str = """ 1
    2

    3   """
    array = ak.from_json(str)
    assert ak.to_list(array) == [1, 2, 3]

    str = """
        1
        2

        3
        """
    array = ak.from_json(str)
    assert ak.to_list(array) == [1, 2, 3]
def test():
    url = "https://raw.githubusercontent.com/Chicago/osd-bike-routes/5f556dc/data/Bikeroutes.geojson"
    try:
        bikeroutes_json = urllib.request.urlopen(url).read()
    except urllib.error.URLError:
        pytest.skip(msg="couldn't download sample dataset")

    # This shouldn't fail (see #1084)
    ak.from_json(bikeroutes_json)
def test_fromfile(tmp_path):
    with open(os.path.join(str(tmp_path), "tmp1.json"), "w") as f:
        f.write("[[1.1, 2.2, 3], [], [4, 5.5]]")

    a = ak.from_json(os.path.join(str(tmp_path), "tmp1.json"))
    assert ak.to_list(a) == [[1.1, 2.2, 3.0], [], [4.0, 5.5]]

    with pytest.raises(ValueError):
        ak.from_json("nonexistent.json")

    with open(os.path.join(str(tmp_path), "tmp2.json"), "w") as f:
        f.write("[[1.1, 2.2, 3], []], [4, 5.5]]")

    with pytest.raises(ValueError):
        ak.from_json(os.path.join(str(tmp_path), "tmp2.json"))
def test_empty_array_slice():
    # inspired by PR021::test_getitem
    a = ak.from_json("[[], [[], []], [[], [], []]]")
    assert ak.to_list(a[2, 1, np.array([], dtype=int)]) == []
    assert ak.to_list(a[2,
                        np.array([1], dtype=int),
                        np.array([], dtype=int)]) == []

    # inspired by PR015::test_deep_numpy
    content = ak.layout.NumpyArray(
        np.array([[0.0, 1.1], [2.2, 3.3], [4.4, 5.5], [6.6, 7.7], [8.8, 9.9]]))
    listarray = ak.layout.ListArray64(
        ak.layout.Index64(np.array([0, 3, 3])),
        ak.layout.Index64(np.array([3, 3, 5])),
        content,
    )
    assert ak.to_list(listarray[[2, 0, 0, -1], [1, -1, 0, 0],
                                [0, 1, 0, 1]]) == [
                                    8.8,
                                    5.5,
                                    0.0,
                                    7.7,
                                ]
    assert ak.to_list(listarray[2, 1, np.array([], dtype=int)]) == []
    assert ak.to_list(listarray[2, 1, []]) == []
    assert ak.to_list(listarray[2, [1], []]) == []
    assert ak.to_list(listarray[2, [], []]) == []
def test_tostring():
    content = ak.layout.NumpyArray(np.arange(2 * 3 * 5 * 7).reshape(-1, 7))
    offsetsA = np.arange(0, 2 * 3 * 5 + 5, 5)
    offsetsB = np.arange(0, 2 * 3 + 3, 3)
    startsA, stopsA = offsetsA[:-1], offsetsA[1:]
    startsB, stopsB = offsetsB[:-1], offsetsB[1:]

    listoffsetarrayA32 = ak.layout.ListOffsetArray32(
        ak.layout.Index32(offsetsA), content)
    listarrayA32 = ak.layout.ListArray32(ak.layout.Index32(startsA),
                                         ak.layout.Index32(stopsA), content)
    modelA = np.arange(2 * 3 * 5 * 7).reshape(2 * 3, 5, 7)

    listoffsetarrayB32 = ak.layout.ListOffsetArray32(
        ak.layout.Index32(offsetsB), listoffsetarrayA32)
    listarrayB32 = ak.layout.ListArray32(ak.layout.Index32(startsB),
                                         ak.layout.Index32(stopsB),
                                         listarrayA32)
    modelB = np.arange(2 * 3 * 5 * 7).reshape(2, 3, 5, 7)

    assert content.tojson() == json.dumps(ak.to_list(content),
                                          separators=(",", ":"))
    assert listoffsetarrayA32.tojson() == json.dumps(modelA.tolist(),
                                                     separators=(",", ":"))
    assert listoffsetarrayB32.tojson() == json.dumps(modelB.tolist(),
                                                     separators=(",", ":"))
    ak.to_json(
        ak.from_json("[[1.1,2.2,3],[],[4,5.5]]")) == "[[1.1,2.2,3],[],[4,5.5]]"
def test_tofile(tmp_path):
    ak.to_json(
        ak.from_json("[[1.1,2.2,3],[],[4,5.5]]"),
        os.path.join(str(tmp_path), "tmp1.json"),
    )

    with open(os.path.join(str(tmp_path), "tmp1.json"), "r") as f:
        f.read() == "[[1.1,2.2,3],[],[4,5.5]]"
예제 #9
0
def test_fromiter_fromjson():
    assert ak.to_list(ak.from_iter(["one", "two",
                                    "three"])) == ["one", "two", "three"]
    assert ak.to_list(
        ak.from_iter([["one", "two", "three"], [],
                      ["four", "five"]])) == [["one", "two", "three"], [],
                                              ["four", "five"]]

    assert ak.to_list(ak.from_json('["one", "two", "three"]')) == [
        "one",
        "two",
        "three",
    ]
    assert ak.to_list(
        ak.from_json('[["one", "two", "three"], [], ["four", "five"]]')) == [[
            "one", "two", "three"
        ], [], ["four", "five"]]
예제 #10
0
def test_json():
    dataset = [
        '[{"one":1,"two":1.1},{"one":2,"two":2.2},{"one":3,"two":3.3}]',
        '[{"one":1,"two":[1.1,2.2,3.3]},{"one":2,"two":[]},{"one":3,"two":[4.4,5.5]}]',
        '[[{"one":1,"two":1.1},{"one":2,"two":2.2},{"one":3,"two":3.3}],[],[{"one":4,"two":4.4},{"one":5,"two":5.5}]]',
        '[{"one":{"x":1,"y":1},"two":1.1},{"one":{"x":2,"y":2},"two":2.2},{"one":{"x":3,"y":3},"two":3.3}]',
    ]
    for datum in dataset:
        assert ak.to_json(ak.from_json(datum)) == datum
예제 #11
0
def test_jpivarski():
    assert ak.from_json('{"x": 1, "y": [1, 2, 3]}').tolist() == {
        "x": 1,
        "y": [1, 2, 3]
    }
    assert ak.from_json(
        '{"x": 1, "y": [1, 2, 3]} {"x": 2, "y": []}').tolist() == [
            {
                "x": 1,
                "y": [1, 2, 3]
            },
            {
                "x": 2,
                "y": []
            },
        ]
    assert ak.from_json('{"x": 1, "y": [1, 2, 3]} 123').tolist() == [
        {
            "x": 1,
            "y": [1, 2, 3]
        },
        123,
    ]
    assert ak.from_json(
        '{"x": 1, "y": [1, 2, 3]} [1, 2, 3, 4, 5]').tolist() == [
            {
                "x": 1,
                "y": [1, 2, 3]
            },
            [1, 2, 3, 4, 5],
        ]
    assert ak.from_json("123") == 123
    assert ak.from_json("123 456").tolist() == [123, 456]
    assert ak.from_json('123 {"x": 1, "y": [1, 2, 3]}').tolist() == [
        123,
        {
            "x": 1,
            "y": [1, 2, 3]
        },
    ]
    assert ak.from_json("null") is None
    assert ak.from_json("null 123").tolist() == [None, 123]
    assert ak.from_json("123 null").tolist() == [123, None]
def test_fromstring():
    # read multiple json fragments from a string
    str = """{"x": 1.1, "y": []}
             {"x": 2.2, "y": [1]}
             {"x": 3.3, "y": [1, 2]}
             {"x": 4.4, "y": [1, 2, 3]}
             {"x": 5.5, "y": [1, 2, 3, 4]}
             {"x": 6.6, "y": [1, 2, 3, 4, 5]}"""

    array = ak.from_json(str)
    assert ak.to_list(array) == [
        {"x": 1.1, "y": []},
        {"x": 2.2, "y": [1]},
        {"x": 3.3, "y": [1, 2]},
        {"x": 4.4, "y": [1, 2, 3]},
        {"x": 5.5, "y": [1, 2, 3, 4]},
        {"x": 6.6, "y": [1, 2, 3, 4, 5]},
    ]
예제 #13
0
def test_tostring():
    # write a json string from an array built from
    # multiple json fragments from a string
    str = """{"x": 1.1, "y": []}
             {"x": 2.2, "y": [1]}
             {"x": 3.3, "y": [1, 2]}
             {"x": 4.4, "y": [1, 2, 3]}
             {"x": 5.5, "y": [1, 2, 3, 4]}
             {"x": 6.6, "y": [1, 2, 3, 4, 5]}"""

    array = ak.from_json(str)
    assert ak.to_list(array) == [
        {
            "x": 1.1,
            "y": []
        },
        {
            "x": 2.2,
            "y": [1]
        },
        {
            "x": 3.3,
            "y": [1, 2]
        },
        {
            "x": 4.4,
            "y": [1, 2, 3]
        },
        {
            "x": 5.5,
            "y": [1, 2, 3, 4]
        },
        {
            "x": 6.6,
            "y": [1, 2, 3, 4, 5]
        },
    ]

    assert (
        ak.to_json(array) ==
        '[{"x":1.1,"y":[]},{"x":2.2,"y":[1]},{"x":3.3,"y":[1,2]},{"x":4.4,"y":[1,2,3]},{"x":5.5,"y":[1,2,3,4]},{"x":6.6,"y":[1,2,3,4,5]}]'
    )
def test_from_json():
    array = ak.from_json('[{"r": 1.1, "i": 1.0}, {"r": 2.2, "i": 2.0}]')
    assert array.tolist() == [
        {
            "r": 1.1,
            "i": 1.0
        },
        {
            "r": 2.2,
            "i": 2.0
        },
    ]
    array = ak.from_json('[{"r": 1.1, "i": 1.0}, {"r": 2.2, "i": 2.0}]',
                         complex_record_fields=("r", "i"))
    assert array.tolist() == [(1.1 + 1j), (2.2 + 2j)]

    # Somewhere in from_json, a handler that turns integer record fields into
    # parts of a complex number is missing.
    array = ak.from_json('[{"r": 1, "i": 1}, {"r": 2, "i": 2}]',
                         complex_record_fields=("r", "i"))
    assert array.tolist() == [(1 + 1j), (2 + 2j)]

    # This should fail with some message like "complex number fields must be numbers,"
    # not "called 'end_record' without 'begin_record' at the same level before it."
    with pytest.raises(ValueError) as err:
        array = ak.from_json(
            '[{"r": [], "i": 1}, {"r": [1, 2], "i": 2}]',
            complex_record_fields=("r", "i"),
        )
        assert array["r"].type == array["i"].type
    assert "Complex number fields must be numbers" in str(err)

    # These shouldn't be recognized as complex number records because they have
    # only one of the two fields.
    assert ak.from_json('[{"r": 1}, {"r": 2}]',
                        complex_record_fields=("r", "i")).tolist() == [{
                            "r": 1
                        }, {
                            "r": 2
                        }]
    assert ak.from_json('[{"i": 1}, {"i": 2}]',
                        complex_record_fields=("r", "i")).tolist() == [{
                            "i": 1
                        }, {
                            "i": 2
                        }]
    assert ak.from_json('[{"r": 1.1}, {"r": 2.2}]',
                        complex_record_fields=("r", "i")).tolist() == [{
                            "r": 1.1
                        }, {
                            "r": 2.2
                        }]
    assert ak.from_json('[{"i": 1.1}, {"i": 2.2}]',
                        complex_record_fields=("r", "i")).tolist() == [{
                            "i": 1.1
                        }, {
                            "i": 2.2
                        }]

    # In this one, the extra field should simply be ignored. A record with *at least*
    # the two specified fields should be recognized as a complex number, so that
    # the protocol can include a type marker, as some protocols do.
    array = ak.from_json(
        '[{"r": 1.1, "i": 1.0, "another": []}, {"r": 2.2, "i": 2.0, "another": [1, 2, 3]}]',
        complex_record_fields=("r", "i"),
    )
    assert array.tolist() == [(1.1 + 1j), (2.2 + 2j)]
예제 #15
0
def test_lazy_arrayset():
    array = ak.from_json(
        """
    [
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 3},
            "masked": null
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": [{"item1": 2}],
            "masked": 4
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 4},
            "masked": 4
        }
    ]"""
    )

    canary = Canary()
    prefix = "kitty"
    form, container, npart = ak.to_arrayset(array, container=canary, prefix=prefix)
    assert not any(op[0] == "get" for op in canary.ops)
    canary.ops = []

    cache = {}
    out = ak.from_arrayset(
        form,
        container,
        lazy=True,
        lazy_cache=cache,
        lazy_lengths=3,
        prefix=prefix,
        lazy_cache_key="hello",
    )
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert len(out) == 3
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert ak.to_list(ak.num(out.listcollection)) == [3, 3, 3]
    assert set(canary.ops) == {("get", "kitty-node1-offsets")}
    assert set(cache) == {"hello", "hello-kitty-node1-virtual"}
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.unioncollection) == [
        {"item1": 3},
        [{"item1": 2}],
        {"item1": 4},
    ]
    assert set(canary.ops) == {
        ("get", "kitty-node11-tags"),
        ("get", "kitty-node11-index"),
        ("get", "kitty-node14-offsets"),
        ("get", "kitty-node13"),
        ("get", "kitty-node16"),
    }
    assert set(cache) == {
        "hello",
        "hello-kitty-node11-virtual",
        "hello-kitty-node13-virtual",
        "hello-kitty-node16-virtual",
    }
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.masked) == [None, 4, 4]
    assert set(canary.ops) == {("get", "kitty-node17-index"), ("get", "kitty-node18")}
    assert set(cache) == {"hello", "hello-kitty-node17-virtual"}
    canary.ops = []
    cache.clear()
예제 #16
0
def test_unfinished_fragment_exception():
    # read unfinished json fragments
    strs0 = """{"one": 1, "two": 2.2,"""
    with pytest.raises(ValueError):
        ak.from_json(strs0)

    strs1 = """{"one": 1,
        "two": 2.2,"""
    with pytest.raises(ValueError):
        ak.from_json(strs1)

    strs2 = """{"one": 1,
        "two": 2.2,
        """
    with pytest.raises(ValueError):
        ak.from_json(strs2)

    strs3 = """{"one": 1, "two": 2.2, "three": "THREE"}
        {"one": 10, "two": 22,"""
    with pytest.raises(ValueError):
        ak.from_json(strs3)

    strs4 = """{"one": 1, "two": 2.2, "three": "THREE"}
        {"one": 10, "two": 22,
        """
    with pytest.raises(ValueError):
        ak.from_json(strs4)

    strs5 = """["one", "two","""
    with pytest.raises(ValueError):
        ak.from_json(strs5)

    strs6 = """["one",
        "two","""
    with pytest.raises(ValueError):
        ak.from_json(strs6)

    strs7 = """["one",
        "two",
        """
    with pytest.raises(ValueError):
        ak.from_json(strs7)
def test_fromstring():
    a = ak.from_json("[[1.1, 2.2, 3], [], [4, 5.5]]")
    assert ak.to_list(a) == [[1.1, 2.2, 3.0], [], [4.0, 5.5]]

    with pytest.raises(ValueError):
        ak.from_json("[[1.1, 2.2, 3], [blah], [4, 5.5]]")
def test_lazy_buffers():
    array = ak.from_json("""
    [
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 3},
            "masked": null
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": [{"item1": 2}],
            "masked": 4
        },
        {
            "listcollection": [
                {"item1": 1, "item2": 2},
                {"item1": 2, "item2": 4},
                {"item1": 3, "item2": 6}
            ],
            "collection": {"item1": 3, "item2": 4},
            "singleton": 5,
            "listsingleton": [1, 2, 3],
            "unioncollection": {"item1": 4},
            "masked": 4
        }
    ]""")

    canary = Canary()
    key_format = "kitty-{form_key}-{attribute}"
    form, length, container = ak.to_buffers(array,
                                            container=canary,
                                            key_format=key_format)
    assert not any(op[0] == "get" for op in canary.ops)
    canary.ops = []

    cache = {}
    out = ak.from_buffers(
        form,
        length,
        container,
        key_format=key_format,
        lazy=True,
        lazy_cache=cache,
        lazy_cache_key="hello",
    )
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert len(out) == 3
    assert len(canary.ops) == 0
    assert len(cache) == 0

    assert ak.to_list(ak.num(out.listcollection)) == [3, 3, 3]
    assert set(canary.ops) == {("get", "kitty-node1-offsets")}
    assert "hello" in cache
    assert "hello(kitty-node1-virtual)" in cache
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.unioncollection) == [
        {
            "item1": 3
        },
        [{
            "item1": 2
        }],
        {
            "item1": 4
        },
    ]
    assert set(canary.ops) == {
        ("get", "kitty-node11-tags"),
        ("get", "kitty-node11-index"),
        ("get", "kitty-node14-offsets"),
        ("get", "kitty-node13-data"),
        ("get", "kitty-node16-data"),
    }
    assert "hello" in cache
    assert "hello(kitty-node11-virtual)" in cache
    assert "hello(kitty-node13-virtual)" in cache
    assert "hello(kitty-node16-virtual)" in cache
    canary.ops = []
    cache.clear()

    assert ak.to_list(out.masked) == [None, 4, 4]
    assert set(canary.ops) == {
        ("get", "kitty-node17-index"),
        ("get", "kitty-node18-data"),
    }
    assert "hello" in cache
    assert "hello(kitty-node17-virtual)" in cache
    canary.ops = []
    cache.clear()
def test_fromiter():
    builder = ak.layout.ArrayBuilder()

    builder.integer(0)
    builder.integer(1)
    builder.integer(2)
    builder.beginlist()
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.real(2.2)
    builder.endlist()
    builder.beginlist()
    builder.real(1.1)
    builder.real(2.2)
    builder.real(3.3)
    builder.endlist()

    assert ak.to_list(builder.snapshot()) == [
        0,
        1,
        2,
        [],
        [1.1],
        [1.1, 2.2],
        [1.1, 2.2, 3.3],
    ]

    assert ak.to_list(
        ak.from_iter([0, 1, 2, [], [1.1], [1.1, 2.2], [1.1, 2.2, 3.3]
                      ])) == [0, 1, 2, [], [1.1], [1.1, 2.2], [1.1, 2.2, 3.3]]
    assert ak.to_list(
        ak.from_iter([
            0,
            1,
            2,
            [],
            "zero",
            [1.1],
            "one",
            [1.1, 2.2],
            "two",
            [1.1, 2.2, 3.3],
            "three",
        ])) == [
            0,
            1,
            2,
            [],
            "zero",
            [1.1],
            "one",
            [1.1, 2.2],
            "two",
            [1.1, 2.2, 3.3],
            "three",
        ]
    assert ak.to_list(
        ak.from_json(
            '[0, 1, 2, [], "zero", [1.1], "one", [1.1, 2.2], "two", [1.1, 2.2, 3.3], "three"]'
        )) == [
            0,
            1,
            2,
            [],
            "zero",
            [1.1],
            "one",
            [1.1, 2.2],
            "two",
            [1.1, 2.2, 3.3],
            "three",
        ]
    assert (ak.to_json(
        ak.from_json(
            '[0,1,2,[],"zero",[1.1],"one",[1.1,2.2],"two",[1.1,2.2,3.3],"three"]'
        )
    ) == '[0,1,2,[],"zero",[1.1],"one",[1.1,2.2],"two",[1.1,2.2,3.3],"three"]')
예제 #20
0
def test_two_arrays():

    str = """{"one": 1, "two": 2.2}{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}     {"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \t "two": 2.2}{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}  \t   {"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n\r{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}     \n     {"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}     \n\r     {"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}{"one": 10, "two": 22}\n"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}{"one": 10, "two": 22}\n\r"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n{"one": 10, "two": 22}\n"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n\r{"one": 10, "two": 22}\n\r"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n"two": 2.2}{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n\r"two": 2.2}{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n"two": 2.2}\n{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n\r"two": 2.2}\n\r{"one": 10, "two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n"two": 2.2}\n{"one": 10, "two": 22}\n"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, \n\r"two": 2.2}\n\r{"one": 10, "two": 22}\n\r"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}{"one": 10, \n"two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}{"one": 10, \n\r"two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n{"one": 10, \n"two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """{"one": 1, "two": 2.2}\n\r{"one": 10, \n\r"two": 22}"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [{
        "one": 1,
        "two": 2.2
    }, {
        "one": 10,
        "two": 22.0
    }]

    str = """["one", "two"]["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", "two"]\n["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", "two"]\n\r["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", "two"]     ["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", "two"]  \n   ["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", "two"]  \n\r   ["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", \n "two"]["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", \n "two"]\n["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", \n\r "two"]["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = """["one", \n\r "two"]\n\r["uno", "dos"]"""
    array = ak.from_json(str)
    assert ak.to_list(array) == [["one", "two"], ["uno", "dos"]]

    str = '"one""two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    str = '"one"\n"two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    str = '"one"\n\r"two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    str = '"one"     "two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    str = '"one"  \n   "two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    str = '"one"  \n\r   "two"'
    array = ak.from_json(str)
    assert ak.to_list(array) == ["one", "two"]

    array = ak.from_json(os.path.join(DIR, "samples/test-two-arrays.json"))
    assert ak.to_list(array) == [
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        {
            "one": 1,
            "two": 2.2
        },
        {
            "one": 10,
            "two": 22.0
        },
        ["one", "two"],
        ["uno", "dos"],
        ["one", "two"],
        ["uno", "dos"],
        ["one", "two"],
        ["uno", "dos"],
        ["one", "two"],
        ["uno", "dos"],
        ["one", "two"],
        ["uno", "dos"],
        ["one", "two"],
        ["uno", "dos"],
        "one",
        "two",
        "one",
        "two",
        "one",
        "two",
        "one",
        "two",
        "one",
        "two",
        "one",
        "two",
    ]
예제 #21
0
def test_three():
    array = ak.from_json('["one", \n"two"] \n ["three"]')
    assert ak.to_list(array) == [["one", "two"], ["three"]]
예제 #22
0
def test_fromfile():
    # read multiple json fragments from a json file
    array = ak.from_json(os.path.join(DIR, "samples/test-record-array.json"))
    assert ak.to_list(array) == [
        {
            "x": 1.1,
            "y": []
        },
        {
            "x": 2.2,
            "y": [1]
        },
        {
            "x": 3.3,
            "y": [1, 2]
        },
        {
            "x": 4.4,
            "y": [1, 2, 3]
        },
        {
            "x": 5.5,
            "y": [1, 2, 3, 4]
        },
        {
            "x": 6.6,
            "y": [1, 2, 3, 4, 5]
        },
    ]

    # read json file containg 'nan' and 'inf' user-defined strings
    # and replace 'nan' and 'inf' strings with floats
    array = ak.from_json(
        os.path.join(DIR, "samples/test.json"),
        infinity_string="inf",
        minus_infinity_string="-inf",
    )

    assert ak.to_list(array) == [
        1.1,
        2.2,
        3.3,
        float("inf"),
        float("-inf"),
        [4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1, 11.11],
        [12.12, 13.13, 14.14, 15.15, 16.16, 17.17],
        [
            [
                [18.18, 19.19, 20.2, 21.21, 22.22],
                [
                    23.23,
                    24.24,
                    25.25,
                    26.26,
                    27.27,
                    28.28,
                    29.29,
                    30.3,
                    31.31,
                    32.32,
                    33.33,
                    34.34,
                    35.35,
                    36.36,
                    37.37,
                ],
                [38.38],
                [39.39, 40.4, "NaN", "NaN", 41.41, 42.42, 43.43],
            ],
            [
                [44.44, 45.45, 46.46, 47.47, 48.48],
                [
                    49.49,
                    50.5,
                    51.51,
                    52.52,
                    53.53,
                    54.54,
                    55.55,
                    56.56,
                    57.57,
                    58.58,
                    59.59,
                    60.6,
                    61.61,
                    62.62,
                    63.63,
                ],
                [64.64],
                [65.65, 66.66, "NaN", "NaN", 67.67, 68.68, 69.69],
            ],
        ],
    ]

    # read json file containg 'nan' and 'inf' user-defined strings
    array = ak.from_json(os.path.join(DIR, "samples/test.json"))

    assert ak.to_list(array) == [
        1.1,
        2.2,
        3.3,
        "inf",
        "-inf",
        [4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1, 11.11],
        [12.12, 13.13, 14.14, 15.15, 16.16, 17.17],
        [
            [
                [18.18, 19.19, 20.2, 21.21, 22.22],
                [
                    23.23,
                    24.24,
                    25.25,
                    26.26,
                    27.27,
                    28.28,
                    29.29,
                    30.3,
                    31.31,
                    32.32,
                    33.33,
                    34.34,
                    35.35,
                    36.36,
                    37.37,
                ],
                [38.38],
                [39.39, 40.4, "NaN", "NaN", 41.41, 42.42, 43.43],
            ],
            [
                [44.44, 45.45, 46.46, 47.47, 48.48],
                [
                    49.49,
                    50.5,
                    51.51,
                    52.52,
                    53.53,
                    54.54,
                    55.55,
                    56.56,
                    57.57,
                    58.58,
                    59.59,
                    60.6,
                    61.61,
                    62.62,
                    63.63,
                ],
                [64.64],
                [65.65, 66.66, "NaN", "NaN", 67.67, 68.68, 69.69],
            ],
        ],
    ]

    # read json file containg 'nan' and 'inf' user-defined strings
    # and replace 'nan' and 'inf' strings with a predefined 'None' string
    array = ak.from_json(
        os.path.join(DIR, "samples/test.json"),
        infinity_string="inf",
        minus_infinity_string="-inf",
        nan_string="NaN",
    )

    def fix(obj):
        if isinstance(obj, list):
            return [fix(x) for x in obj]
        elif np.isnan(obj):
            return "COMPARE-NAN"
        else:
            return obj

    assert fix(ak.to_list(array)) == fix([
        1.1,
        2.2,
        3.3,
        float("inf"),
        float("-inf"),
        [4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1, 11.11],
        [12.12, 13.13, 14.14, 15.15, 16.16, 17.17],
        [
            [
                [18.18, 19.19, 20.2, 21.21, 22.22],
                [
                    23.23,
                    24.24,
                    25.25,
                    26.26,
                    27.27,
                    28.28,
                    29.29,
                    30.3,
                    31.31,
                    32.32,
                    33.33,
                    34.34,
                    35.35,
                    36.36,
                    37.37,
                ],
                [38.38],
                [39.39, 40.4,
                 float("nan"),
                 float("nan"), 41.41, 42.42, 43.43],
            ],
            [
                [44.44, 45.45, 46.46, 47.47, 48.48],
                [
                    49.49,
                    50.5,
                    51.51,
                    52.52,
                    53.53,
                    54.54,
                    55.55,
                    56.56,
                    57.57,
                    58.58,
                    59.59,
                    60.6,
                    61.61,
                    62.62,
                    63.63,
                ],
                [64.64],
                [
                    65.65, 66.66,
                    float("nan"),
                    float("nan"), 67.67, 68.68, 69.69
                ],
            ],
        ],
    ])

    # read json file containing multiple definitions of 'nan' and 'inf'
    # user-defined strings
    # replace can only work for one string definition
    array = ak.from_json(
        os.path.join(DIR, "samples/test-nan-inf.json"),
        infinity_string="Infinity",
        nan_string="None",
    )

    assert ak.to_list(array) == [
        1.1,
        2.2,
        3.3,
        "inf",
        "-inf",
        [4.4, float("inf"), 6.6, 7.7, 8.8, "NaN", 10.1, 11.11],
        [12.12, 13.13, 14.14, 15.15, 16.16, 17.17],
        [
            [
                [18.18, 19.19, 20.2, 21.21, 22.22],
                [
                    23.23,
                    24.24,
                    25.25,
                    26.26,
                    27.27,
                    28.28,
                    29.29,
                    30.3,
                    31.31,
                    32.32,
                    33.33,
                    34.34,
                    35.35,
                    36.36,
                    37.37,
                ],
                [38.38],
                [39.39, 40.4, "NaN", "NaN", 41.41, 42.42, 43.43],
            ],
            [
                [44.44, 45.45, 46.46, 47.47, 48.48],
                [
                    49.49,
                    50.5,
                    51.51,
                    52.52,
                    53.53,
                    54.54,
                    55.55,
                    56.56,
                    57.57,
                    58.58,
                    59.59,
                    60.6,
                    61.61,
                    62.62,
                    63.63,
                ],
                [64.64],
                [65.65, 66.66, "NaN", "NaN", 67.67, 68.68, 69.69],
            ],
        ],
    ]