Example #1
0
 def __new__(mcs, name, bases, attr):
     # use only with Output subclasses
     if not CommonBase.get_parents(bases, OutputBase):
         return super(OutputBase, mcs).__new__(mcs, name, bases, attr)
     # set param file full path if outputs path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(OutputBase, mcs).__new__(mcs, name, bases, attr)
Example #2
0
 def __new__(mcs, name, bases, attr):
     # use only with Calc subclasses
     if not CommonBase.get_parents(bases, DataSourceBase):
         return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
     # set param file full path if calculation path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
Example #3
0
 def __new__(mcs, name, bases, attr):
     # use only with Output subclasses
     if not CommonBase.get_parents(bases, OutputBase):
         return super(OutputBase, mcs).__new__(mcs, name, bases, attr)
     # set param file full path if outputs path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(OutputBase, mcs).__new__(mcs, name, bases, attr)
Example #4
0
    def __new__(mcs, name, bases, attr):
        # use only with Model subclasses
        if not CommonBase.get_parents(bases, ModelBase):
            return super(ModelBase, mcs).__new__(mcs, name, bases, attr)

        meta = attr.pop('Meta', None)
        if meta is not None:
            # set model file full path if model path and file specified or
            # try to set parameters from class attributes except private/magic
            modelpath = getattr(meta, mcs._path_attr, None)  # mandatory attr: path to models and layers
            modelfile = getattr(meta, mcs._file_attr, None)  # model file
        else:
            modelpath = modelfile = None
        layer_cls_names = attr.get(mcs._layers_cls_attr)

        # check bases for model parameters b/c attr doesn't include bases
        is_path_attr_from_base = False
        is_file_attr_from_base = False
        for base in bases:
            if layer_cls_names is None:
                layer_cls_names = getattr(base, mcs._layers_cls_attr, None)
            # get the Meta class from the base, and check if they have attributes we're missing
            base_meta = getattr(base, 'Meta', None)
            if (modelpath is None) & (hasattr(base_meta, mcs._path_attr)):
                modelpath = getattr(base_meta, mcs._path_attr)
                is_path_attr_from_base = True
            if (modelfile is None) & (hasattr(base_meta, mcs._file_attr)):
                modelfile = getattr(base_meta, mcs._file_attr)
                is_file_attr_from_base = True

        # in case modelpath or modelfile come from a base class, put it into the child's Meta class
        if meta is not None:
            if is_path_attr_from_base:
                setattr(meta, mcs._path_attr, modelpath)
            if is_file_attr_from_base:
                setattr(meta, mcs._file_attr, modelfile)
            attr['_meta'] = meta
        else:  # if we have meta parameters from base but no Meta class in child
            # create Meta class: it is always going to be needed to store the project (model) path: see Model methods
            class Meta:
                pass
            meta = Meta
            # assign the attributes obtained from bases
            if is_path_attr_from_base:
                setattr(meta, mcs._path_attr, modelpath)
            if is_file_attr_from_base:
                setattr(meta, mcs._file_attr, modelfile)
            attr['_meta'] = meta

        if None not in [modelpath, modelfile]:
            attr[mcs._file_attr] = os.path.join(modelpath, 'models', modelfile)  # using same attribute name
        elif layer_cls_names is not None:
            attr['model'] = dict.fromkeys(layer_cls_names)
            for k in attr['model']:
                attr['model'][k] = attr.pop(k, None)
        return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
Example #5
0
 def __new__(mcs, name, bases, attr):
     # use only with Calc subclasses
     if not CommonBase.get_parents(bases, CalcBase):
         return super(CalcBase, mcs).__new__(mcs, name, bases, attr)
     # set _meta combined from bases
     attr = mcs.set_meta(bases, attr)
     # set param file full path if calculations path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(CalcBase, mcs).__new__(mcs, name, bases, attr)
Example #6
0
 def __new__(mcs, name, bases, attr):
     # use only with Formula subclasses
     if not CommonBase.get_parents(bases, FormulaBase):
         return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
     # set _meta combined from bases
     attr = mcs.set_meta(bases, attr)
     # set param file full path if formulas path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
Example #7
0
 def __new__(mcs, name, bases, attr):
     # use only with Formula subclasses
     if not CommonBase.get_parents(bases, FormulaBase):
         return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
     # TODO: convert any methods starting with f_ to static methods
     # for a, v in attr.iteritems():
     #     if a.startswith('f_'):
     #         attr[a] = staticmethod(v)
     # set param file full path if formulas path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
Example #8
0
 def __new__(mcs, name, bases, attr):
     # use only with Formula subclasses
     if not CommonBase.get_parents(bases, FormulaBase):
         return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
     # pop the data reader so it can be overwritten
     importer = attr.pop(mcs._importer_attr, None)
     # set param file full path if formulas path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # set data-reader attribute if in subclass, otherwise read it from base
     if importer is not None:
         attr[mcs._importer_attr] = importer
     return super(FormulaBase, mcs).__new__(mcs, name, bases, attr)
Example #9
0
 def __new__(mcs, name, bases, attr):
     # use only with Model subclasses
     if not CommonBase.get_parents(bases, ModelBase):
         return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
     attr = mcs.set_meta(bases, attr)
     # set param file full path if data source path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # set default meta attributes
     meta = attr[mcs._meta_attr]
     for ma, dflt in mcs._attr_default.iteritems():
         a = getattr(meta, ma, None)
         if a is None:
             setattr(meta, ma, dflt)
     return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
Example #10
0
 def __new__(mcs, name, bases, attr):
     # use only with Model subclasses
     if not CommonBase.get_parents(bases, ModelBase):
         return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
     attr = mcs.set_meta(bases, attr)
     # set param file full path if data source path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # set default meta attributes
     meta = attr[mcs._meta_attr]
     for ma, dflt in mcs._attr_default.iteritems():
         a = getattr(meta, ma, None)
         if a is None:
             setattr(meta, ma, dflt)
     return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
