|
1 | 1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
2 | 2 |
|
| 3 | +from types import FunctionType |
| 4 | +from contextlib import contextmanager |
| 5 | + |
3 | 6 | from astropy.table import QTable |
| 7 | +from astropy.utils import wraps |
| 8 | + |
| 9 | +__all__ = ['BaseTimeSeries', 'autocheck_required_columns'] |
| 10 | + |
| 11 | +COLUMN_RELATED_METHODS = ['add_column', |
| 12 | + 'add_columns', |
| 13 | + 'keep_columns', |
| 14 | + 'remove_column', |
| 15 | + 'remove_columns', |
| 16 | + 'rename_column'] |
| 17 | + |
| 18 | + |
| 19 | +def autocheck_required_columns(cls): |
| 20 | + """ |
| 21 | + This is a decorator that ensures that the table contains specific |
| 22 | + methods indicated by the _required_columns attribute. The aim is to |
| 23 | + decorate all methods that might affect the columns in the table and check |
| 24 | + for consistency after the methods have been run. |
| 25 | + """ |
| 26 | + |
| 27 | + def decorator_method(method): |
| 28 | + |
| 29 | + @wraps(method) |
| 30 | + def wrapper(self, *args, **kwargs): |
| 31 | + result = method(self, *args, **kwargs) |
| 32 | + self._check_required_columns() |
| 33 | + return result |
| 34 | + |
| 35 | + return wrapper |
4 | 36 |
|
5 | | -__all__ = ['BaseTimeSeries'] |
| 37 | + for name in COLUMN_RELATED_METHODS: |
| 38 | + if (not hasattr(cls, name) or |
| 39 | + not isinstance(getattr(cls, name), FunctionType)): |
| 40 | + raise ValueError("{0} is not a valid method".format(name)) |
| 41 | + setattr(cls, name, decorator_method(getattr(cls, name))) |
| 42 | + |
| 43 | + return cls |
6 | 44 |
|
7 | 45 |
|
8 | 46 | class BaseTimeSeries(QTable): |
9 | 47 |
|
10 | 48 | _required_columns = None |
| 49 | + _required_columns_enabled = True |
11 | 50 |
|
12 | | - def add_columns(self, cols, indexes=None, names=None, **kwargs): |
| 51 | + # If _required_column_relax is True, we don't require the columns to be |
| 52 | + # present but we do require them to be the correct ones IF present. Note |
| 53 | + # that this is a temporary state - as soon as the required columns |
| 54 | + # are all present, we toggle this to False |
| 55 | + _required_columns_relax = False |
13 | 56 |
|
14 | | - if names is None: |
15 | | - new_names = [col.info.name for col in cols] |
16 | | - else: |
17 | | - new_names = names |
| 57 | + def _check_required_columns(self): |
18 | 58 |
|
19 | | - existing_and_new = set(new_names) | set(self.colnames) |
| 59 | + if not self._required_columns_enabled: |
| 60 | + return |
20 | 61 |
|
21 | 62 | if self._required_columns is not None: |
22 | 63 |
|
23 | | - if len(set(self._required_columns) & existing_and_new) == len(self._required_columns): |
24 | | - # Existing and new columns include all required columns, so we are |
25 | | - # good to go |
26 | | - self._required_columns = None |
27 | | - elif len(existing_and_new - set(self._required_columns)) == 0: |
28 | | - # All existing and new columns are a strict subset of required |
29 | | - # columns - it doesn't mean that all required columns are present, |
30 | | - # but no non-required columns are. So we are good to go. |
31 | | - pass |
| 64 | + if self._required_columns_relax: |
| 65 | + required_columns = self._required_columns[:len(self.colnames)] |
32 | 66 | else: |
33 | | - # There are columns from the required ones missing but some non- |
34 | | - # required columns are being added |
35 | | - for colname in self._required_columns: |
36 | | - if colname not in existing_and_new: |
37 | | - raise ValueError("{0} requires a column called '{1}' to be set " |
38 | | - "before data can be added" |
39 | | - .format(self.__class__.__name__, colname)) |
40 | | - |
41 | | - return super().add_columns(cols, indexes=indexes, names=names, **kwargs) |
| 67 | + required_columns = self._required_columns |
| 68 | + |
| 69 | + plural = 's' if len(required_columns) > 1 else '' |
| 70 | + |
| 71 | + if not self._required_columns_relax and len(self.colnames) == 0: |
| 72 | + |
| 73 | + raise ValueError("{0} object is invalid - expected '{1}' " |
| 74 | + "as the first column{2} but time series has no columns" |
| 75 | + .format(self.__class__.__name__, required_columns[0], plural)) |
| 76 | + |
| 77 | + elif self.colnames[:len(required_columns)] != required_columns: |
| 78 | + |
| 79 | + raise ValueError("{0} object is invalid - expected '{1}' " |
| 80 | + "as the first column{2} but found '{3}'" |
| 81 | + .format(self.__class__.__name__, required_columns[0], plural, self.colnames[0])) |
| 82 | + |
| 83 | + if (self._required_columns_relax |
| 84 | + and self._required_columns == self.colnames[:len(self._required_columns)]): |
| 85 | + self._required_columns_relax = False |
| 86 | + |
| 87 | + @contextmanager |
| 88 | + def _delay_required_column_checks(self): |
| 89 | + self._required_columns_enabled = False |
| 90 | + yield |
| 91 | + self._required_columns_enabled = True |
| 92 | + self._check_required_columns() |
0 commit comments