Skip to content

Commit

Permalink
Added Keywords class to represent keyword sets
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Apr 13, 2017
1 parent 84fb568 commit 4e7b052
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion holoviews/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import param
from .tree import AttrTree
from .util import sanitize_identifier, group_sanitizer,label_sanitizer
from .util import sanitize_identifier, group_sanitizer,label_sanitizer, basestring
from .pprint import InfoPrinter


Expand Down Expand Up @@ -125,6 +125,56 @@ def __exit__(self, etype, value, traceback):
raise AbbreviatedException(etype, value, traceback)


class Keywords(param.Parameterized):
"""
A keywords objects represents a set of Python keywords. It is
list-like and ordered but it is also a set without duplicates. When
passed as **kwargs, Python keywords are not ordered but this class
always lists keywords in sorted order.
In addition to containing the list of keywords, Keywords has an
optional target which describes what the keywords are applicable to.
This class is for internal use only and should not be in the user
namespace.
"""

values = param.List(doc="Set of keywords as a sorted list.")

target = param.String(allow_None=True, doc="""
Optional string description of what the keywords apply to.""")

def __init__(self, values=[], target=None):

strings = [isinstance(v, (str,basestring)) for v in values]
if False in strings:
raise ValueError('All keywords must be strings: {0}'.format(values))
super(Keywords, self).__init__(values=sorted(values),
target=target)

def __add__(self, other):
if (self.target and other.target) and (self.target != other.target):
raise Exception('Targets must match to combine Keywords')
target = self.target or other.target
return Keywords(sorted(set(self.values + other.values)), target=target)

def __repr__(self):
if self.target:
msg = 'Keywords({values}, target={target})'
info = dict(values=self.values, target=self.target)
else:
msg = 'Keywords({values})'
info = dict(values=self.values)
return msg.format(**info)

def __str__(self): return str(self.values)
def __iter__(self): return iter(self.values)
def __bool__(self): return bool(self.values)
def __nonzero__(self): return bool(self.values)
def __contains__(self, val): return val in self.values



class Cycle(param.Parameterized):
"""
A simple container class that specifies cyclic options. A typical
Expand Down

0 comments on commit 4e7b052

Please sign in to comment.