MCPcopy Create free account
hub / github.com/ccxt/ccxt / assert_structure

Function assert_structure

python/ccxt/test/exchange/base/test_shared_methods.py:60–111  ·  view source on GitHub ↗
(exchange, skipped_properties, method, entry, format, empty_allowed_for=None, deep=False)

Source from the content-addressed store, hash-verified

58
59
60def assert_structure(exchange, skipped_properties, method, entry, format, empty_allowed_for=None, deep=False):
61 log_text = log_template(exchange, method, entry)
62 assert entry is not None, 'item is null/undefined' + log_text
63 # get all expected & predefined keys for this specific item and ensure thos ekeys exist in parsed structure
64 allow_empty_skips = exchange.safe_list(skipped_properties, 'allowNull', [])
65 if empty_allowed_for is not None:
66 empty_allowed_for = concat(empty_allowed_for, allow_empty_skips)
67 if isinstance(format, list):
68 assert isinstance(entry, list), 'entry is not an array' + log_text
69 real_length = len(entry)
70 expected_length = len(format)
71 assert real_length == expected_length, 'entry length is not equal to expected length of ' + str(expected_length) + log_text
72 for i in range(0, len(format)):
73 empty_allowed_for_this_key = (empty_allowed_for is None) or exchange.in_array(i, empty_allowed_for)
74 value = entry[i]
75 if i in skipped_properties:
76 continue
77 # check when:
78 # - it's not inside "allowe empty values" list
79 # - it's not undefined
80 if empty_allowed_for_this_key and (value is None):
81 continue
82 assert value is not None, str(i) + ' index is expected to have a value' + log_text
83 # because of other langs, this is needed for arrays
84 type_assertion = assert_type(exchange, skipped_properties, entry, i, format)
85 assert type_assertion, str(i) + ' index does not have an expected type ' + log_text
86 else:
87 assert exchange.is_dictionary(entry), 'entry is not a dict' + log_text
88 keys = list(format.keys())
89 for i in range(0, len(keys)):
90 key = keys[i]
91 if key in skipped_properties:
92 continue
93 assert key in entry, '"' + string_value(key) + '" key is missing from structure' + log_text
94 if key in skipped_properties:
95 continue
96 empty_allowed_for_this_key = (empty_allowed_for is None) or exchange.in_array(key, empty_allowed_for)
97 value = entry[key]
98 # check when:
99 # - it's not inside "allowe empty values" list
100 # - it's not undefined
101 if empty_allowed_for_this_key and (value is None):
102 continue
103 # if it was in needed keys, then it should have value.
104 assert value is not None, '"' + string_value(key) + '" key is expected to have a value' + log_text
105 # add exclusion for info key, as it can be any type
106 if key != 'info':
107 type_assertion = assert_type(exchange, skipped_properties, entry, key, format)
108 assert type_assertion, '"' + string_value(key) + '" key is neither undefined, neither of expected type' + log_text
109 if deep:
110 if exchange.is_dictionary(value) or isinstance(value, list):
111 assert_structure(exchange, skipped_properties, method, value, format[key], empty_allowed_for, deep)
112
113
114def assert_timestamp(exchange, skipped_properties, method, entry, now_to_check=None, key_name_or_index='timestamp', allow_null=True):

Callers

nothing calls this directly

Calls 8

log_templateFunction · 0.85
assert_typeFunction · 0.85
string_valueFunction · 0.85
safe_listMethod · 0.80
in_arrayMethod · 0.80
is_dictionaryMethod · 0.80
concatFunction · 0.70
rangeFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…