Browse files

coping with PVector static/instance conflicts by using operator overl…

…oading
  • Loading branch information...
1 parent 3f2da8a commit a634dbf0c8a5888399b1aab509bccb58d8f2740d @jdf committed Sep 14, 2010
View
18 runtime/src/jycessing/core.py
@@ -17,14 +17,28 @@
from processing.core import PStyle
from processing.core import PTriangle
from processing.core import PVector
-from processing.core import PVector
+# Thanks, Guido!
+# http://mail.python.org/pipermail/python-dev/2008-January/076194.html
def monkeypatch_method(cls):
def decorator(func):
setattr(cls, func.__name__, func)
return func
return decorator
+# Because PVector isn't serializable, we have to add __deepcopy__
@monkeypatch_method(PVector)
def __deepcopy__(self, memo):
- return PVector(self.x, self.y, self.z)
+ return PVector(self.x, self.y, self.z)
+
+@monkeypatch_method(PVector)
+def __sub__(a, b):
+ return PVector(a.x - b.x, a.y - b.y, a.z - b.z)
+
+@monkeypatch_method(PVector)
+def __add__(a, b):
+ return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
+
+@monkeypatch_method(PVector)
+def __mul__(a, b):
+ return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
View
6 testing/test_resources/test_pvector.py
@@ -0,0 +1,6 @@
+a = PVector(5, 7, 11)
+b = PVector(13, 17, 23)
+print a - b
+print b - a
+print b + a
+print a + b
View
11 testing/tests/test/jycessing/JycessingTests.java
@@ -57,7 +57,8 @@ public void static_size() throws Exception {
@Test
public void set_builtins() throws Exception {
- assertEquals("128\nset(['banana'])\nissubclass: True\nset(['baz'])\n", run("set"));
+ assertEquals("128\nset(['banana'])\nissubclass: True\nset(['baz'])\n",
+ run("set"));
}
@Test
@@ -97,9 +98,15 @@ public void calendar() throws Exception {
@Test
public void processing_core() throws Exception {
- assertEquals("[ 1.0, 2.0, 3.0 ]\n<type 'processing.core.PFont'>\n", run("pcore"));
+ assertEquals("[ 1.0, 2.0, 3.0 ]\n<type 'processing.core.PFont'>\n",
+ run("pcore"));
}
+ // @Test
+ // public void pvector() throws Exception {
+ // assertEquals("[ 8.0, 10.0, 12.0 ]\n", run("pvector"));
+ // }
+
public static void main(final String[] args) {
JUnitCore.runClasses(JycessingTests.class);
}

0 comments on commit a634dbf

Please sign in to comment.