From 49fac3a5cafcb3ae5ea012fd2977151f909714f6 Mon Sep 17 00:00:00 2001 From: Deep Bhatt Date: Tue, 29 Nov 2022 22:31:42 -0500 Subject: [PATCH 1/2] Implemented IndefiniteList as subclass of UserList. --- pycardano/plutus.py | 2 +- pycardano/serialization.py | 17 +++++---------- test/pycardano/test_serialization.py | 31 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/pycardano/plutus.py b/pycardano/plutus.py index a2e058fe..ff773a02 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -530,7 +530,7 @@ def _dfs(obj): elif isinstance(obj, list): return [_dfs(item) for item in obj] elif isinstance(obj, IndefiniteList): - return {"list": [_dfs(item) for item in obj.items]} + return {"list": [_dfs(item) for item in obj.data]} elif isinstance(obj, dict): return {"map": [{"v": _dfs(v), "k": _dfs(k)} for k, v in obj.items()]} elif isinstance(obj, PlutusData): diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 5e40a150..637e0d4a 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -3,7 +3,7 @@ from __future__ import annotations import re -from collections import OrderedDict, defaultdict +from collections import OrderedDict, defaultdict, UserList from copy import deepcopy from dataclasses import Field, dataclass, fields from datetime import datetime @@ -36,15 +36,8 @@ ] -class IndefiniteList: - def __init__(self, items): - self.items = items - - def __eq__(self, other): - if isinstance(other, IndefiniteList): - return self.items == other.items - else: - return False +class IndefiniteList(UserList): + pass @dataclass @@ -146,7 +139,7 @@ def default_encoder( # handling here to explicitly write header (b'\x9f'), each body item, and footer (b'\xff') to # the output bytestring. encoder.write(b"\x9f") - for item in value.items: + for item in value.data: encoder.encode(item) encoder.write(b"\xff") elif isinstance(value, RawCBOR): @@ -240,7 +233,7 @@ def _dfs(value): elif isinstance(value, list): return [_helper(k) for k in value] elif isinstance(value, IndefiniteList): - return IndefiniteList([_helper(k) for k in value.items]) + return IndefiniteList([_helper(k) for k in value.data]) elif isinstance(value, CBORTag): return CBORTag(value.tag, _helper(value.value)) else: diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index 0f236980..c039827e 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -10,6 +10,7 @@ DictCBORSerializable, MapCBORSerializable, limit_primitive_type, + IndefiniteList ) @@ -133,3 +134,33 @@ def test_dict_cbor_serializable(): # Make sure the cbor of a and b are exactly the same even when their items are inserted in different orders. assert a.to_cbor() == b.to_cbor() + + +def test_indefinite_list(): + + a = IndefiniteList((4, 5)) + + a.append(6) + # append should add element and return IndefiniteList + assert a == IndefiniteList((4,5,6)) and type(a) == IndefiniteList + + b = a + IndefiniteList((7,8)) + # addition of two IndefiniteLists should return IndefiniteList + assert type(b) == IndefiniteList + + a.extend((7,8)) + # extend should add elements and return IndefiniteList + assert a == IndefiniteList((4,5,6,7,8)) and type(a) == IndefiniteList + + # testing eq operator + assert a == b + + b.pop() + # pop should remove last element and return IndefiniteList + assert b == IndefiniteList((4,5,6,7)) and type(b) == IndefiniteList + + b.remove(5) + # remove should remove element and return IndefiniteList + assert b == IndefiniteList((4,6,7)) and type(b) == IndefiniteList + + \ No newline at end of file From 713ff0e80aeb753606ea1316ffc923f77a1025bf Mon Sep 17 00:00:00 2001 From: Deep Bhatt Date: Fri, 2 Dec 2022 16:03:10 -0500 Subject: [PATCH 2/2] fixing code format and static type errors. --- pycardano/plutus.py | 2 +- pycardano/serialization.py | 7 ++++--- test/pycardano/test_serialization.py | 20 +++++++++----------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pycardano/plutus.py b/pycardano/plutus.py index ff773a02..e6a422ef 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -530,7 +530,7 @@ def _dfs(obj): elif isinstance(obj, list): return [_dfs(item) for item in obj] elif isinstance(obj, IndefiniteList): - return {"list": [_dfs(item) for item in obj.data]} + return {"list": [_dfs(item) for item in obj]} elif isinstance(obj, dict): return {"map": [{"v": _dfs(v), "k": _dfs(k)} for k, v in obj.items()]} elif isinstance(obj, PlutusData): diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 637e0d4a..bf9d58a4 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -37,7 +37,8 @@ class IndefiniteList(UserList): - pass + def __init__(self, list: [Primitive]): # type: ignore + super().__init__(list) @dataclass @@ -139,7 +140,7 @@ def default_encoder( # handling here to explicitly write header (b'\x9f'), each body item, and footer (b'\xff') to # the output bytestring. encoder.write(b"\x9f") - for item in value.data: + for item in value: encoder.encode(item) encoder.write(b"\xff") elif isinstance(value, RawCBOR): @@ -233,7 +234,7 @@ def _dfs(value): elif isinstance(value, list): return [_helper(k) for k in value] elif isinstance(value, IndefiniteList): - return IndefiniteList([_helper(k) for k in value.data]) + return IndefiniteList([_helper(k) for k in value]) elif isinstance(value, CBORTag): return CBORTag(value.tag, _helper(value.value)) else: diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index c039827e..5d63c39a 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -10,7 +10,7 @@ DictCBORSerializable, MapCBORSerializable, limit_primitive_type, - IndefiniteList + IndefiniteList, ) @@ -138,29 +138,27 @@ def test_dict_cbor_serializable(): def test_indefinite_list(): - a = IndefiniteList((4, 5)) - + a = IndefiniteList([4, 5]) + a.append(6) # append should add element and return IndefiniteList - assert a == IndefiniteList((4,5,6)) and type(a) == IndefiniteList + assert a == IndefiniteList([4, 5, 6]) and type(a) == IndefiniteList - b = a + IndefiniteList((7,8)) + b = a + IndefiniteList([7, 8]) # addition of two IndefiniteLists should return IndefiniteList assert type(b) == IndefiniteList - a.extend((7,8)) + a.extend([7, 8]) # extend should add elements and return IndefiniteList - assert a == IndefiniteList((4,5,6,7,8)) and type(a) == IndefiniteList + assert a == IndefiniteList([4, 5, 6, 7, 8]) and type(a) == IndefiniteList # testing eq operator assert a == b b.pop() # pop should remove last element and return IndefiniteList - assert b == IndefiniteList((4,5,6,7)) and type(b) == IndefiniteList + assert b == IndefiniteList([4, 5, 6, 7]) and type(b) == IndefiniteList b.remove(5) # remove should remove element and return IndefiniteList - assert b == IndefiniteList((4,6,7)) and type(b) == IndefiniteList - - \ No newline at end of file + assert b == IndefiniteList([4, 6, 7]) and type(b) == IndefiniteList