# -*- coding: utf-8 -*-
"""
processor sugar
===============
This model provides some not-really-syntactic sugar for the processor objects.
"""
import numpy as np
import inspect
class returns(object):
def __init__(self, dtype, unit=None):
self.dtype = np.dtype(dtype)
self.unit = unit
def __call__(self, f):
f._returntype_ = self.dtype
f._unit_ = self.unit
return f
def cacheable(f):
f._cacheable_ = True
return f
class pprop(object):
"""
Indicates a persistent property
"""
_isPersistentProperty = True
def __init__(self, propertySpec):
self.propertySpec = propertySpec
self.dependencies = inspect.getargspec(propertySpec).args
self.restriction = None
def isApplicable(self, availableProducts):
if self.restriction is None:
return True
args = [availableProducts[d] for d in self.restrictionDependencies]
return self.restriction(*args)
def compute(self, availableProducts):
args = [availableProducts[d] for d in self.dependencies]
return self.propertySpec(*args)
def __str__(self):
return '<pprop %s(%s) %s>'%(self.propertySpec, self.dependencies, self.restriction)
class dpprop(object):
"""
Indicates a persistent property which is available only on some given condition
"""
def __init__(self, restriction):
self.restriction = restriction
def __call__(self, propertySpec):
p = pprop(propertySpec)
p.restriction = self.restriction
p.restrictionDependencies = inspect.getargspec(self.restriction).args
return p
class PassedPart(object):
_isPassedPart = True
def __init__(self, component, part):
self.component = component
self.part = part
def __call__(self, product):
return product.components[self.component][self.part].get()
__all__ = ['returns', 'cacheable', 'PassedPart', 'pprop', 'dpprop']
|