Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified IndefiniteList as subclass of UserList. #138

Merged
merged 2 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pycardano/plutus.py
Expand Up @@ -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]}
elif isinstance(obj, dict):
return {"map": [{"v": _dfs(v), "k": _dfs(k)} for k, v in obj.items()]}
elif isinstance(obj, PlutusData):
Expand Down
18 changes: 6 additions & 12 deletions pycardano/serialization.py
Expand Up @@ -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
Expand Down Expand Up @@ -36,15 +36,9 @@
]


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):
def __init__(self, list: [Primitive]): # type: ignore
super().__init__(list)


@dataclass
Expand Down Expand Up @@ -146,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.items:
for item in value:
encoder.encode(item)
encoder.write(b"\xff")
elif isinstance(value, RawCBOR):
Expand Down Expand Up @@ -240,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.items])
return IndefiniteList([_helper(k) for k in value])
elif isinstance(value, CBORTag):
return CBORTag(value.tag, _helper(value.value))
else:
Expand Down
29 changes: 29 additions & 0 deletions test/pycardano/test_serialization.py
Expand Up @@ -10,6 +10,7 @@
DictCBORSerializable,
MapCBORSerializable,
limit_primitive_type,
IndefiniteList,
)


Expand Down Expand Up @@ -133,3 +134,31 @@ 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