def test(): array = awkward1.Array([{"x": "one"}, {"x": "two"}, {"x": "three"}]) assert awkward1.tolist(array) == [{ "x": "one" }, { "x": "two" }, { "x": "three" }] assert awkward1.tolist(awkward1.fromiter(awkward1.tolist(array))) == [{ "x": "one" }, { "x": "two" }, { "x": "three" }] assert awkward1.tolist(array.layout) == [{ "x": "one" }, { "x": "two" }, { "x": "three" }] assert awkward1.tolist(awkward1.fromiter(awkward1.tolist( array.layout))) == [{ "x": "one" }, { "x": "two" }, { "x": "three" }]
def test_fromiter(): builder = awkward1.layout.FillableArray() 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 awkward1.tolist(builder.snapshot()) == [ 0, 1, 2, [], [1.1], [1.1, 2.2], [1.1, 2.2, 3.3] ] assert awkward1.tolist( awkward1.fromiter( [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 awkward1.tolist( awkward1.fromiter([ 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 awkward1.tolist( awkward1.fromjson( '[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 awkward1.tojson( awkward1.fromjson( '[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"]'
def test_fromiter_fromjson(): assert awkward1.tolist(awkward1.fromiter(["one", "two", "three" ])) == ["one", "two", "three"] assert awkward1.tolist( awkward1.fromiter([["one", "two", "three"], [], ["four", "five"]])) == [["one", "two", "three"], [], ["four", "five"]] assert awkward1.tolist(awkward1.fromjson('["one", "two", "three"]')) == [ "one", "two", "three" ] assert awkward1.tolist( awkward1.fromjson( '[["one", "two", "three"], [], ["four", "five"]]')) == [[ "one", "two", "three" ], [], ["four", "five"]]
def test_fromiter(): dataset = [ [(1, 1.1), (2, 2.2), (3, 3.3)], [(1, [1.1, 2.2, 3.3]), (2, []), (3, [4.4, 5.5])], [[(1, 1.1), (2, 2.2), (3, 3.3)], [], [(4, 4.4), (5, 5.5)]], [((1, 1), 1.1), ((2, 2), 2.2), ((3, 3), 3.3)], [({"x": 1, "y": 1}, 1.1), ({"x": 2, "y": 2}, 2.2), ({"x": 3, "y": 3}, 3.3)], [{"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}], [{"one": (1, 1), "two": 1.1}, {"one": (2, 2), "two": 2.2}, {"one": (3, 3), "two": 3.3}], ] for datum in dataset: assert awkward1.tolist(awkward1.fromiter(datum)) == datum
sizepyobj0 = len(pyobj0) FRAC = 1 REPS = 2 starttime = time.time() for i in range(REPS): q = awkward.fromiter(pyobj0) walltime = (time.time() - starttime) * FRAC / REPS print("awkward.fromiter(pyobj0)\t", walltime, "sec;\t", sizepyobj0 / walltime / 1e6, "million floats/sec") FRAC = 1 REPS = 2 starttime = time.time() for i in range(REPS): q = awkward1.fromiter(pyobj0, initial=sizepyobj0 + 1) walltime = (time.time() - starttime) * FRAC / REPS print("awkward1.fromiter(pyobj0)\t", walltime, "sec;\t", sizepyobj0 / walltime / 1e6, "million floats/sec") pyobj1 = awkward1.tolist(array1.content.content[:200000]) # 200000 takes 1 sec sizepyobj1 = sum(len(x) for x in pyobj1) FRAC = 1 REPS = 2 starttime = time.time() for i in range(REPS): q = awkward.fromiter(pyobj1) walltime = (time.time() - starttime) * FRAC / REPS print("awkward.fromiter(pyobj1)\t", walltime, "sec;\t", sizepyobj1 / walltime / 1e6, "million floats/sec")