Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

# -*- 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']