1
1
# Import built-in modules
2
2
from abc import ABC
3
3
from abc import abstractclassmethod
4
+ import sys
4
5
from typing import Any
5
6
6
- from ..utils import *
7
- from ._type_mapper import *
7
+ from ..utils import str2id
8
+ from ._type_mapper import pack
9
+ from ._type_mapper import parsetype
10
+ from ._type_mapper import unpack
8
11
from .action_descriptor_iterator import ActionDescriptor_Iterator
9
12
10
13
11
- class ActionDescriptor :
14
+ class ActionDescriptor ( ABC ) :
12
15
"""A vessel for my extra utils.
13
16
You should not use, and cannot initialize it
14
17
because it is an abstract class."""
@@ -20,7 +23,7 @@ def load(cls, adict: dict, namespace: dict): # pass globals() for namespace
20
23
for k , v in adict .items ():
21
24
if k == "_classID" :
22
25
continue
23
- v = v if (dtype := parsetype (v )) == "others" else namespace [dtype ].load (v )
26
+ v = v if (dtype := parsetype (v )) == "others" else namespace [dtype ].load (v ) # noqa
24
27
new .uput (k , v )
25
28
return new
26
29
@@ -51,7 +54,9 @@ def dump(self) -> dict:
51
54
"""Convert an ActionDescriptor to a python object."""
52
55
# This is a dict comprehension.
53
56
ddict = {"_classID" : self .classID }
54
- ddict .update ({key : (value .dump () if hasattr (value := self .uget (key ), "dump" ) else value ) for key in self })
57
+ ddict .update (
58
+ {key : (value .dump () if hasattr (value := self .uget (key ), "dump" ) else value ) for key in self } # noqa
59
+ )
55
60
return ddict
56
61
57
62
def _unpacker (self ) -> tuple :
@@ -60,3 +65,32 @@ def _unpacker(self) -> tuple:
60
65
raise RuntimeError ("Do not use old methods and new methods mixedly." )
61
66
clsid = str2id (self .classID )
62
67
return (clsid , value )
68
+
69
+
70
+ # Monkey patching for py37
71
+
72
+
73
+ if sys .version .split ("(" )[0 ].split ("." )[1 ] <= 7 :
74
+
75
+ @abstractclassmethod
76
+ def load (cls , adict : dict , namespace : dict ): # pass globals() for namespace
77
+ clsid = adict ["_classID" ] if "_classID" in adict else None
78
+ new = cls (classID = clsid )
79
+ for k , v in adict .items ():
80
+ if k == "_classID" :
81
+ continue
82
+ v = v if parsetype (v ) == "others" else namespace [parsetype (v )].load (v )
83
+ new .uput (k , v )
84
+ return new
85
+
86
+ def dump (self ) -> dict :
87
+ """Convert an ActionDescriptor to a python object."""
88
+ # This is a dict comprehension.
89
+ ddict = {"_classID" : self .classID }
90
+ ddict .update (
91
+ {key : (self .uget (key ).dump () if hasattr (self .uget (key ), "dump" ) else self .uget (key )) for key in self }
92
+ )
93
+ return ddict
94
+
95
+ ActionDescriptor .load = load
96
+ ActionDescriptor .dump = dump
0 commit comments