From 53dd1798a6022d5b523f2303c3a96f5b86115d25 Mon Sep 17 00:00:00 2001 From: Valdir Stumm Junior Date: Sun, 20 Nov 2016 21:27:49 -0200 Subject: [PATCH] add implementation for set.intersection() --- python/common/org/python/types/Set.java | 7 +++++-- tests/datatypes/test_set.py | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/python/common/org/python/types/Set.java b/python/common/org/python/types/Set.java index df39e6f04f..76aad71876 100644 --- a/python/common/org/python/types/Set.java +++ b/python/common/org/python/types/Set.java @@ -380,10 +380,13 @@ public org.python.Object __iadd__(org.python.Object other) { } @org.python.Method( - __doc__ = "" + __doc__ = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)", + args = {"other"} ) public org.python.Object intersection(org.python.Object other) { - throw new org.python.exceptions.NotImplementedError("intersection() has not been implemented."); + java.util.Set set = ((Set) this.copy()).value; + set.retainAll(((Set) other).value); + return new Set(set); } @org.python.Method( diff --git a/tests/datatypes/test_set.py b/tests/datatypes/test_set.py index 1de30c5995..07af39d71a 100644 --- a/tests/datatypes/test_set.py +++ b/tests/datatypes/test_set.py @@ -100,7 +100,13 @@ def test_discard(self): print(x) """) - + def test_intersection(self): + self.assertCodeExecution(""" + x = {1, 2, 3} + y = {3, 4, 5} + z = x.intersection(y) + print(z) + """) class UnarySetOperationTests(UnaryOperationTestCase, TranspileTestCase):