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

1from builtins import zip 

2from functools import wraps 

3from collections import OrderedDict 

4 

5__all__ = ["cached", "compound", "register_class", "register_method"] 

6 

7#----------------------------------------------------------------------  

8# Define decorators for get_* methods 

9 

10# The cached decorator specifies that once the column is computed for 

11# a given database chunk, it is cached in memory and not computed again. 

12 

13 

14def cached(f): 

15 """Decorator for specifying that the computed result should be cached""" 

16 if not f.__name__.startswith('get_'): 16 ↛ 17line 16 didn't jump to line 17, because the condition on line 16 was never true

17 raise ValueError("@cached can only be applied to get_* methods: " 

18 "Method '%s' invalid." % f.__name__) 

19 colname = f.__name__.replace('get_','',1) 

20 @wraps(f) 

21 def new_f(self, *args, **kwargs): 

22 if colname in self._column_cache: 

23 result = self._column_cache[colname] 

24 else: 

25 result = f(self, *args, **kwargs) 

26 self._column_cache[colname] = result 

27 return result 

28 new_f._cache_results = True 

29 return new_f 

30 

31def compound(*colnames): 

32 """Specifies that a column is a "compound column", 

33 that is, it returns multiple values. This is useful in the case of, 

34 e.g. RA/DEC, or magnitudes. 

35 

36 For example, to return an RA and a DEC together, use, e.g.:: 

37 

38 @compound('ra_corr', 'dec_corr') 

39 def get_point_correction(self): 

40 raJ2000 = self.column_by_name('raJ2000') 

41 decJ2000 - self.column_by_name('decJ2000') 

42 ra_corr, dec_corr = precess(raJ2000, decJ2000) 

43 return (ra_corr, dec_corr) 

44 

45""" 

46 def wrapper(f): 

47 @cached 

48 @wraps(f) 

49 def new_f(self, *args, **kwargs): 

50 results = f(self, *args, **kwargs) 

51 return OrderedDict(zip(colnames, results)) 

52 new_f._compound_column = True 

53 new_f._colnames = colnames 

54 return new_f 

55 return wrapper 

56 

57def register_class(cls): 

58 cls._methodRegistry = {} 

59 for methodname in dir(cls): 

60 method=getattr(cls, methodname) 

61 if hasattr(method, '_registryKey'): 

62 if method._registryKey not in cls._methodRegistry: 62 ↛ 59line 62 didn't jump to line 59, because the condition on line 62 was never false

63 cls._methodRegistry.update({method._registryKey:method}) 

64 return cls 

65 

66def register_method(key): 

67 def wrapper(func): 

68 func._registryKey=key 

69 return func 

70 return wrapper