Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
[Refact] Slotted Model instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 30, 2016
1 parent 78cb211 commit 4c137d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
14 changes: 3 additions & 11 deletions plume/plume.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ def __new__(cls, clsname, bases, attrs):
# Add instance factory class
attrs['_factory'] = namedtuple('InstanceFactory', fieldnames)

# Slots Model and custom Model instances
attrs['__slots__'] = ('_values',)

# Create the new class.
new_class = super().__new__(cls, clsname, bases, attrs)

Expand Down Expand Up @@ -512,17 +515,6 @@ def __init__(self, **kwargs):

kwargs.setdefault('pk', None)
self._values = self._factory(**kwargs)
"""
for attr_name, attr_value in kwargs.items():
if hasattr(self, attr_name):
setattr(self, attr_name, attr_value)
else:
raise AttributeError("<{}> model has no field '{}'.".format(self.__class__.__name__, attr_name))
for fieldname, value in self._values.items():
if getattr(self.__class__, fieldname).required and value is None:
raise AttributeError("<{}> '{}' field is required: you need to provide a value.".format(self.__class__.__name__, fieldname))
"""

def __str__(self):
return '{model}<{values}>'.format(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from plume.plume import Manager, Model

from utils import Trainer

import pytest


Expand Down Expand Up @@ -29,4 +31,17 @@ def test_two_different_models_are_not_equals(self):
m1 = Model(pk=1)
m2 = Model(pk=2)
assert m1 != m2

def test_model_is_sloted(self):
m1 = Model(pk=1)

with pytest.raises(AttributeError):
m1.__dict__

def test_custom_model_is_sloted(self):
sacha = Trainer(name='Sacha', age=42)

with pytest.raises(AttributeError):
sacha.__dict__


0 comments on commit 4c137d3

Please sign in to comment.