File: test_0511_copy_and_deepcopy.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (94 lines) | stat: -rw-r--r-- 2,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import copy

import numpy as np
import pytest  # noqa: F401

import awkward as ak

to_list = ak.operations.to_list


def test():
    np_array = np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
    one = ak.Array(np_array)

    np_array[1] = 999
    assert to_list(one) == [0.0, 999, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]

    two = copy.copy(one)
    np_array[3] = 123
    assert to_list(two) == [0.0, 999, 2.2, 123, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]

    three = copy.deepcopy(two)
    four = np.copy(two)
    five = ak.operations.copy(two)
    np_array[5] = 321
    assert to_list(three) == [0.0, 999, 2.2, 123, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]
    assert to_list(four) == [0.0, 999, 2.2, 123, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]
    assert to_list(five) == [0.0, 999, 2.2, 123, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]

    assert to_list(copy.deepcopy(ak.Array([[1, 2, 3], [], [4, 5]]))) == to_list(
        ak.operations.copy(ak.Array([[1, 2, 3], [], [4, 5]]))
    )
    assert to_list(copy.deepcopy(ak.Record({"one": 1, "two": 2.2}))) == to_list(
        ak.operations.copy(ak.Record({"one": 1, "two": 2.2}))
    )

    underlying_array = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
    wrapper = ak.Array(underlying_array)
    duplicate = ak.operations.copy(wrapper)
    underlying_array[2] = 123

    assert to_list(underlying_array) == [1.1, 2.2, 123.0, 4.4, 5.5]
    assert to_list(wrapper) == [1.1, 2.2, 123, 4.4, 5.5]
    assert to_list(duplicate) == [1.1, 2.2, 3.3, 4.4, 5.5]

    original = ak.Array([{"x": 1}, {"x": 2}, {"x": 3}])
    shallow_copy = copy.copy(original)
    shallow_copy["y"] = original.x**2
    assert to_list(shallow_copy) == [
        {"x": 1, "y": 1},
        {"x": 2, "y": 4},
        {"x": 3, "y": 9},
    ]
    assert to_list(original) == [{"x": 1}, {"x": 2}, {"x": 3}]

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

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

    a = ak.Array(
        [
            {"x": 0, "y": 0.0},
            {"x": 1, "y": 1.1},
            {"x": 2, "y": 2.2},
            {"x": 3, "y": 3.3},
            {"x": 4, "y": 4.4},
        ]
    )
    record = copy.deepcopy(a[2].layout)
    a["z"] = a.x**2

    assert to_list(a) == [
        {"x": 0, "y": 0.0, "z": 0},
        {"x": 1, "y": 1.1, "z": 1},
        {"x": 2, "y": 2.2, "z": 4},
        {"x": 3, "y": 3.3, "z": 9},
        {"x": 4, "y": 4.4, "z": 16},
    ]
    assert to_list(record) == {"x": 2, "y": 2.2}
    assert to_list(a[2]) == {"x": 2, "y": 2.2, "z": 4}