Skip to content

Commit 7faa94b

Browse files
UPDATE. explicitly assigning NativeScript subclass instantiation type by individual conditional check
UPDATE. simplify from_dict() method UPDATE. create a JSON_TAG_TO_INT map
1 parent 6919848 commit 7faa94b

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

pycardano/nativescript.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ def from_primitive(
4747
)
4848

4949
script_type: int = value[0]
50-
for t in [
51-
ScriptPubkey,
52-
ScriptAll,
53-
ScriptAny,
54-
ScriptNofK,
55-
InvalidBefore,
56-
InvalidHereAfter,
57-
]:
58-
if t._TYPE == script_type: # type: ignore
59-
return super(NativeScript, t).from_primitive(value[1:]) # type: ignore
50+
if script_type == ScriptPubkey._TYPE:
51+
return super(NativeScript, ScriptPubkey).from_primitive(value[1:])
52+
elif script_type == ScriptAll._TYPE:
53+
return super(NativeScript, ScriptAll).from_primitive(value[1:])
54+
elif script_type == ScriptAny._TYPE:
55+
return super(NativeScript, ScriptAny).from_primitive(value[1:])
56+
elif script_type == ScriptNofK._TYPE:
57+
return super(NativeScript, ScriptNofK).from_primitive(value[1:])
58+
elif script_type == InvalidBefore._TYPE:
59+
return super(NativeScript, InvalidBefore).from_primitive(value[1:])
60+
elif script_type == InvalidHereAfter._TYPE:
61+
return super(NativeScript, InvalidHereAfter).from_primitive(value[1:])
6062
else:
6163
raise DeserializeException(f"Unknown script type indicator: {script_type}")
6264

@@ -73,43 +75,16 @@ def from_dict(
7375
ScriptPubkey, ScriptAll, ScriptAny, ScriptNofK, InvalidBefore, InvalidHereAfter
7476
]:
7577
"""Parse a standard native script dictionary (potentially parsed from a JSON file)."""
76-
77-
types = {
78-
p.json_tag: p # type: ignore
79-
for p in [
80-
ScriptPubkey,
81-
ScriptAll,
82-
ScriptAny,
83-
ScriptNofK,
84-
InvalidBefore,
85-
InvalidHereAfter,
86-
]
87-
}
88-
script_type = script_json["type"]
89-
target_class = types[script_type]
9078
script_primitive = cls._script_json_to_primitive(script_json)
91-
return super(NativeScript, target_class).from_primitive(script_primitive[1:]) # type: ignore
79+
return cls.from_primitive(script_primitive)
9280

9381
@classmethod
9482
def _script_json_to_primitive(
9583
cls: Type[NativeScript], script_json: JsonDict
9684
) -> List[Primitive]:
9785
"""Serialize a standard JSON native script into a primitive array"""
98-
99-
types = {
100-
p.json_tag: p # type: ignore
101-
for p in [
102-
ScriptPubkey,
103-
ScriptAll,
104-
ScriptAny,
105-
ScriptNofK,
106-
InvalidBefore,
107-
InvalidHereAfter,
108-
]
109-
}
110-
11186
script_type: str = script_json["type"]
112-
native_script: List[Primitive] = [types[script_type]._TYPE] # type: ignore
87+
native_script: List[Primitive] = [JSON_TAG_TO_INT[script_type]]
11388

11489
for key, value in script_json.items():
11590
if key == "type":
@@ -225,3 +200,13 @@ class InvalidHereAfter(NativeScript):
225200
_TYPE: int = field(default=5, init=False)
226201

227202
after: int
203+
204+
205+
JSON_TAG_TO_INT = {
206+
ScriptPubkey.json_tag: ScriptPubkey._TYPE,
207+
ScriptAll.json_tag: ScriptAll._TYPE,
208+
ScriptAny.json_tag: ScriptAny._TYPE,
209+
ScriptNofK.json_tag: ScriptNofK._TYPE,
210+
InvalidBefore.json_tag: InvalidBefore._TYPE,
211+
InvalidHereAfter.json_tag: InvalidHereAfter._TYPE,
212+
}

0 commit comments

Comments
 (0)