From ce92055be25b1f9ef34ce68f6f95277dbf95768e Mon Sep 17 00:00:00 2001 From: Yao-Yuan Mao Date: Fri, 24 Aug 2018 09:38:01 -0400 Subject: [PATCH] add attribtue access --- GCR/composite.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/GCR/composite.py b/GCR/composite.py index b0a7df8..4b8eb50 100644 --- a/GCR/composite.py +++ b/GCR/composite.py @@ -76,6 +76,11 @@ class CompositeCatalog(BaseGenericCatalog): If set to a string, the reader will use that column to match to the master catalog and do a left join. + only_use_master_attr : bool, optional (default: False) + If set to False, add-on catalogs will overwrite master catalog's + attributes just like how they overwrite quantities. + If set to True, only the master catalog's attributes are inherited. + Example ------- >>> cat0.list_all_quantities() @@ -100,7 +105,8 @@ class CompositeCatalog(BaseGenericCatalog): >>> cc.get_quantity_modifier('f') ('_2', 'f') """ - def __init__(self, catalog_instances, catalog_identifiers=None, matching_methods=None, **kwargs): + def __init__(self, catalog_instances, catalog_identifiers=None, + matching_methods=None, only_use_master_attr=False, **kwargs): warnings.warn('CompositeCatalog is still an experimental feature. Use with care!') self._catalogs = list() for i, (instance, identifier, matching_method) in enumerate( @@ -152,6 +158,8 @@ def __init__(self, catalog_instances, catalog_identifiers=None, matching_methods self._native_quantities.add(key) self._quantity_modifiers[q] = key + self.only_use_master_attr = bool(only_use_master_attr) + super(CompositeCatalog, self).__init__(**kwargs) def _subclass_init(self, **kwargs): @@ -240,3 +248,10 @@ def _iter_native_dataset(self, native_filters=None): if catalog.matching_format: dataset[catalog.identifier] = next(catalog.iterator, None) yield dataset + + def __getattr__(self, name): + if not self.only_use_master_attr: + for catalog in reversed(self._catalogs): + if hasattr(catalog.instance, name): + return getattr(catalog.instance, name) + return getattr(self.master, name)