Example #11
0
 def __new__(mcs, name, bases, attr):
     # use only with Simulation subclasses
     if not CommonBase.get_parents(bases, SimBase):
         LOGGER.debug('bases:\n%r', bases)
         return super(SimBase, mcs).__new__(mcs, name, bases, attr)
     # let some attributes in subclasses be override super
     attributes = attr.pop(mcs._attributes, None)
     deprecated = attr.pop(mcs._deprecated, None)
     # set param file full path if simulations path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # reset subclass attributes
     if attributes is not None:
         attr[mcs._attributes] = attributes
     if deprecated is not None:
         attr[mcs._deprecated] = deprecated
     LOGGER.debug('attibutes:\n%r', attr)
     return super(SimBase, mcs).__new__(mcs, name, bases, attr)
Example #12
0
 def __new__(mcs, name, bases, attr):
     # use only with Simulation subclasses
     if not CommonBase.get_parents(bases, SimBase):
         LOGGER.debug('bases:\n%r', bases)
         return super(SimBase, mcs).__new__(mcs, name, bases, attr)
     # let some attributes in subclasses be override super
     attributes = attr.pop(mcs._attributes, None)
     deprecated = attr.pop(mcs._deprecated, None)
     # set param file full path if simulations path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # reset subclass attributes
     if attributes is not None:
         attr[mcs._attributes] = attributes
     if deprecated is not None:
         attr[mcs._deprecated] = deprecated
     LOGGER.debug('attibutes:\n%r', attr)
     return super(SimBase, mcs).__new__(mcs, name, bases, attr)
Example #13
0
 def __new__(mcs, name, bases, attr):
     # use only with DataSource subclasses
     if not CommonBase.get_parents(bases, DataSourceBase):
         return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
     # pop the data reader so it can be overwritten
     reader = attr.pop(mcs._reader_attr, None)
     cache_enabled = attr.pop(mcs._enable_cache_attr, None)
     meta = attr.pop('Meta', None)
     # set param file full path if data source path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # set data-reader attribute if in subclass, otherwise read it from base
     if reader is not None:
         attr[mcs._reader_attr] = reader
     if cache_enabled is not None:
         attr[mcs._enable_cache_attr] = cache_enabled
     if meta is not None:
         attr['_meta'] = meta
     return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
Example #14
0
 def __new__(mcs, name, bases, attr):
     # use only with DataSource subclasses
     if not CommonBase.get_parents(bases, DataSourceBase):
         return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
     # pop the data reader so it can be overwritten
     reader = attr.pop(mcs._reader_attr, None)
     cache_enabled = attr.pop(mcs._enable_cache_attr, None)
     meta = attr.pop('Meta', None)
     # set param file full path if data source path and file specified or
     # try to set parameters from class attributes except private/magic
     attr = mcs.set_param_file_or_parameters(attr)
     # set data-reader attribute if in subclass, otherwise read it from base
     if reader is not None:
         attr['data_reader'] = reader
     if cache_enabled is not None:
         attr['data_cache_enabled'] = cache_enabled
     if meta is not None:
         attr['_meta'] = meta
     return super(DataSourceBase, mcs).__new__(mcs, name, bases, attr)
Example #15
0
 def __new__(mcs, name, bases, attr):
     # use only with Model subclasses
     if not CommonBase.get_parents(bases, ModelBase):
         return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
     # set model file full path if model path and file specified or
     # try to set parameters from class attributes except private/magic
     modelpath = attr.get(mcs._path_attr)  # path to models and layers
     modelfile = attr.pop(mcs._file_attr, None)  # model file
     layer_cls_names = attr.get(mcs._layers_cls_attr)
     # check bases for model parameters b/c attr doesn't include bases
     for base in bases:
         if layer_cls_names is None:
             layer_cls_names = getattr(base, mcs._layers_cls_attr, None)
         if modelpath is None:
             modelpath = getattr(base, mcs._path_attr, None)
         if modelfile is None:
             modelfile = getattr(base, mcs._file_attr, None)
     if None not in [modelpath, modelfile]:
         attr[mcs._file_attr] = os.path.join(modelpath, 'models', modelfile)
     elif layer_cls_names is not None:
         attr['model'] = dict.fromkeys(layer_cls_names)
         for k in attr['model']:
             attr['model'][k] = attr.pop(k, None)
     return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
Example #16
0
 def __new__(mcs, name, bases, attr):
     # use only with Model subclasses
     if not CommonBase.get_parents(bases, ModelBase):
         return super(ModelBase, mcs).__new__(mcs, name, bases, attr)
     # set model file full path if model path and file specified or
     # try to set parameters from class attributes except private/magic
     modelpath = attr.get(mcs._path_attr)  # path to models and layers
     modelfile = attr.pop(mcs._file_attr, None)  # model file
     layer_cls_names = attr.get(mcs._layers_cls_attr)
     # check bases for model parameters b/c attr doesn't include bases
     for base in bases:
         if layer_cls_names is None:
             layer_cls_names = getattr(base, mcs._layers_cls_attr, None)
         if modelpath is None:
             modelpath = getattr(base, mcs._path_attr, None)
         if modelfile is None:
             modelfile = getattr(base, mcs._file_attr, None)
     if None not in [modelpath, modelfile]:
         attr[mcs._file_attr] = os.path.join(modelpath, 'models', modelfile)
     elif layer_cls_names is not None:
         attr['model'] = dict.fromkeys(layer_cls_names)
         for k in attr['model']:
             attr['model'][k] = attr.pop(k, None)
     return super(ModelBase, mcs).__new__(mcs, name, bases, attr)