File: test_1511_set_attribute.py

package info (click to toggle)
python-awkward 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,524 kB
  • sloc: python: 187,940; cpp: 33,928; sh: 432; makefile: 21; javascript: 8
file content (95 lines) | stat: -rw-r--r-- 2,757 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
95
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import numpy as np
import pytest

import awkward as ak


def test_array():
    record = ak.contents.RecordArray([ak.contents.NumpyArray(np.arange(10))], ["x"])
    array = ak.Array(record)

    with pytest.raises(AttributeError):
        array.x = 10

    with pytest.raises(AttributeError):
        array.not_an_existing_attribute = 10

    array._not_an_existing_attribute = 10
    assert array._not_an_existing_attribute == 10


def test_record():
    record = ak.contents.RecordArray([ak.contents.NumpyArray(np.arange(10))], ["x"])
    array = ak.Array(record)
    record = array[0]

    with pytest.raises(AttributeError):
        record.x = 10

    with pytest.raises(AttributeError):
        record.not_an_existing_attribute = 10

    record._not_an_existing_attribute = 10
    assert record._not_an_existing_attribute == 10


def test_bad_behavior_array():
    class BadBehaviorBase:
        FIELD_STRING = "I am not a list of fields!"

        @property
        def fields(self):
            return self.__class__.FIELD_STRING

        @fields.setter
        def fields(self, value):
            self.__class__.FIELD_STRING = value

    class BadBehaviorArray(BadBehaviorBase, ak.Array):
        pass

    class BadBehaviorRecord(BadBehaviorBase, ak.Record):
        pass

    behavior = {("*", "bad"): BadBehaviorArray, "bad": BadBehaviorRecord}

    record = ak.contents.RecordArray([ak.contents.NumpyArray(np.arange(10))], ["x"])
    array = ak.Array(record, with_name="bad", behavior=behavior)
    assert isinstance(array, BadBehaviorArray)

    assert array.fields == BadBehaviorArray.FIELD_STRING
    array.fields = "yo ho ho and a bottle of rum"
    assert array.fields == "yo ho ho and a bottle of rum"


def test_bad_behavior_record():
    class BadBehaviorBase:
        FIELD_STRING = "I am not a list of fields!"

        @property
        def fields(self):
            return self.__class__.FIELD_STRING

        @fields.setter
        def fields(self, value):
            self.__class__.FIELD_STRING = value

    class BadBehaviorArray(BadBehaviorBase, ak.Array):
        pass

    class BadBehaviorRecord(BadBehaviorBase, ak.Record):
        pass

    behavior = {("*", "bad"): BadBehaviorArray, "bad": BadBehaviorRecord}
    record = ak.contents.RecordArray([ak.contents.NumpyArray(np.arange(10))], ["x"])
    array = ak.Array(record, with_name="bad", behavior=behavior)
    record = array[0]
    assert isinstance(record, BadBehaviorRecord)

    assert record.fields == BadBehaviorRecord.FIELD_STRING
    record.fields = "yo ho ho and a bottle of rum"
    assert record.fields == "yo ho ho and a bottle of rum"