Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #41 from SamWhited/serializable_options
Browse files Browse the repository at this point in the history
Serializable options
  • Loading branch information
campaul committed Jun 2, 2015
2 parents 871a9ff + 5ad8a3f commit bd047bc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
58 changes: 55 additions & 3 deletions rawkit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Options(object):

"""
Represents a set of options which can be used when processing raw data.
:param attrs: a subscriptable object from which to take the initial state
of the options object.
:type attrs: :class:`dict`
"""

__slots__ = [
Expand All @@ -189,12 +193,60 @@ class Options(object):
]
"""The options which are supported by this class."""

def __init__(self):
def __init__(self, attrs=None):
"""
Create the options object, initializing values to ``None``.
Create the options object, initializing values to ``None`` or their
corresponding value from `attrs`.
"""
for i in self.__slots__:
setattr(self, i, None)
try:
param = i[1:]
setattr(self, param, attrs[param])
except (KeyError, TypeError):
setattr(self, i, None)

def __iter__(self):
"""Allow iterating over the options."""
idx = 0
while True:
idx += 1
try:
yield self.keys()[idx - 1]
except IndexError:
raise StopIteration

def __repr__(self):
"""
Represents the options as a dict.
"""
return repr(dict(self))

def keys(self):
"""
A list of keys which have a value other than ``None`` and which have
been set by the user (even if those options are set to the default
value).
:returns: List of option keys which have been set
:rtype: :class:`tuple`
"""
return [slot[1:] for slot in self.__slots__ if getattr(self, slot) is
not None]

def values(self):
"""
The values of all options which appear in :func:`keys`.
:returns: List of options values
:rtype: :class:`tuple`
"""
return [self.__getitem__(k) for k in self.keys()]

def __getitem__(self, k):
"""
Allow accessing options with dictionary syntax eg. opts['half_size'].
"""
return getattr(self, k)

@option(param='output_color', ctype=ctypes.c_int)
def colorspace(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,27 @@ def test_map_params_fails_on_invalid(options):
# with pytest.raises(AttributeError):
params = Mock()
options._map_to_libraw_params(params)


def test_options_are_iterable(options):
options.half_size = True
assert 'half_size' in options
assert 'bps' not in options


def test_options_repr(options):
options.half_size = True

assert repr(options) == repr({'half_size': True})


def test_options_keys(options):
options.half_size = True

assert options.keys() == ['half_size']


def test_options_values(options):
options.half_size = True

assert options.values() == [True]

0 comments on commit bd047bc

Please sign in to comment.