From 93a9f40419772aa4fb6fd191442d6a7a485600b8 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 17 May 2019 14:55:34 -0700 Subject: [PATCH 1/8] Adding iterator method to Dict --- py/dict.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/py/dict.go b/py/dict.go index a849973f..0f76823d 100644 --- a/py/dict.go +++ b/py/dict.go @@ -100,6 +100,16 @@ func (a StringDict) M__repr__() (Object, error) { return String(out.String()), nil } + +func (d StringDict) M__iter__() (Object, error) { + o := make([]Object, 0, len(d)) + for k := range d { + o = append(o, String(k)) + } + return NewIterator(o), nil +} + + func (d StringDict) M__getitem__(key Object) (Object, error) { str, ok := key.(String) if ok { From abedc258de6ce0a18fddbfc8f018ef2a17410903 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 17 May 2019 15:50:25 -0700 Subject: [PATCH 2/8] Fixing formatting --- py/dict.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/py/dict.go b/py/dict.go index 0f76823d..b4cba416 100644 --- a/py/dict.go +++ b/py/dict.go @@ -100,16 +100,14 @@ func (a StringDict) M__repr__() (Object, error) { return String(out.String()), nil } - func (d StringDict) M__iter__() (Object, error) { - o := make([]Object, 0, len(d)) - for k := range d { - o = append(o, String(k)) - } + o := make([]Object, 0, len(d)) + for k := range d { + o = append(o, String(k)) + } return NewIterator(o), nil } - func (d StringDict) M__getitem__(key Object) (Object, error) { str, ok := key.(String) if ok { From 30a14d71f65eed9b76711e5f63deff3d4d348eb3 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 17 May 2019 15:59:45 -0700 Subject: [PATCH 3/8] Adding test for dict iterator --- py/tests/dict.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/tests/dict.py b/py/tests/dict.py index 3581ba53..c180920d 100644 --- a/py/tests/dict.py +++ b/py/tests/dict.py @@ -12,4 +12,8 @@ a = repr({"a":"b","c":5.5}) assert a == "{'a': 'b', 'c': 5.5}" or a == "{'c': 5.5, 'a': 'b'}" +doc="in" +a = repr({"a":"b","c":5.5}) +assert "a" in a + doc="finished" From f85056c5800670cba9b982d1a193a21523ead9bb Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Fri, 24 May 2019 15:19:13 -0700 Subject: [PATCH 4/8] Adding some docs --- py/dict.go | 1 + 1 file changed, 1 insertion(+) diff --git a/py/dict.go b/py/dict.go index b4cba416..57af34d8 100644 --- a/py/dict.go +++ b/py/dict.go @@ -100,6 +100,7 @@ func (a StringDict) M__repr__() (Object, error) { return String(out.String()), nil } +// Returns a list of keys from the dict func (d StringDict) M__iter__() (Object, error) { o := make([]Object, 0, len(d)) for k := range d { From 7d9728d200a2059bc87c617ed6cf77a775db05b2 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Mon, 27 May 2019 18:41:18 -0700 Subject: [PATCH 5/8] Changing docs --- py/tests/dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/tests/dict.py b/py/tests/dict.py index c180920d..acd0c59f 100644 --- a/py/tests/dict.py +++ b/py/tests/dict.py @@ -12,7 +12,7 @@ a = repr({"a":"b","c":5.5}) assert a == "{'a': 'b', 'c': 5.5}" or a == "{'c': 5.5, 'a': 'b'}" -doc="in" +doc="check __iter__" a = repr({"a":"b","c":5.5}) assert "a" in a From 6b4991281c43404209b674611dc577a1e3b9c646 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Mon, 27 May 2019 22:24:32 -0700 Subject: [PATCH 6/8] Fixing error in test --- py/tests/dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/tests/dict.py b/py/tests/dict.py index acd0c59f..edcdf9d1 100644 --- a/py/tests/dict.py +++ b/py/tests/dict.py @@ -13,7 +13,7 @@ assert a == "{'a': 'b', 'c': 5.5}" or a == "{'c': 5.5, 'a': 'b'}" doc="check __iter__" -a = repr({"a":"b","c":5.5}) +a = {"a":"b","c":5.5} assert "a" in a doc="finished" From 36041575a1abe07b6e77bcfd464e90d79a7eaa0f Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Tue, 28 May 2019 10:35:12 -0700 Subject: [PATCH 7/8] Extending dict __iter__ tests --- py/tests/dict.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/py/tests/dict.py b/py/tests/dict.py index edcdf9d1..0672f899 100644 --- a/py/tests/dict.py +++ b/py/tests/dict.py @@ -14,6 +14,9 @@ doc="check __iter__" a = {"a":"b","c":5.5} -assert "a" in a +l = list(iter(a)) +assert "a" in l +assert "c" in l +assert len(l) == 2 doc="finished" From a39291e30ff32bca4ba86e0001315a48fff00822 Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Sat, 1 Jun 2019 15:56:55 -0700 Subject: [PATCH 8/8] Adding items method to dict --- py/dict.go | 11 +++++++++++ py/tests/dict.py | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/py/dict.go b/py/dict.go index 57af34d8..f2b93d26 100644 --- a/py/dict.go +++ b/py/dict.go @@ -27,6 +27,17 @@ var ( expectingDict = ExceptionNewf(TypeError, "a dict is required") ) +func init() { + StringDictType.Dict["items"] = MustNewMethod("items", func(self Object, args Tuple) (Object, error) { + sMap := self.(StringDict) + o := make([]Object, 0, len(sMap)) + for k, v := range sMap { + o = append(o, Tuple{String(k), v}) + } + return NewIterator(o), nil + }, 0, "items() -> list of D's (key, value) pairs, as 2-tuples") +} + // String to object dictionary // // Used for variables etc where the keys can only be strings diff --git a/py/tests/dict.py b/py/tests/dict.py index 0672f899..dcd1f2c9 100644 --- a/py/tests/dict.py +++ b/py/tests/dict.py @@ -19,4 +19,13 @@ assert "c" in l assert len(l) == 2 +doc="check items" +a = {"a":"b","c":5.5} +for k, v in a.items(): + assert k in ["a", "c"] + if k == "a": + assert v == "b" + if k == "c": + assert v == 5.5 + doc="finished"