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

Add support for reading TDatime #407

Merged
merged 6 commits into from
Aug 19, 2021
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
1 change: 1 addition & 0 deletions src/uproot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
import uproot.models.TObjArray
import uproot.models.TObjString
import uproot.models.TAtt
import uproot.models.TDatime
import uproot.models.TRef

import uproot.models.TTree
Expand Down
18 changes: 18 additions & 0 deletions src/uproot/behaviors/TDatime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE

"""
This module defines the behaviors of ``TDatime``.
"""

from __future__ import absolute_import

import uproot


class TDatime(object):
"""
Behaviors for TDatime: return values as py:class:`datetime.datetime`
"""

def to_datetime(self):
return uproot._util.code_to_datetime(self._members["fDatime"])
1 change: 1 addition & 0 deletions src/uproot/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def reset_classes():
reload(uproot.models.TObjArray)
reload(uproot.models.TObjString)
reload(uproot.models.TAtt)
reload(uproot.models.TDatime)
reload(uproot.models.TRef)
reload(uproot.models.TTree)
reload(uproot.models.TBranch)
Expand Down
77 changes: 77 additions & 0 deletions src/uproot/models/TDatime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE

"""
This module defines versioned models for ``TDatime``.
"""

from __future__ import absolute_import

import struct

import numpy

import uproot
import uproot.behaviors.TDatime

_tdatime_format1 = struct.Struct(">I")


class Model_TDatime(uproot.behaviors.TDatime.TDatime, uproot.model.Model):
"""
A versionless :doc:`uproot.model.Model` for ``TDatime``.
"""

def read_numbytes_version(self, chunk, cursor, context):
pass

def read_members(self, chunk, cursor, context, file):
self._members["fDatime"] = cursor.field(chunk, _tdatime_format1, context)

@classmethod
def strided_interpretation(
cls, file, header=False, tobject_header=True, breadcrumbs=(), original=None
):
members = []
if header:
members.append(("@num_bytes", numpy.dtype(">u4")))
members.append(("@instance_version", numpy.dtype(">u2")))
members.append(("fDatime", numpy.dtype(">u4")))
return uproot.interpretation.objects.AsStridedObjects(
cls, members, original=original
)

@classmethod
def awkward_form(
cls, file, index_format="i64", header=False, tobject_header=True, breadcrumbs=()
):
jpivarski marked this conversation as resolved.
Show resolved Hide resolved
awkward = uproot.extras.awkward()
contents = {}
if header:
contents["@num_bytes"] = uproot._util.awkward_form(
numpy.dtype("u4"),
file,
index_format,
header,
tobject_header,
breadcrumbs,
)
contents["@instance_version"] = uproot._util.awkward_form(
numpy.dtype("u2"),
file,
index_format,
header,
tobject_header,
breadcrumbs,
)
contents["fDatime"] = uproot._util.awkward_form(
numpy.dtype(">u4"), file, index_format, header, tobject_header, breadcrumbs
)
return awkward.forms.RecordForm(contents, parameters={"__record__": "TDatime"})

base_names_versions = []
member_names = ["fDatime"]
class_flags = {}
class_code = None


uproot.classes["TDatime"] = Model_TDatime
38 changes: 38 additions & 0 deletions tests/test_0407-read-TDatime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/main/LICENSE

from __future__ import absolute_import

import datetime

import numpy
import pytest
import skhep_testdata

import uproot


@pytest.fixture(scope="module")
def datafile(tmpdir_factory):
yield skhep_testdata.data_path("uproot-issue-407.root")


@pytest.fixture(params=["foo", "foo_padded"])
def _object(request, datafile):
with uproot.open(datafile) as f:
yield f[request.param]


@pytest.fixture
def tree(datafile):
with uproot.open(datafile) as f:
yield f["tree"]


def test_streamer(_object):
assert _object.members["d"].to_datetime() == datetime.datetime(2021, 1, 1, 0, 0, 0)


def test_strided_interpretation(tree):
assert list(tree.iterate(how=tuple))[0][0].fDatime[
0
] == uproot._util.datetime_to_code(datetime.datetime(2021, 1, 1, 0, 0, 0))