def create_type_delegate(self, var_type): """ Create a variable value delegator for var_type The delegator will convert automatically between the given variable type and the netcdf compatible one Parameters ---------- var_type : str the variable type Returns ------- NetCDFPlus.Value_Delegate the delegator instance """ getter = None setter = None store = None if 'obj.' in var_type or 'uuid.' in var_type: store_name = str(var_type.split('.')[1]) store = self._stores[store_name] base_type = store.content_class # get_is_iterable = lambda v: \ # v.base_cls is not base_type if hasattr(v, 'base_cls') else \ # hasattr(v, '__iter__') get_numpy_iterable = lambda v: isinstance(v, np.ndarray) set_is_iterable = lambda v: \ v.base_cls is not base_type if hasattr(v, 'base_cls') else \ hasattr(v, '__iter__') if var_type == 'int': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type == 'bool': getter = lambda v: v.astype(bool).tolist() setter = lambda v: np.array(v, dtype=np.int8) elif var_type == 'index': getter = lambda v: \ [None if int(w) < 0 else int(w) for w in v.tolist()] \ if hasattr(v, '__iter__') else None if int(v) < 0 else int(v) setter = lambda v: \ [-1 if w is None else w for w in v] \ if hasattr(v, '__iter__') else -1 if v is None else v elif var_type == 'float': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type.startswith('numpy.'): pass elif var_type == 'jsonobj': setter = lambda v: self.simplifier.to_json_object(v) getter = lambda v: self.simplifier.from_json(v) elif var_type == 'json': setter = lambda v: self.simplifier.to_json(v) getter = lambda v: self.simplifier.from_json(v) elif var_type.startswith('obj.'): getter = lambda v: [ None if w[0] == '-' else store.load(int(UUID(w))) for w in v ] if get_numpy_iterable(v) else \ None if v[0] == '-' else store.load(int(UUID(v))) setter = lambda v: \ ''.join(['-' * 36 if w is None else str(UUID(int=store.save(w))) for w in list.__iter__(v)]) \ if set_is_iterable(v) else \ '-' * 36 if v is None else str(UUID(int=store.save(v))) elif var_type.startswith('lazyobj.'): getter = lambda v: [ None if w[0] == '-' else LoaderProxy.new(store, int(UUID(w))) for w in v ] if isinstance(v, np.ndarray) else \ None if v[0] == '-' else LoaderProxy.new(store, int(UUID(v))) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(UUID(int=store.save(w))) for w in list.__iter__(v) ]) if set_is_iterable(v) else \ '-' * 36 if v is None else str(UUID(int=store.save(v))) elif var_type == 'uuid': getter = lambda v: \ [None if w[0] == '-' else int(UUID(w)) for w in v] \ if type(v) is not unicode else None \ if v[0] == '-' else int(UUID(v)) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(UUID(int=w)) for w in list.__iter__(v) ]) if hasattr(v, '__iter__') else \ '-' * 36 if v is None else str(UUID(int=v)) elif var_type == 'store': setter = lambda v: v.prefix getter = lambda v: self.objects[v] return getter, setter, store
def create_type_delegate(self, var_type): """ Create a variable value delegator for var_type The delegator will convert automatically between the given variable type and the netcdf compatible one Parameters ---------- var_type : str the variable type Returns ------- NetCDFPlus.Value_Delegate the delegator instance """ getter = None setter = None store = None if 'obj.' in var_type or 'uuid.' in var_type: store_name = str(var_type.split('.')[1]) store = self._stores[store_name] base_type = store.content_class # get_is_iterable = lambda v: \ # v.base_cls is not base_type if hasattr(v, 'base_cls') else \ # hasattr(v, '__iter__') get_numpy_iterable = lambda v: isinstance(v, np.ndarray) set_is_iterable = lambda v: \ v.base_cls is not base_type if hasattr(v, 'base_cls') else \ hasattr(v, '__iter__') if var_type == 'int': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type == 'bool': getter = lambda v: v.astype(np.bool).tolist() setter = lambda v: np.array(v, dtype=np.int8) elif var_type == 'index': getter = lambda v: \ [None if int(w) < 0 else int(w) for w in v.tolist()] \ if hasattr(v, '__iter__') else None if int(v) < 0 else int(v) setter = lambda v: \ [-1 if w is None else w for w in v] \ if hasattr(v, '__iter__') else -1 if v is None else v elif var_type == 'float': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type.startswith('numpy.'): pass elif var_type == 'jsonobj': setter = lambda v: self.simplifier.to_json_object(v) getter = lambda v: self.simplifier.from_json(v) elif var_type == 'json': setter = lambda v: self.simplifier.to_json(v) getter = lambda v: self.simplifier.from_json(v) elif var_type.startswith('obj.'): getter = lambda v: [ None if w[0] == '-' else store.load(int(UUID(w))) for w in v ] if get_numpy_iterable(v) else \ None if v[0] == '-' else store.load(int(UUID(v))) setter = lambda v: \ ''.join(['-' * 36 if w is None else str(UUID(int=store.save(w))) for w in list.__iter__(v)]) \ if set_is_iterable(v) else \ '-' * 36 if v is None else str(UUID(int=store.save(v))) elif var_type.startswith('lazyobj.'): getter = lambda v: [ None if w[0] == '-' else LoaderProxy.new(store, int(UUID(w))) for w in v ] if isinstance(v, np.ndarray) else \ None if v[0] == '-' else LoaderProxy.new(store, int(UUID(v))) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(UUID(int=store.save(w))) for w in list.__iter__(v) ]) if set_is_iterable(v) else \ '-' * 36 if v is None else str(UUID(int=store.save(v))) elif var_type == 'uuid': getter = lambda v: \ [None if w[0] == '-' else int(UUID(w)) for w in v] \ if type(v) is not unicode else None \ if v[0] == '-' else int(UUID(v)) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(UUID(int=w)) for w in list.__iter__(v) ]) if hasattr(v, '__iter__') else \ '-' * 36 if v is None else str(UUID(int=v)) elif var_type == 'store': setter = lambda v: v.prefix getter = lambda v: self.objects[v] return getter, setter, store
def create_type_delegate(self, var_type): """ Create a variable value delegator for var_type The delegator will convert automatically between the given variable type and the netcdf compatible one Parameters ---------- var_type : str the variable type Returns ------- NetCDFPlus.Value_Delegate the delegator instance """ getter = None setter = None store = None to_uuid_chunks = lambda x: [x[i:i + 36] for i in range(0, len(x), 36)] if var_type.startswith('obj.') or var_type.startswith('lazyobj.'): store_name = str(var_type.split('.')[1]) store = self._stores[store_name] base_type = store.content_class get_is_iterable = lambda v: \ v.base_cls is not base_type if hasattr(v, 'base_cls') else \ hasattr(v, '__iter__') set_is_iterable = lambda v: \ v.base_cls is not base_type if hasattr(v, 'base_cls') else \ hasattr(v, '__iter__') if var_type == 'int': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type == 'bool': getter = lambda v: v.astype(np.bool).tolist() setter = lambda v: np.array(v, dtype=np.int8) elif var_type == 'index': getter = lambda v: \ [None if int(w) < 0 else int(w) for w in v.tolist()] \ if hasattr(v, '__iter__') else None if int(v) < 0 else int(v) setter = lambda v: \ [-1 if w is None else w for w in v] \ if hasattr(v, '__iter__') else -1 if v is None else v elif var_type == 'float': getter = lambda v: v.tolist() setter = lambda v: np.array(v) elif var_type.startswith('numpy.'): pass elif var_type == 'jsonobj': setter = lambda v: self.simplifier.to_json_object(v) getter = lambda v: self.simplifier.from_json(v) elif var_type == 'json': setter = lambda v: self.simplifier.to_json(v) getter = lambda v: self.simplifier.from_json(v) elif var_type.startswith('obj.'): if not self.reference_by_uuid: getter = lambda v: [ None if int(w) < 0 else store.load(int(w)) for w in v.tolist() ] if get_is_iterable(v) else \ None if int(v) < 0 else store.load(int(v)) setter = lambda v: \ np.array( [-1 if w is None else store.save(w) for w in v], dtype=np.int32 ) if set_is_iterable(v) else \ -1 if v is None else store.save(v) else: getter = lambda v: [ None if w[0] == '-' else store.load(UUID(w)) for w in to_uuid_chunks(v) ] if get_is_iterable(v) else \ None if v[0] == '-' else store.load(UUID(v)) setter = lambda v: \ ''.join(['-' * 36 if w is None else str(store.save(w)) for w in list.__iter__(v)]) \ if set_is_iterable(v) else \ '-' * 36 if v is None else str(store.save(v)) elif var_type == 'obj': # arbitrary object set_iterable_simple = lambda v: \ False if hasattr(v, 'base_cls') else hasattr(v, '__iter__') getter = lambda v: [ None if int(w[1]) < 0 else self.stores[int(w[0])].load(int(w[1])) for w in v.tolist()] \ if len(v.shape) > 1 else None if int(v[1]) < 0 else \ self.stores[int(v[0])].load(int(v[1])) setter = lambda v: \ np.array( [(-1, -1) if w is None else self.save(w)[1:] for w in v], dtype=np.int32) if set_iterable_simple(v) else \ (-1, -1) if v is None else self.save(v)[1:] elif var_type.startswith('lazyobj.'): if not self.reference_by_uuid: getter = lambda v: [ None if int(w) < 0 else LoaderProxy(store, int(w)) for w in v.tolist() ] if get_is_iterable(v) else \ None if int(v) < 0 else LoaderProxy(store, int(v)) setter = lambda v: \ np.array( [-1 if w is None else store.save(w) for w in v], dtype=np.int32) if set_is_iterable(v) else \ -1 if v is None else store.save(v) else: getter = lambda v: [ None if w[0] == '-' else LoaderProxy(store, UUID(w)) for w in to_uuid_chunks(v) ] if get_is_iterable(v) else \ None if v[0] == '-' else LoaderProxy(store, UUID(v)) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(store.save(w)) for w in list.__iter__(v) ]) if set_is_iterable(v) else \ '-' * 36 if v is None else str(store.save(v)) elif var_type == 'uuid': getter = lambda v: \ [None if w[0] == '-' else UUID(w) for w in v] \ if type(v) is not unicode else None if v[0] == '-' else UUID(v) setter = lambda v: \ ''.join([ '-' * 36 if w is None else str(w) for w in list.__iter__(v) ]) if hasattr(v, '__iter__') else \ '-' * 36 if v is None else str(v) elif var_type == 'lazyobj': # arbitrary object set_iterable_simple = lambda v: \ False if hasattr(v, 'base_cls') else hasattr(v, '__iter__') getter = lambda v: [ None if int(w[1]) < 0 else LoaderProxy(self.stores[int(w[0])], int(w[1])) for w in v.tolist() ] if len(v.shape) > 1 else \ None if int(v[1]) < 0 else \ LoaderProxy(self.stores[int(v[0])], int(v[1])) setter = lambda v: \ np.array( [(-1, -1) if w is None else self.save(w)[1:] for w in v], dtype=np.int32 ) if set_iterable_simple(v) else \ (-1, -1) if v is None else self.save(v)[1:] elif var_type == 'store': setter = lambda v: v.prefix getter = lambda v: self.objects[v] return getter, setter, store