Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a Field class similar to fields in Django, Marshmallow and DRF #68

Merged
merged 16 commits into from Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions carousel/core/__init__.py
Expand Up @@ -277,3 +277,16 @@ def get_parents(bases, parent):
:rtype: list
"""
return [b for b in bases if isinstance(b, parent)]


class Field(object):
_attrs = []

def __init__(self, *args, **kwargs):
for attr, val in zip(self._attrs, args):
setattr(self, attr, val)
for key, val in kwargs.iteritems():
if key in self._attrs:
setattr(self, key, val)
else:
LOGGER.warning('This key: "%s" is not an attribute.', key)
13 changes: 12 additions & 1 deletion carousel/tests/test_core.py
Expand Up @@ -2,7 +2,7 @@
Test Carousel core.
"""

from carousel.core import Q_, UREG
from carousel.core import Q_, UREG, Field
from nose.tools import eq_, ok_


Expand All @@ -15,3 +15,14 @@ def test_pv_context():
esun = Q_(0.8765, UREG.suns)
ok_(esun.dimensionless)
eq_(esun.to('W / m ** 2', 'pv'), 876.5 * UREG.W / UREG.m / UREG.m)


def test_fields():
"""
Test that Carousel field creates an object with attributes
"""
Field._attrs = ['units', 'isconstant']
test_field = Field('my test field', units='W/m**2', isconstant=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so here 'my test field' will be assigned to the 'units' attribute first? But then replaced by 'W/m**2' it looks like, is that correct?

ok_(isinstance(test_field, Field))
eq_(getattr(test_field, 'units'), 'W/m**2')
return test_field
30 changes: 30 additions & 0 deletions carousel/tests/test_fields.py
@@ -0,0 +1,30 @@
"""
Testing field subclasses in different layers.
"""

from carousel.core import Field, UREG
from carousel.core.data_sources import DataSource
from carousel.contrib.readers import ArgumentReader


class VelocityData(DataSource):
data_reader = ArgumentReader
data_cache_enabled = False
Field._attrs = ['units', 'isconstant', 'uncertainty', 'argpos']
distance = Field(units='m', isconstant=True, uncertainty=0.01, argpos=0)
elapsed_time = Field(units='s', isconstant=True, uncertainty=0.01, argpos=1)

def __prepare_data__(self):
pass


def test_data_with_fields():
v = VelocityData(distance=96540, elapsed_time=3600)
assert isinstance(v, DataSource)
assert v['distance'] == 96540 * UREG.m
assert v['distance'] == 3600 * UREG.s
return v


if __name__ == '__main__':
v = test_data_with_fields()