Skip to content

Commit 1db3a57

Browse files
authored
Modified IndefiniteList as subclass of UserList. (#138)
* Implemented IndefiniteList as subclass of UserList. * fixing code format and static type errors. Co-authored-by: Deep Bhatt <[email protected]>
1 parent 7375f04 commit 1db3a57

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

pycardano/plutus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def _dfs(obj):
530530
elif isinstance(obj, list):
531531
return [_dfs(item) for item in obj]
532532
elif isinstance(obj, IndefiniteList):
533-
return {"list": [_dfs(item) for item in obj.items]}
533+
return {"list": [_dfs(item) for item in obj]}
534534
elif isinstance(obj, dict):
535535
return {"map": [{"v": _dfs(v), "k": _dfs(k)} for k, v in obj.items()]}
536536
elif isinstance(obj, PlutusData):

pycardano/serialization.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import re
6-
from collections import OrderedDict, defaultdict
6+
from collections import OrderedDict, defaultdict, UserList
77
from copy import deepcopy
88
from dataclasses import Field, dataclass, fields
99
from datetime import datetime
@@ -36,15 +36,9 @@
3636
]
3737

3838

39-
class IndefiniteList:
40-
def __init__(self, items):
41-
self.items = items
42-
43-
def __eq__(self, other):
44-
if isinstance(other, IndefiniteList):
45-
return self.items == other.items
46-
else:
47-
return False
39+
class IndefiniteList(UserList):
40+
def __init__(self, list: [Primitive]): # type: ignore
41+
super().__init__(list)
4842

4943

5044
@dataclass
@@ -146,7 +140,7 @@ def default_encoder(
146140
# handling here to explicitly write header (b'\x9f'), each body item, and footer (b'\xff') to
147141
# the output bytestring.
148142
encoder.write(b"\x9f")
149-
for item in value.items:
143+
for item in value:
150144
encoder.encode(item)
151145
encoder.write(b"\xff")
152146
elif isinstance(value, RawCBOR):
@@ -240,7 +234,7 @@ def _dfs(value):
240234
elif isinstance(value, list):
241235
return [_helper(k) for k in value]
242236
elif isinstance(value, IndefiniteList):
243-
return IndefiniteList([_helper(k) for k in value.items])
237+
return IndefiniteList([_helper(k) for k in value])
244238
elif isinstance(value, CBORTag):
245239
return CBORTag(value.tag, _helper(value.value))
246240
else:

test/pycardano/test_serialization.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
DictCBORSerializable,
1111
MapCBORSerializable,
1212
limit_primitive_type,
13+
IndefiniteList,
1314
)
1415

1516

@@ -133,3 +134,31 @@ def test_dict_cbor_serializable():
133134

134135
# Make sure the cbor of a and b are exactly the same even when their items are inserted in different orders.
135136
assert a.to_cbor() == b.to_cbor()
137+
138+
139+
def test_indefinite_list():
140+
141+
a = IndefiniteList([4, 5])
142+
143+
a.append(6)
144+
# append should add element and return IndefiniteList
145+
assert a == IndefiniteList([4, 5, 6]) and type(a) == IndefiniteList
146+
147+
b = a + IndefiniteList([7, 8])
148+
# addition of two IndefiniteLists should return IndefiniteList
149+
assert type(b) == IndefiniteList
150+
151+
a.extend([7, 8])
152+
# extend should add elements and return IndefiniteList
153+
assert a == IndefiniteList([4, 5, 6, 7, 8]) and type(a) == IndefiniteList
154+
155+
# testing eq operator
156+
assert a == b
157+
158+
b.pop()
159+
# pop should remove last element and return IndefiniteList
160+
assert b == IndefiniteList([4, 5, 6, 7]) and type(b) == IndefiniteList
161+
162+
b.remove(5)
163+
# remove should remove element and return IndefiniteList
164+
assert b == IndefiniteList([4, 6, 7]) and type(b) == IndefiniteList

0 commit comments

Comments
 (0)