Skip to content

Commit

Permalink
Merge pull request #55 from mikofski/model_metaclass
Browse files Browse the repository at this point in the history
Model metaclass

* fixes #56 
* model layers can be declared in model class as dictionaries or classes
* consolidate BasicModel into Model class
* adds BaseModel meta class
  • Loading branch information
mikofski committed Sep 22, 2016
2 parents e0e283d + e3d8845 commit 67abaa1
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 153 deletions.
4 changes: 2 additions & 2 deletions carousel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
__author__ = u'Mark Mikofski'
__email__ = u'mark.mikofski@sunpowercorp.com'
__url__ = u'https://github.com/SunPower/Carousel'
__version__ = u'0.2.4'
__release__ = u'Blue Balloons'
__version__ = u'0.2.6'
__release__ = u'Bicycle Bears'
3 changes: 3 additions & 0 deletions carousel/core/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def index_registry(args, arg_key, reg, ts, idx=None):


class CalcBase(CommonBase):
"""
Base calculation meta class.
"""
_path_attr = 'calcs_path'
_file_attr = 'calcs_file'

Expand Down
6 changes: 3 additions & 3 deletions carousel/core/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ def register(self, newdata, *args, **kwargs):

class DataSourceBase(CommonBase):
"""
Most general data source.
Base data source meta class.
"""
_path_attr = 'data_path'
_file_attr = 'data_file'
_reader_attr = 'data_reader'
_enable_cache_attr = 'data_cache_enabled'

def __new__(mcs, name, bases, attr):
# use only with Calc subclasses
# 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 calculation path and file specified or
# 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
Expand Down
23 changes: 13 additions & 10 deletions carousel/core/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@

import importlib
import os
from carousel.core.simulations import SimRegistry
from carousel.core.data_sources import DataRegistry
from carousel.core.formulas import FormulaRegistry
from carousel.core.calculations import CalcRegistry
from carousel.core.outputs import OutputRegistry
from carousel.core.simulations import SimRegistry, Simulation
from carousel.core.data_sources import DataRegistry, DataSource
from carousel.core.formulas import FormulaRegistry, Formula
from carousel.core.calculations import CalcRegistry, Calc
from carousel.core.outputs import OutputRegistry, Output


class Layer(object):
"""
A layer in the model.
:param layer_data: Dictionary of model data specific to this layer.
:type layer_data: dict
:param sources: Dictionary of model parameters specific to this layer.
:type sources: dict
"""
reg_cls = NotImplemented #: registry class
src_cls = NotImplemented #: source class

def __init__(self, sources=None):
#: dictionary of layer sources
Expand Down Expand Up @@ -104,9 +105,6 @@ class Data(Layer):
"""
The Data layer of the model.
:param data: Dictionary of model data specific to the data layer.
:type data: :class:`~carousel.core.data_sources.DataRegistry`
The :attr:`~Layer.layer` attribute is a dictionary of data sources names
as keys of dictionaries for each data source with the module and optionally
the package containing the module, the filename, which can be ``None``,
Expand All @@ -115,6 +113,7 @@ class Data(Layer):
to Carousel is used. External data files should specify the path.
"""
reg_cls = DataRegistry #: data layer registry
src_cls = DataSource #: data layer source

def add(self, data_source, module, package=None):
"""
Expand Down Expand Up @@ -219,6 +218,7 @@ class Formulas(Layer):
Layer containing formulas.
"""
reg_cls = FormulaRegistry #: formula layer registry
src_cls = Formula #: formula layer source

def add(self, formula, module, package=None):
"""
Expand Down Expand Up @@ -259,6 +259,7 @@ class Calculations(Layer):
Layer containing formulas.
"""
reg_cls = CalcRegistry #: calculations layer registry
src_cls = Calc #: calculation layer source

def add(self, calc, module, package=None):
"""
Expand Down Expand Up @@ -289,6 +290,7 @@ class Outputs(Layer):
Layer containing output sources.
"""
reg_cls = OutputRegistry #: output layer registry
src_cls = Output #: output layer source

def add(self, output, module, package=None):
"""
Expand Down Expand Up @@ -318,6 +320,7 @@ class Simulations(Layer):
Layer containing simulation sources.
"""
reg_cls = SimRegistry #: simulation layer registry
src_cls = Simulation #: simulation layer source

def add(self, sim, module, package=None):
"""
Expand Down
Loading

0 comments on commit 67abaa1

Please sign in to comment.