Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve Array initialisation performance #1700

Merged
merged 6 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/awkward/_v2/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ def __getattr__(self, where):
Note that while fields can be accessed as attributes, they cannot be
*assigned* as attributes. See #ak.Array.__setitem__ for more.
"""
if where in dir(type(self)):
if hasattr(type(self), where):
return super().__getattribute__(where)
else:
if where in self._layout.fields:
Expand Down Expand Up @@ -1135,7 +1135,7 @@ def __setattr__(self, name, value):

to add or modify a field.
"""
if name in dir(type(self)) or name.startswith("_"):
if name.startswith("_") or hasattr(type(self), name):
super().__setattr__(name, value)
elif name in self._layout.fields:
raise ak._v2._util.error(
Expand Down Expand Up @@ -1817,7 +1817,7 @@ def __getattr__(self, where):
* the field name is not a valid Python identifier or is a Python
keyword.
"""
if where in dir(type(self)):
if hasattr(type(self), where):
return super().__getattribute__(where)
else:
if where in self._layout.fields:
Expand Down
67 changes: 66 additions & 1 deletion tests/v2/test_1511-set-attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np


def test():
def test_array():
record = ak._v2.contents.RecordArray(
[ak._v2.contents.NumpyArray(np.arange(10))], ["x"]
)
Expand All @@ -19,3 +19,68 @@ def test():

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


def test_record():
record = ak._v2.contents.RecordArray(
[ak._v2.contents.NumpyArray(np.arange(10))], ["x"]
)
array = ak._v2.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


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._v2.Array):
pass


class BadBehaviorRecord(BadBehaviorBase, ak._v2.Record):
pass


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


def test_bad_behavior_array():
record = ak._v2.contents.RecordArray(
[ak._v2.contents.NumpyArray(np.arange(10))], ["x"]
)
array = ak._v2.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():
record = ak._v2.contents.RecordArray(
[ak._v2.contents.NumpyArray(np.arange(10))], ["x"]
)
array = ak._v2.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"