MCPcopy Index your code
hub / github.com/TraderAlice/OpenAlice / decode

Function decode

packages/ibkr/ref/source/pythonclient/ibapi/utils.py:87–143  ·  view source on GitHub ↗
(the_type, fields, show_unset=False, use_unicode=False)

Source from the content-addressed store, hash-verified

85
86
87def decode(the_type, fields, show_unset=False, use_unicode=False):
88 try:
89 s = next(fields)
90 except StopIteration:
91 raise BadMessage("no more fields")
92
93 logger.debug("decode %s %s", the_type, s)
94
95 if the_type is Decimal:
96 if (
97 s is None
98 or len(s) == 0
99 or s.decode() == "2147483647"
100 or s.decode() == "9223372036854775807"
101 or s.decode() == "1.7976931348623157E308"
102 or s.decode() == "-9223372036854775808"
103 ):
104 return UNSET_DECIMAL
105 return the_type(s.decode())
106
107 if the_type is str:
108 if type(s) is str:
109 return s
110 if type(s) is bytes:
111 return s.decode(
112 "unicode-escape" if use_unicode else "UTF-8", errors="backslashreplace"
113 )
114 else:
115 raise TypeError(
116 "unsupported incoming type " + type(s) + " for desired type 'str"
117 )
118
119 orig_type = the_type
120 if the_type is bool:
121 the_type = int
122
123 if the_type is float:
124 if s.decode() == INFINITY_STR:
125 return DOUBLE_INFINITY
126
127 if show_unset:
128 if s is None or len(s) == 0:
129 if the_type is float:
130 n = UNSET_DOUBLE
131 elif the_type is int:
132 n = UNSET_INTEGER
133 else:
134 raise TypeError("unsupported desired type for empty value" + the_type)
135 else:
136 n = the_type(s)
137 else:
138 n = the_type(s or 0)
139
140 if orig_type is bool:
141 n = n != 0
142
143 return n
144

Callers 15

decodeMethod · 0.90
decodeMethod · 0.90
decodeMethod · 0.90
decodeMethod · 0.90
decodeMethod · 0.90
decodeOrderIdMethod · 0.90
decodeContractFieldsMethod · 0.90
decodeActionMethod · 0.90
decodeTotalQuantityMethod · 0.90
decodeOrderTypeMethod · 0.90
decodeLmtPriceMethod · 0.90
decodeAuxPriceMethod · 0.90

Calls 4

nextFunction · 0.85
debugMethod · 0.80
BadMessageClass · 0.70
decodeMethod · 0.65

Tested by

no test coverage detected