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

67

68

69

70

from builtins import zip 

from functools import wraps 

from collections import OrderedDict 

 

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

 

#----------------------------------------------------------------------  

# Define decorators for get_* methods 

 

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

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

 

 

def cached(f): 

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

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

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

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

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

@wraps(f) 

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

if colname in self._column_cache: 

result = self._column_cache[colname] 

else: 

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

self._column_cache[colname] = result 

return result 

new_f._cache_results = True 

return new_f 

 

def compound(*colnames): 

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

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

e.g. RA/DEC, or magnitudes. 

 

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

 

@compound('ra_corr', 'dec_corr') 

def get_point_correction(self): 

raJ2000 = self.column_by_name('raJ2000') 

decJ2000 - self.column_by_name('decJ2000') 

ra_corr, dec_corr = precess(raJ2000, decJ2000) 

return (ra_corr, dec_corr) 

 

""" 

def wrapper(f): 

@cached 

@wraps(f) 

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

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

return OrderedDict(zip(colnames, results)) 

new_f._compound_column = True 

new_f._colnames = colnames 

return new_f 

return wrapper 

 

def register_class(cls): 

cls._methodRegistry = {} 

for methodname in dir(cls): 

method=getattr(cls, methodname) 

if hasattr(method, '_registryKey'): 

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

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

return cls 

 

def register_method(key): 

def wrapper(func): 

func._registryKey=key 

return func 

return wrapper