Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #335 from ndawe/master
Browse files Browse the repository at this point in the history
Silence cling error in tests and some clarification in docs
  • Loading branch information
ndawe committed Jul 18, 2017
2 parents b717b19 + b81d80e commit 69179c7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include MANIFEST.in
recursive-include root_numpy *.root *.h *.cpp *.pyx *.pxi
recursive-include examples *.py
include *.py
include README.rst
include LICENSE
global-exclude *.pyc *.pyo *.pyd
28 changes: 22 additions & 6 deletions root_numpy/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,20 @@ def root2array(filenames,
``length``. This truncation is after any object selection performed
with the ``object_selection`` argument.
selection : str, optional (default=None)
Only include entries fulfilling this condition.
Only include entries fulfilling this condition. If the condition
evaluates to multiple values per tree entry (e.g. conditions on array
branches) then an entry will be included if the condition evaluates to
true for at least one array element.
object_selection : dict, optional (default=None)
A dictionary mapping selection strings to branch names or lists of
branch names. Only elements passing the selection strings will be
branch names. Only array elements passing the selection strings will be
included in the output array per entry in the tree. The branches
specified must be variable-length array-type branches.
specified must be variable-length array-type branches and the length of
the selection and branches it acts on must match for each tree entry.
For example ``object_selection={'a > 0': ['a', 'b']}`` will include all
elements of 'a' and corresponding elements of 'b' where 'a > 0' for
each tree entry. 'a' and 'b' must have the same length in every tree
entry.
start, stop, step: int, optional (default=None)
The meaning of the ``start``, ``stop`` and ``step`` parameters is the
same as for Python slices. If a range is supplied (by setting some of
Expand Down Expand Up @@ -313,12 +321,20 @@ def tree2array(tree,
``length``. This truncation is after any object selection performed
with the ``object_selection`` argument.
selection : str, optional (default=None)
Only include entries fulfilling this condition.
Only include entries fulfilling this condition. If the condition
evaluates to multiple values per tree entry (e.g. conditions on array
branches) then an entry will be included if the condition evaluates to
true for at least one array element.
object_selection : dict, optional (default=None)
A dictionary mapping selection strings to branch names or lists of
branch names. Only elements passing the selection strings will be
branch names. Only array elements passing the selection strings will be
included in the output array per entry in the tree. The branches
specified must be variable-length array-type branches.
specified must be variable-length array-type branches and the length of
the selection and branches it acts on must match for each tree entry.
For example ``object_selection={'a > 0': ['a', 'b']}`` will include all
elements of 'a' and corresponding elements of 'b' where 'a > 0' for
each tree entry. 'a' and 'b' must have the same length in every tree
entry.
start, stop, step: int, optional (default=None)
The meaning of the ``start``, ``stop`` and ``step`` parameters is the
same as for Python slices. If a range is supplied (by setting some of
Expand Down
69 changes: 61 additions & 8 deletions root_numpy/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,82 @@
import os
import sys
import warnings
import ROOT
import root_numpy as rnp
from numpy.random import RandomState
import tempfile
from contextlib import contextmanager
import root_numpy as rnp
from root_numpy.testdata import get_filepath
import threading

LOCK = threading.RLock()

ROOT.gErrorIgnoreLevel = ROOT.kFatal
warnings.filterwarnings('ignore', category=DeprecationWarning)
warnings.filterwarnings('ignore', category=rnp.RootNumpyUnconvertibleWarning)
RNG = RandomState(42)

from root_numpy.testdata import get_filepath

def load(data):
if isinstance(data, list):
return [get_filepath(x) for x in data]
return get_filepath(data)

import tempfile
from contextlib import contextmanager

@contextmanager
def temp():
tmp_fd, tmp_path = tempfile.mkstemp(suffix='.root')
tmp_root = ROOT.TFile.Open(tmp_path, 'recreate')
yield tmp_root
tmp_root.Close()
os.close(tmp_fd)
os.remove(tmp_path)
try:
yield tmp_root
finally:
tmp_root.Close()
os.close(tmp_fd)
os.remove(tmp_path)


@contextmanager
def silence_sout():
LOCK.acquire()
sys.__stdout__.flush()
origstdout = sys.__stdout__
oldstdout_fno = os.dup(sys.__stdout__.fileno())
devnull = os.open(os.devnull, os.O_WRONLY)
newstdout = os.dup(1)
os.dup2(devnull, 1)
os.close(devnull)
sys.__stdout__ = os.fdopen(newstdout, 'w')
try:
yield
finally:
sys.__stdout__ = origstdout
sys.__stdout__.flush()
os.dup2(oldstdout_fno, 1)
LOCK.release()


@contextmanager
def silence_serr():
LOCK.acquire()
sys.__stderr__.flush()
origstderr = sys.__stderr__
oldstderr_fno = os.dup(sys.__stderr__.fileno())
devnull = os.open(os.devnull, os.O_WRONLY)
newstderr = os.dup(2)
os.dup2(devnull, 2)
os.close(devnull)
sys.__stderr__ = os.fdopen(newstderr, 'w')
try:
yield
finally:
sys.__stderr__ = origstderr
sys.__stderr__.flush()
os.dup2(oldstderr_fno, 2)
LOCK.release()


@contextmanager
def silence():
with silence_sout():
with silence_serr():
yield
6 changes: 4 additions & 2 deletions root_numpy/tests/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import root_numpy as rnp
from numpy.testing import assert_array_equal
from nose.tools import assert_raises
from . import RNG
from . import RNG, silence_serr


def test_evaluate_func():
Expand Down Expand Up @@ -39,7 +39,9 @@ def test_evaluate_func():
assert_raises(ValueError, rnp.evaluate, f3, arr_1d)
assert_raises(ValueError, rnp.evaluate, f3, arr_2d)

assert_raises(ValueError, rnp.evaluate, "f", arr_1d)
with silence_serr(): # silence cling error
assert_raises(ValueError, rnp.evaluate, "f", arr_1d)

assert_raises(ValueError, rnp.evaluate, "x*y", arr_1d)
assert_raises(ValueError, rnp.evaluate, "x", arr_2d)
assert_raises(ValueError, rnp.evaluate, "x*y", arr_3d)
Expand Down

0 comments on commit 69179c7

Please sign in to comment.