Skip to content

Commit

Permalink
Merge pull request #85 from ZLLentz/small-md
Browse files Browse the repository at this point in the history
ENH: Don't add a metadata instance with only one object
  • Loading branch information
ZLLentz authored Mar 19, 2018
2 parents cf1c0d9 + 1a7483f commit 4ab1d18
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ utils.py
safe_load
get_current_experiment
IterableNamespace
count_ns_leaves
extract_objs
find_object
find_class
Expand Down
9 changes: 7 additions & 2 deletions hutch_python/load_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from .namespace import class_namespace, metadata_namespace
from .qs_load import get_qs_objs
from .user_load import get_user_objs
from .utils import get_current_experiment, safe_load, hutch_banner
from .utils import (get_current_experiment, safe_load, hutch_banner,
count_ns_leaves)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -240,7 +241,11 @@ def load_conf(conf, hutch_dir=None):
if hutch is not None:
meta = metadata_namespace(['beamline', 'stand'],
scope='hutch_python.db')
cache(**meta.__dict__)
# Prune meta, remove branches with only one object
for name, space in meta.__dict__.items():
if count_ns_leaves(space) > 1:
cache(**{name: space})

default_class_namespace(object, 'all_objects', cache)

# Write db.txt info file to the user's module
Expand Down
1 change: 1 addition & 0 deletions hutch_python/tests/test_load_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_file_load():
err = '{} was overriden by a namespace'
for elem in should_have:
assert not isinstance(objs[elem], SimpleNamespace), err.format(elem)
assert 'tst' in objs


def test_no_file():
Expand Down
16 changes: 16 additions & 0 deletions hutch_python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def test_iterable_namespace():
assert len(ns) == 3


def test_count_leaves():
logger.debug('test_count_leaves')

ns0 = utils.IterableNamespace(a=utils.IterableNamespace())
ns1 = utils.IterableNamespace(a=1, b=utils.IterableNamespace())
ns2 = utils.IterableNamespace(a=utils.IterableNamespace(a=1),
b=utils.IterableNamespace(b=2))
ns3 = utils.IterableNamespace(a=1,
b=utils.IterableNamespace(a=1, b=2))

assert utils.count_ns_leaves(ns0) == 0
assert utils.count_ns_leaves(ns1) == 1
assert utils.count_ns_leaves(ns2) == 2
assert utils.count_ns_leaves(ns3) == 3


def test_extract_objs():
logger.debug('test_extract_objs')
# Has no __all__ keyword
Expand Down
17 changes: 17 additions & 0 deletions hutch_python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ def __len__(self):
return len(self.__dict__)


def count_ns_leaves(namespace):
"""
Count the number of objects in a nested `IterableNamespace`.
Given an `IterableNamespace` that contains other `IterableNamespace`
objects that may in themselves contain `IterableNamespace` objects,
determine how many non-`IterableNamespace` objects are in the tree.
"""
count = 0
for obj in namespace:
if isinstance(obj, IterableNamespace):
count += count_ns_leaves(obj)
else:
count += 1
return count


def extract_objs(scope=None, skip_hidden=True, stack_offset=0):
"""
Return all objects with the ``scope``.
Expand Down

0 comments on commit 4ab1d18

Please sign in to comment.