Skip to content

Commit

Permalink
Fix design pattern examples
Browse files Browse the repository at this point in the history
  • Loading branch information
onyb committed Aug 10, 2017
1 parent cc9bcc6 commit 9bfa302
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,5 @@ pyvenv.cfg
pip-selfcheck.json

# End of https://www.gitignore.io/api/python,pycharm,linux,virtualenv

README.rst
9 changes: 4 additions & 5 deletions examples/flyweight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import unittest

from reobject.models.model import Model
from reobject.models import Model, Field


class Card(Model):
def __init__(self, suit, color):
self.suit, self.color = suit, color
suit = Field()
color = Field()


class TestCard(unittest.TestCase):
Expand All @@ -27,8 +27,7 @@ def test_flyweight(self):
c1.delete()
self.assertEqual(Card.objects.count(), 1)

c2.delete()
self.assertEqual(Card.objects.count(), 1)
self.assertNotIn(c2, Card.objects.all())

c3.delete()
self.assertEqual(Card.objects.count(), 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
from copy import deepcopy

from reobject import Model
from reobject.models import Model


class Prototype(Model):
Expand Down
11 changes: 5 additions & 6 deletions examples/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import unittest

from reobject import Model
from reobject.models import Model, Field


class Singleton(Model):
def __init__(self, value=None):
self.value = value
value = Field(default=None)

def __new__(cls, *args, **kwargs):
if Singleton.objects.count():
return Singleton.objects.get()
if cls.objects.count() == 0:
return super().__new__(cls)
else:
return super().__new__(cls, *args, **kwargs)
return cls.objects.get()


class TestSingleton(unittest.TestCase):
Expand Down
6 changes: 5 additions & 1 deletion reobject/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def __new__(cls, name, bases, attrs):

class Model(object, metaclass=ModelBase):
def __attrs_post_init__(self):
return type(self).objects._add(self)
pass

def __new__(cls, *args, **kwargs):
instance = super(Model, cls).__new__(cls)
return cls.objects._add(instance)

@property
def id(self) -> int:
Expand Down

0 comments on commit 9bfa302

Please sign in to comment.