From 0db1a711b38a718e04a5fb2b9cf0b6fbd7c925cf Mon Sep 17 00:00:00 2001 From: DoDaek Date: Sun, 15 Sep 2019 22:23:05 +0900 Subject: [PATCH 1/3] set: implement __and__ of set --- py/set.go | 14 ++++++++++++++ py/tests/set.py | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 py/tests/set.py diff --git a/py/set.go b/py/set.go index cc27a464..d899e105 100644 --- a/py/set.go +++ b/py/set.go @@ -111,6 +111,20 @@ func (s *Set) M__iter__() (Object, error) { return NewIterator(items), nil } +func (s *Set) M__and__(other Object) (Object, error) { + ret := NewSet() + b, ok := other.(*Set) + if !ok { + return nil, TypeError + } + for i := range b.items { + if _, ok := s.items[i]; ok { + ret.items[i] = SetValue{} + } + } + return ret, nil +} + // Check interface is satisfied var _ I__len__ = (*Set)(nil) var _ I__bool__ = (*Set)(nil) diff --git a/py/tests/set.py b/py/tests/set.py new file mode 100644 index 00000000..733a39e6 --- /dev/null +++ b/py/tests/set.py @@ -0,0 +1,12 @@ +# Copyright 2018 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +doc="__and__" +a = {1, 2, 3} +b = {2, 3, 4, 5} +c = a.__and__(b) +assert 2 in c +assert 3 in c + +doc="finished" \ No newline at end of file From 542fc6844ec1885ca4280b879df9ac212615c8b4 Mon Sep 17 00:00:00 2001 From: DoDaek Date: Sun, 15 Sep 2019 22:37:02 +0900 Subject: [PATCH 2/3] set: add test code of set --- py/tests/set.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/tests/set.py b/py/tests/set.py index 733a39e6..a641e976 100644 --- a/py/tests/set.py +++ b/py/tests/set.py @@ -9,4 +9,8 @@ assert 2 in c assert 3 in c +d = a & b +assert 2 in d +assert 3 in d + doc="finished" \ No newline at end of file From ca6e4118a82bf5b9a2554473ee058e0db1fb70a8 Mon Sep 17 00:00:00 2001 From: DoDaek Date: Mon, 16 Sep 2019 08:39:35 +0900 Subject: [PATCH 3/3] set: modify year of the copyright and return value of the function --- py/set.go | 2 +- py/tests/set.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/py/set.go b/py/set.go index d899e105..f38b32ff 100644 --- a/py/set.go +++ b/py/set.go @@ -115,7 +115,7 @@ func (s *Set) M__and__(other Object) (Object, error) { ret := NewSet() b, ok := other.(*Set) if !ok { - return nil, TypeError + return nil, ExceptionNewf(TypeError, "unsupported operand type(s) for &: '%s' and '%s'", s.Type().Name, other.Type().Name) } for i := range b.items { if _, ok := s.items[i]; ok { diff --git a/py/tests/set.py b/py/tests/set.py index a641e976..e1500ed1 100644 --- a/py/tests/set.py +++ b/py/tests/set.py @@ -1,4 +1,4 @@ -# Copyright 2018 The go-python Authors. All rights reserved. +# Copyright 2019 The go-python Authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file.