aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-11-01 14:19:58 -0400
committerJesse Luehrs <doy@tozt.net>2014-11-01 14:19:58 -0400
commitf6e1a96ee451a6ef72631a35095781645a56a941 (patch)
treea0ed6fd1788e1fde06649188176908927eee1688
parente0b55720b049c12b3d988614a202fa52b370bea9 (diff)
downloadpython-mop-f6e1a96ee451a6ef72631a35095781645a56a941.tar.gz
python-mop-f6e1a96ee451a6ef72631a35095781645a56a941.zip
use python-style constructors
-rw-r--r--mop/__init__.py53
-rw-r--r--t/database_test.py12
-rw-r--r--t/mop_test.py28
-rw-r--r--t/overrides_test.py70
4 files changed, 84 insertions, 79 deletions
diff --git a/mop/__init__.py b/mop/__init__.py
index 5df6ab8..2bea205 100644
--- a/mop/__init__.py
+++ b/mop/__init__.py
@@ -206,6 +206,11 @@ def bootstrap():
"new", new
))
+ # not strictly necessary, but makes constructors nicer
+ Class.add_method(bootstrap_create_method(
+ "__call__", new
+ ))
+
# Phase 4: Object construction works, just need attributes to construct with
def add_attribute(self, attr):
@@ -224,14 +229,14 @@ def bootstrap():
# and now object creation works! add the method attributes now to allow
# creating method objects
- Method.add_attribute(Attribute.new(name="name"))
- Method.add_attribute(Attribute.new(name="body"))
+ Method.add_attribute(Attribute(name="name"))
+ Method.add_attribute(Attribute(name="body"))
# Phase 5: now we can populate the rest of the mop
def value(self, instance):
return instance.slots[self.name()]
- Attribute.add_method(Method.new(
+ Attribute.add_method(Method(
name="value", body=value
))
@@ -240,23 +245,23 @@ def bootstrap():
def gen_reader(name):
return lambda self: self.metaclass.all_attributes()[name].value(self)
- Method.add_method(Method.new(
+ Method.add_method(Method(
name="name", body=gen_reader("name")
))
- Method.add_method(Method.new(
+ Method.add_method(Method(
name="body", body=gen_reader("body")
))
- Class.add_attribute(Attribute.new(name="name"))
- Class.add_attribute(Attribute.new(name="superclass"))
- Class.add_attribute(Attribute.new(name="attributes", default=lambda: {}))
- Class.add_attribute(Attribute.new(name="methods", default=lambda: {}))
+ Class.add_attribute(Attribute(name="name"))
+ Class.add_attribute(Attribute(name="superclass"))
+ Class.add_attribute(Attribute(name="attributes", default=lambda: {}))
+ Class.add_attribute(Attribute(name="methods", default=lambda: {}))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="name", body=gen_reader("name")
))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="local_methods", body=gen_reader("methods")
))
@@ -265,37 +270,37 @@ def bootstrap():
for c in reversed(self.mro()):
methods.update(c.local_methods())
return methods
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="all_methods", body=all_methods
))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="attribute_class", body=lambda self: Attribute
))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="method_class", body=lambda self: Method
))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="base_object_class", body=lambda self: Object
))
def finalize(self):
for method in self.all_methods().values():
python_install_method(self, method.name(), method)
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="finalize", body=finalize
))
def isa(self, other):
mro = self.metaclass.mro()
return other in mro
- Object.add_method(Method.new(
+ Object.add_method(Method(
name="isa", body=isa
))
def can(self, method_name):
return self.metaclass.all_methods().get(method_name)
- Object.add_method(Method.new(
+ Object.add_method(Method(
name="can", body=can
))
@@ -303,7 +308,7 @@ def bootstrap():
def add_method(self, method):
self.local_methods()[method.name()] = method
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="add_method", body=add_method
))
@@ -323,22 +328,22 @@ def bootstrap():
def execute(self, invocant, args, kwargs):
body = self.metaclass.all_attributes()["body"].value(self)
return execute_method(body, invocant, args, kwargs)
- Method.add_method(Method.new(
+ Method.add_method(Method(
name="execute", body=execute
))
# do the same thing with accessor methods that we installed with our
# temporary version of gen_reader
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="superclass", body=gen_reader("superclass")
))
- Class.add_method(Method.new(
+ Class.add_method(Method(
name="local_attributes", body=gen_reader("attributes")
))
- Attribute.add_method(Method.new(
+ Attribute.add_method(Method(
name="default", body=gen_reader("default")
))
- Attribute.add_method(Method.new(
+ Attribute.add_method(Method(
name="name", body=gen_reader("name")
))
diff --git a/t/database_test.py b/t/database_test.py
index 696b649..b56d07e 100644
--- a/t/database_test.py
+++ b/t/database_test.py
@@ -24,23 +24,23 @@ class DatabaseTest(unittest.TestCase):
"bar": '{"data":[1,2,"b"],"type":"plain"}',
}
- Point = mop.Class.new(
+ Point = mop.Class(
name="Point",
superclass=mop.Class.base_object_class(),
)
- Point.add_attribute(Point.attribute_class().new(name="x"))
- Point.add_attribute(Point.attribute_class().new(name="y"))
- Point.add_method(Point.method_class().new(
+ Point.add_attribute(Point.attribute_class()(name="x"))
+ Point.add_attribute(Point.attribute_class()(name="y"))
+ Point.add_method(Point.method_class()(
name="x",
body=lambda self: self.metaclass.all_attributes()["x"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="y",
body=lambda self: self.metaclass.all_attributes()["y"].value(self)
))
Point.finalize()
- point = Point.new(x=10, y=23)
+ point = Point(x=10, y=23)
assert point.x() == 10
assert point.y() == 23
diff --git a/t/mop_test.py b/t/mop_test.py
index f48d041..c7c8c02 100644
--- a/t/mop_test.py
+++ b/t/mop_test.py
@@ -43,23 +43,23 @@ class MopTest(unittest.TestCase):
assert mop.Attribute.mro() == [ mop.Attribute, mop.Object ]
def test_class_creation(self):
- Point = mop.Class.new(
+ Point = mop.Class(
name="Point",
superclass=mop.Class.base_object_class()
)
- Point.add_attribute(Point.attribute_class().new(name="x", default=0))
- Point.add_attribute(Point.attribute_class().new(name="y", default=0))
+ Point.add_attribute(Point.attribute_class()(name="x", default=0))
+ Point.add_attribute(Point.attribute_class()(name="y", default=0))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="x",
body=lambda self: self.metaclass.all_attributes()["x"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="y",
body=lambda self: self.metaclass.all_attributes()["y"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="set_x",
body=lambda self, new_value: self.metaclass.all_attributes()["x"].set_value(self, new_value)
))
@@ -70,7 +70,7 @@ class MopTest(unittest.TestCase):
assert Point.superclass() is mop.Object
assert Point.mro() == [ Point, mop.Object ]
- point = Point.new(x=1, y=2)
+ point = Point(x=1, y=2)
assert point.isa(Point)
assert point.metaclass is Point
assert point.x() == 1
@@ -78,19 +78,19 @@ class MopTest(unittest.TestCase):
point.set_x(10)
assert point.x() == 10
- point2 = Point.new(x=3, y=4)
+ point2 = Point(x=3, y=4)
assert point is not point2
assert point.x() == 10
assert point.y() == 2
assert point2.x() == 3
assert point2.y() == 4
- Point3D = Point.metaclass.new(
+ Point3D = Point.metaclass(
name="Point3D",
superclass=Point,
)
- Point3D.add_attribute(Point3D.attribute_class().new(name="z", default=0))
- Point3D.add_method(Point3D.method_class().new(
+ Point3D.add_attribute(Point3D.attribute_class()(name="z", default=0))
+ Point3D.add_method(Point3D.method_class()(
name="z",
body=lambda self: self.metaclass.all_attributes()["z"].value(self)
))
@@ -101,7 +101,7 @@ class MopTest(unittest.TestCase):
assert Point3D.superclass() is Point
assert Point3D.mro() == [ Point3D, Point, mop.Object ]
- point3d = Point3D.new(x=3, y=4, z=5)
+ point3d = Point3D(x=3, y=4, z=5)
assert point3d.isa(Point3D)
assert point3d.isa(Point)
assert point3d.isa(mop.Object)
@@ -117,9 +117,9 @@ class MopTest(unittest.TestCase):
assert not point.isa(Point3D)
assert point3d.isa(Point3D)
- point_default = Point.new()
+ point_default = Point()
assert point_default.x() == 0
assert point_default.y() == 0
- point3d_default = Point3D.new()
+ point3d_default = Point3D()
assert point3d_default.x() == 0
assert point3d_default.y() == 0
diff --git a/t/overrides_test.py b/t/overrides_test.py
index 683d071..3993af7 100644
--- a/t/overrides_test.py
+++ b/t/overrides_test.py
@@ -15,39 +15,39 @@ def call_method_at_class(c, method_name, invocant, *args, **kwargs):
class OverridesTest(unittest.TestCase):
def test_accessor_generation(self):
- AccessorsMetaclass = mop.Class.new(
+ AccessorsMetaclass = mop.Class(
name="AccessorsMetaclass",
superclass=mop.Class,
)
def add_attribute(self, attr):
name = attr.name()
call_method_at_class(mop.Class, "add_attribute", self, attr)
- self.add_method(self.method_class().new(
+ self.add_method(self.method_class()(
name=name,
body=lambda self: self.metaclass.all_attributes()[name].value(self),
))
- AccessorsMetaclass.add_method(AccessorsMetaclass.metaclass.method_class().new(
+ AccessorsMetaclass.add_method(AccessorsMetaclass.metaclass.method_class()(
name="add_attribute",
body=add_attribute,
))
AccessorsMetaclass.finalize()
- Point = AccessorsMetaclass.new(
+ Point = AccessorsMetaclass(
name="Point",
superclass=AccessorsMetaclass.base_object_class(),
)
- Point.add_attribute(Point.attribute_class().new(name="x", default=0))
- Point.add_attribute(Point.attribute_class().new(name="y", default=0))
+ Point.add_attribute(Point.attribute_class()(name="x", default=0))
+ Point.add_attribute(Point.attribute_class()(name="y", default=0))
Point.finalize()
- point = Point.new(x=1, y=2)
+ point = Point(x=1, y=2)
assert point.x() == 1
assert point.y() == 2
def test_trace_method_calls(self):
methods_called = []
- TraceMethod = mop.Class.new(
+ TraceMethod = mop.Class(
name="TraceMethod",
superclass=mop.Method,
)
@@ -55,39 +55,39 @@ class OverridesTest(unittest.TestCase):
def execute(self, invocant, args, kwargs):
methods_called.append(self.name())
return call_method_at_class(mop.Method, "execute", self, invocant, args, kwargs)
- TraceMethod.add_method(TraceMethod.metaclass.method_class().new(
+ TraceMethod.add_method(TraceMethod.metaclass.method_class()(
name="execute",
body=execute,
))
TraceMethod.finalize()
- TraceClass = mop.Class.new(
+ TraceClass = mop.Class(
name="TraceClass",
superclass=mop.Class,
)
- TraceClass.add_method(TraceClass.metaclass.method_class().new(
+ TraceClass.add_method(TraceClass.metaclass.method_class()(
name="method_class",
body=lambda self: TraceMethod,
))
TraceClass.finalize()
- Point = TraceClass.new(
+ Point = TraceClass(
name="Point",
superclass=TraceClass.base_object_class(),
)
- Point.add_attribute(Point.attribute_class().new(name="x", default=0))
- Point.add_attribute(Point.attribute_class().new(name="y", default=0))
- Point.add_method(Point.method_class().new(
+ Point.add_attribute(Point.attribute_class()(name="x", default=0))
+ Point.add_attribute(Point.attribute_class()(name="y", default=0))
+ Point.add_method(Point.method_class()(
name="x",
body=lambda self: self.metaclass.all_attributes()["x"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="y",
body=lambda self: self.metaclass.all_attributes()["y"].value(self)
))
Point.finalize()
- point = Point.new(x=1, y=2)
+ point = Point(x=1, y=2)
assert methods_called == []
assert point.x() == 1
assert methods_called == ['x']
@@ -95,79 +95,79 @@ class OverridesTest(unittest.TestCase):
assert methods_called == ['x', 'y']
def test_db_backed_object(self):
- DatabaseAttribute = mop.Class.new(
+ DatabaseAttribute = mop.Class(
name="DatabaseAttribute",
superclass=mop.Attribute,
)
- DatabaseAttribute.add_attribute(DatabaseAttribute.attribute_class().new(
+ DatabaseAttribute.add_attribute(DatabaseAttribute.attribute_class()(
name="db",
))
- DatabaseAttribute.add_method(DatabaseAttribute.method_class().new(
+ DatabaseAttribute.add_method(DatabaseAttribute.method_class()(
name="db",
body=lambda self: self.metaclass.all_attributes()["db"].value(self)
))
def value(self, instance):
key = str(hash(instance)) + ":" + self.name()
return self.db().lookup(key)
- DatabaseAttribute.add_method(DatabaseAttribute.method_class().new(
+ DatabaseAttribute.add_method(DatabaseAttribute.method_class()(
name="value",
body=value,
))
def set_value(self, instance, new_value):
key = str(hash(instance)) + ":" + self.name()
self.db().insert(key, new_value)
- DatabaseAttribute.add_method(DatabaseAttribute.method_class().new(
+ DatabaseAttribute.add_method(DatabaseAttribute.method_class()(
name="set_value",
body=set_value,
))
DatabaseAttribute.finalize()
- DatabaseBackedClass = mop.Class.new(
+ DatabaseBackedClass = mop.Class(
name="DatabaseBackedClass",
superclass=mop.Class,
)
- DatabaseBackedClass.add_attribute(DatabaseBackedClass.attribute_class().new(
+ DatabaseBackedClass.add_attribute(DatabaseBackedClass.attribute_class()(
name="db",
))
- DatabaseBackedClass.add_method(DatabaseBackedClass.method_class().new(
+ DatabaseBackedClass.add_method(DatabaseBackedClass.method_class()(
name="db",
body=lambda self: self.metaclass.all_attributes()["db"].value(self)
))
def add_attribute(self, attr):
attr.metaclass.all_attributes()["db"].set_value(attr, self.db())
call_method_at_class(mop.Class, "add_attribute", self, attr)
- DatabaseBackedClass.add_method(DatabaseBackedClass.method_class().new(
+ DatabaseBackedClass.add_method(DatabaseBackedClass.method_class()(
name="add_attribute",
body=add_attribute,
))
- DatabaseBackedClass.add_method(DatabaseBackedClass.method_class().new(
+ DatabaseBackedClass.add_method(DatabaseBackedClass.method_class()(
name="attribute_class",
body=lambda self: DatabaseAttribute,
))
DatabaseBackedClass.finalize()
- Point = DatabaseBackedClass.new(
+ Point = DatabaseBackedClass(
name="Point",
superclass=DatabaseBackedClass.base_object_class(),
db=InMemoryDatabase(),
)
- Point.add_attribute(Point.attribute_class().new(name="x", default=0))
- Point.add_attribute(Point.attribute_class().new(name="y", default=0))
- Point.add_method(Point.method_class().new(
+ Point.add_attribute(Point.attribute_class()(name="x", default=0))
+ Point.add_attribute(Point.attribute_class()(name="y", default=0))
+ Point.add_method(Point.method_class()(
name="x",
body=lambda self: self.metaclass.all_attributes()["x"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="y",
body=lambda self: self.metaclass.all_attributes()["y"].value(self)
))
- Point.add_method(Point.method_class().new(
+ Point.add_method(Point.method_class()(
name="set_x",
body=lambda self, new_value: self.metaclass.all_attributes()["x"].set_value(self, new_value)
))
Point.finalize()
- point = Point.new(x=3, y=7)
+ point = Point(x=3, y=7)
assert point.x() == 3
assert point.y() == 7
assert point.slots == {}
@@ -177,7 +177,7 @@ class OverridesTest(unittest.TestCase):
assert point.slots == {}
assert len(Point.db().store) == 2
- point2 = Point.new(x=123, y=-5)
+ point2 = Point(x=123, y=-5)
assert point.x() == 12
assert point.y() == 7
assert point.slots == {}