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

Replace classmethod() Calls With Decorators #3

Open
ldo opened this issue Oct 9, 2020 · 0 comments
Open

Replace classmethod() Calls With Decorators #3

ldo opened this issue Oct 9, 2020 · 0 comments

Comments

@ldo
Copy link

ldo commented Oct 9, 2020

Cleaner to use the decorator syntax for this:

diff --git a/euclid.py b/euclid.py
index 2f3209a..cd75766 100644
--- a/euclid.py
+++ b/euclid.py
@@ -689,25 +689,26 @@ class Matrix3:
         return self

     # Static constructors
+    @classmethod
     def new_identity(cls):
         self = cls()
         return self
-    new_identity = classmethod(new_identity)

+    @classmethod
     def new_scale(cls, x, y):
         self = cls()
         self.a = x
         self.f = y
         return self
-    new_scale = classmethod(new_scale)

+    @classmethod
     def new_translate(cls, x, y):
         self = cls()
         self.c = x
         self.g = y
         return self
-    new_translate = classmethod(new_translate)

+    @classmethod
     def new_rotate(cls, angle):
         self = cls()
         s = math.sin(angle)
@@ -716,7 +717,6 @@ class Matrix3:
         self.b = -s
         self.e = s
         return self
-    new_rotate = classmethod(new_rotate)

     def determinant(self):
         return (self.a*self.f*self.k
@@ -1001,33 +1001,34 @@ class Matrix4:
         return M

     # Static constructors
+    @classmethod
     def new(cls, *values):
         M = cls()
         M[:] = values
         return M
-    new = classmethod(new)

+    @classmethod
     def new_identity(cls):
         self = cls()
         return self
-    new_identity = classmethod(new_identity)

+    @classmethod
     def new_scale(cls, x, y, z):
         self = cls()
         self.a = x
         self.f = y
         self.k = z
         return self
-    new_scale = classmethod(new_scale)

+    @classmethod
     def new_translate(cls, x, y, z):
         self = cls()
         self.d = x
         self.h = y
         self.l = z
         return self
-    new_translate = classmethod(new_translate)

+    @classmethod
     def new_rotatex(cls, angle):
         self = cls()
         s = math.sin(angle)
@@ -1036,8 +1037,8 @@ class Matrix4:
         self.g = -s
         self.j = s
         return self
-    new_rotatex = classmethod(new_rotatex)

+    @classmethod
     def new_rotatey(cls, angle):
         self = cls()
         s = math.sin(angle)
@@ -1046,8 +1047,8 @@ class Matrix4:
         self.c = s
         self.i = -s
         return self
-    new_rotatey = classmethod(new_rotatey)

+    @classmethod
     def new_rotatez(cls, angle):
         self = cls()
         s = math.sin(angle)
@@ -1056,8 +1057,8 @@ class Matrix4:
         self.b = -s
         self.e = s
         return self
-    new_rotatez = classmethod(new_rotatez)

+    @classmethod
     def new_rotate_axis(cls, angle, axis):
         assert(isinstance(axis, Vector3))
         vector = axis.normalized()
@@ -1081,8 +1082,8 @@ class Matrix4:
         self.j = y * z * c1 + x * s
         self.k = z * z * c1 + c
         return self
-    new_rotate_axis = classmethod(new_rotate_axis)

+    @classmethod
     def new_rotate_euler(cls, heading, attitude, bank):
         # from http://www.euclideanspace.com/
         ch = math.cos(heading)
@@ -1103,8 +1104,8 @@ class Matrix4:
         self.j = sh * sa * cb + ch * sb
         self.k = -sh * sa * sb + ch * cb
         return self
-    new_rotate_euler = classmethod(new_rotate_euler)

+    @classmethod
     def new_rotate_triple_axis(cls, x, y, z):
       m = cls()

@@ -1113,8 +1114,8 @@ class Matrix4:
       m.i, m.j, m.k = x.z, y.z, z.z

       return m
-    new_rotate_triple_axis = classmethod(new_rotate_triple_axis)

+    @classmethod
     def new_look_at(cls, eye, at, up):
       z = (eye - at).normalized()
       x = up.cross(z).normalized()
@@ -1123,8 +1124,8 @@ class Matrix4:
       m = cls.new_rotate_triple_axis(x, y, z)
       m.d, m.h, m.l = eye.x, eye.y, eye.z
       return m
-    new_look_at = classmethod(new_look_at)

+    @classmethod
     def new_perspective(cls, fov_y, aspect, near, far):
         # from the gluPerspective man page
         f = 1 / math.tan(fov_y / 2)
@@ -1137,7 +1138,6 @@ class Matrix4:
         self.o = -1
         self.p = 0
         return self
-    new_perspective = classmethod(new_perspective)

     def determinant(self):
         return ((self.a * self.f - self.e * self.b)
@@ -1393,10 +1393,11 @@ class Quaternion:
         return M

     # Static constructors
+    @classmethod
     def new_identity(cls):
         return cls()
-    new_identity = classmethod(new_identity)

+    @classmethod
     def new_rotate_axis(cls, angle, axis):
         assert(isinstance(axis, Vector3))
         axis = axis.normalized()
@@ -1407,8 +1408,8 @@ class Quaternion:
         Q.y = axis.y * s
         Q.z = axis.z * s
         return Q
-    new_rotate_axis = classmethod(new_rotate_axis)

+    @classmethod
     def new_rotate_euler(cls, heading, attitude, bank):
         Q = cls()
         c1 = math.cos(heading / 2)
@@ -1423,8 +1424,8 @@ class Quaternion:
         Q.y = s1 * c2 * c3 + c1 * s2 * s3
         Q.z = c1 * s2 * c3 - s1 * c2 * s3
         return Q
-    new_rotate_euler = classmethod(new_rotate_euler)

+    @classmethod
     def new_rotate_matrix(cls, m):
       if m[0*4 + 0] + m[1*4 + 1] + m[2*4 + 2] > 0.00000001:
         t = m[0*4 + 0] + m[1*4 + 1] + m[2*4 + 2] + 1.0
@@ -1469,8 +1470,8 @@ class Quaternion:
           (m[1*4 + 2] + m[2*4 + 1])*s,
           s*t
           )
-    new_rotate_matrix = classmethod(new_rotate_matrix)

+    @classmethod
     def new_interpolate(cls, q1, q2, t):
         assert isinstance(q1, Quaternion) and isinstance(q2, Quaternion)
         Q = cls()
@@ -1506,7 +1507,6 @@ class Quaternion:
         Q.y = q1.y * ratio1 + q2.y * ratio2
         Q.z = q1.z * ratio1 + q2.z * ratio2
         return Q
-    new_interpolate = classmethod(new_interpolate)

 # Geometry
 # Much maths thanks to Paul Bourke, http://astronomy.swin.edu.au/~pbourke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant