-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fixed #383 and moved behavior_of to its own module. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * And documentation for the new module. * Fix precommits. * Added a test. (It would trigger the bug.) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
04f3b10
commit 54541a6
Showing
4 changed files
with
137 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE | ||
|
||
""" | ||
This module defines utilities for adding behaviors to C++ objects in Python. | ||
Behaviors are defined by specially named classes in specially named modules | ||
that get auto-detected by :doc:`uproot.behavior.behavior_of`. | ||
""" | ||
|
||
from __future__ import absolute_import | ||
|
||
import pkgutil | ||
|
||
import uproot.behaviors | ||
|
||
|
||
def behavior_of(classname): | ||
""" | ||
Finds and loads the behavior class for C++ (decoded) classname or returns | ||
None if there isn't one. | ||
Behaviors do not have a required base class, and they may be used with | ||
Awkward Array's ``ak.behavior``. | ||
The search strategy for finding behavior classes is: | ||
1. Translate the ROOT class name from C++ to Python with | ||
:doc:`uproot.model.classname_encode`. For example, | ||
``"ROOT::RThing"`` becomes ``"Model_ROOT_3a3a_RThing"``. | ||
2. Look for a submodule of ``uproot.behaviors`` without | ||
the ``"Model_"`` prefix. For example, ``"ROOT_3a3a_RThing"``. | ||
3. Look for a class in that submodule with the fully encoded | ||
name. For example, ``"Model_ROOT_3a3a_RThing"``. | ||
See :doc:`uproot.behaviors` for details. | ||
""" | ||
name = uproot.model.classname_encode(classname) | ||
assert name.startswith("Model_") | ||
name = name[6:] | ||
|
||
specialization = None | ||
for param in behavior_of._specializations: | ||
if name.endswith(param): | ||
specialization = param | ||
name = name[: -len(param)] | ||
break | ||
|
||
if name not in globals(): | ||
if name in behavior_of._module_names: | ||
exec( | ||
compile( | ||
"import uproot.behaviors.{0}".format(name), "<dynamic>", "exec" | ||
), | ||
globals(), | ||
) | ||
module = eval("uproot.behaviors.{0}".format(name)) | ||
behavior_cls = getattr(module, name, None) | ||
if behavior_cls is not None: | ||
globals()[name] = behavior_cls | ||
|
||
cls = globals().get(name) | ||
|
||
if cls is None or specialization is None: | ||
return cls | ||
else: | ||
return cls(specialization) | ||
|
||
|
||
behavior_of._module_names = [ | ||
module_name | ||
for loader, module_name, is_pkg in pkgutil.walk_packages(uproot.behaviors.__path__) | ||
] | ||
|
||
behavior_of._specializations = [ | ||
"_3c_bool_3e_", | ||
"_3c_char_3e_", | ||
"_3c_unsigned_20_char_3e_", | ||
"_3c_short_3e_", | ||
"_3c_unsigned_20_short_3e_", | ||
"_3c_int_3e_", | ||
"_3c_unsigned_20_int_3e_", | ||
"_3c_long_3e_", | ||
"_3c_unsigned_20_long_3e_", | ||
"_3c_long_20_long_3e_", | ||
"_3c_unsigned_20_long_20_long_3e_", | ||
"_3c_size_5f_t_3e_", | ||
"_3c_ssize_5f_t_3e_", | ||
"_3c_float_3e_", | ||
"_3c_double_3e_", | ||
"_3c_long_20_double_3e_", | ||
"_3c_Bool_5f_t_3e_", | ||
"_3c_Char_5f_t_3e_", | ||
"_3c_UChar_5f_t_3e_", | ||
"_3c_Short_5f_t_3e_", | ||
"_3c_UShort_5f_t_3e_", | ||
"_3c_Int_5f_t_3e_", | ||
"_3c_UInt_5f_t_3e_", | ||
"_3c_Long_5f_t_3e_", | ||
"_3c_ULong_5f_t_3e_", | ||
"_3c_Long64_5f_t_3e_", | ||
"_3c_ULong64_5f_t_3e_", | ||
"_3c_Size_5f_t_3e_", | ||
"_3c_Float_5f_t_3e_", | ||
"_3c_Double_5f_t_3e_", | ||
"_3c_LongDouble_5f_t_3e_", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE | ||
|
||
import os | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import uproot | ||
import uproot.writing | ||
|
||
ROOT = pytest.importorskip("ROOT") | ||
|
||
|
||
def test_recreate(tmp_path): | ||
filename = os.path.join(tmp_path, "whatever.root") | ||
|
||
f1 = ROOT.TFile(filename, "recreate") | ||
mat = ROOT.TMatrixD(3, 3) | ||
mat[0, 1] = 4 | ||
mat[1, 0] = 8 | ||
mat[2, 2] = 3 | ||
mat.Write("mat") | ||
f1.Close() | ||
|
||
with uproot.open(filename) as f2: | ||
assert f2["mat"].member("fNrows") == 3 | ||
assert f2["mat"].member("fNcols") == 3 | ||
assert np.array_equal( | ||
f2["mat"].member("fElements"), [0, 4, 0, 8, 0, 0, 0, 0, 3] | ||
) |