Skip to content

Commit

Permalink
fixed units checking for @expects
Browse files Browse the repository at this point in the history
don't check function arguments as long it's not Numeric instance or Device class
  • Loading branch information
Pavel Rybalko committed Oct 24, 2014
1 parent 3bea2ec commit 34c37bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions concert/helpers.py
@@ -1,4 +1,5 @@
import inspect
import functools


class Command(object):
Expand Down Expand Up @@ -76,7 +77,6 @@ def __init__(self, func, e_args, f_args, f_defaults, e_keywords):
self.outputs = e_keywords['output']
self.e_keywords = e_keywords
self._isfunction = True
self.__name__ = func.__name__

def __call__(self, *args, **kwargs):
self._check_args(*args, **kwargs)
Expand All @@ -103,16 +103,18 @@ def _check_args(self, *args, **kwargs):

def _check_type_correctness(self, arg_name, expected, given):
from concert.devices.base import Device
import inspect
if expected is not None:
if isinstance(expected, Numeric):
self._check_numeric(arg_name, expected, given)

elif issubclass(expected, Device) and not isinstance(given, expected):
raise TypeError(
'Sorry, argument "{}" expected to get {}, but got {}'.format(
arg_name,
expected.__name__,
given.__class__.__name__))
elif inspect.isclass(expected):
if issubclass(expected, Device) and not isinstance(given, expected):
raise TypeError(
'Sorry, argument "{}" expected to get {}, but got {}'.format(
arg_name,
expected.__name__,
given.__class__.__name__))

def _check_numeric(self, arg_name, expected, given):
if (expected.units is not None) ^ hasattr(given, 'units'):
Expand Down Expand Up @@ -176,6 +178,7 @@ def __call__(self, f):
f_args,
f_defaults,
self.e_keywords)
functools.update_wrapper(self.func, f)
return self.func


Expand Down
2 changes: 1 addition & 1 deletion concert/processes.py
Expand Up @@ -175,7 +175,7 @@ def dscan(parameter_list, n_intervals, handler):
return ascan(parameter_list, n_intervals, handler, initial_values)


@expects(Camera, LinearMotor, measure=np.std, opt_kwargs=None,
@expects(Camera, LinearMotor, measure=None, opt_kwargs=None,
plot_consumer=None, frame_consumer=None, output=Numeric(1))
def focus(camera, motor, measure=np.std, opt_kwargs=None,
plot_consumer=None, frame_consumer=None):
Expand Down

0 comments on commit 34c37bd

Please sign in to comment.