Wrapper for a collection to signify that each element is one possible parameter value
| 31 | |
| 32 | |
| 33 | class Options(object): |
| 34 | """Wrapper for a collection to signify that each element is one possible |
| 35 | parameter value""" |
| 36 | |
| 37 | def __init__(self, *args): |
| 38 | if args is None or len(args) < 1: |
| 39 | raise ValueError("No options given!") |
| 40 | |
| 41 | if len(args) == 1 and hasattr(args, '__len__'): |
| 42 | self.values = args[0] # given a list |
| 43 | else: |
| 44 | self.values = args # given individual objects |
| 45 | |
| 46 | def __len__(self): |
| 47 | return len(self.values) |
| 48 | |
| 49 | # deliberately don't act like a collection so that we fail fast if |
| 50 | # code doesn't know that this is supposed to represent Options, rather |
| 51 | # than a collection of values. This is mostly to ensure that Options |
| 52 | # are always expanded out when generating sets of parameters. |
| 53 | def __getitem__(self, idx): |
| 54 | self._raise() |
| 55 | |
| 56 | def __setitem__(self, idx, item): |
| 57 | self._raise() |
| 58 | |
| 59 | def _raise(self): |
| 60 | raise TypeError("Options object is not a collection; use options.values" |
| 61 | " to access the collection of individual options") |
| 62 | |
| 63 | |
| 64 | # ================================================================ Funcs |