Skip to content

Commit

Permalink
Moved merge_option_dicts utility out of util.parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Nov 13, 2018
1 parent d562031 commit 99663c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
25 changes: 25 additions & 0 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ def default(self, obj):
return id(obj)


def merge_option_dicts(old_opts, new_opts):
"""
Update the old_opts option dictionary with the options defined in
new_opts. Instead of a shallow update as would be performed by calling
old_opts.update(new_opts), this updates the dictionaries of all option
types separately.
Given two dictionaries
old_opts = {'a': {'x': 'old', 'y': 'old'}}
and
new_opts = {'a': {'y': 'new', 'z': 'new'}, 'b': {'k': 'new'}}
this returns a dictionary
{'a': {'x': 'old', 'y': 'new', 'z': 'new'}, 'b': {'k': 'new'}}
"""
merged = dict(old_opts)

for option_type, options in new_opts.items():
if option_type not in merged:
merged[option_type] = {}

merged[option_type].update(options)

return merged


class periodic(Thread):
"""
Run a callback count times with a given period without blocking.
Expand Down
5 changes: 2 additions & 3 deletions holoviews/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..core import DynamicMap, HoloMap, Dimensioned, ViewableElement, StoreOptions, Store
from ..core.options import options_policy, Keywords, Options
from ..core.operation import Operation
from ..core.util import Aliases, basestring # noqa (API import)
from ..core.util import Aliases, basestring, merge_option_dicts # noqa (API import)
from ..core.operation import OperationCallable
from ..core.spaces import Callable
from ..core import util
Expand Down Expand Up @@ -127,7 +127,6 @@ def expand_options(cls, options, backend=None):
{'Image': {'plot': dict(show_title=False), 'style': dict(cmap='viridis')}}
"""
from .parser import OptsSpec # Move this utility!
current_backend = Store.current_backend
backend_options = Store.options(backend=backend or current_backend)
expanded = {}
Expand All @@ -139,7 +138,7 @@ def expand_options(cls, options, backend=None):
else:
new_opts = {obj.key: obj.kwargs}

merged_options = OptsSpec._merge_options(merged_options, new_opts)
merged_options = merge_option_dicts(merged_options, new_opts)
options = merged_options

for objspec, options in options.items():
Expand Down
26 changes: 2 additions & 24 deletions holoviews/util/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pyparsing as pp

from ..core.options import Options, Cycle, Palette
from ..core.util import merge_option_dicts
from ..operation import Compositor

ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Expand Down Expand Up @@ -284,30 +285,7 @@ def _group_paths_without_options(cls, line_parse_result):
yield active_pathspecs, {}


@classmethod
def _merge_options(cls, old_opts, new_opts):
"""
Update the old_opts option dictionary with the options defined in
new_opts. Instead of a shallow update as would be performed by calling
old_opts.update(new_opts), this updates the dictionaries of all option
types separately.
Given two dictionaries
old_opts = {'a': {'x': 'old', 'y': 'old'}}
and
new_opts = {'a': {'y': 'new', 'z': 'new'}, 'b': {'k': 'new'}}
this returns a dictionary
{'a': {'x': 'old', 'y': 'new', 'z': 'new'}, 'b': {'k': 'new'}}
"""
merged = dict(old_opts)

for option_type, options in new_opts.items():
if option_type not in merged:
merged[option_type] = {}

merged[option_type].update(options)

return merged

@classmethod
def apply_deprecations(cls, path):
Expand Down Expand Up @@ -356,7 +334,7 @@ def parse(cls, line, ns={}):
options['style'] = {cls.aliases.get(k,k):v for k,v in opts.items()}

for pathspec in pathspecs:
parse[pathspec] = cls._merge_options(parse.get(pathspec, {}), options)
parse[pathspec] = merge_option_dicts(parse.get(pathspec, {}), options)

return {
cls.apply_deprecations(path): {
Expand Down

0 comments on commit 99663c2

Please sign in to comment.