sequence
stringlengths 1.19k
35k
| code
stringlengths 75
8.58k
|
---|---|
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'greedy_max_inden_setcover'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'max_covers'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '17', '23', '29', '33', '152', '159', '166']}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'assignment', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'uncovered_set'}, {'id': '13', 'type': 'call', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '15', 'type': 'argument_list', 'children': ['16']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'rejected_keys'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '22', 'type': 'argument_list', 'children': []}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'accepted_keys'}; {'id': '26', 'type': 'call', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '28', 'type': 'argument_list', 'children': []}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'covered_items_list'}; {'id': '32', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '33', 'type': 'while_statement', 'children': ['34', '35']}; {'id': '34', 'type': 'True', 'children': []}, {'id': '35', 'type': 'block', 'children': ['36', '49', '53', '58', '116', '122', '128', '135', '145']}; {'id': '36', 'type': 'if_statement', 'children': ['37', '47']}, {'id': '37', 'type': 'boolean_operator', 'children': ['38', '41'], 'value': 'and'}; {'id': '38', 'type': 'comparison_operator', 'children': ['39', '40'], 'value': 'is'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'max_covers'}; {'id': '40', 'type': 'None', 'children': []}, {'id': '41', 'type': 'comparison_operator', 'children': ['42', '46'], 'value': '>='}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'covered_items_list'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'max_covers'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'break_statement', 'children': []}, {'id': '49', 'type': 'expression_statement', 'children': ['50']}; {'id': '50', 'type': 'assignment', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'maxkey'}; {'id': '52', 'type': 'None', 'children': []}, {'id': '53', 'type': 'expression_statement', 'children': ['54']}; {'id': '54', 'type': 'assignment', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'maxlen'}; {'id': '56', 'type': 'unary_operator', 'children': ['57'], 'value': '-'}, {'id': '57', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '58', 'type': 'for_statement', 'children': ['59', '62', '68']}, {'id': '59', 'type': 'pattern_list', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'candidate_items'}; {'id': '62', 'type': 'call', 'children': ['63', '66']}, {'id': '63', 'type': 'attribute', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'iteritems'}; {'id': '66', 'type': 'argument_list', 'children': ['67']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}; {'id': '68', 'type': 'block', 'children': ['69', '79', '86']}, {'id': '69', 'type': 'if_statement', 'children': ['70', '77']}; {'id': '70', 'type': 'boolean_operator', 'children': ['71', '74'], 'value': 'or'}, {'id': '71', 'type': 'comparison_operator', 'children': ['72', '73'], 'value': 'in'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'rejected_keys'}; {'id': '74', 'type': 'comparison_operator', 'children': ['75', '76'], 'value': 'in'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'accepted_keys'}, {'id': '77', 'type': 'block', 'children': ['78']}; {'id': '78', 'type': 'continue_statement', 'children': []}, {'id': '79', 'type': 'expression_statement', 'children': ['80']}; {'id': '80', 'type': 'assignment', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'lenval'}; {'id': '82', 'type': 'call', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '84', 'type': 'argument_list', 'children': ['85']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'candidate_items'}; {'id': '86', 'type': 'if_statement', 'children': ['87', '93', '107']}, {'id': '87', 'type': 'call', 'children': ['88', '91']}; {'id': '88', 'type': 'attribute', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'uncovered_set'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'issuperset'}, {'id': '91', 'type': 'argument_list', 'children': ['92']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'candidate_items'}, {'id': '93', 'type': 'block', 'children': ['94']}; {'id': '94', 'type': 'if_statement', 'children': ['95', '98']}, {'id': '95', 'type': 'comparison_operator', 'children': ['96', '97'], 'value': '>'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'lenval'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'maxlen'}; {'id': '98', 'type': 'block', 'children': ['99', '103']}, {'id': '99', 'type': 'expression_statement', 'children': ['100']}; {'id': '100', 'type': 'assignment', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'maxkey'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'assignment', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'maxlen'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'lenval'}, {'id': '107', 'type': 'else_clause', 'children': ['108']}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'call', 'children': ['111', '114']}, {'id': '111', 'type': 'attribute', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'rejected_keys'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '116', 'type': 'if_statement', 'children': ['117', '120']}, {'id': '117', 'type': 'comparison_operator', 'children': ['118', '119'], 'value': 'is'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'maxkey'}, {'id': '119', 'type': 'None', 'children': []}; {'id': '120', 'type': 'block', 'children': ['121']}, {'id': '121', 'type': 'break_statement', 'children': []}; {'id': '122', 'type': 'expression_statement', 'children': ['123']}, {'id': '123', 'type': 'assignment', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'maxval'}, {'id': '125', 'type': 'subscript', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'maxkey'}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'call', 'children': ['130', '133']}; {'id': '130', 'type': 'attribute', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'accepted_keys'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '133', 'type': 'argument_list', 'children': ['134']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'maxkey'}, {'id': '135', 'type': 'expression_statement', 'children': ['136']}; {'id': '136', 'type': 'call', 'children': ['137', '140']}, {'id': '137', 'type': 'attribute', 'children': ['138', '139']}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'covered_items_list'}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '140', 'type': 'argument_list', 'children': ['141']}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'maxval'}, {'id': '145', 'type': 'expression_statement', 'children': ['146']}; {'id': '146', 'type': 'call', 'children': ['147', '150']}, {'id': '147', 'type': 'attribute', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'uncovered_set'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'difference_update'}; {'id': '150', 'type': 'argument_list', 'children': ['151']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'maxval'}; {'id': '152', 'type': 'expression_statement', 'children': ['153']}, {'id': '153', 'type': 'assignment', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'uncovered_items'}, {'id': '155', 'type': 'call', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'uncovered_set'}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'assignment', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'covertup'}; {'id': '162', 'type': 'expression_list', 'children': ['163', '164', '165']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'uncovered_items'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'covered_items_list'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'accepted_keys'}; {'id': '166', 'type': 'return_statement', 'children': ['167']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'covertup'} | def greedy_max_inden_setcover(candidate_sets_dict, items, max_covers=None):
uncovered_set = set(items)
rejected_keys = set()
accepted_keys = set()
covered_items_list = []
while True:
if max_covers is not None and len(covered_items_list) >= max_covers:
break
maxkey = None
maxlen = -1
for key, candidate_items in six.iteritems(candidate_sets_dict):
if key in rejected_keys or key in accepted_keys:
continue
lenval = len(candidate_items)
if uncovered_set.issuperset(candidate_items):
if lenval > maxlen:
maxkey = key
maxlen = lenval
else:
rejected_keys.add(key)
if maxkey is None:
break
maxval = candidate_sets_dict[maxkey]
accepted_keys.add(maxkey)
covered_items_list.append(list(maxval))
uncovered_set.difference_update(maxval)
uncovered_items = list(uncovered_set)
covertup = uncovered_items, covered_items_list, accepted_keys
return covertup |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '17']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'setcover_greedy'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '14']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'set_weights'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'item_values'}, {'id': '13', 'type': 'None', 'children': []}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'max_weight'}; {'id': '16', 'type': 'None', 'children': []}, {'id': '17', 'type': 'block', 'children': ['18', '20', '25', '29', '47', '78', '105', '117', '136', '235']}; {'id': '18', 'type': 'expression_statement', 'children': ['19']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '20', 'type': 'import_statement', 'children': ['21']}, {'id': '21', 'type': 'aliased_import', 'children': ['22', '24']}; {'id': '22', 'type': 'dotted_name', 'children': ['23']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'solution_cover'}; {'id': '28', 'type': 'dictionary', 'children': []}, {'id': '29', 'type': 'if_statement', 'children': ['30', '33']}; {'id': '30', 'type': 'comparison_operator', 'children': ['31', '32'], 'value': 'is'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '32', 'type': 'None', 'children': []}, {'id': '33', 'type': 'block', 'children': ['34']}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '37', 'type': 'call', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'flatten'}, {'id': '41', 'type': 'argument_list', 'children': ['42']}; {'id': '42', 'type': 'call', 'children': ['43', '46']}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '46', 'type': 'argument_list', 'children': []}, {'id': '47', 'type': 'if_statement', 'children': ['48', '51', '56']}; {'id': '48', 'type': 'comparison_operator', 'children': ['49', '50'], 'value': 'is'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'set_weights'}; {'id': '50', 'type': 'None', 'children': []}, {'id': '51', 'type': 'block', 'children': ['52']}; {'id': '52', 'type': 'expression_statement', 'children': ['53']}, {'id': '53', 'type': 'assignment', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'get_weight'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '56', 'type': 'else_clause', 'children': ['57']}, {'id': '57', 'type': 'block', 'children': ['58']}; {'id': '58', 'type': 'function_definition', 'children': ['59', '60', '62']}, {'id': '59', 'type': 'function_name', 'children': [], 'value': 'get_weight'}; {'id': '60', 'type': 'parameters', 'children': ['61']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'solution_cover'}; {'id': '62', 'type': 'block', 'children': ['63']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'call', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'sum'}; {'id': '66', 'type': 'argument_list', 'children': ['67']}, {'id': '67', 'type': 'list_comprehension', 'children': ['68', '71']}; {'id': '68', 'type': 'subscript', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'set_weights'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '71', 'type': 'for_in_clause', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '73', 'type': 'call', 'children': ['74', '77']}; {'id': '74', 'type': 'attribute', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'solution_cover'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '77', 'type': 'argument_list', 'children': []}; {'id': '78', 'type': 'if_statement', 'children': ['79', '82', '87']}, {'id': '79', 'type': 'comparison_operator', 'children': ['80', '81'], 'value': 'is'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'item_values'}, {'id': '81', 'type': 'None', 'children': []}; {'id': '82', 'type': 'block', 'children': ['83']}, {'id': '83', 'type': 'expression_statement', 'children': ['84']}; {'id': '84', 'type': 'assignment', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'get_value'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '87', 'type': 'else_clause', 'children': ['88']}; {'id': '88', 'type': 'block', 'children': ['89']}, {'id': '89', 'type': 'function_definition', 'children': ['90', '91', '93']}; {'id': '90', 'type': 'function_name', 'children': [], 'value': 'get_value'}, {'id': '91', 'type': 'parameters', 'children': ['92']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'vals'}, {'id': '93', 'type': 'block', 'children': ['94']}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'call', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'sum'}, {'id': '97', 'type': 'argument_list', 'children': ['98']}; {'id': '98', 'type': 'list_comprehension', 'children': ['99', '102']}, {'id': '99', 'type': 'subscript', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'item_values'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '102', 'type': 'for_in_clause', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'vals'}, {'id': '105', 'type': 'if_statement', 'children': ['106', '109']}; {'id': '106', 'type': 'comparison_operator', 'children': ['107', '108'], 'value': 'is'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'max_weight'}; {'id': '108', 'type': 'None', 'children': []}, {'id': '109', 'type': 'block', 'children': ['110']}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'assignment', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'max_weight'}, {'id': '113', 'type': 'call', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'get_weight'}, {'id': '115', 'type': 'argument_list', 'children': ['116']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}; {'id': '120', 'type': 'dictionary_comprehension', 'children': ['121', '127']}, {'id': '121', 'type': 'pair', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '123', 'type': 'call', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '125', 'type': 'argument_list', 'children': ['126']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '127', 'type': 'for_in_clause', 'children': ['128', '131']}; {'id': '128', 'type': 'pattern_list', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '131', 'type': 'call', 'children': ['132', '135']}; {'id': '132', 'type': 'attribute', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '135', 'type': 'argument_list', 'children': []}; {'id': '136', 'type': 'while_statement', 'children': ['137', '150']}, {'id': '137', 'type': 'boolean_operator', 'children': ['138', '144'], 'value': 'and'}; {'id': '138', 'type': 'comparison_operator', 'children': ['139', '143'], 'value': '<'}, {'id': '139', 'type': 'call', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'get_weight'}, {'id': '141', 'type': 'argument_list', 'children': ['142']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'solution_cover'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'max_weight'}; {'id': '144', 'type': 'comparison_operator', 'children': ['145', '149'], 'value': '>'}, {'id': '145', 'type': 'call', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '147', 'type': 'argument_list', 'children': ['148']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '149', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '150', 'type': 'block', 'children': ['151', '157', '172', '181', '189', '202', '208', '216', '220']}, {'id': '151', 'type': 'expression_statement', 'children': ['152']}; {'id': '152', 'type': 'call', 'children': ['153', '156']}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '156', 'type': 'argument_list', 'children': []}, {'id': '157', 'type': 'expression_statement', 'children': ['158']}; {'id': '158', 'type': 'assignment', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'uncovered_values'}; {'id': '160', 'type': 'call', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '162', 'type': 'argument_list', 'children': ['163']}, {'id': '163', 'type': 'call', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'map'}, {'id': '165', 'type': 'argument_list', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'get_value'}, {'id': '167', 'type': 'call', 'children': ['168', '171']}; {'id': '168', 'type': 'attribute', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '171', 'type': 'argument_list', 'children': []}; {'id': '172', 'type': 'expression_statement', 'children': ['173']}, {'id': '173', 'type': 'assignment', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'chosen_idx'}, {'id': '175', 'type': 'call', 'children': ['176', '179']}; {'id': '176', 'type': 'attribute', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'argmax'}, {'id': '179', 'type': 'argument_list', 'children': ['180']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'uncovered_values'}, {'id': '181', 'type': 'if_statement', 'children': ['182', '187']}; {'id': '182', 'type': 'comparison_operator', 'children': ['183', '186'], 'value': '<='}, {'id': '183', 'type': 'subscript', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'uncovered_values'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'chosen_idx'}; {'id': '186', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '187', 'type': 'block', 'children': ['188']}; {'id': '188', 'type': 'break_statement', 'children': []}, {'id': '189', 'type': 'expression_statement', 'children': ['190']}; {'id': '190', 'type': 'assignment', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'chosen_key'}; {'id': '192', 'type': 'subscript', 'children': ['193', '201']}, {'id': '193', 'type': 'call', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '195', 'type': 'argument_list', 'children': ['196']}; {'id': '196', 'type': 'call', 'children': ['197', '200']}, {'id': '197', 'type': 'attribute', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '200', 'type': 'argument_list', 'children': []}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'chosen_idx'}; {'id': '202', 'type': 'expression_statement', 'children': ['203']}, {'id': '203', 'type': 'assignment', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'chosen_set'}, {'id': '205', 'type': 'subscript', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'chosen_key'}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'assignment', 'children': ['210', '213']}; {'id': '210', 'type': 'subscript', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'solution_cover'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'chosen_key'}, {'id': '213', 'type': 'subscript', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'candidate_sets_dict'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'chosen_key'}; {'id': '216', 'type': 'delete_statement', 'children': ['217']}, {'id': '217', 'type': 'subscript', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'chosen_key'}; {'id': '220', 'type': 'for_statement', 'children': ['221', '222', '227']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'vals'}; {'id': '222', 'type': 'call', 'children': ['223', '226']}, {'id': '223', 'type': 'attribute', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'avail_covers'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '226', 'type': 'argument_list', 'children': []}, {'id': '227', 'type': 'block', 'children': ['228']}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'call', 'children': ['230', '233']}; {'id': '230', 'type': 'attribute', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'vals'}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'difference_update'}, {'id': '233', 'type': 'argument_list', 'children': ['234']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'chosen_set'}, {'id': '235', 'type': 'return_statement', 'children': ['236']} | def setcover_greedy(candidate_sets_dict, items=None, set_weights=None, item_values=None, max_weight=None):
r
import utool as ut
solution_cover = {}
if items is None:
items = ut.flatten(candidate_sets_dict.values())
if set_weights is None:
get_weight = len
else:
def get_weight(solution_cover):
sum([set_weights[key] for key in solution_cover.keys()])
if item_values is None:
get_value = len
else:
def get_value(vals):
sum([item_values[v] for v in vals])
if max_weight is None:
max_weight = get_weight(candidate_sets_dict)
avail_covers = {key: set(val) for key, val in candidate_sets_dict.items()}
while get_weight(solution_cover) < max_weight and len(avail_covers) > 0:
avail_covers.values()
uncovered_values = list(map(get_value, avail_covers.values()))
chosen_idx = ut.argmax(uncovered_values)
if uncovered_values[chosen_idx] <= 0:
break
chosen_key = list(avail_covers.keys())[chosen_idx]
chosen_set = avail_covers[chosen_key]
solution_cover[chosen_key] = candidate_sets_dict[chosen_key]
del avail_covers[chosen_key]
for vals in avail_covers.values():
vals.difference_update(chosen_set)
return solution_cover |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_nth_prime'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'max_prime'}, {'id': '7', 'type': 'integer', 'children': [], 'value': '4100'}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'safe'}; {'id': '10', 'type': 'True', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '166']}; {'id': '12', 'type': 'if_statement', 'children': ['13', '16', '129']}, {'id': '13', 'type': 'comparison_operator', 'children': ['14', '15'], 'value': '<='}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '15', 'type': 'integer', 'children': [], 'value': '100'}; {'id': '16', 'type': 'block', 'children': ['17', '121']}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'first_100_primes'}; {'id': '20', 'type': 'tuple', 'children': ['21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120']}, {'id': '21', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '22', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '23', 'type': 'integer', 'children': [], 'value': '5'}; {'id': '24', 'type': 'integer', 'children': [], 'value': '7'}, {'id': '25', 'type': 'integer', 'children': [], 'value': '11'}; {'id': '26', 'type': 'integer', 'children': [], 'value': '13'}, {'id': '27', 'type': 'integer', 'children': [], 'value': '17'}; {'id': '28', 'type': 'integer', 'children': [], 'value': '19'}, {'id': '29', 'type': 'integer', 'children': [], 'value': '23'}; {'id': '30', 'type': 'integer', 'children': [], 'value': '29'}, {'id': '31', 'type': 'integer', 'children': [], 'value': '31'}; {'id': '32', 'type': 'integer', 'children': [], 'value': '37'}, {'id': '33', 'type': 'integer', 'children': [], 'value': '41'}; {'id': '34', 'type': 'integer', 'children': [], 'value': '43'}, {'id': '35', 'type': 'integer', 'children': [], 'value': '47'}; {'id': '36', 'type': 'integer', 'children': [], 'value': '53'}, {'id': '37', 'type': 'integer', 'children': [], 'value': '59'}; {'id': '38', 'type': 'integer', 'children': [], 'value': '61'}, {'id': '39', 'type': 'integer', 'children': [], 'value': '67'}; {'id': '40', 'type': 'integer', 'children': [], 'value': '71'}, {'id': '41', 'type': 'integer', 'children': [], 'value': '73'}; {'id': '42', 'type': 'integer', 'children': [], 'value': '79'}, {'id': '43', 'type': 'integer', 'children': [], 'value': '83'}; {'id': '44', 'type': 'integer', 'children': [], 'value': '89'}, {'id': '45', 'type': 'integer', 'children': [], 'value': '97'}; {'id': '46', 'type': 'integer', 'children': [], 'value': '101'}, {'id': '47', 'type': 'integer', 'children': [], 'value': '103'}; {'id': '48', 'type': 'integer', 'children': [], 'value': '107'}, {'id': '49', 'type': 'integer', 'children': [], 'value': '109'}; {'id': '50', 'type': 'integer', 'children': [], 'value': '113'}, {'id': '51', 'type': 'integer', 'children': [], 'value': '127'}; {'id': '52', 'type': 'integer', 'children': [], 'value': '131'}, {'id': '53', 'type': 'integer', 'children': [], 'value': '137'}; {'id': '54', 'type': 'integer', 'children': [], 'value': '139'}, {'id': '55', 'type': 'integer', 'children': [], 'value': '149'}; {'id': '56', 'type': 'integer', 'children': [], 'value': '151'}, {'id': '57', 'type': 'integer', 'children': [], 'value': '157'}; {'id': '58', 'type': 'integer', 'children': [], 'value': '163'}, {'id': '59', 'type': 'integer', 'children': [], 'value': '167'}; {'id': '60', 'type': 'integer', 'children': [], 'value': '173'}, {'id': '61', 'type': 'integer', 'children': [], 'value': '179'}; {'id': '62', 'type': 'integer', 'children': [], 'value': '181'}, {'id': '63', 'type': 'integer', 'children': [], 'value': '191'}; {'id': '64', 'type': 'integer', 'children': [], 'value': '193'}, {'id': '65', 'type': 'integer', 'children': [], 'value': '197'}; {'id': '66', 'type': 'integer', 'children': [], 'value': '199'}, {'id': '67', 'type': 'integer', 'children': [], 'value': '211'}; {'id': '68', 'type': 'integer', 'children': [], 'value': '223'}, {'id': '69', 'type': 'integer', 'children': [], 'value': '227'}; {'id': '70', 'type': 'integer', 'children': [], 'value': '229'}, {'id': '71', 'type': 'integer', 'children': [], 'value': '233'}; {'id': '72', 'type': 'integer', 'children': [], 'value': '239'}, {'id': '73', 'type': 'integer', 'children': [], 'value': '241'}; {'id': '74', 'type': 'integer', 'children': [], 'value': '251'}, {'id': '75', 'type': 'integer', 'children': [], 'value': '257'}; {'id': '76', 'type': 'integer', 'children': [], 'value': '263'}, {'id': '77', 'type': 'integer', 'children': [], 'value': '269'}; {'id': '78', 'type': 'integer', 'children': [], 'value': '271'}, {'id': '79', 'type': 'integer', 'children': [], 'value': '277'}; {'id': '80', 'type': 'integer', 'children': [], 'value': '281'}, {'id': '81', 'type': 'integer', 'children': [], 'value': '283'}; {'id': '82', 'type': 'integer', 'children': [], 'value': '293'}, {'id': '83', 'type': 'integer', 'children': [], 'value': '307'}; {'id': '84', 'type': 'integer', 'children': [], 'value': '311'}, {'id': '85', 'type': 'integer', 'children': [], 'value': '313'}; {'id': '86', 'type': 'integer', 'children': [], 'value': '317'}, {'id': '87', 'type': 'integer', 'children': [], 'value': '331'}; {'id': '88', 'type': 'integer', 'children': [], 'value': '337'}, {'id': '89', 'type': 'integer', 'children': [], 'value': '347'}; {'id': '90', 'type': 'integer', 'children': [], 'value': '349'}, {'id': '91', 'type': 'integer', 'children': [], 'value': '353'}; {'id': '92', 'type': 'integer', 'children': [], 'value': '359'}, {'id': '93', 'type': 'integer', 'children': [], 'value': '367'}; {'id': '94', 'type': 'integer', 'children': [], 'value': '373'}, {'id': '95', 'type': 'integer', 'children': [], 'value': '379'}; {'id': '96', 'type': 'integer', 'children': [], 'value': '383'}, {'id': '97', 'type': 'integer', 'children': [], 'value': '389'}; {'id': '98', 'type': 'integer', 'children': [], 'value': '397'}, {'id': '99', 'type': 'integer', 'children': [], 'value': '401'}; {'id': '100', 'type': 'integer', 'children': [], 'value': '409'}, {'id': '101', 'type': 'integer', 'children': [], 'value': '419'}; {'id': '102', 'type': 'integer', 'children': [], 'value': '421'}, {'id': '103', 'type': 'integer', 'children': [], 'value': '431'}; {'id': '104', 'type': 'integer', 'children': [], 'value': '433'}, {'id': '105', 'type': 'integer', 'children': [], 'value': '439'}; {'id': '106', 'type': 'integer', 'children': [], 'value': '443'}, {'id': '107', 'type': 'integer', 'children': [], 'value': '449'}; {'id': '108', 'type': 'integer', 'children': [], 'value': '457'}, {'id': '109', 'type': 'integer', 'children': [], 'value': '461'}; {'id': '110', 'type': 'integer', 'children': [], 'value': '463'}, {'id': '111', 'type': 'integer', 'children': [], 'value': '467'}; {'id': '112', 'type': 'integer', 'children': [], 'value': '479'}, {'id': '113', 'type': 'integer', 'children': [], 'value': '487'}; {'id': '114', 'type': 'integer', 'children': [], 'value': '491'}, {'id': '115', 'type': 'integer', 'children': [], 'value': '499'}; {'id': '116', 'type': 'integer', 'children': [], 'value': '503'}, {'id': '117', 'type': 'integer', 'children': [], 'value': '509'}; {'id': '118', 'type': 'integer', 'children': [], 'value': '521'}, {'id': '119', 'type': 'integer', 'children': [], 'value': '523'}; {'id': '120', 'type': 'integer', 'children': [], 'value': '541'}, {'id': '121', 'type': 'expression_statement', 'children': ['122']}; {'id': '122', 'type': 'assignment', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'nth_prime'}; {'id': '124', 'type': 'subscript', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'first_100_primes'}; {'id': '126', 'type': 'binary_operator', 'children': ['127', '128'], 'value': '-'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '128', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '129', 'type': 'else_clause', 'children': ['130']}; {'id': '130', 'type': 'block', 'children': ['131']}, {'id': '131', 'type': 'if_statement', 'children': ['132', '133', '157']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'safe'}, {'id': '133', 'type': 'block', 'children': ['134', '151']}; {'id': '134', 'type': 'expression_statement', 'children': ['135']}, {'id': '135', 'type': 'assignment', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'primes'}, {'id': '137', 'type': 'list_comprehension', 'children': ['138', '139', '146']}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '139', 'type': 'for_in_clause', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '143', 'type': 'argument_list', 'children': ['144', '145']}; {'id': '144', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'max_prime'}; {'id': '146', 'type': 'if_clause', 'children': ['147']}, {'id': '147', 'type': 'call', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'is_prime'}, {'id': '149', 'type': 'argument_list', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '151', 'type': 'expression_statement', 'children': ['152']}; {'id': '152', 'type': 'assignment', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'nth_prime'}; {'id': '154', 'type': 'subscript', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'primes'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '157', 'type': 'else_clause', 'children': ['158']}; {'id': '158', 'type': 'block', 'children': ['159']}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'assignment', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'nth_prime'}; {'id': '162', 'type': 'call', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'get_nth_prime_bruteforce'}; {'id': '164', 'type': 'argument_list', 'children': ['165']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '166', 'type': 'return_statement', 'children': ['167']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'nth_prime'} | def get_nth_prime(n, max_prime=4100, safe=True):
if n <= 100:
first_100_primes = (
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439,
443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, )
nth_prime = first_100_primes[n - 1]
else:
if safe:
primes = [num for num in range(2, max_prime) if is_prime(num)]
nth_prime = primes[n]
else:
nth_prime = get_nth_prime_bruteforce(n)
return nth_prime |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'knapsack_ilp'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '8', 'type': 'False', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '13', '23', '33', '43', '55', '82', '104', '129', '140', '152', '171', '187', '234']}; {'id': '10', 'type': 'import_statement', 'children': ['11']}, {'id': '11', 'type': 'dotted_name', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'pulp'}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'assignment', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '16', 'type': 'list_comprehension', 'children': ['17', '20']}, {'id': '17', 'type': 'subscript', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '19', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '20', 'type': 'for_in_clause', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'weights'}; {'id': '26', 'type': 'list_comprehension', 'children': ['27', '30']}, {'id': '27', 'type': 'subscript', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '29', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '30', 'type': 'for_in_clause', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'indices'}; {'id': '36', 'type': 'list_comprehension', 'children': ['37', '40']}, {'id': '37', 'type': 'subscript', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '39', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '40', 'type': 'for_in_clause', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'assignment', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'prob'}; {'id': '46', 'type': 'call', 'children': ['47', '50']}, {'id': '47', 'type': 'attribute', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'pulp'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'LpProblem'}; {'id': '50', 'type': 'argument_list', 'children': ['51', '52']}, {'id': '51', 'type': 'string', 'children': [], 'value': '"Knapsack"'}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'pulp'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'LpMaximize'}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'assignment', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '58', 'type': 'call', 'children': ['59', '64']}, {'id': '59', 'type': 'attribute', 'children': ['60', '63']}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'pulp'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'LpVariable'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'dicts'}; {'id': '64', 'type': 'argument_list', 'children': ['65', '68', '71', '74', '77']}, {'id': '65', 'type': 'keyword_argument', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '67', 'type': 'string', 'children': [], 'value': "'x'"}; {'id': '68', 'type': 'keyword_argument', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'indexs'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'indices'}, {'id': '71', 'type': 'keyword_argument', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'lowBound'}, {'id': '73', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '74', 'type': 'keyword_argument', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'upBound'}; {'id': '76', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '77', 'type': 'keyword_argument', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'cat'}, {'id': '79', 'type': 'attribute', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'pulp'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'LpInteger'}; {'id': '82', 'type': 'expression_statement', 'children': ['83']}, {'id': '83', 'type': 'assignment', 'children': ['84', '87']}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'prob'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'objective'}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'sum'}, {'id': '89', 'type': 'generator_expression', 'children': ['90', '95']}; {'id': '90', 'type': 'binary_operator', 'children': ['91', '92'], 'value': '*'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '92', 'type': 'subscript', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '95', 'type': 'for_in_clause', 'children': ['96', '99']}; {'id': '96', 'type': 'pattern_list', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '99', 'type': 'call', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'indices'}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'prob'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'comparison_operator', 'children': ['111', '128'], 'value': '<='}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'sum'}, {'id': '113', 'type': 'generator_expression', 'children': ['114', '119']}; {'id': '114', 'type': 'binary_operator', 'children': ['115', '116'], 'value': '*'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '116', 'type': 'subscript', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '119', 'type': 'for_in_clause', 'children': ['120', '123']}; {'id': '120', 'type': 'pattern_list', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '123', 'type': 'call', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '125', 'type': 'argument_list', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'indices'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'maxweight'}, {'id': '129', 'type': 'expression_statement', 'children': ['130']}; {'id': '130', 'type': 'call', 'children': ['131', '138']}, {'id': '131', 'type': 'attribute', 'children': ['132', '137']}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'pulp'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'PULP_CBC_CMD'}; {'id': '136', 'type': 'argument_list', 'children': []}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'solve'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'prob'}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'assignment', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'flags'}, {'id': '143', 'type': 'list_comprehension', 'children': ['144', '149']}; {'id': '144', 'type': 'attribute', 'children': ['145', '148']}, {'id': '145', 'type': 'subscript', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'varValue'}, {'id': '149', 'type': 'for_in_clause', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'indices'}; {'id': '152', 'type': 'expression_statement', 'children': ['153']}, {'id': '153', 'type': 'assignment', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'total_value'}, {'id': '155', 'type': 'call', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'sum'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'list_comprehension', 'children': ['159', '160', '169']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '160', 'type': 'for_in_clause', 'children': ['161', '164']}, {'id': '161', 'type': 'pattern_list', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'flag'}; {'id': '164', 'type': 'call', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '166', 'type': 'argument_list', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'flags'}, {'id': '169', 'type': 'if_clause', 'children': ['170']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'flag'}, {'id': '171', 'type': 'expression_statement', 'children': ['172']}; {'id': '172', 'type': 'assignment', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'items_subset'}; {'id': '174', 'type': 'list_comprehension', 'children': ['175', '176', '185']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '176', 'type': 'for_in_clause', 'children': ['177', '180']}, {'id': '177', 'type': 'pattern_list', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'flag'}; {'id': '180', 'type': 'call', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '182', 'type': 'argument_list', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'flags'}, {'id': '185', 'type': 'if_clause', 'children': ['186']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'flag'}, {'id': '187', 'type': 'if_statement', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '189', 'type': 'block', 'children': ['190', '195', '200', '226']}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'call', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '193', 'type': 'argument_list', 'children': ['194']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'prob'}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'call', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '198', 'type': 'argument_list', 'children': ['199']}, {'id': '199', 'type': 'string', 'children': [], 'value': "'OPT:'"}; {'id': '200', 'type': 'expression_statement', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '203', 'type': 'argument_list', 'children': ['204']}; {'id': '204', 'type': 'call', 'children': ['205', '208']}, {'id': '205', 'type': 'attribute', 'children': ['206', '207']}; {'id': '206', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '208', 'type': 'argument_list', 'children': ['209']}, {'id': '209', 'type': 'list_comprehension', 'children': ['210', '223']}; {'id': '210', 'type': 'binary_operator', 'children': ['211', '212'], 'value': '%'}, {'id': '211', 'type': 'string', 'children': [], 'value': "' %s = %s'"}; {'id': '212', 'type': 'tuple', 'children': ['213', '218']}, {'id': '213', 'type': 'attribute', 'children': ['214', '217']}; {'id': '214', 'type': 'subscript', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '218', 'type': 'attribute', 'children': ['219', '222']}, {'id': '219', 'type': 'subscript', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'varValue'}, {'id': '223', 'type': 'for_in_clause', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'indices'}; {'id': '226', 'type': 'expression_statement', 'children': ['227']}, {'id': '227', 'type': 'call', 'children': ['228', '229']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '229', 'type': 'argument_list', 'children': ['230']}; {'id': '230', 'type': 'binary_operator', 'children': ['231', '232'], 'value': '%'}, {'id': '231', 'type': 'string', 'children': [], 'value': "'total_value = %r'"}; {'id': '232', 'type': 'tuple', 'children': ['233']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'total_value'}; {'id': '234', 'type': 'return_statement', 'children': ['235']}, {'id': '235', 'type': 'expression_list', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'total_value'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'items_subset'} | def knapsack_ilp(items, maxweight, verbose=False):
import pulp
values = [t[0] for t in items]
weights = [t[1] for t in items]
indices = [t[2] for t in items]
prob = pulp.LpProblem("Knapsack", pulp.LpMaximize)
x = pulp.LpVariable.dicts(name='x', indexs=indices,
lowBound=0, upBound=1, cat=pulp.LpInteger)
prob.objective = sum(v * x[i] for v, i in zip(values, indices))
prob.add(sum(w * x[i] for w, i in zip(weights, indices)) <= maxweight)
pulp.PULP_CBC_CMD().solve(prob)
flags = [x[i].varValue for i in indices]
total_value = sum([val for val, flag in zip(values, flags) if flag])
items_subset = [item for item, flag in zip(items, flags) if flag]
if verbose:
print(prob)
print('OPT:')
print('\n'.join([' %s = %s' % (x[i].name, x[i].varValue) for i in indices]))
print('total_value = %r' % (total_value,))
return total_value, items_subset |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'knapsack_iterative_int'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '19', '29', '35', '49', '61', '65', '80', '203', '207', '242', '249', '259', '272']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '12', 'type': 'list_comprehension', 'children': ['13', '16']}, {'id': '13', 'type': 'subscript', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '15', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '16', 'type': 'for_in_clause', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'assignment', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'weights'}; {'id': '22', 'type': 'list_comprehension', 'children': ['23', '26']}, {'id': '23', 'type': 'subscript', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '25', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '26', 'type': 'for_in_clause', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'maxsize'}; {'id': '32', 'type': 'binary_operator', 'children': ['33', '34'], 'value': '+'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '34', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'lambda', 'children': ['42']}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'lambda', 'children': ['46']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'inf'}, {'id': '49', 'type': 'expression_statement', 'children': ['50']}; {'id': '50', 'type': 'assignment', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '52', 'type': 'call', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '54', 'type': 'argument_list', 'children': ['55']}, {'id': '55', 'type': 'lambda', 'children': ['56']}; {'id': '56', 'type': 'call', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '58', 'type': 'argument_list', 'children': ['59']}, {'id': '59', 'type': 'lambda', 'children': ['60']}; {'id': '60', 'type': 'False', 'children': []}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}; {'id': '64', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '65', 'type': 'for_statement', 'children': ['66', '67', '71']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '67', 'type': 'call', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'maxsize'}, {'id': '71', 'type': 'block', 'children': ['72']}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '79']}; {'id': '74', 'type': 'subscript', 'children': ['75', '78']}, {'id': '75', 'type': 'subscript', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'dpmat'}, {'id': '77', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '79', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '80', 'type': 'for_statement', 'children': ['81', '82', '89']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '82', 'type': 'call', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '84', 'type': 'argument_list', 'children': ['85']}, {'id': '85', 'type': 'call', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '87', 'type': 'argument_list', 'children': ['88']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '89', 'type': 'block', 'children': ['90', '96', '102']}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'item_val'}, {'id': '93', 'type': 'subscript', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'item_weight'}, {'id': '99', 'type': 'subscript', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '102', 'type': 'for_statement', 'children': ['103', '104', '108']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '104', 'type': 'call', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '106', 'type': 'argument_list', 'children': ['107']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'maxsize'}; {'id': '108', 'type': 'block', 'children': ['109', '115', '152', '158', '164']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'valid_item'}; {'id': '112', 'type': 'comparison_operator', 'children': ['113', '114'], 'value': '<='}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'item_weight'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '115', 'type': 'if_statement', 'children': ['116', '119', '142']}; {'id': '116', 'type': 'comparison_operator', 'children': ['117', '118'], 'value': '>'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '118', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '119', 'type': 'block', 'children': ['120', '130']}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'assignment', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'prev_val'}, {'id': '123', 'type': 'subscript', 'children': ['124', '129']}; {'id': '124', 'type': 'subscript', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '126', 'type': 'binary_operator', 'children': ['127', '128'], 'value': '-'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '128', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'assignment', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'prev_noitem_val'}, {'id': '133', 'type': 'subscript', 'children': ['134', '139']}; {'id': '134', 'type': 'subscript', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '136', 'type': 'binary_operator', 'children': ['137', '138'], 'value': '-'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '138', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '139', 'type': 'binary_operator', 'children': ['140', '141'], 'value': '-'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'item_weight'}; {'id': '142', 'type': 'else_clause', 'children': ['143']}, {'id': '143', 'type': 'block', 'children': ['144', '148']}; {'id': '144', 'type': 'expression_statement', 'children': ['145']}, {'id': '145', 'type': 'assignment', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'prev_val'}, {'id': '147', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'prev_noitem_val'}, {'id': '151', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '152', 'type': 'expression_statement', 'children': ['153']}, {'id': '153', 'type': 'assignment', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '155', 'type': 'binary_operator', 'children': ['156', '157'], 'value': '+'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'item_val'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'prev_noitem_val'}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}, {'id': '161', 'type': 'comparison_operator', 'children': ['162', '163'], 'value': '>'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'prev_val'}; {'id': '164', 'type': 'if_statement', 'children': ['165', '168', '185']}, {'id': '165', 'type': 'boolean_operator', 'children': ['166', '167'], 'value': 'and'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'valid_item'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}; {'id': '168', 'type': 'block', 'children': ['169', '177']}, {'id': '169', 'type': 'expression_statement', 'children': ['170']}; {'id': '170', 'type': 'assignment', 'children': ['171', '176']}, {'id': '171', 'type': 'subscript', 'children': ['172', '175']}; {'id': '172', 'type': 'subscript', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '177', 'type': 'expression_statement', 'children': ['178']}; {'id': '178', 'type': 'assignment', 'children': ['179', '184']}, {'id': '179', 'type': 'subscript', 'children': ['180', '183']}; {'id': '180', 'type': 'subscript', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '184', 'type': 'True', 'children': []}, {'id': '185', 'type': 'else_clause', 'children': ['186']}; {'id': '186', 'type': 'block', 'children': ['187', '195']}, {'id': '187', 'type': 'expression_statement', 'children': ['188']}; {'id': '188', 'type': 'assignment', 'children': ['189', '194']}, {'id': '189', 'type': 'subscript', 'children': ['190', '193']}; {'id': '190', 'type': 'subscript', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'prev_val'}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'assignment', 'children': ['197', '202']}, {'id': '197', 'type': 'subscript', 'children': ['198', '201']}; {'id': '198', 'type': 'subscript', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '202', 'type': 'False', 'children': []}, {'id': '203', 'type': 'expression_statement', 'children': ['204']}; {'id': '204', 'type': 'assignment', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'K'}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'maxweight'}, {'id': '207', 'type': 'for_statement', 'children': ['208', '209', '219']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '209', 'type': 'call', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'reversed'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'call', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '214', 'type': 'argument_list', 'children': ['215']}, {'id': '215', 'type': 'call', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '217', 'type': 'argument_list', 'children': ['218']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '219', 'type': 'block', 'children': ['220']}; {'id': '220', 'type': 'if_statement', 'children': ['221', '226']}, {'id': '221', 'type': 'subscript', 'children': ['222', '225']}; {'id': '222', 'type': 'subscript', 'children': ['223', '224']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'K'}; {'id': '226', 'type': 'block', 'children': ['227', '234']}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'call', 'children': ['229', '232']}, {'id': '229', 'type': 'attribute', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '232', 'type': 'argument_list', 'children': ['233']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '234', 'type': 'expression_statement', 'children': ['235']}, {'id': '235', 'type': 'assignment', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'K'}, {'id': '237', 'type': 'binary_operator', 'children': ['238', '239'], 'value': '-'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'K'}, {'id': '239', 'type': 'subscript', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '242', 'type': 'expression_statement', 'children': ['243']}, {'id': '243', 'type': 'assignment', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '245', 'type': 'call', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '247', 'type': 'argument_list', 'children': ['248']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'assignment', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'items_subset'}; {'id': '252', 'type': 'list_comprehension', 'children': ['253', '256']}, {'id': '253', 'type': 'subscript', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '256', 'type': 'for_in_clause', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '259', 'type': 'expression_statement', 'children': ['260']}; {'id': '260', 'type': 'assignment', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'total_value'}; {'id': '262', 'type': 'subscript', 'children': ['263', '271']}, {'id': '263', 'type': 'subscript', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'dpmat'}, {'id': '265', 'type': 'binary_operator', 'children': ['266', '270'], 'value': '-'}; {'id': '266', 'type': 'call', 'children': ['267', '268']}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '268', 'type': 'argument_list', 'children': ['269']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '270', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '272', 'type': 'return_statement', 'children': ['273']}, {'id': '273', 'type': 'expression_list', 'children': ['274', '275']}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'total_value'}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'items_subset'} | def knapsack_iterative_int(items, maxweight):
r
values = [t[0] for t in items]
weights = [t[1] for t in items]
maxsize = maxweight + 1
dpmat = defaultdict(lambda: defaultdict(lambda: np.inf))
kmat = defaultdict(lambda: defaultdict(lambda: False))
idx_subset = []
for w in range(maxsize):
dpmat[0][w] = 0
for idx in range(len(items)):
item_val = values[idx]
item_weight = weights[idx]
for w in range(maxsize):
valid_item = item_weight <= w
if idx > 0:
prev_val = dpmat[idx - 1][w]
prev_noitem_val = dpmat[idx - 1][w - item_weight]
else:
prev_val = 0
prev_noitem_val = 0
withitem_val = item_val + prev_noitem_val
more_valuable = withitem_val > prev_val
if valid_item and more_valuable:
dpmat[idx][w] = withitem_val
kmat[idx][w] = True
else:
dpmat[idx][w] = prev_val
kmat[idx][w] = False
K = maxweight
for idx in reversed(range(len(items))):
if kmat[idx][K]:
idx_subset.append(idx)
K = K - weights[idx]
idx_subset = sorted(idx_subset)
items_subset = [items[i] for i in idx_subset]
total_value = dpmat[len(items) - 1][maxweight]
return total_value, items_subset |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'knapsack_iterative_numpy'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '6', 'type': 'block', 'children': ['7', '16', '24', '38', '44', '58', '66', '75', '81', '98', '118', '122', '137', '235', '239', '274', '281', '291', '304']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'array'}; {'id': '14', 'type': 'argument_list', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '19', 'type': 'subscript', 'children': ['20', '23']}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'T'}, {'id': '23', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '24', 'type': 'expression_statement', 'children': ['25']}, {'id': '25', 'type': 'assignment', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'max_exp'}, {'id': '27', 'type': 'call', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'max'}, {'id': '29', 'type': 'argument_list', 'children': ['30']}; {'id': '30', 'type': 'list_comprehension', 'children': ['31', '35']}, {'id': '31', 'type': 'call', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'number_of_decimals'}, {'id': '33', 'type': 'argument_list', 'children': ['34']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'w_'}, {'id': '35', 'type': 'for_in_clause', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'w_'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'weights'}; {'id': '38', 'type': 'expression_statement', 'children': ['39']}, {'id': '39', 'type': 'assignment', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'coeff'}, {'id': '41', 'type': 'binary_operator', 'children': ['42', '43'], 'value': '**'}; {'id': '42', 'type': 'integer', 'children': [], 'value': '10'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'max_exp'}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '47', 'type': 'call', 'children': ['48', '54']}; {'id': '48', 'type': 'attribute', 'children': ['49', '53']}, {'id': '49', 'type': 'parenthesized_expression', 'children': ['50']}; {'id': '50', 'type': 'binary_operator', 'children': ['51', '52'], 'value': '*'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'weights'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'coeff'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'astype'}; {'id': '54', 'type': 'argument_list', 'children': ['55']}, {'id': '55', 'type': 'attribute', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '58', 'type': 'expression_statement', 'children': ['59']}, {'id': '59', 'type': 'assignment', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '61', 'type': 'subscript', 'children': ['62', '65']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'T'}, {'id': '65', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'assignment', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'MAXWEIGHT'}, {'id': '69', 'type': 'call', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'int'}, {'id': '71', 'type': 'argument_list', 'children': ['72']}; {'id': '72', 'type': 'binary_operator', 'children': ['73', '74'], 'value': '*'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'coeff'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'W_SIZE'}; {'id': '78', 'type': 'binary_operator', 'children': ['79', '80'], 'value': '+'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'MAXWEIGHT'}; {'id': '80', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '81', 'type': 'expression_statement', 'children': ['82']}; {'id': '82', 'type': 'assignment', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '84', 'type': 'call', 'children': ['85', '88']}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'full'}; {'id': '88', 'type': 'argument_list', 'children': ['89', '95']}, {'id': '89', 'type': 'tuple', 'children': ['90', '94']}; {'id': '90', 'type': 'call', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'W_SIZE'}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'inf'}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'assignment', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'kmat'}, {'id': '101', 'type': 'call', 'children': ['102', '105']}; {'id': '102', 'type': 'attribute', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'full'}, {'id': '105', 'type': 'argument_list', 'children': ['106', '112', '113']}; {'id': '106', 'type': 'tuple', 'children': ['107', '111']}, {'id': '107', 'type': 'call', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'W_SIZE'}; {'id': '112', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '113', 'type': 'keyword_argument', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'dtype'}, {'id': '115', 'type': 'attribute', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'bool'}; {'id': '118', 'type': 'expression_statement', 'children': ['119']}, {'id': '119', 'type': 'assignment', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '121', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '122', 'type': 'for_statement', 'children': ['123', '124', '128']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '124', 'type': 'call', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '126', 'type': 'argument_list', 'children': ['127']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'W_SIZE'}; {'id': '128', 'type': 'block', 'children': ['129']}, {'id': '129', 'type': 'expression_statement', 'children': ['130']}; {'id': '130', 'type': 'assignment', 'children': ['131', '136']}, {'id': '131', 'type': 'subscript', 'children': ['132', '135']}; {'id': '132', 'type': 'subscript', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '134', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '136', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '137', 'type': 'for_statement', 'children': ['138', '139', '147']}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '139', 'type': 'call', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '141', 'type': 'argument_list', 'children': ['142', '143']}; {'id': '142', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '143', 'type': 'call', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '145', 'type': 'argument_list', 'children': ['146']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '147', 'type': 'block', 'children': ['148', '154', '160']}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'item_val'}, {'id': '151', 'type': 'subscript', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '154', 'type': 'expression_statement', 'children': ['155']}, {'id': '155', 'type': 'assignment', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'item_weight'}, {'id': '157', 'type': 'subscript', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '160', 'type': 'for_statement', 'children': ['161', '162', '166']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '162', 'type': 'call', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '164', 'type': 'argument_list', 'children': ['165']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'W_SIZE'}; {'id': '166', 'type': 'block', 'children': ['167', '173', '183', '216', '227']}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'assignment', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'valid_item'}; {'id': '170', 'type': 'comparison_operator', 'children': ['171', '172'], 'value': '<='}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'item_weight'}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '173', 'type': 'expression_statement', 'children': ['174']}; {'id': '174', 'type': 'assignment', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'prev_val'}; {'id': '176', 'type': 'subscript', 'children': ['177', '182']}, {'id': '177', 'type': 'subscript', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'dpmat'}, {'id': '179', 'type': 'binary_operator', 'children': ['180', '181'], 'value': '-'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '181', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '183', 'type': 'if_statement', 'children': ['184', '185', '210']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'valid_item'}, {'id': '185', 'type': 'block', 'children': ['186', '198', '204']}; {'id': '186', 'type': 'expression_statement', 'children': ['187']}, {'id': '187', 'type': 'assignment', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'prev_noitem_val'}, {'id': '189', 'type': 'subscript', 'children': ['190', '195']}; {'id': '190', 'type': 'subscript', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'dpmat'}; {'id': '192', 'type': 'binary_operator', 'children': ['193', '194'], 'value': '-'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '194', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '195', 'type': 'binary_operator', 'children': ['196', '197'], 'value': '-'}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'item_weight'}; {'id': '198', 'type': 'expression_statement', 'children': ['199']}, {'id': '199', 'type': 'assignment', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '201', 'type': 'binary_operator', 'children': ['202', '203'], 'value': '+'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'item_val'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'prev_noitem_val'}; {'id': '204', 'type': 'expression_statement', 'children': ['205']}, {'id': '205', 'type': 'assignment', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}, {'id': '207', 'type': 'comparison_operator', 'children': ['208', '209'], 'value': '>'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'prev_val'}; {'id': '210', 'type': 'else_clause', 'children': ['211']}, {'id': '211', 'type': 'block', 'children': ['212']}; {'id': '212', 'type': 'expression_statement', 'children': ['213']}, {'id': '213', 'type': 'assignment', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}, {'id': '215', 'type': 'False', 'children': []}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '223']}; {'id': '218', 'type': 'subscript', 'children': ['219', '222']}, {'id': '219', 'type': 'subscript', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'dpmat'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '223', 'type': 'conditional_expression', 'children': ['224', '225', '226'], 'value': 'if'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'withitem_val'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'prev_val'}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '234']}, {'id': '229', 'type': 'subscript', 'children': ['230', '233']}; {'id': '230', 'type': 'subscript', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'more_valuable'}, {'id': '235', 'type': 'expression_statement', 'children': ['236']}; {'id': '236', 'type': 'assignment', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'K'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'MAXWEIGHT'}, {'id': '239', 'type': 'for_statement', 'children': ['240', '241', '252']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '241', 'type': 'call', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'reversed'}, {'id': '243', 'type': 'argument_list', 'children': ['244']}; {'id': '244', 'type': 'call', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '246', 'type': 'argument_list', 'children': ['247', '248']}, {'id': '247', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '248', 'type': 'call', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '250', 'type': 'argument_list', 'children': ['251']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '252', 'type': 'block', 'children': ['253']}, {'id': '253', 'type': 'if_statement', 'children': ['254', '258']}; {'id': '254', 'type': 'subscript', 'children': ['255', '256', '257']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'kmat'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'K'}; {'id': '258', 'type': 'block', 'children': ['259', '266']}, {'id': '259', 'type': 'expression_statement', 'children': ['260']}; {'id': '260', 'type': 'call', 'children': ['261', '264']}, {'id': '261', 'type': 'attribute', 'children': ['262', '263']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '264', 'type': 'argument_list', 'children': ['265']}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '266', 'type': 'expression_statement', 'children': ['267']}, {'id': '267', 'type': 'assignment', 'children': ['268', '269']}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'K'}, {'id': '269', 'type': 'binary_operator', 'children': ['270', '271'], 'value': '-'}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'K'}, {'id': '271', 'type': 'subscript', 'children': ['272', '273']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'weights'}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '274', 'type': 'expression_statement', 'children': ['275']}, {'id': '275', 'type': 'assignment', 'children': ['276', '277']}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '277', 'type': 'call', 'children': ['278', '279']}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '279', 'type': 'argument_list', 'children': ['280']}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '281', 'type': 'expression_statement', 'children': ['282']}; {'id': '282', 'type': 'assignment', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'items_subset'}; {'id': '284', 'type': 'list_comprehension', 'children': ['285', '288']}, {'id': '285', 'type': 'subscript', 'children': ['286', '287']}; {'id': '286', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '288', 'type': 'for_in_clause', 'children': ['289', '290']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '290', 'type': 'identifier', 'children': [], 'value': 'idx_subset'}, {'id': '291', 'type': 'expression_statement', 'children': ['292']}; {'id': '292', 'type': 'assignment', 'children': ['293', '294']}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'total_value'}; {'id': '294', 'type': 'subscript', 'children': ['295', '303']}, {'id': '295', 'type': 'subscript', 'children': ['296', '297']}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'dpmat'}, {'id': '297', 'type': 'binary_operator', 'children': ['298', '302'], 'value': '-'}; {'id': '298', 'type': 'call', 'children': ['299', '300']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '300', 'type': 'argument_list', 'children': ['301']}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '302', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '303', 'type': 'identifier', 'children': [], 'value': 'MAXWEIGHT'}; {'id': '304', 'type': 'return_statement', 'children': ['305']}, {'id': '305', 'type': 'expression_list', 'children': ['306', '307']}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'total_value'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'items_subset'} | def knapsack_iterative_numpy(items, maxweight):
items = np.array(items)
weights = items.T[1]
max_exp = max([number_of_decimals(w_) for w_ in weights])
coeff = 10 ** max_exp
weights = (weights * coeff).astype(np.int)
values = items.T[0]
MAXWEIGHT = int(maxweight * coeff)
W_SIZE = MAXWEIGHT + 1
dpmat = np.full((len(items), W_SIZE), np.inf)
kmat = np.full((len(items), W_SIZE), 0, dtype=np.bool)
idx_subset = []
for w in range(W_SIZE):
dpmat[0][w] = 0
for idx in range(1, len(items)):
item_val = values[idx]
item_weight = weights[idx]
for w in range(W_SIZE):
valid_item = item_weight <= w
prev_val = dpmat[idx - 1][w]
if valid_item:
prev_noitem_val = dpmat[idx - 1][w - item_weight]
withitem_val = item_val + prev_noitem_val
more_valuable = withitem_val > prev_val
else:
more_valuable = False
dpmat[idx][w] = withitem_val if more_valuable else prev_val
kmat[idx][w] = more_valuable
K = MAXWEIGHT
for idx in reversed(range(1, len(items))):
if kmat[idx, K]:
idx_subset.append(idx)
K = K - weights[idx]
idx_subset = sorted(idx_subset)
items_subset = [items[i] for i in idx_subset]
total_value = dpmat[len(items) - 1][MAXWEIGHT]
return total_value, items_subset |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'knapsack_greedy'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '13', '17', '21', '61']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'items_subset'}; {'id': '12', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'assignment', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'total_weight'}; {'id': '16', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'total_value'}; {'id': '20', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '21', 'type': 'for_statement', 'children': ['22', '23', '24']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '24', 'type': 'block', 'children': ['25', '36']}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '30']}, {'id': '27', 'type': 'pattern_list', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'weight'}; {'id': '30', 'type': 'subscript', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '32', 'type': 'slice', 'children': ['33', '34', '35']}, {'id': '33', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '34', 'type': 'colon', 'children': []}, {'id': '35', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '36', 'type': 'if_statement', 'children': ['37', '42', '44']}, {'id': '37', 'type': 'comparison_operator', 'children': ['38', '41'], 'value': '>'}; {'id': '38', 'type': 'binary_operator', 'children': ['39', '40'], 'value': '+'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'total_weight'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'weight'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'maxweight'}; {'id': '42', 'type': 'block', 'children': ['43']}, {'id': '43', 'type': 'continue_statement', 'children': []}; {'id': '44', 'type': 'else_clause', 'children': ['45']}, {'id': '45', 'type': 'block', 'children': ['46', '53', '57']}; {'id': '46', 'type': 'expression_statement', 'children': ['47']}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'items_subset'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '53', 'type': 'expression_statement', 'children': ['54']}; {'id': '54', 'type': 'augmented_assignment', 'children': ['55', '56'], 'value': '+='}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'total_weight'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'weight'}, {'id': '57', 'type': 'expression_statement', 'children': ['58']}; {'id': '58', 'type': 'augmented_assignment', 'children': ['59', '60'], 'value': '+='}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'total_value'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '61', 'type': 'return_statement', 'children': ['62']}; {'id': '62', 'type': 'expression_list', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'total_value'} | def knapsack_greedy(items, maxweight):
r
items_subset = []
total_weight = 0
total_value = 0
for item in items:
value, weight = item[0:2]
if total_weight + weight > maxweight:
continue
else:
items_subset.append(item)
total_weight += weight
total_value += value
return total_value, items_subset |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'ungroup_gen'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'grouped_items'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'groupxs'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'fill'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '15', '32', '45', '54', '63', '73', '82', '86', '90', '94', '116', '122', '141']}; {'id': '10', 'type': 'import_statement', 'children': ['11']}, {'id': '11', 'type': 'aliased_import', 'children': ['12', '14']}; {'id': '12', 'type': 'dotted_name', 'children': ['13']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'minpergroup'}; {'id': '18', 'type': 'list_comprehension', 'children': ['19', '29']}, {'id': '19', 'type': 'conditional_expression', 'children': ['20', '24', '28'], 'value': 'if'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'min'}; {'id': '22', 'type': 'argument_list', 'children': ['23']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'xs'}; {'id': '24', 'type': 'call', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '26', 'type': 'argument_list', 'children': ['27']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'xs'}; {'id': '28', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '29', 'type': 'for_in_clause', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'xs'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'groupxs'}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'minval'}, {'id': '35', 'type': 'conditional_expression', 'children': ['36', '40', '44'], 'value': 'if'}; {'id': '36', 'type': 'call', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'min'}; {'id': '38', 'type': 'argument_list', 'children': ['39']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'minpergroup'}; {'id': '40', 'type': 'call', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '42', 'type': 'argument_list', 'children': ['43']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'minpergroup'}; {'id': '44', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'flat_groupx'}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'flatten'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'groupxs'}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'sortx'}, {'id': '57', 'type': 'call', 'children': ['58', '61']}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'argsort'}, {'id': '61', 'type': 'argument_list', 'children': ['62']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'flat_groupx'}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'groupx_sorted'}; {'id': '66', 'type': 'call', 'children': ['67', '70']}, {'id': '67', 'type': 'attribute', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'take'}; {'id': '70', 'type': 'argument_list', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'flat_groupx'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'sortx'}, {'id': '73', 'type': 'expression_statement', 'children': ['74']}; {'id': '74', 'type': 'assignment', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'flat_items'}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'iflatten'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'grouped_items'}; {'id': '82', 'type': 'expression_statement', 'children': ['83']}, {'id': '83', 'type': 'assignment', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'toyeild'}, {'id': '85', 'type': 'dictionary', 'children': []}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}, {'id': '89', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'current_index'}, {'id': '93', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'num_fills_before'}, {'id': '97', 'type': 'binary_operator', 'children': ['98', '114'], 'value': '+'}; {'id': '98', 'type': 'binary_operator', 'children': ['99', '101'], 'value': '+'}, {'id': '99', 'type': 'list', 'children': ['100'], 'value': '[minval]'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'minval'}, {'id': '101', 'type': 'call', 'children': ['102', '113']}; {'id': '102', 'type': 'attribute', 'children': ['103', '112']}, {'id': '103', 'type': 'parenthesized_expression', 'children': ['104']}; {'id': '104', 'type': 'binary_operator', 'children': ['105', '111'], 'value': '-'}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'diff'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'groupx_sorted'}, {'id': '111', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'tolist'}, {'id': '113', 'type': 'argument_list', 'children': []}; {'id': '114', 'type': 'list', 'children': ['115'], 'value': '[0]'}, {'id': '115', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '116', 'type': 'expression_statement', 'children': ['117']}, {'id': '117', 'type': 'assignment', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '119', 'type': 'subscript', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'num_fills_before'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}; {'id': '122', 'type': 'if_statement', 'children': ['123', '126']}, {'id': '123', 'type': 'comparison_operator', 'children': ['124', '125'], 'value': '>'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '125', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '126', 'type': 'block', 'children': ['127']}, {'id': '127', 'type': 'for_statement', 'children': ['128', '129', '133']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '129', 'type': 'call', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '131', 'type': 'argument_list', 'children': ['132']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '133', 'type': 'block', 'children': ['134', '137']}; {'id': '134', 'type': 'expression_statement', 'children': ['135']}, {'id': '135', 'type': 'yield', 'children': ['136']}; {'id': '136', 'type': 'None', 'children': []}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'augmented_assignment', 'children': ['139', '140'], 'value': '+='}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'current_index'}; {'id': '140', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '141', 'type': 'for_statement', 'children': ['142', '145', '150']}; {'id': '142', 'type': 'pattern_list', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'yeild_at'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '145', 'type': 'call', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '147', 'type': 'argument_list', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'flat_groupx'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'flat_items'}; {'id': '150', 'type': 'block', 'children': ['151']}, {'id': '151', 'type': 'if_statement', 'children': ['152', '155', '162']}; {'id': '152', 'type': 'comparison_operator', 'children': ['153', '154'], 'value': '>'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'yeild_at'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'current_index'}, {'id': '155', 'type': 'block', 'children': ['156']}; {'id': '156', 'type': 'expression_statement', 'children': ['157']}, {'id': '157', 'type': 'assignment', 'children': ['158', '161']}; {'id': '158', 'type': 'subscript', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'toyeild'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'yeild_at'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '162', 'type': 'elif_clause', 'children': ['163', '166']}, {'id': '163', 'type': 'comparison_operator', 'children': ['164', '165'], 'value': '=='}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'yeild_at'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'current_index'}; {'id': '166', 'type': 'block', 'children': ['167', '170', '174', '178', '184', '203']}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'yield', 'children': ['169']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'augmented_assignment', 'children': ['172', '173'], 'value': '+='}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'current_index'}, {'id': '173', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '174', 'type': 'expression_statement', 'children': ['175']}, {'id': '175', 'type': 'augmented_assignment', 'children': ['176', '177'], 'value': '+='}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}, {'id': '177', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '178', 'type': 'expression_statement', 'children': ['179']}, {'id': '179', 'type': 'assignment', 'children': ['180', '181']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '181', 'type': 'subscript', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'num_fills_before'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}; {'id': '184', 'type': 'if_statement', 'children': ['185', '188']}, {'id': '185', 'type': 'comparison_operator', 'children': ['186', '187'], 'value': '>'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '187', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '188', 'type': 'block', 'children': ['189']}, {'id': '189', 'type': 'for_statement', 'children': ['190', '191', '195']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '191', 'type': 'call', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '193', 'type': 'argument_list', 'children': ['194']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '195', 'type': 'block', 'children': ['196', '199']}; {'id': '196', 'type': 'expression_statement', 'children': ['197']}, {'id': '197', 'type': 'yield', 'children': ['198']}; {'id': '198', 'type': 'None', 'children': []}, {'id': '199', 'type': 'expression_statement', 'children': ['200']}; {'id': '200', 'type': 'augmented_assignment', 'children': ['201', '202'], 'value': '+='}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'current_index'}; {'id': '202', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '203', 'type': 'while_statement', 'children': ['204', '207']}; {'id': '204', 'type': 'comparison_operator', 'children': ['205', '206'], 'value': 'in'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'current_index'}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'toyeild'}, {'id': '207', 'type': 'block', 'children': ['208', '217', '220', '224', '228', '234']}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'assignment', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '211', 'type': 'call', 'children': ['212', '215']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'toyeild'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '215', 'type': 'argument_list', 'children': ['216']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'current_index'}, {'id': '217', 'type': 'expression_statement', 'children': ['218']}; {'id': '218', 'type': 'yield', 'children': ['219']}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '220', 'type': 'expression_statement', 'children': ['221']}, {'id': '221', 'type': 'augmented_assignment', 'children': ['222', '223'], 'value': '+='}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'current_index'}, {'id': '223', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '224', 'type': 'expression_statement', 'children': ['225']}, {'id': '225', 'type': 'augmented_assignment', 'children': ['226', '227'], 'value': '+='}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}, {'id': '227', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'assignment', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '231', 'type': 'subscript', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'num_fills_before'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'items_yeilded'}; {'id': '234', 'type': 'if_statement', 'children': ['235', '238']}, {'id': '235', 'type': 'comparison_operator', 'children': ['236', '237'], 'value': '>'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '237', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '238', 'type': 'block', 'children': ['239']}, {'id': '239', 'type': 'for_statement', 'children': ['240', '241', '245']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '241', 'type': 'call', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '243', 'type': 'argument_list', 'children': ['244']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'fills'}, {'id': '245', 'type': 'block', 'children': ['246', '249']}; {'id': '246', 'type': 'expression_statement', 'children': ['247']}, {'id': '247', 'type': 'yield', 'children': ['248']}; {'id': '248', 'type': 'None', 'children': []}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'augmented_assignment', 'children': ['251', '252'], 'value': '+='}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'current_index'} | def ungroup_gen(grouped_items, groupxs, fill=None):
import utool as ut
minpergroup = [min(xs) if len(xs) else 0 for xs in groupxs]
minval = min(minpergroup) if len(minpergroup) else 0
flat_groupx = ut.flatten(groupxs)
sortx = ut.argsort(flat_groupx)
groupx_sorted = ut.take(flat_groupx, sortx)
flat_items = ut.iflatten(grouped_items)
toyeild = {}
items_yeilded = 0
current_index = 0
num_fills_before = [minval] + (np.diff(groupx_sorted) - 1).tolist() + [0]
fills = num_fills_before[items_yeilded]
if fills > 0:
for _ in range(fills):
yield None
current_index += 1
for yeild_at, item in zip(flat_groupx, flat_items):
if yeild_at > current_index:
toyeild[yeild_at] = item
elif yeild_at == current_index:
yield item
current_index += 1
items_yeilded += 1
fills = num_fills_before[items_yeilded]
if fills > 0:
for _ in range(fills):
yield None
current_index += 1
while current_index in toyeild:
item = toyeild.pop(current_index)
yield item
current_index += 1
items_yeilded += 1
fills = num_fills_before[items_yeilded]
if fills > 0:
for _ in range(fills):
yield None
current_index += 1 |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'standardize_boolexpr'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'boolexpr_'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'parens'}, {'id': '7', 'type': 'False', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '11', '16', '19', '23', '34', '45', '56', '67', '78', '96', '108', '117', '138', '148', '158', '184', '196', '202', '208', '225', '242', '283', '396', '410', '428', '437']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '11', 'type': 'import_statement', 'children': ['12']}; {'id': '12', 'type': 'aliased_import', 'children': ['13', '15']}, {'id': '13', 'type': 'dotted_name', 'children': ['14']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '16', 'type': 'import_statement', 'children': ['17']}, {'id': '17', 'type': 'dotted_name', 'children': ['18']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'assignment', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'boolexpr_'}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '26', 'type': 'call', 'children': ['27', '30']}, {'id': '27', 'type': 'attribute', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'sub'}; {'id': '30', 'type': 'argument_list', 'children': ['31', '32', '33']}, {'id': '31', 'type': 'string', 'children': [], 'value': "'\\\\bnot\\\\b'"}; {'id': '32', 'type': 'string', 'children': [], 'value': "''"}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}, {'id': '37', 'type': 'call', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 're'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'sub'}, {'id': '41', 'type': 'argument_list', 'children': ['42', '43', '44']}; {'id': '42', 'type': 'string', 'children': [], 'value': "'\\\\band\\\\b'"}, {'id': '43', 'type': 'string', 'children': [], 'value': "''"}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'sub'}; {'id': '52', 'type': 'argument_list', 'children': ['53', '54', '55']}, {'id': '53', 'type': 'string', 'children': [], 'value': "'\\\\bor\\\\b'"}; {'id': '54', 'type': 'string', 'children': [], 'value': "''"}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '56', 'type': 'expression_statement', 'children': ['57']}, {'id': '57', 'type': 'assignment', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}, {'id': '59', 'type': 'call', 'children': ['60', '63']}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 're'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'sub'}, {'id': '63', 'type': 'argument_list', 'children': ['64', '65', '66']}; {'id': '64', 'type': 'string', 'children': [], 'value': "'\\\\('"}, {'id': '65', 'type': 'string', 'children': [], 'value': "''"}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}, {'id': '67', 'type': 'expression_statement', 'children': ['68']}; {'id': '68', 'type': 'assignment', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '70', 'type': 'call', 'children': ['71', '74']}, {'id': '71', 'type': 'attribute', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'sub'}; {'id': '74', 'type': 'argument_list', 'children': ['75', '76', '77']}, {'id': '75', 'type': 'string', 'children': [], 'value': "'\\\\)'"}; {'id': '76', 'type': 'string', 'children': [], 'value': "''"}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'varnames'}, {'id': '81', 'type': 'call', 'children': ['82', '94']}; {'id': '82', 'type': 'attribute', 'children': ['83', '93']}, {'id': '83', 'type': 'call', 'children': ['84', '92']}; {'id': '84', 'type': 'attribute', 'children': ['85', '91']}, {'id': '85', 'type': 'call', 'children': ['86', '89']}; {'id': '86', 'type': 'attribute', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'remove_doublspaces'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'onlyvars'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'strip'}; {'id': '92', 'type': 'argument_list', 'children': []}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '94', 'type': 'argument_list', 'children': ['95']}, {'id': '95', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}, {'id': '99', 'type': 'dictionary_comprehension', 'children': ['100', '105']}; {'id': '100', 'type': 'pair', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'var'}; {'id': '102', 'type': 'list', 'children': ['103', '104'], 'value': '[True, False]'}, {'id': '103', 'type': 'True', 'children': []}; {'id': '104', 'type': 'False', 'children': []}, {'id': '105', 'type': 'for_in_clause', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'var'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'varnames'}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'bool_states'}, {'id': '111', 'type': 'call', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'all_dict_combinations'}, {'id': '115', 'type': 'argument_list', 'children': ['116']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'outputs'}; {'id': '120', 'type': 'list_comprehension', 'children': ['121', '135']}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'eval'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '125', '130']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'boolexpr_'}, {'id': '125', 'type': 'call', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'copy'}, {'id': '129', 'type': 'argument_list', 'children': []}; {'id': '130', 'type': 'call', 'children': ['131', '134']}, {'id': '131', 'type': 'attribute', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'copy'}; {'id': '134', 'type': 'argument_list', 'children': []}, {'id': '135', 'type': 'for_in_clause', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'bool_states'}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'assignment', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'true_states'}, {'id': '141', 'type': 'call', 'children': ['142', '145']}; {'id': '142', 'type': 'attribute', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'compress'}, {'id': '145', 'type': 'argument_list', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'bool_states'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'outputs'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'true_tuples'}, {'id': '151', 'type': 'call', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'take_column'}, {'id': '155', 'type': 'argument_list', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'true_states'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'varnames'}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'true_cases'}, {'id': '161', 'type': 'list_comprehension', 'children': ['162', '181']}; {'id': '162', 'type': 'call', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '164', 'type': 'argument_list', 'children': ['165']}, {'id': '165', 'type': 'call', 'children': ['166', '169']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'string', 'children': [], 'value': "''"}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '169', 'type': 'argument_list', 'children': ['170']}; {'id': '170', 'type': 'list_comprehension', 'children': ['171', '178']}, {'id': '171', 'type': 'call', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '173', 'type': 'argument_list', 'children': ['174']}; {'id': '174', 'type': 'call', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '176', 'type': 'argument_list', 'children': ['177']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '178', 'type': 'for_in_clause', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'tup'}, {'id': '181', 'type': 'for_in_clause', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'tup'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'true_tuples'}; {'id': '184', 'type': 'expression_statement', 'children': ['185']}, {'id': '185', 'type': 'assignment', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'ones_bin'}, {'id': '187', 'type': 'list_comprehension', 'children': ['188', '193']}; {'id': '188', 'type': 'call', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '190', 'type': 'argument_list', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '192', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '193', 'type': 'for_in_clause', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'true_cases'}; {'id': '196', 'type': 'import_from_statement', 'children': ['197', '200']}, {'id': '197', 'type': 'dotted_name', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'quine_mccluskey'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'qm'}; {'id': '200', 'type': 'dotted_name', 'children': ['201']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'QuineMcCluskey'}; {'id': '202', 'type': 'expression_statement', 'children': ['203']}, {'id': '203', 'type': 'assignment', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'qm'}, {'id': '205', 'type': 'call', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'QuineMcCluskey'}, {'id': '207', 'type': 'argument_list', 'children': []}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'assignment', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '211', 'type': 'call', 'children': ['212', '215']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'qm'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'simplify'}, {'id': '215', 'type': 'argument_list', 'children': ['216', '219']}; {'id': '216', 'type': 'keyword_argument', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'ones'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'ones_bin'}, {'id': '219', 'type': 'keyword_argument', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'num_bits'}, {'id': '221', 'type': 'call', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '223', 'type': 'argument_list', 'children': ['224']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'varnames'}, {'id': '225', 'type': 'expression_statement', 'children': ['226']}; {'id': '226', 'type': 'assignment', 'children': ['227', '228']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'grouped_terms'}; {'id': '228', 'type': 'list_comprehension', 'children': ['229', '239']}, {'id': '229', 'type': 'call', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '231', 'type': 'argument_list', 'children': ['232']}; {'id': '232', 'type': 'call', 'children': ['233', '236']}, {'id': '233', 'type': 'attribute', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'group_items'}; {'id': '236', 'type': 'argument_list', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'varnames'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'rs'}, {'id': '239', 'type': 'for_in_clause', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'rs'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '242', 'type': 'function_definition', 'children': ['243', '244', '247']}, {'id': '243', 'type': 'function_name', 'children': [], 'value': 'parenjoin'}; {'id': '244', 'type': 'parameters', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'char'}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '247', 'type': 'block', 'children': ['248']}; {'id': '248', 'type': 'if_statement', 'children': ['249', '255', '258']}, {'id': '249', 'type': 'comparison_operator', 'children': ['250', '254'], 'value': '=='}; {'id': '250', 'type': 'call', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '252', 'type': 'argument_list', 'children': ['253']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'list_'}; {'id': '254', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '255', 'type': 'block', 'children': ['256']}; {'id': '256', 'type': 'return_statement', 'children': ['257']}, {'id': '257', 'type': 'string', 'children': [], 'value': "''"}; {'id': '258', 'type': 'else_clause', 'children': ['259']}, {'id': '259', 'type': 'block', 'children': ['260']}; {'id': '260', 'type': 'if_statement', 'children': ['261', '262', '274']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'parens'}; {'id': '262', 'type': 'block', 'children': ['263']}, {'id': '263', 'type': 'return_statement', 'children': ['264']}; {'id': '264', 'type': 'binary_operator', 'children': ['265', '273'], 'value': '+'}, {'id': '265', 'type': 'binary_operator', 'children': ['266', '267'], 'value': '+'}; {'id': '266', 'type': 'string', 'children': [], 'value': "'('"}, {'id': '267', 'type': 'call', 'children': ['268', '271']}; {'id': '268', 'type': 'attribute', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'char'}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '271', 'type': 'argument_list', 'children': ['272']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '273', 'type': 'string', 'children': [], 'value': "')'"}; {'id': '274', 'type': 'else_clause', 'children': ['275']}, {'id': '275', 'type': 'block', 'children': ['276']}; {'id': '276', 'type': 'return_statement', 'children': ['277']}, {'id': '277', 'type': 'call', 'children': ['278', '281']}; {'id': '278', 'type': 'attribute', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'char'}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '281', 'type': 'argument_list', 'children': ['282']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '283', 'type': 'if_statement', 'children': ['284', '285', '341']}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'parens'}, {'id': '285', 'type': 'block', 'children': ['286']}; {'id': '286', 'type': 'expression_statement', 'children': ['287']}, {'id': '287', 'type': 'assignment', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'expanded_terms'}, {'id': '289', 'type': 'list_comprehension', 'children': ['290', '338']}; {'id': '290', 'type': 'parenthesized_expression', 'children': ['291']}, {'id': '291', 'type': 'binary_operator', 'children': ['292', '315'], 'value': '+'}; {'id': '292', 'type': 'binary_operator', 'children': ['293', '300'], 'value': '+'}, {'id': '293', 'type': 'call', 'children': ['294', '297']}; {'id': '294', 'type': 'attribute', 'children': ['295', '296']}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'term'}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '297', 'type': 'argument_list', 'children': ['298', '299']}; {'id': '298', 'type': 'string', 'children': [], 'value': "'1'"}, {'id': '299', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '300', 'type': 'list_comprehension', 'children': ['301', '306']}, {'id': '301', 'type': 'binary_operator', 'children': ['302', '305'], 'value': '+'}; {'id': '302', 'type': 'binary_operator', 'children': ['303', '304'], 'value': '+'}, {'id': '303', 'type': 'string', 'children': [], 'value': "'(not '"}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '305', 'type': 'string', 'children': [], 'value': "')'"}; {'id': '306', 'type': 'for_in_clause', 'children': ['307', '308']}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'b'}; {'id': '308', 'type': 'call', 'children': ['309', '312']}, {'id': '309', 'type': 'attribute', 'children': ['310', '311']}; {'id': '310', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '312', 'type': 'argument_list', 'children': ['313', '314']}, {'id': '313', 'type': 'string', 'children': [], 'value': "'0'"}; {'id': '314', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '315', 'type': 'list', 'children': ['316', '327'], 'value': "[\n parenjoin(' ^ ', term.get('^', [])),\n parenjoin(' ~ ', term.get('~', [])),\n ]"}; {'id': '316', 'type': 'call', 'children': ['317', '318']}, {'id': '317', 'type': 'identifier', 'children': [], 'value': 'parenjoin'}; {'id': '318', 'type': 'argument_list', 'children': ['319', '320']}, {'id': '319', 'type': 'string', 'children': [], 'value': "' ^ '"}; {'id': '320', 'type': 'call', 'children': ['321', '324']}, {'id': '321', 'type': 'attribute', 'children': ['322', '323']}; {'id': '322', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '323', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '324', 'type': 'argument_list', 'children': ['325', '326']}, {'id': '325', 'type': 'string', 'children': [], 'value': "'^'"}; {'id': '326', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '327', 'type': 'call', 'children': ['328', '329']}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'parenjoin'}, {'id': '329', 'type': 'argument_list', 'children': ['330', '331']}; {'id': '330', 'type': 'string', 'children': [], 'value': "' ~ '"}, {'id': '331', 'type': 'call', 'children': ['332', '335']}; {'id': '332', 'type': 'attribute', 'children': ['333', '334']}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'term'}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '335', 'type': 'argument_list', 'children': ['336', '337']}; {'id': '336', 'type': 'string', 'children': [], 'value': "'~'"}, {'id': '337', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '338', 'type': 'for_in_clause', 'children': ['339', '340']}, {'id': '339', 'type': 'identifier', 'children': [], 'value': 'term'}; {'id': '340', 'type': 'identifier', 'children': [], 'value': 'grouped_terms'}, {'id': '341', 'type': 'else_clause', 'children': ['342']}; {'id': '342', 'type': 'block', 'children': ['343']}, {'id': '343', 'type': 'expression_statement', 'children': ['344']}; {'id': '344', 'type': 'assignment', 'children': ['345', '346']}, {'id': '345', 'type': 'identifier', 'children': [], 'value': 'expanded_terms'}; {'id': '346', 'type': 'list_comprehension', 'children': ['347', '393']}, {'id': '347', 'type': 'parenthesized_expression', 'children': ['348']}; {'id': '348', 'type': 'binary_operator', 'children': ['349', '370'], 'value': '+'}, {'id': '349', 'type': 'binary_operator', 'children': ['350', '357'], 'value': '+'}; {'id': '350', 'type': 'call', 'children': ['351', '354']}, {'id': '351', 'type': 'attribute', 'children': ['352', '353']}; {'id': '352', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '353', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '354', 'type': 'argument_list', 'children': ['355', '356']}, {'id': '355', 'type': 'string', 'children': [], 'value': "'1'"}; {'id': '356', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '357', 'type': 'list_comprehension', 'children': ['358', '361']}; {'id': '358', 'type': 'binary_operator', 'children': ['359', '360'], 'value': '+'}, {'id': '359', 'type': 'string', 'children': [], 'value': "'not '"}; {'id': '360', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '361', 'type': 'for_in_clause', 'children': ['362', '363']}; {'id': '362', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '363', 'type': 'call', 'children': ['364', '367']}; {'id': '364', 'type': 'attribute', 'children': ['365', '366']}, {'id': '365', 'type': 'identifier', 'children': [], 'value': 'term'}; {'id': '366', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '367', 'type': 'argument_list', 'children': ['368', '369']}; {'id': '368', 'type': 'string', 'children': [], 'value': "'0'"}, {'id': '369', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '370', 'type': 'list', 'children': ['371', '382'], 'value': "[\n parenjoin(' ^ ', term.get('^', [])),\n parenjoin(' ~ ', term.get('~', [])),\n ]"}, {'id': '371', 'type': 'call', 'children': ['372', '373']}; {'id': '372', 'type': 'identifier', 'children': [], 'value': 'parenjoin'}, {'id': '373', 'type': 'argument_list', 'children': ['374', '375']}; {'id': '374', 'type': 'string', 'children': [], 'value': "' ^ '"}, {'id': '375', 'type': 'call', 'children': ['376', '379']}; {'id': '376', 'type': 'attribute', 'children': ['377', '378']}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'term'}; {'id': '378', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '379', 'type': 'argument_list', 'children': ['380', '381']}; {'id': '380', 'type': 'string', 'children': [], 'value': "'^'"}, {'id': '381', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '382', 'type': 'call', 'children': ['383', '384']}, {'id': '383', 'type': 'identifier', 'children': [], 'value': 'parenjoin'}; {'id': '384', 'type': 'argument_list', 'children': ['385', '386']}, {'id': '385', 'type': 'string', 'children': [], 'value': "' ~ '"}; {'id': '386', 'type': 'call', 'children': ['387', '390']}, {'id': '387', 'type': 'attribute', 'children': ['388', '389']}; {'id': '388', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '389', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '390', 'type': 'argument_list', 'children': ['391', '392']}, {'id': '391', 'type': 'string', 'children': [], 'value': "'~'"}; {'id': '392', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '393', 'type': 'for_in_clause', 'children': ['394', '395']}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '395', 'type': 'identifier', 'children': [], 'value': 'grouped_terms'}; {'id': '396', 'type': 'expression_statement', 'children': ['397']}, {'id': '397', 'type': 'assignment', 'children': ['398', '399']}; {'id': '398', 'type': 'identifier', 'children': [], 'value': 'final_terms'}, {'id': '399', 'type': 'list_comprehension', 'children': ['400', '407']}; {'id': '400', 'type': 'list_comprehension', 'children': ['401', '402', '405']}, {'id': '401', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '402', 'type': 'for_in_clause', 'children': ['403', '404']}, {'id': '403', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '404', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '405', 'type': 'if_clause', 'children': ['406']}; {'id': '406', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '407', 'type': 'for_in_clause', 'children': ['408', '409']}; {'id': '408', 'type': 'identifier', 'children': [], 'value': 'term'}, {'id': '409', 'type': 'identifier', 'children': [], 'value': 'expanded_terms'}; {'id': '410', 'type': 'expression_statement', 'children': ['411']}, {'id': '411', 'type': 'assignment', 'children': ['412', '413']}; {'id': '412', 'type': 'identifier', 'children': [], 'value': 'products'}, {'id': '413', 'type': 'list_comprehension', 'children': ['414', '425']}; {'id': '414', 'type': 'call', 'children': ['415', '416']}, {'id': '415', 'type': 'identifier', 'children': [], 'value': 'parenjoin'}; {'id': '416', 'type': 'argument_list', 'children': ['417', '418']}, {'id': '417', 'type': 'string', 'children': [], 'value': "' and '"}; {'id': '418', 'type': 'list_comprehension', 'children': ['419', '420', '423']}, {'id': '419', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '420', 'type': 'for_in_clause', 'children': ['421', '422']}, {'id': '421', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '422', 'type': 'identifier', 'children': [], 'value': 'form'}, {'id': '423', 'type': 'if_clause', 'children': ['424']}; {'id': '424', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '425', 'type': 'for_in_clause', 'children': ['426', '427']}; {'id': '426', 'type': 'identifier', 'children': [], 'value': 'form'}, {'id': '427', 'type': 'identifier', 'children': [], 'value': 'final_terms'}; {'id': '428', 'type': 'expression_statement', 'children': ['429']}, {'id': '429', 'type': 'assignment', 'children': ['430', '431']}; {'id': '430', 'type': 'identifier', 'children': [], 'value': 'final_expr'}, {'id': '431', 'type': 'call', 'children': ['432', '435']}; {'id': '432', 'type': 'attribute', 'children': ['433', '434']}, {'id': '433', 'type': 'string', 'children': [], 'value': "' or '"}; {'id': '434', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '435', 'type': 'argument_list', 'children': ['436']}; {'id': '436', 'type': 'identifier', 'children': [], 'value': 'products'}, {'id': '437', 'type': 'return_statement', 'children': ['438']} | def standardize_boolexpr(boolexpr_, parens=False):
r
import utool as ut
import re
onlyvars = boolexpr_
onlyvars = re.sub('\\bnot\\b', '', onlyvars)
onlyvars = re.sub('\\band\\b', '', onlyvars)
onlyvars = re.sub('\\bor\\b', '', onlyvars)
onlyvars = re.sub('\\(', '', onlyvars)
onlyvars = re.sub('\\)', '', onlyvars)
varnames = ut.remove_doublspaces(onlyvars).strip().split(' ')
varied_dict = {var: [True, False] for var in varnames}
bool_states = ut.all_dict_combinations(varied_dict)
outputs = [eval(boolexpr_, state.copy(), state.copy()) for state in bool_states]
true_states = ut.compress(bool_states, outputs)
true_tuples = ut.take_column(true_states, varnames)
true_cases = [str(''.join([str(int(t)) for t in tup])) for tup in true_tuples]
ones_bin = [int(x, 2) for x in true_cases]
from quine_mccluskey.qm import QuineMcCluskey
qm = QuineMcCluskey()
result = qm.simplify(ones=ones_bin, num_bits=len(varnames))
grouped_terms = [dict(ut.group_items(varnames, rs)) for rs in result]
def parenjoin(char, list_):
if len(list_) == 0:
return ''
else:
if parens:
return '(' + char.join(list_) + ')'
else:
return char.join(list_)
if parens:
expanded_terms = [
(
term.get('1', []) +
['(not ' + b + ')' for b in term.get('0', [])] +
[
parenjoin(' ^ ', term.get('^', [])),
parenjoin(' ~ ', term.get('~', [])),
]
) for term in grouped_terms
]
else:
expanded_terms = [
(
term.get('1', []) +
['not ' + b for b in term.get('0', [])] +
[
parenjoin(' ^ ', term.get('^', [])),
parenjoin(' ~ ', term.get('~', [])),
]
) for term in grouped_terms
]
final_terms = [[t for t in term if t] for term in expanded_terms]
products = [parenjoin(' and ', [f for f in form if f]) for form in final_terms]
final_expr = ' or '.join(products)
return final_expr |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'factors'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '5', 'type': 'block', 'children': ['6']}; {'id': '6', 'type': 'return_statement', 'children': ['7']}, {'id': '7', 'type': 'call', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '9', 'type': 'argument_list', 'children': ['10']}; {'id': '10', 'type': 'call', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'reduce'}; {'id': '12', 'type': 'argument_list', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': '__add__'}; {'id': '16', 'type': 'generator_expression', 'children': ['17', '22', '36']}, {'id': '17', 'type': 'list', 'children': ['18', '19'], 'value': '[i, n // i]'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '19', 'type': 'binary_operator', 'children': ['20', '21'], 'value': '//'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '22', 'type': 'for_in_clause', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '24', 'type': 'call', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '26', 'type': 'argument_list', 'children': ['27', '28']}, {'id': '27', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '28', 'type': 'binary_operator', 'children': ['29', '35'], 'value': '+'}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'int'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'binary_operator', 'children': ['33', '34'], 'value': '**'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '34', 'type': 'float', 'children': [], 'value': '0.5'}, {'id': '35', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '36', 'type': 'if_clause', 'children': ['37']}, {'id': '37', 'type': 'comparison_operator', 'children': ['38', '41'], 'value': '=='}; {'id': '38', 'type': 'binary_operator', 'children': ['39', '40'], 'value': '%'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '41', 'type': 'integer', 'children': [], 'value': '0'} | def factors(n):
return set(reduce(list.__add__,
([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'dict_stack'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict_list'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'key_prefix'}, {'id': '7', 'type': 'string', 'children': [], 'value': "''"}; {'id': '8', 'type': 'block', 'children': ['9', '11', '18', '44', '51']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '11', 'type': 'expression_statement', 'children': ['12']}; {'id': '12', 'type': 'assignment', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'dict_stacked_'}; {'id': '14', 'type': 'call', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '16', 'type': 'argument_list', 'children': ['17']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '18', 'type': 'for_statement', 'children': ['19', '20', '21']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'dict_'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'dict_list'}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'for_statement', 'children': ['23', '26', '32']}, {'id': '23', 'type': 'pattern_list', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '26', 'type': 'call', 'children': ['27', '30']}, {'id': '27', 'type': 'attribute', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'iteritems'}; {'id': '30', 'type': 'argument_list', 'children': ['31']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'dict_'}; {'id': '32', 'type': 'block', 'children': ['33']}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'call', 'children': ['35', '42']}, {'id': '35', 'type': 'attribute', 'children': ['36', '41']}; {'id': '36', 'type': 'subscript', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'dict_stacked_'}; {'id': '38', 'type': 'binary_operator', 'children': ['39', '40'], 'value': '+'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'key_prefix'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '42', 'type': 'argument_list', 'children': ['43']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'dict_stacked'}, {'id': '47', 'type': 'call', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '49', 'type': 'argument_list', 'children': ['50']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'dict_stacked_'}, {'id': '51', 'type': 'return_statement', 'children': ['52']} | def dict_stack(dict_list, key_prefix=''):
r
dict_stacked_ = defaultdict(list)
for dict_ in dict_list:
for key, val in six.iteritems(dict_):
dict_stacked_[key_prefix + key].append(val)
dict_stacked = dict(dict_stacked_)
return dict_stacked |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'invert_dict'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'unique_vals'}, {'id': '7', 'type': 'True', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '57']}, {'id': '9', 'type': 'if_statement', 'children': ['10', '11', '39']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'unique_vals'}, {'id': '11', 'type': 'block', 'children': ['12', '29']}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'inverted_items'}, {'id': '15', 'type': 'list_comprehension', 'children': ['16', '19']}; {'id': '16', 'type': 'tuple', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '19', 'type': 'for_in_clause', 'children': ['20', '23']}; {'id': '20', 'type': 'pattern_list', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '23', 'type': 'call', 'children': ['24', '27']}; {'id': '24', 'type': 'attribute', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'six'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'iteritems'}, {'id': '27', 'type': 'argument_list', 'children': ['28']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'inverted_dict'}; {'id': '32', 'type': 'call', 'children': ['33', '37']}, {'id': '33', 'type': 'call', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'type'}, {'id': '35', 'type': 'argument_list', 'children': ['36']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'inverted_items'}, {'id': '39', 'type': 'else_clause', 'children': ['40']}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'inverted_dict'}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'group_items'}; {'id': '46', 'type': 'argument_list', 'children': ['47', '52']}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'dict_'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '51', 'type': 'argument_list', 'children': []}; {'id': '52', 'type': 'call', 'children': ['53', '56']}, {'id': '53', 'type': 'attribute', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '56', 'type': 'argument_list', 'children': []}, {'id': '57', 'type': 'return_statement', 'children': ['58']} | def invert_dict(dict_, unique_vals=True):
if unique_vals:
inverted_items = [(val, key) for key, val in six.iteritems(dict_)]
inverted_dict = type(dict_)(inverted_items)
else:
inverted_dict = group_items(dict_.keys(), dict_.values())
return inverted_dict |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'all_dict_combinations_lbls'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'remove_singles'}, {'id': '7', 'type': 'True', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'allow_lone_singles'}; {'id': '10', 'type': 'False', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '41', '105', '118', '148', '171']}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'is_lone_single'}, {'id': '15', 'type': 'call', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'all'}, {'id': '17', 'type': 'argument_list', 'children': ['18']}; {'id': '18', 'type': 'list_comprehension', 'children': ['19', '33']}, {'id': '19', 'type': 'boolean_operator', 'children': ['20', '27'], 'value': 'and'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '22', 'type': 'argument_list', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '24', 'type': 'tuple', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '27', 'type': 'comparison_operator', 'children': ['28', '32'], 'value': '=='}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '30', 'type': 'argument_list', 'children': ['31']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '32', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '33', 'type': 'for_in_clause', 'children': ['34', '37']}; {'id': '34', 'type': 'pattern_list', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'iteritems_sorted'}, {'id': '39', 'type': 'argument_list', 'children': ['40']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}, {'id': '41', 'type': 'if_statement', 'children': ['42', '49', '69']}; {'id': '42', 'type': 'boolean_operator', 'children': ['43', '45'], 'value': 'or'}, {'id': '43', 'type': 'not_operator', 'children': ['44']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'remove_singles'}, {'id': '45', 'type': 'parenthesized_expression', 'children': ['46']}; {'id': '46', 'type': 'boolean_operator', 'children': ['47', '48'], 'value': 'and'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'allow_lone_singles'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'is_lone_single'}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'multitups_list'}, {'id': '53', 'type': 'list_comprehension', 'children': ['54', '61']}; {'id': '54', 'type': 'list_comprehension', 'children': ['55', '58']}, {'id': '55', 'type': 'tuple', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '58', 'type': 'for_in_clause', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '61', 'type': 'for_in_clause', 'children': ['62', '65']}; {'id': '62', 'type': 'pattern_list', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '65', 'type': 'call', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'iteritems_sorted'}, {'id': '67', 'type': 'argument_list', 'children': ['68']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}, {'id': '69', 'type': 'else_clause', 'children': ['70']}; {'id': '70', 'type': 'block', 'children': ['71']}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'multitups_list'}; {'id': '74', 'type': 'list_comprehension', 'children': ['75', '82', '90']}, {'id': '75', 'type': 'list_comprehension', 'children': ['76', '79']}; {'id': '76', 'type': 'tuple', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '79', 'type': 'for_in_clause', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '82', 'type': 'for_in_clause', 'children': ['83', '86']}, {'id': '83', 'type': 'pattern_list', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '86', 'type': 'call', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'iteritems_sorted'}; {'id': '88', 'type': 'argument_list', 'children': ['89']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'varied_dict'}; {'id': '90', 'type': 'if_clause', 'children': ['91']}, {'id': '91', 'type': 'boolean_operator', 'children': ['92', '99'], 'value': 'and'}; {'id': '92', 'type': 'call', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '94', 'type': 'argument_list', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '96', 'type': 'tuple', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '99', 'type': 'comparison_operator', 'children': ['100', '104'], 'value': '>'}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '104', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '105', 'type': 'expression_statement', 'children': ['106']}; {'id': '106', 'type': 'assignment', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'combtup_list'}; {'id': '108', 'type': 'call', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '110', 'type': 'argument_list', 'children': ['111']}, {'id': '111', 'type': 'call', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'it'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'product'}, {'id': '115', 'type': 'argument_list', 'children': ['116']}; {'id': '116', 'type': 'list_splat', 'children': ['117']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'multitups_list'}; {'id': '118', 'type': 'expression_statement', 'children': ['119']}, {'id': '119', 'type': 'assignment', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'combtup_list2'}, {'id': '121', 'type': 'list_comprehension', 'children': ['122', '145']}; {'id': '122', 'type': 'list_comprehension', 'children': ['123', '140']}, {'id': '123', 'type': 'conditional_expression', 'children': ['124', '127', '134'], 'value': 'if'}; {'id': '124', 'type': 'tuple', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '127', 'type': 'call', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '129', 'type': 'argument_list', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '131', 'type': 'attribute', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'string_types'}; {'id': '134', 'type': 'tuple', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '136', 'type': 'call', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'repr'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '140', 'type': 'for_in_clause', 'children': ['141', '144']}, {'id': '141', 'type': 'tuple_pattern', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'combtup'}, {'id': '145', 'type': 'for_in_clause', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'combtup'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'combtup_list'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'comb_lbls'}, {'id': '151', 'type': 'list_comprehension', 'children': ['152', '168']}; {'id': '152', 'type': 'call', 'children': ['153', '156']}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'string', 'children': [], 'value': "','"}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '156', 'type': 'argument_list', 'children': ['157']}, {'id': '157', 'type': 'list_comprehension', 'children': ['158', '163']}; {'id': '158', 'type': 'binary_operator', 'children': ['159', '160'], 'value': '%'}, {'id': '159', 'type': 'string', 'children': [], 'value': "'%s=%s'"}; {'id': '160', 'type': 'tuple', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '163', 'type': 'for_in_clause', 'children': ['164', '167']}; {'id': '164', 'type': 'tuple_pattern', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'combtup'}; {'id': '168', 'type': 'for_in_clause', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'combtup'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'combtup_list2'}, {'id': '171', 'type': 'return_statement', 'children': ['172']} | def all_dict_combinations_lbls(varied_dict, remove_singles=True, allow_lone_singles=False):
is_lone_single = all([
isinstance(val_list, (list, tuple)) and len(val_list) == 1
for key, val_list in iteritems_sorted(varied_dict)
])
if not remove_singles or (allow_lone_singles and is_lone_single):
multitups_list = [
[(key, val) for val in val_list]
for key, val_list in iteritems_sorted(varied_dict)
]
else:
multitups_list = [
[(key, val) for val in val_list]
for key, val_list in iteritems_sorted(varied_dict)
if isinstance(val_list, (list, tuple)) and len(val_list) > 1]
combtup_list = list(it.product(*multitups_list))
combtup_list2 = [
[(key, val) if isinstance(val, six.string_types) else (key, repr(val))
for (key, val) in combtup]
for combtup in combtup_list]
comb_lbls = [','.join(['%s=%s' % (key, val) for (key, val) in combtup])
for combtup in combtup_list2]
return comb_lbls |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '18']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'update_existing'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '12', '15']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict1'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'dict2'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'copy'}; {'id': '8', 'type': 'False', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'assert_exists'}, {'id': '11', 'type': 'False', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'iswarning'}; {'id': '14', 'type': 'False', 'children': []}, {'id': '15', 'type': 'default_parameter', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'alias_dict'}, {'id': '17', 'type': 'None', 'children': []}; {'id': '18', 'type': 'block', 'children': ['19', '21', '61', '71', '80', '112']}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '21', 'type': 'if_statement', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'assert_exists'}, {'id': '23', 'type': 'block', 'children': ['24']}; {'id': '24', 'type': 'try_statement', 'children': ['25', '32']}, {'id': '25', 'type': 'block', 'children': ['26']}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'call', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'assert_keys_are_subset'}, {'id': '29', 'type': 'argument_list', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'dict1'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'dict2'}; {'id': '32', 'type': 'except_clause', 'children': ['33', '37']}, {'id': '33', 'type': 'as_pattern', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'AssertionError'}, {'id': '35', 'type': 'as_pattern_target', 'children': ['36']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'ex'}, {'id': '37', 'type': 'block', 'children': ['38', '43', '56']}; {'id': '38', 'type': 'import_from_statement', 'children': ['39', '41']}, {'id': '39', 'type': 'dotted_name', 'children': ['40']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '41', 'type': 'dotted_name', 'children': ['42']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'util_dbg'}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'call', 'children': ['45', '48']}, {'id': '45', 'type': 'attribute', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'util_dbg'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'printex'}; {'id': '48', 'type': 'argument_list', 'children': ['49', '50', '53']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'ex'}; {'id': '50', 'type': 'keyword_argument', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'iswarning'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'iswarning'}, {'id': '53', 'type': 'keyword_argument', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'N'}, {'id': '55', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '56', 'type': 'if_statement', 'children': ['57', '59']}, {'id': '57', 'type': 'not_operator', 'children': ['58']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'iswarning'}, {'id': '59', 'type': 'block', 'children': ['60']}; {'id': '60', 'type': 'raise_statement', 'children': []}, {'id': '61', 'type': 'if_statement', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'copy'}, {'id': '63', 'type': 'block', 'children': ['64']}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'dict1'}, {'id': '67', 'type': 'call', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'dict1'}, {'id': '71', 'type': 'if_statement', 'children': ['72', '75']}; {'id': '72', 'type': 'comparison_operator', 'children': ['73', '74'], 'value': 'is'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'alias_dict'}; {'id': '74', 'type': 'None', 'children': []}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'expression_statement', 'children': ['77']}, {'id': '77', 'type': 'assignment', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'alias_dict'}, {'id': '79', 'type': 'dictionary', 'children': []}; {'id': '80', 'type': 'for_statement', 'children': ['81', '84', '90']}, {'id': '81', 'type': 'pattern_list', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '84', 'type': 'call', 'children': ['85', '88']}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'iteritems'}; {'id': '88', 'type': 'argument_list', 'children': ['89']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'dict2'}; {'id': '90', 'type': 'block', 'children': ['91', '101']}, {'id': '91', 'type': 'expression_statement', 'children': ['92']}; {'id': '92', 'type': 'assignment', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '94', 'type': 'call', 'children': ['95', '98']}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'alias_dict'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '98', 'type': 'argument_list', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '101', 'type': 'if_statement', 'children': ['102', '105']}; {'id': '102', 'type': 'comparison_operator', 'children': ['103', '104'], 'value': 'in'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'dict1'}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '111']}; {'id': '108', 'type': 'subscript', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'dict1'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '112', 'type': 'return_statement', 'children': ['113']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'dict1'} | def update_existing(dict1, dict2, copy=False, assert_exists=False,
iswarning=False, alias_dict=None):
r
if assert_exists:
try:
assert_keys_are_subset(dict1, dict2)
except AssertionError as ex:
from utool import util_dbg
util_dbg.printex(ex, iswarning=iswarning, N=1)
if not iswarning:
raise
if copy:
dict1 = dict(dict1)
if alias_dict is None:
alias_dict = {}
for key, val in six.iteritems(dict2):
key = alias_dict.get(key, key)
if key in dict1:
dict1[key] = val
return dict1 |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'groupby_tags'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'item_list'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'tags_list'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '16', '39']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'groupid_to_items'}; {'id': '12', 'type': 'call', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '14', 'type': 'argument_list', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '16', 'type': 'for_statement', 'children': ['17', '20', '25']}, {'id': '17', 'type': 'pattern_list', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'tags'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '22', 'type': 'argument_list', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'tags_list'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'item_list'}, {'id': '25', 'type': 'block', 'children': ['26']}; {'id': '26', 'type': 'for_statement', 'children': ['27', '28', '29']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'tag'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'tags'}, {'id': '29', 'type': 'block', 'children': ['30']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'call', 'children': ['32', '37']}; {'id': '32', 'type': 'attribute', 'children': ['33', '36']}, {'id': '33', 'type': 'subscript', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'groupid_to_items'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'tag'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '39', 'type': 'return_statement', 'children': ['40']} | def groupby_tags(item_list, tags_list):
r
groupid_to_items = defaultdict(list)
for tags, item in zip(tags_list, item_list):
for tag in tags:
groupid_to_items[tag].append(item)
return groupid_to_items |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'group_items'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'by'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'sorted_'}; {'id': '10', 'type': 'True', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '75', '82', '97']}; {'id': '12', 'type': 'if_statement', 'children': ['13', '16', '69']}, {'id': '13', 'type': 'comparison_operator', 'children': ['14', '15'], 'value': 'is'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'by'}, {'id': '15', 'type': 'None', 'children': []}; {'id': '16', 'type': 'block', 'children': ['17', '28']}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '22', 'type': 'argument_list', 'children': ['23']}, {'id': '23', 'type': 'call', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '25', 'type': 'argument_list', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'by'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '28', 'type': 'if_statement', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'sorted_'}; {'id': '30', 'type': 'block', 'children': ['31']}, {'id': '31', 'type': 'try_statement', 'children': ['32', '48']}; {'id': '32', 'type': 'block', 'children': ['33']}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '36', 'type': 'call', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '38', 'type': 'argument_list', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '40', 'type': 'keyword_argument', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '42', 'type': 'call', 'children': ['43', '46']}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'op'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'itemgetter'}; {'id': '46', 'type': 'argument_list', 'children': ['47']}, {'id': '47', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '48', 'type': 'except_clause', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'TypeError'}; {'id': '50', 'type': 'block', 'children': ['51']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '54', 'type': 'call', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '56', 'type': 'argument_list', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '58', 'type': 'keyword_argument', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '60', 'type': 'lambda', 'children': ['61', '63']}, {'id': '61', 'type': 'lambda_parameters', 'children': ['62']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'tup'}, {'id': '63', 'type': 'call', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '65', 'type': 'argument_list', 'children': ['66']}; {'id': '66', 'type': 'subscript', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'tup'}; {'id': '68', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '69', 'type': 'else_clause', 'children': ['70']}; {'id': '70', 'type': 'block', 'children': ['71']}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'pairs'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'groupid_to_items'}; {'id': '78', 'type': 'call', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '82', 'type': 'for_statement', 'children': ['83', '86', '87']}, {'id': '83', 'type': 'pattern_list', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'groupid'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'pairs'}, {'id': '87', 'type': 'block', 'children': ['88']}; {'id': '88', 'type': 'expression_statement', 'children': ['89']}, {'id': '89', 'type': 'call', 'children': ['90', '95']}; {'id': '90', 'type': 'attribute', 'children': ['91', '94']}, {'id': '91', 'type': 'subscript', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'groupid_to_items'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'groupid'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '97', 'type': 'return_statement', 'children': ['98']} | def group_items(items, by=None, sorted_=True):
if by is not None:
pairs = list(zip(by, items))
if sorted_:
try:
pairs = sorted(pairs, key=op.itemgetter(0))
except TypeError:
pairs = sorted(pairs, key=lambda tup: str(tup[0]))
else:
pairs = items
groupid_to_items = defaultdict(list)
for groupid, item in pairs:
groupid_to_items[groupid].append(item)
return groupid_to_items |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '12']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'hierarchical_map_vals'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'func'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'max_depth'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'depth'}, {'id': '11', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '12', 'type': 'block', 'children': ['13']}, {'id': '13', 'type': 'if_statement', 'children': ['14', '20', '26', '41']}; {'id': '14', 'type': 'not_operator', 'children': ['15']}, {'id': '15', 'type': 'call', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'hasattr'}, {'id': '17', 'type': 'argument_list', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '19', 'type': 'string', 'children': [], 'value': "'items'"}; {'id': '20', 'type': 'block', 'children': ['21']}, {'id': '21', 'type': 'return_statement', 'children': ['22']}; {'id': '22', 'type': 'call', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'func'}; {'id': '24', 'type': 'argument_list', 'children': ['25']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '26', 'type': 'elif_clause', 'children': ['27', '34']}, {'id': '27', 'type': 'boolean_operator', 'children': ['28', '31'], 'value': 'and'}; {'id': '28', 'type': 'comparison_operator', 'children': ['29', '30'], 'value': 'is'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'max_depth'}; {'id': '30', 'type': 'None', 'children': []}, {'id': '31', 'type': 'comparison_operator', 'children': ['32', '33'], 'value': '>='}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'depth'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'max_depth'}; {'id': '34', 'type': 'block', 'children': ['35']}, {'id': '35', 'type': 'return_statement', 'children': ['36']}; {'id': '36', 'type': 'call', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'map_dict_vals'}; {'id': '38', 'type': 'argument_list', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'func'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '41', 'type': 'else_clause', 'children': ['42']}; {'id': '42', 'type': 'block', 'children': ['43', '67']}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'assignment', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'keyval_list'}; {'id': '46', 'type': 'list_comprehension', 'children': ['47', '58']}, {'id': '47', 'type': 'tuple', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '49', 'type': 'call', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'hierarchical_map_vals'}, {'id': '51', 'type': 'argument_list', 'children': ['52', '53', '54', '55']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'func'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'max_depth'}, {'id': '55', 'type': 'binary_operator', 'children': ['56', '57'], 'value': '+'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'depth'}, {'id': '57', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '58', 'type': 'for_in_clause', 'children': ['59', '62']}, {'id': '59', 'type': 'pattern_list', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '62', 'type': 'call', 'children': ['63', '66']}, {'id': '63', 'type': 'attribute', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '66', 'type': 'argument_list', 'children': []}, {'id': '67', 'type': 'if_statement', 'children': ['68', '73', '79']}; {'id': '68', 'type': 'call', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '70', 'type': 'argument_list', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}, {'id': '73', 'type': 'block', 'children': ['74']}; {'id': '74', 'type': 'return_statement', 'children': ['75']}, {'id': '75', 'type': 'call', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}, {'id': '77', 'type': 'argument_list', 'children': ['78']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'keyval_list'}, {'id': '79', 'type': 'else_clause', 'children': ['80']}; {'id': '80', 'type': 'block', 'children': ['81']}, {'id': '81', 'type': 'return_statement', 'children': ['82']}; {'id': '82', 'type': 'call', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '84', 'type': 'argument_list', 'children': ['85']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'keyval_list'} | def hierarchical_map_vals(func, node, max_depth=None, depth=0):
if not hasattr(node, 'items'):
return func(node)
elif max_depth is not None and depth >= max_depth:
return map_dict_vals(func, node)
else:
keyval_list = [(key, hierarchical_map_vals(func, val, max_depth, depth + 1)) for key, val in node.items()]
if isinstance(node, OrderedDict):
return OrderedDict(keyval_list)
else:
return dict(keyval_list) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '14']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'sort_dict'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '7', 'type': 'string', 'children': [], 'value': "'keys'"}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'reverse'}, {'id': '13', 'type': 'False', 'children': []}; {'id': '14', 'type': 'block', 'children': ['15', '45', '73', '91', '98']}, {'id': '15', 'type': 'if_statement', 'children': ['16', '19', '24', '35']}; {'id': '16', 'type': 'comparison_operator', 'children': ['17', '18'], 'value': '=='}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'part'}; {'id': '18', 'type': 'string', 'children': [], 'value': "'keys'"}, {'id': '19', 'type': 'block', 'children': ['20']}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'assignment', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '23', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '24', 'type': 'elif_clause', 'children': ['25', '30']}, {'id': '25', 'type': 'comparison_operator', 'children': ['26', '27'], 'value': 'in'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '27', 'type': 'set', 'children': ['28', '29']}; {'id': '28', 'type': 'string', 'children': [], 'value': "'vals'"}, {'id': '29', 'type': 'string', 'children': [], 'value': "'values'"}; {'id': '30', 'type': 'block', 'children': ['31']}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'index'}; {'id': '34', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '35', 'type': 'else_clause', 'children': ['36']}; {'id': '36', 'type': 'block', 'children': ['37']}, {'id': '37', 'type': 'raise_statement', 'children': ['38']}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'binary_operator', 'children': ['42', '43'], 'value': '%'}; {'id': '42', 'type': 'string', 'children': [], 'value': "'Unknown method part=%r'"}, {'id': '43', 'type': 'tuple', 'children': ['44']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '45', 'type': 'if_statement', 'children': ['46', '49', '59']}; {'id': '46', 'type': 'comparison_operator', 'children': ['47', '48'], 'value': 'is'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '48', 'type': 'None', 'children': []}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': '_key'}, {'id': '53', 'type': 'call', 'children': ['54', '57']}; {'id': '54', 'type': 'attribute', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'op'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'itemgetter'}, {'id': '57', 'type': 'argument_list', 'children': ['58']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '59', 'type': 'else_clause', 'children': ['60']}; {'id': '60', 'type': 'block', 'children': ['61']}, {'id': '61', 'type': 'function_definition', 'children': ['62', '63', '65']}; {'id': '62', 'type': 'function_name', 'children': [], 'value': '_key'}, {'id': '63', 'type': 'parameters', 'children': ['64']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '65', 'type': 'block', 'children': ['66']}; {'id': '66', 'type': 'return_statement', 'children': ['67']}, {'id': '67', 'type': 'call', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'subscript', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '73', 'type': 'expression_statement', 'children': ['74']}; {'id': '74', 'type': 'assignment', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'sorted_items'}; {'id': '76', 'type': 'call', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '78', 'type': 'argument_list', 'children': ['79', '85', '88']}, {'id': '79', 'type': 'call', 'children': ['80', '83']}; {'id': '80', 'type': 'attribute', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'six'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'iteritems'}, {'id': '83', 'type': 'argument_list', 'children': ['84']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '85', 'type': 'keyword_argument', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': '_key'}; {'id': '88', 'type': 'keyword_argument', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'reverse'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'reverse'}, {'id': '91', 'type': 'expression_statement', 'children': ['92']}; {'id': '92', 'type': 'assignment', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'sorted_dict'}; {'id': '94', 'type': 'call', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}; {'id': '96', 'type': 'argument_list', 'children': ['97']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'sorted_items'}; {'id': '98', 'type': 'return_statement', 'children': ['99']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'sorted_dict'} | def sort_dict(dict_, part='keys', key=None, reverse=False):
if part == 'keys':
index = 0
elif part in {'vals', 'values'}:
index = 1
else:
raise ValueError('Unknown method part=%r' % (part,))
if key is None:
_key = op.itemgetter(index)
else:
def _key(item):
return key(item[index])
sorted_items = sorted(six.iteritems(dict_), key=_key, reverse=reverse)
sorted_dict = OrderedDict(sorted_items)
return sorted_dict |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'order_dict_by'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'key_order'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '20', '29', '39', '57']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'dict_keys'}; {'id': '12', 'type': 'call', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '14', 'type': 'argument_list', 'children': ['15']}, {'id': '15', 'type': 'call', 'children': ['16', '19']}; {'id': '16', 'type': 'attribute', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'dict_'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '19', 'type': 'argument_list', 'children': []}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'assignment', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'other_keys'}, {'id': '23', 'type': 'binary_operator', 'children': ['24', '25'], 'value': '-'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'dict_keys'}, {'id': '25', 'type': 'call', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '27', 'type': 'argument_list', 'children': ['28']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'key_order'}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'key_order'}; {'id': '32', 'type': 'call', 'children': ['33', '36']}, {'id': '33', 'type': 'attribute', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'it'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'chain'}; {'id': '36', 'type': 'argument_list', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'key_order'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'other_keys'}, {'id': '39', 'type': 'expression_statement', 'children': ['40']}; {'id': '40', 'type': 'assignment', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'sorted_dict'}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}; {'id': '44', 'type': 'generator_expression', 'children': ['45', '50', '53']}, {'id': '45', 'type': 'tuple', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '47', 'type': 'subscript', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '50', 'type': 'for_in_clause', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'key_order'}, {'id': '53', 'type': 'if_clause', 'children': ['54']}; {'id': '54', 'type': 'comparison_operator', 'children': ['55', '56'], 'value': 'in'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'dict_keys'}, {'id': '57', 'type': 'return_statement', 'children': ['58']} | def order_dict_by(dict_, key_order):
r
dict_keys = set(dict_.keys())
other_keys = dict_keys - set(key_order)
key_order = it.chain(key_order, other_keys)
sorted_dict = OrderedDict(
(key, dict_[key]) for key in key_order if key in dict_keys
)
return sorted_dict |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'iteritems_sorted'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '5', 'type': 'block', 'children': ['6']}; {'id': '6', 'type': 'if_statement', 'children': ['7', '12', '20']}, {'id': '7', 'type': 'call', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '9', 'type': 'argument_list', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'dict_'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}; {'id': '12', 'type': 'block', 'children': ['13']}, {'id': '13', 'type': 'return_statement', 'children': ['14']}; {'id': '14', 'type': 'call', 'children': ['15', '18']}, {'id': '15', 'type': 'attribute', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'iteritems'}; {'id': '18', 'type': 'argument_list', 'children': ['19']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'dict_'}; {'id': '20', 'type': 'else_clause', 'children': ['21']}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'return_statement', 'children': ['23']}, {'id': '23', 'type': 'call', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'iter'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'call', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '28', 'type': 'argument_list', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '33']}; {'id': '30', 'type': 'attribute', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'six'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'iteritems'}, {'id': '33', 'type': 'argument_list', 'children': ['34']} | def iteritems_sorted(dict_):
if isinstance(dict_, OrderedDict):
return six.iteritems(dict_)
else:
return iter(sorted(six.iteritems(dict_))) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'num_fmt'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'max_digits'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '11', '18', '36', '71']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '11', 'type': 'if_statement', 'children': ['12', '15']}; {'id': '12', 'type': 'comparison_operator', 'children': ['13', '14'], 'value': 'is'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '14', 'type': 'None', 'children': []}, {'id': '15', 'type': 'block', 'children': ['16']}; {'id': '16', 'type': 'return_statement', 'children': ['17']}, {'id': '17', 'type': 'string', 'children': [], 'value': "'None'"}; {'id': '18', 'type': 'function_definition', 'children': ['19', '20', '23']}, {'id': '19', 'type': 'function_name', 'children': [], 'value': 'num_in_mag'}; {'id': '20', 'type': 'parameters', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'mag'}, {'id': '23', 'type': 'block', 'children': ['24']}; {'id': '24', 'type': 'return_statement', 'children': ['25']}, {'id': '25', 'type': 'boolean_operator', 'children': ['26', '29'], 'value': 'and'}; {'id': '26', 'type': 'comparison_operator', 'children': ['27', '28'], 'value': '>'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'mag'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '29', 'type': 'comparison_operator', 'children': ['30', '31'], 'value': '>'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '31', 'type': 'parenthesized_expression', 'children': ['32']}; {'id': '32', 'type': 'binary_operator', 'children': ['33', '35'], 'value': '*'}, {'id': '33', 'type': 'unary_operator', 'children': ['34'], 'value': '-'}; {'id': '34', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'mag'}; {'id': '36', 'type': 'if_statement', 'children': ['37', '40']}, {'id': '37', 'type': 'comparison_operator', 'children': ['38', '39'], 'value': 'is'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'max_digits'}, {'id': '39', 'type': 'None', 'children': []}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'if_statement', 'children': ['42', '47', '65']}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'num_in_mag'}; {'id': '44', 'type': 'argument_list', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '46', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'if_statement', 'children': ['49', '54', '59']}, {'id': '49', 'type': 'call', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'num_in_mag'}, {'id': '51', 'type': 'argument_list', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '53', 'type': 'float', 'children': [], 'value': '.1'}; {'id': '54', 'type': 'block', 'children': ['55']}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'assignment', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'max_digits'}; {'id': '58', 'type': 'integer', 'children': [], 'value': '4'}, {'id': '59', 'type': 'else_clause', 'children': ['60']}; {'id': '60', 'type': 'block', 'children': ['61']}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'max_digits'}; {'id': '64', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '65', 'type': 'else_clause', 'children': ['66']}; {'id': '66', 'type': 'block', 'children': ['67']}, {'id': '67', 'type': 'expression_statement', 'children': ['68']}; {'id': '68', 'type': 'assignment', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'max_digits'}; {'id': '70', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '71', 'type': 'if_statement', 'children': ['72', '78', '137', '150']}; {'id': '72', 'type': 'call', 'children': ['73', '76']}, {'id': '73', 'type': 'attribute', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'util_type'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'is_float'}; {'id': '76', 'type': 'argument_list', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '78', 'type': 'block', 'children': ['79', '93', '107', '121', '135']}, {'id': '79', 'type': 'expression_statement', 'children': ['80']}; {'id': '80', 'type': 'assignment', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'num_str'}; {'id': '82', 'type': 'binary_operator', 'children': ['83', '92'], 'value': '%'}, {'id': '83', 'type': 'parenthesized_expression', 'children': ['84']}; {'id': '84', 'type': 'binary_operator', 'children': ['85', '91'], 'value': '+'}, {'id': '85', 'type': 'binary_operator', 'children': ['86', '87'], 'value': '+'}; {'id': '86', 'type': 'string', 'children': [], 'value': "'%.'"}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'max_digits'}, {'id': '91', 'type': 'string', 'children': [], 'value': "'f'"}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'num_str'}; {'id': '96', 'type': 'call', 'children': ['97', '105']}, {'id': '97', 'type': 'attribute', 'children': ['98', '104']}; {'id': '98', 'type': 'call', 'children': ['99', '102']}, {'id': '99', 'type': 'attribute', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'num_str'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'rstrip'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'string', 'children': [], 'value': "'0'"}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'lstrip'}, {'id': '105', 'type': 'argument_list', 'children': ['106']}; {'id': '106', 'type': 'string', 'children': [], 'value': "'0'"}, {'id': '107', 'type': 'if_statement', 'children': ['108', '114']}; {'id': '108', 'type': 'call', 'children': ['109', '112']}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'num_str'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '112', 'type': 'argument_list', 'children': ['113']}, {'id': '113', 'type': 'string', 'children': [], 'value': "'.'"}; {'id': '114', 'type': 'block', 'children': ['115']}, {'id': '115', 'type': 'expression_statement', 'children': ['116']}; {'id': '116', 'type': 'assignment', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'num_str'}; {'id': '118', 'type': 'binary_operator', 'children': ['119', '120'], 'value': '+'}, {'id': '119', 'type': 'string', 'children': [], 'value': "'0'"}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'num_str'}, {'id': '121', 'type': 'if_statement', 'children': ['122', '128']}; {'id': '122', 'type': 'call', 'children': ['123', '126']}, {'id': '123', 'type': 'attribute', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'num_str'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'endswith'}; {'id': '126', 'type': 'argument_list', 'children': ['127']}, {'id': '127', 'type': 'string', 'children': [], 'value': "'.'"}; {'id': '128', 'type': 'block', 'children': ['129']}, {'id': '129', 'type': 'expression_statement', 'children': ['130']}; {'id': '130', 'type': 'assignment', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'num_str'}; {'id': '132', 'type': 'binary_operator', 'children': ['133', '134'], 'value': '+'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'num_str'}; {'id': '134', 'type': 'string', 'children': [], 'value': "'0'"}, {'id': '135', 'type': 'return_statement', 'children': ['136']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'num_str'}, {'id': '137', 'type': 'elif_clause', 'children': ['138', '144']}; {'id': '138', 'type': 'call', 'children': ['139', '142']}, {'id': '139', 'type': 'attribute', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'util_type'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'is_int'}; {'id': '142', 'type': 'argument_list', 'children': ['143']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '144', 'type': 'block', 'children': ['145']}, {'id': '145', 'type': 'return_statement', 'children': ['146']}; {'id': '146', 'type': 'call', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'int_comma_str'}; {'id': '148', 'type': 'argument_list', 'children': ['149']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '150', 'type': 'else_clause', 'children': ['151']}, {'id': '151', 'type': 'block', 'children': ['152']}; {'id': '152', 'type': 'return_statement', 'children': ['153']}, {'id': '153', 'type': 'string', 'children': [], 'value': "'%r'"} | def num_fmt(num, max_digits=None):
r
if num is None:
return 'None'
def num_in_mag(num, mag):
return mag > num and num > (-1 * mag)
if max_digits is None:
if num_in_mag(num, 1):
if num_in_mag(num, .1):
max_digits = 4
else:
max_digits = 3
else:
max_digits = 1
if util_type.is_float(num):
num_str = ('%.' + str(max_digits) + 'f') % num
num_str = num_str.rstrip('0').lstrip('0')
if num_str.startswith('.'):
num_str = '0' + num_str
if num_str.endswith('.'):
num_str = num_str + '0'
return num_str
elif util_type.is_int(num):
return int_comma_str(num)
else:
return '%r' |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '16']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'inject_print_functions'}, {'id': '3', 'type': 'parameters', 'children': ['4', '7', '10', '13']}; {'id': '4', 'type': 'default_parameter', 'children': ['5', '6']}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'module_name'}; {'id': '6', 'type': 'None', 'children': []}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'module_prefix'}, {'id': '9', 'type': 'string', 'children': [], 'value': "'[???]'"}; {'id': '10', 'type': 'default_parameter', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'DEBUG'}; {'id': '12', 'type': 'False', 'children': []}, {'id': '13', 'type': 'default_parameter', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '15', 'type': 'None', 'children': []}; {'id': '16', 'type': 'block', 'children': ['17', '25', '323', '330']}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': '_get_module'}; {'id': '22', 'type': 'argument_list', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'module_name'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '25', 'type': 'if_statement', 'children': ['26', '27', '49']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'SILENT'}, {'id': '27', 'type': 'block', 'children': ['28', '35', '42']}; {'id': '28', 'type': 'function_definition', 'children': ['29', '30', '33']}, {'id': '29', 'type': 'function_name', 'children': [], 'value': 'print'}; {'id': '30', 'type': 'parameters', 'children': ['31']}, {'id': '31', 'type': 'list_splat_pattern', 'children': ['32']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '33', 'type': 'block', 'children': ['34']}; {'id': '34', 'type': 'pass_statement', 'children': []}, {'id': '35', 'type': 'function_definition', 'children': ['36', '37', '40']}; {'id': '36', 'type': 'function_name', 'children': [], 'value': 'printDBG'}, {'id': '37', 'type': 'parameters', 'children': ['38']}; {'id': '38', 'type': 'list_splat_pattern', 'children': ['39']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'pass_statement', 'children': []}; {'id': '42', 'type': 'function_definition', 'children': ['43', '44', '47']}, {'id': '43', 'type': 'function_name', 'children': [], 'value': 'print_'}; {'id': '44', 'type': 'parameters', 'children': ['45']}, {'id': '45', 'type': 'list_splat_pattern', 'children': ['46']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'pass_statement', 'children': []}, {'id': '49', 'type': 'else_clause', 'children': ['50']}; {'id': '50', 'type': 'block', 'children': ['51', '113', '158', '169', '195', '220', '229', '238', '255', '268']}, {'id': '51', 'type': 'if_statement', 'children': ['52', '53', '95']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'DEBUG_PRINT'}, {'id': '53', 'type': 'block', 'children': ['54']}; {'id': '54', 'type': 'function_definition', 'children': ['55', '56', '59']}, {'id': '55', 'type': 'function_name', 'children': [], 'value': 'print'}; {'id': '56', 'type': 'parameters', 'children': ['57']}, {'id': '57', 'type': 'list_splat_pattern', 'children': ['58']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '59', 'type': 'block', 'children': ['60', '67', '84']}; {'id': '60', 'type': 'import_from_statement', 'children': ['61', '65']}, {'id': '61', 'type': 'dotted_name', 'children': ['62', '63', '64']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': '_internal'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'meta_util_dbg'}, {'id': '65', 'type': 'dotted_name', 'children': ['66']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'get_caller_name'}, {'id': '67', 'type': 'expression_statement', 'children': ['68']}; {'id': '68', 'type': 'assignment', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'calltag'}; {'id': '70', 'type': 'call', 'children': ['71', '74']}, {'id': '71', 'type': 'attribute', 'children': ['72', '73']}; {'id': '72', 'type': 'string', 'children': [], 'value': "''"}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '74', 'type': 'argument_list', 'children': ['75']}, {'id': '75', 'type': 'tuple', 'children': ['76', '77', '83']}; {'id': '76', 'type': 'string', 'children': [], 'value': "'[caller:'"}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'get_caller_name'}, {'id': '79', 'type': 'argument_list', 'children': ['80']}; {'id': '80', 'type': 'keyword_argument', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'N'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'DEBUG_PRINT_N'}, {'id': '83', 'type': 'string', 'children': [], 'value': "']'"}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'call', 'children': ['86', '91']}; {'id': '86', 'type': 'call', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'util_logging'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': '_utool_print'}; {'id': '90', 'type': 'argument_list', 'children': []}, {'id': '91', 'type': 'argument_list', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'calltag'}, {'id': '93', 'type': 'list_splat', 'children': ['94']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '95', 'type': 'else_clause', 'children': ['96']}; {'id': '96', 'type': 'block', 'children': ['97']}, {'id': '97', 'type': 'function_definition', 'children': ['98', '99', '102']}; {'id': '98', 'type': 'function_name', 'children': [], 'value': 'print'}, {'id': '99', 'type': 'parameters', 'children': ['100']}; {'id': '100', 'type': 'list_splat_pattern', 'children': ['101']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '102', 'type': 'block', 'children': ['103']}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'call', 'children': ['105', '110']}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'util_logging'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': '_utool_print'}, {'id': '109', 'type': 'argument_list', 'children': []}; {'id': '110', 'type': 'argument_list', 'children': ['111']}, {'id': '111', 'type': 'list_splat', 'children': ['112']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '113', 'type': 'if_statement', 'children': ['114', '115', '140']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': '__AGGROFLUSH__'}, {'id': '115', 'type': 'block', 'children': ['116']}; {'id': '116', 'type': 'function_definition', 'children': ['117', '118', '121']}, {'id': '117', 'type': 'function_name', 'children': [], 'value': 'print_'}; {'id': '118', 'type': 'parameters', 'children': ['119']}, {'id': '119', 'type': 'list_splat_pattern', 'children': ['120']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '121', 'type': 'block', 'children': ['122', '132']}; {'id': '122', 'type': 'expression_statement', 'children': ['123']}, {'id': '123', 'type': 'call', 'children': ['124', '129']}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'util_logging'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': '_utool_write'}; {'id': '128', 'type': 'argument_list', 'children': []}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'list_splat', 'children': ['131']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '132', 'type': 'expression_statement', 'children': ['133']}, {'id': '133', 'type': 'call', 'children': ['134', '139']}; {'id': '134', 'type': 'call', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'util_logging'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': '_utool_flush'}; {'id': '138', 'type': 'argument_list', 'children': []}, {'id': '139', 'type': 'argument_list', 'children': []}; {'id': '140', 'type': 'else_clause', 'children': ['141']}, {'id': '141', 'type': 'block', 'children': ['142']}; {'id': '142', 'type': 'function_definition', 'children': ['143', '144', '147']}, {'id': '143', 'type': 'function_name', 'children': [], 'value': 'print_'}; {'id': '144', 'type': 'parameters', 'children': ['145']}, {'id': '145', 'type': 'list_splat_pattern', 'children': ['146']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '147', 'type': 'block', 'children': ['148']}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'call', 'children': ['150', '155']}; {'id': '150', 'type': 'call', 'children': ['151', '154']}, {'id': '151', 'type': 'attribute', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'util_logging'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': '_utool_write'}; {'id': '154', 'type': 'argument_list', 'children': []}, {'id': '155', 'type': 'argument_list', 'children': ['156']}; {'id': '156', 'type': 'list_splat', 'children': ['157']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'dotpos'}, {'id': '161', 'type': 'call', 'children': ['162', '167']}; {'id': '162', 'type': 'attribute', 'children': ['163', '166']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': '__name__'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'rfind'}, {'id': '167', 'type': 'argument_list', 'children': ['168']}; {'id': '168', 'type': 'string', 'children': [], 'value': "'.'"}, {'id': '169', 'type': 'if_statement', 'children': ['170', '174', '181']}; {'id': '170', 'type': 'comparison_operator', 'children': ['171', '172'], 'value': '=='}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'dotpos'}; {'id': '172', 'type': 'unary_operator', 'children': ['173'], 'value': '-'}, {'id': '173', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '174', 'type': 'block', 'children': ['175']}, {'id': '175', 'type': 'expression_statement', 'children': ['176']}; {'id': '176', 'type': 'assignment', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'module_name'}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': '__name__'}, {'id': '181', 'type': 'else_clause', 'children': ['182']}; {'id': '182', 'type': 'block', 'children': ['183']}, {'id': '183', 'type': 'expression_statement', 'children': ['184']}; {'id': '184', 'type': 'assignment', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'module_name'}; {'id': '186', 'type': 'subscript', 'children': ['187', '190']}, {'id': '187', 'type': 'attribute', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '189', 'type': 'identifier', 'children': [], 'value': '__name__'}; {'id': '190', 'type': 'slice', 'children': ['191', '194']}, {'id': '191', 'type': 'binary_operator', 'children': ['192', '193'], 'value': '+'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'dotpos'}, {'id': '193', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '194', 'type': 'colon', 'children': []}, {'id': '195', 'type': 'function_definition', 'children': ['196', '197', '199']}; {'id': '196', 'type': 'function_name', 'children': [], 'value': '_replchars'}, {'id': '197', 'type': 'parameters', 'children': ['198']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'str_'}, {'id': '199', 'type': 'block', 'children': ['200']}; {'id': '200', 'type': 'return_statement', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '217']}; {'id': '202', 'type': 'attribute', 'children': ['203', '216']}, {'id': '203', 'type': 'call', 'children': ['204', '213']}; {'id': '204', 'type': 'attribute', 'children': ['205', '212']}, {'id': '205', 'type': 'call', 'children': ['206', '209']}; {'id': '206', 'type': 'attribute', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'str_'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'replace'}, {'id': '209', 'type': 'argument_list', 'children': ['210', '211']}; {'id': '210', 'type': 'string', 'children': [], 'value': "'_'"}, {'id': '211', 'type': 'string', 'children': [], 'value': "'-'"}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'replace'}, {'id': '213', 'type': 'argument_list', 'children': ['214', '215']}; {'id': '214', 'type': 'string', 'children': [], 'value': "']'"}, {'id': '215', 'type': 'string', 'children': [], 'value': "''"}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'replace'}, {'id': '217', 'type': 'argument_list', 'children': ['218', '219']}; {'id': '218', 'type': 'string', 'children': [], 'value': "'['"}, {'id': '219', 'type': 'string', 'children': [], 'value': "''"}; {'id': '220', 'type': 'expression_statement', 'children': ['221']}, {'id': '221', 'type': 'assignment', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'flag1'}, {'id': '223', 'type': 'binary_operator', 'children': ['224', '225'], 'value': '%'}; {'id': '224', 'type': 'string', 'children': [], 'value': "'--debug-%s'"}, {'id': '225', 'type': 'call', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': '_replchars'}, {'id': '227', 'type': 'argument_list', 'children': ['228']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'module_name'}, {'id': '229', 'type': 'expression_statement', 'children': ['230']}; {'id': '230', 'type': 'assignment', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'flag2'}; {'id': '232', 'type': 'binary_operator', 'children': ['233', '234'], 'value': '%'}, {'id': '233', 'type': 'string', 'children': [], 'value': "'--debug-%s'"}; {'id': '234', 'type': 'call', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': '_replchars'}; {'id': '236', 'type': 'argument_list', 'children': ['237']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'module_prefix'}; {'id': '238', 'type': 'expression_statement', 'children': ['239']}, {'id': '239', 'type': 'assignment', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'DEBUG_FLAG'}, {'id': '241', 'type': 'call', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'any'}, {'id': '243', 'type': 'argument_list', 'children': ['244']}; {'id': '244', 'type': 'list_comprehension', 'children': ['245', '250']}, {'id': '245', 'type': 'comparison_operator', 'children': ['246', '247'], 'value': 'in'}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'flag'}, {'id': '247', 'type': 'attribute', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'argv'}; {'id': '250', 'type': 'for_in_clause', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'flag'}; {'id': '252', 'type': 'list', 'children': ['253', '254'], 'value': '[flag1, flag2]'}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'flag1'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'flag2'}, {'id': '255', 'type': 'for_statement', 'children': ['256', '257', '258']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'curflag'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'ARGV_DEBUG_FLAGS'}; {'id': '258', 'type': 'block', 'children': ['259']}, {'id': '259', 'type': 'if_statement', 'children': ['260', '263']}; {'id': '260', 'type': 'comparison_operator', 'children': ['261', '262'], 'value': 'in'}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'curflag'}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'module_prefix'}, {'id': '263', 'type': 'block', 'children': ['264']}; {'id': '264', 'type': 'expression_statement', 'children': ['265']}, {'id': '265', 'type': 'assignment', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'DEBUG_FLAG'}, {'id': '267', 'type': 'True', 'children': []}; {'id': '268', 'type': 'if_statement', 'children': ['269', '274', '314']}, {'id': '269', 'type': 'boolean_operator', 'children': ['270', '273'], 'value': 'or'}; {'id': '270', 'type': 'boolean_operator', 'children': ['271', '272'], 'value': 'or'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': '__DEBUG_ALL__'}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'DEBUG'}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'DEBUG_FLAG'}; {'id': '274', 'type': 'block', 'children': ['275', '284']}, {'id': '275', 'type': 'expression_statement', 'children': ['276']}; {'id': '276', 'type': 'call', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '278', 'type': 'argument_list', 'children': ['279']}, {'id': '279', 'type': 'binary_operator', 'children': ['280', '281'], 'value': '%'}; {'id': '280', 'type': 'string', 'children': [], 'value': "'INJECT_PRINT: %r == %r'"}, {'id': '281', 'type': 'tuple', 'children': ['282', '283']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'module_name'}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'module_prefix'}; {'id': '284', 'type': 'function_definition', 'children': ['285', '286', '289']}, {'id': '285', 'type': 'function_name', 'children': [], 'value': 'printDBG'}; {'id': '286', 'type': 'parameters', 'children': ['287']}, {'id': '287', 'type': 'list_splat_pattern', 'children': ['288']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '289', 'type': 'block', 'children': ['290', '303']}; {'id': '290', 'type': 'expression_statement', 'children': ['291']}, {'id': '291', 'type': 'assignment', 'children': ['292', '293']}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '293', 'type': 'call', 'children': ['294', '297']}; {'id': '294', 'type': 'attribute', 'children': ['295', '296']}, {'id': '295', 'type': 'string', 'children': [], 'value': "', '"}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '297', 'type': 'argument_list', 'children': ['298']}; {'id': '298', 'type': 'call', 'children': ['299', '300']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'map'}; {'id': '300', 'type': 'argument_list', 'children': ['301', '302']}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '302', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '303', 'type': 'expression_statement', 'children': ['304']}; {'id': '304', 'type': 'call', 'children': ['305', '308']}, {'id': '305', 'type': 'attribute', 'children': ['306', '307']}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'util_logging'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': '__UTOOL_PRINTDBG__'}; {'id': '308', 'type': 'argument_list', 'children': ['309']}, {'id': '309', 'type': 'binary_operator', 'children': ['310', '313'], 'value': '+'}; {'id': '310', 'type': 'binary_operator', 'children': ['311', '312'], 'value': '+'}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'module_prefix'}; {'id': '312', 'type': 'string', 'children': [], 'value': "' DEBUG '"}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '314', 'type': 'else_clause', 'children': ['315']}, {'id': '315', 'type': 'block', 'children': ['316']}; {'id': '316', 'type': 'function_definition', 'children': ['317', '318', '321']}, {'id': '317', 'type': 'function_name', 'children': [], 'value': 'printDBG'}; {'id': '318', 'type': 'parameters', 'children': ['319']}, {'id': '319', 'type': 'list_splat_pattern', 'children': ['320']}; {'id': '320', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '321', 'type': 'block', 'children': ['322']}; {'id': '322', 'type': 'pass_statement', 'children': []}, {'id': '323', 'type': 'expression_statement', 'children': ['324']}; {'id': '324', 'type': 'assignment', 'children': ['325', '326']}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'print_funcs'}; {'id': '326', 'type': 'tuple', 'children': ['327', '328', '329']}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'print_'}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'printDBG'}; {'id': '330', 'type': 'return_statement', 'children': ['331']}, {'id': '331', 'type': 'identifier', 'children': [], 'value': 'print_funcs'} | def inject_print_functions(module_name=None, module_prefix='[???]',
DEBUG=False, module=None):
module = _get_module(module_name, module)
if SILENT:
def print(*args):
pass
def printDBG(*args):
pass
def print_(*args):
pass
else:
if DEBUG_PRINT:
def print(*args):
from utool._internal.meta_util_dbg import get_caller_name
calltag = ''.join(('[caller:', get_caller_name(N=DEBUG_PRINT_N), ']' ))
util_logging._utool_print()(calltag, *args)
else:
def print(*args):
util_logging._utool_print()(*args)
if __AGGROFLUSH__:
def print_(*args):
util_logging._utool_write()(*args)
util_logging._utool_flush()()
else:
def print_(*args):
util_logging._utool_write()(*args)
dotpos = module.__name__.rfind('.')
if dotpos == -1:
module_name = module.__name__
else:
module_name = module.__name__[dotpos + 1:]
def _replchars(str_):
return str_.replace('_', '-').replace(']', '').replace('[', '')
flag1 = '--debug-%s' % _replchars(module_name)
flag2 = '--debug-%s' % _replchars(module_prefix)
DEBUG_FLAG = any([flag in sys.argv for flag in [flag1, flag2]])
for curflag in ARGV_DEBUG_FLAGS:
if curflag in module_prefix:
DEBUG_FLAG = True
if __DEBUG_ALL__ or DEBUG or DEBUG_FLAG:
print('INJECT_PRINT: %r == %r' % (module_name, module_prefix))
def printDBG(*args):
msg = ', '.join(map(str, args))
util_logging.__UTOOL_PRINTDBG__(module_prefix + ' DEBUG ' + msg)
else:
def printDBG(*args):
pass
print_funcs = (print, print_, printDBG)
return print_funcs |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '13']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_isobaric_ratios'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8', '9', '10', '11', '12']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'psmfn'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'psmheader'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'denom_channels'}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'min_int'}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'targetfn'}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'accessioncol'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'normalize'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'normratiofn'}, {'id': '13', 'type': 'block', 'children': ['14', '26', '112']}; {'id': '14', 'type': 'expression_statement', 'children': ['15']}, {'id': '15', 'type': 'assignment', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'psm_or_feat_ratios'}, {'id': '17', 'type': 'call', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'get_psmratios'}, {'id': '19', 'type': 'argument_list', 'children': ['20', '21', '22', '23', '24', '25']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'psmfn'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'psmheader'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'denom_channels'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'min_int'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'accessioncol'}; {'id': '26', 'type': 'if_statement', 'children': ['27', '30', '69', '106']}, {'id': '27', 'type': 'boolean_operator', 'children': ['28', '29'], 'value': 'and'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'normalize'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'normratiofn'}; {'id': '30', 'type': 'block', 'children': ['31', '40', '49', '60']}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'normheader'}; {'id': '34', 'type': 'call', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'reader'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'get_tsv_header'}; {'id': '38', 'type': 'argument_list', 'children': ['39']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'normratiofn'}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'normratios'}, {'id': '43', 'type': 'call', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'get_ratios_from_fn'}, {'id': '45', 'type': 'argument_list', 'children': ['46', '47', '48']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'normratiofn'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'normheader'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '49', 'type': 'expression_statement', 'children': ['50']}; {'id': '50', 'type': 'assignment', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'ch_medians'}; {'id': '52', 'type': 'call', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'get_medians'}; {'id': '54', 'type': 'argument_list', 'children': ['55', '56', '57']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'channels'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'normratios'}, {'id': '57', 'type': 'keyword_argument', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'report'}, {'id': '59', 'type': 'True', 'children': []}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'outratios'}, {'id': '63', 'type': 'call', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'calculate_normalized_ratios'}, {'id': '65', 'type': 'argument_list', 'children': ['66', '67', '68']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'psm_or_feat_ratios'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'ch_medians'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '69', 'type': 'elif_clause', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'normalize'}, {'id': '71', 'type': 'block', 'children': ['72', '86', '97']}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'flatratios'}, {'id': '75', 'type': 'list_comprehension', 'children': ['76', '83']}; {'id': '76', 'type': 'list_comprehension', 'children': ['77', '80']}, {'id': '77', 'type': 'subscript', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'feat'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'ch'}; {'id': '80', 'type': 'for_in_clause', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'ch'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '83', 'type': 'for_in_clause', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'feat'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'psm_or_feat_ratios'}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'ch_medians'}, {'id': '89', 'type': 'call', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'get_medians'}, {'id': '91', 'type': 'argument_list', 'children': ['92', '93', '94']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'channels'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'flatratios'}; {'id': '94', 'type': 'keyword_argument', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'report'}; {'id': '96', 'type': 'True', 'children': []}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'outratios'}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'calculate_normalized_ratios'}; {'id': '102', 'type': 'argument_list', 'children': ['103', '104', '105']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'psm_or_feat_ratios'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'ch_medians'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'channels'}; {'id': '106', 'type': 'else_clause', 'children': ['107']}, {'id': '107', 'type': 'block', 'children': ['108']}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'outratios'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'psm_or_feat_ratios'}; {'id': '112', 'type': 'if_statement', 'children': ['113', '116', '136', '150']}, {'id': '113', 'type': 'boolean_operator', 'children': ['114', '115'], 'value': 'and'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'accessioncol'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'targetfn'}; {'id': '116', 'type': 'block', 'children': ['117', '129']}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'outratios'}; {'id': '120', 'type': 'dictionary_comprehension', 'children': ['121', '126']}, {'id': '121', 'type': 'pair', 'children': ['122', '125']}; {'id': '122', 'type': 'subscript', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'ISOQUANTRATIO_FEAT_ACC'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '126', 'type': 'for_in_clause', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'outratios'}, {'id': '129', 'type': 'return_statement', 'children': ['130']}; {'id': '130', 'type': 'call', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'output_to_target_accession_table'}; {'id': '132', 'type': 'argument_list', 'children': ['133', '134', '135']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'targetfn'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'outratios'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'channels'}; {'id': '136', 'type': 'elif_clause', 'children': ['137', '142']}, {'id': '137', 'type': 'boolean_operator', 'children': ['138', '140'], 'value': 'and'}; {'id': '138', 'type': 'not_operator', 'children': ['139']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'accessioncol'}; {'id': '140', 'type': 'not_operator', 'children': ['141']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'targetfn'}; {'id': '142', 'type': 'block', 'children': ['143']}, {'id': '143', 'type': 'return_statement', 'children': ['144']}; {'id': '144', 'type': 'call', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'paste_to_psmtable'}; {'id': '146', 'type': 'argument_list', 'children': ['147', '148', '149']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'psmfn'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'psmheader'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'outratios'}; {'id': '150', 'type': 'elif_clause', 'children': ['151', '155']}, {'id': '151', 'type': 'boolean_operator', 'children': ['152', '153'], 'value': 'and'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'accessioncol'}, {'id': '153', 'type': 'not_operator', 'children': ['154']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'targetfn'}, {'id': '155', 'type': 'block', 'children': ['156']}; {'id': '156', 'type': 'return_statement', 'children': ['157']}, {'id': '157', 'type': 'generator_expression', 'children': ['158', '180']}; {'id': '158', 'type': 'dictionary_comprehension', 'children': ['159', '171']}, {'id': '159', 'type': 'pair', 'children': ['160', '170']}; {'id': '160', 'type': 'parenthesized_expression', 'children': ['161']}, {'id': '161', 'type': 'conditional_expression', 'children': ['162', '163', '167'], 'value': 'if'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '163', 'type': 'not_operator', 'children': ['164']}; {'id': '164', 'type': 'comparison_operator', 'children': ['165', '166'], 'value': '=='}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'ISOQUANTRATIO_FEAT_ACC'}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'prottabledata'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'HEADER_ACCESSION'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '171', 'type': 'for_in_clause', 'children': ['172', '175']}; {'id': '172', 'type': 'pattern_list', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '175', 'type': 'call', 'children': ['176', '179']}; {'id': '176', 'type': 'attribute', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'ratio'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '179', 'type': 'argument_list', 'children': []}; {'id': '180', 'type': 'for_in_clause', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'ratio'} | def get_isobaric_ratios(psmfn, psmheader, channels, denom_channels, min_int,
targetfn, accessioncol, normalize, normratiofn):
psm_or_feat_ratios = get_psmratios(psmfn, psmheader, channels,
denom_channels, min_int, accessioncol)
if normalize and normratiofn:
normheader = reader.get_tsv_header(normratiofn)
normratios = get_ratios_from_fn(normratiofn, normheader, channels)
ch_medians = get_medians(channels, normratios, report=True)
outratios = calculate_normalized_ratios(psm_or_feat_ratios, ch_medians,
channels)
elif normalize:
flatratios = [[feat[ch] for ch in channels]
for feat in psm_or_feat_ratios]
ch_medians = get_medians(channels, flatratios, report=True)
outratios = calculate_normalized_ratios(psm_or_feat_ratios, ch_medians,
channels)
else:
outratios = psm_or_feat_ratios
if accessioncol and targetfn:
outratios = {x[ISOQUANTRATIO_FEAT_ACC]: x for x in outratios}
return output_to_target_accession_table(targetfn, outratios, channels)
elif not accessioncol and not targetfn:
return paste_to_psmtable(psmfn, psmheader, outratios)
elif accessioncol and not targetfn:
return ({(k if not k == ISOQUANTRATIO_FEAT_ACC
else prottabledata.HEADER_ACCESSION): v
for k, v in ratio.items()} for ratio in outratios) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'clean_line_profile_text'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'text'}, {'id': '5', 'type': 'block', 'children': ['6', '13', '22', '42', '49', '62', '71', '78', '82']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'profile_block_list'}, {'id': '9', 'type': 'call', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'parse_rawprofile_blocks'}, {'id': '11', 'type': 'argument_list', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'text'}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'assignment', 'children': ['15', '18']}, {'id': '15', 'type': 'pattern_list', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'prefix_list'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'timemap'}; {'id': '18', 'type': 'call', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'parse_timemap_from_blocks'}; {'id': '20', 'type': 'argument_list', 'children': ['21']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'profile_block_list'}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'sorted_lists'}, {'id': '25', 'type': 'call', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '27', 'type': 'argument_list', 'children': ['28', '34']}; {'id': '28', 'type': 'call', 'children': ['29', '32']}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'iteritems'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'timemap'}; {'id': '34', 'type': 'keyword_argument', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '36', 'type': 'call', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'operator'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'itemgetter'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'newlist'}, {'id': '45', 'type': 'subscript', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'prefix_list'}, {'id': '47', 'type': 'slice', 'children': ['48']}; {'id': '48', 'type': 'colon', 'children': []}, {'id': '49', 'type': 'for_statement', 'children': ['50', '53', '54']}; {'id': '50', 'type': 'pattern_list', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'sorted_lists'}; {'id': '54', 'type': 'block', 'children': ['55']}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'call', 'children': ['57', '60']}, {'id': '57', 'type': 'attribute', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'newlist'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'extend'}; {'id': '60', 'type': 'argument_list', 'children': ['61']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'output_text'}, {'id': '65', 'type': 'call', 'children': ['66', '69']}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'string', 'children': [], 'value': "'\\n'"}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'newlist'}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'summary_text'}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'get_summary'}; {'id': '76', 'type': 'argument_list', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'profile_block_list'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'output_text'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'output_text'}; {'id': '82', 'type': 'return_statement', 'children': ['83']}, {'id': '83', 'type': 'expression_list', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'output_text'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'summary_text'} | def clean_line_profile_text(text):
profile_block_list = parse_rawprofile_blocks(text)
prefix_list, timemap = parse_timemap_from_blocks(profile_block_list)
sorted_lists = sorted(six.iteritems(timemap), key=operator.itemgetter(0))
newlist = prefix_list[:]
for key, val in sorted_lists:
newlist.extend(val)
output_text = '\n'.join(newlist)
summary_text = get_summary(profile_block_list)
output_text = output_text
return output_text, summary_text |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'random_product'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'rng'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '17', '27', '33', '44', '57', '66', '76']}; {'id': '12', 'type': 'import_statement', 'children': ['13']}, {'id': '13', 'type': 'aliased_import', 'children': ['14', '16']}; {'id': '14', 'type': 'dotted_name', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'rng'}; {'id': '20', 'type': 'call', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'ensure_rng'}; {'id': '24', 'type': 'argument_list', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'rng'}; {'id': '26', 'type': 'string', 'children': [], 'value': "'python'"}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'seen'}; {'id': '30', 'type': 'call', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '32', 'type': 'argument_list', 'children': []}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '36', 'type': 'list_comprehension', 'children': ['37', '41']}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '39', 'type': 'argument_list', 'children': ['40']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'g'}, {'id': '41', 'type': 'for_in_clause', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'g'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'max_num'}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'prod'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'call', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'map'}; {'id': '54', 'type': 'argument_list', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '57', 'type': 'if_statement', 'children': ['58', '61']}; {'id': '58', 'type': 'comparison_operator', 'children': ['59', '60'], 'value': 'is'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '60', 'type': 'None', 'children': []}, {'id': '61', 'type': 'block', 'children': ['62']}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'max_num'}; {'id': '66', 'type': 'if_statement', 'children': ['67', '70']}, {'id': '67', 'type': 'comparison_operator', 'children': ['68', '69'], 'value': '>'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'max_num'}; {'id': '70', 'type': 'block', 'children': ['71']}, {'id': '71', 'type': 'raise_statement', 'children': ['72']}; {'id': '72', 'type': 'call', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '74', 'type': 'argument_list', 'children': ['75']}, {'id': '75', 'type': 'string', 'children': [], 'value': "'num exceedes maximum number of products'"}; {'id': '76', 'type': 'if_statement', 'children': ['77', '82', '107']}, {'id': '77', 'type': 'comparison_operator', 'children': ['78', '79'], 'value': '>'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '79', 'type': 'binary_operator', 'children': ['80', '81'], 'value': '//'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'max_num'}, {'id': '81', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '82', 'type': 'block', 'children': ['83']}, {'id': '83', 'type': 'for_statement', 'children': ['84', '85', '103']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'prod'}, {'id': '85', 'type': 'call', 'children': ['86', '89']}; {'id': '86', 'type': 'attribute', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'shuffle'}, {'id': '89', 'type': 'argument_list', 'children': ['90', '100']}; {'id': '90', 'type': 'call', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'call', 'children': ['94', '97']}; {'id': '94', 'type': 'attribute', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'it'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'product'}, {'id': '97', 'type': 'argument_list', 'children': ['98']}; {'id': '98', 'type': 'list_splat', 'children': ['99']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '100', 'type': 'keyword_argument', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'rng'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'rng'}, {'id': '103', 'type': 'block', 'children': ['104']}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'yield', 'children': ['106']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'prod'}, {'id': '107', 'type': 'else_clause', 'children': ['108']}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'while_statement', 'children': ['110', '116']}; {'id': '110', 'type': 'comparison_operator', 'children': ['111', '115'], 'value': '<'}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '113', 'type': 'argument_list', 'children': ['114']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'seen'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '116', 'type': 'block', 'children': ['117', '138']}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'idxs'}; {'id': '120', 'type': 'call', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '122', 'type': 'generator_expression', 'children': ['123', '135']}, {'id': '123', 'type': 'call', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'rng'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'randint'}, {'id': '127', 'type': 'argument_list', 'children': ['128', '129']}; {'id': '128', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '129', 'type': 'binary_operator', 'children': ['130', '134'], 'value': '-'}; {'id': '130', 'type': 'call', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '132', 'type': 'argument_list', 'children': ['133']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'g'}; {'id': '134', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '135', 'type': 'for_in_clause', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'g'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '138', 'type': 'if_statement', 'children': ['139', '142']}, {'id': '139', 'type': 'comparison_operator', 'children': ['140', '141'], 'value': 'not'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'idxs'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'seen'}; {'id': '142', 'type': 'block', 'children': ['143', '150', '168']}, {'id': '143', 'type': 'expression_statement', 'children': ['144']}; {'id': '144', 'type': 'call', 'children': ['145', '148']}, {'id': '145', 'type': 'attribute', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'seen'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '148', 'type': 'argument_list', 'children': ['149']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'idxs'}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'assignment', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'prod'}, {'id': '153', 'type': 'call', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '155', 'type': 'generator_expression', 'children': ['156', '159']}; {'id': '156', 'type': 'subscript', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'g'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '159', 'type': 'for_in_clause', 'children': ['160', '163']}; {'id': '160', 'type': 'pattern_list', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'g'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '163', 'type': 'call', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '165', 'type': 'argument_list', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'idxs'}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'yield', 'children': ['170']} | def random_product(items, num=None, rng=None):
import utool as ut
rng = ut.ensure_rng(rng, 'python')
seen = set()
items = [list(g) for g in items]
max_num = ut.prod(map(len, items))
if num is None:
num = max_num
if num > max_num:
raise ValueError('num exceedes maximum number of products')
if num > max_num // 2:
for prod in ut.shuffle(list(it.product(*items)), rng=rng):
yield prod
else:
while len(seen) < num:
idxs = tuple(rng.randint(0, len(g) - 1) for g in items)
if idxs not in seen:
seen.add(idxs)
prod = tuple(g[x] for g, x in zip(items, idxs))
yield prod |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'parse_dsn'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dsn_string'}, {'id': '5', 'type': 'block', 'children': ['6', '13', '26', '36', '42', '88', '111', '128', '150', '161']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'dsn'}, {'id': '9', 'type': 'call', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'urlparse'}, {'id': '11', 'type': 'argument_list', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'dsn_string'}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'assignment', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'scheme'}; {'id': '16', 'type': 'subscript', 'children': ['17', '25']}, {'id': '17', 'type': 'call', 'children': ['18', '23']}; {'id': '18', 'type': 'attribute', 'children': ['19', '22']}, {'id': '19', 'type': 'attribute', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'dsn'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'scheme'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'string', 'children': [], 'value': "'+'"}, {'id': '25', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'assignment', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'username'}, {'id': '29', 'type': 'assignment', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'password'}, {'id': '31', 'type': 'assignment', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'port'}, {'id': '35', 'type': 'None', 'children': []}; {'id': '36', 'type': 'expression_statement', 'children': ['37']}, {'id': '37', 'type': 'assignment', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '39', 'type': 'attribute', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'dsn'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'netloc'}; {'id': '42', 'type': 'if_statement', 'children': ['43', '46']}, {'id': '43', 'type': 'comparison_operator', 'children': ['44', '45'], 'value': 'in'}; {'id': '44', 'type': 'string', 'children': [], 'value': "'@'"}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'host'}; {'id': '46', 'type': 'block', 'children': ['47', '58', '81']}, {'id': '47', 'type': 'expression_statement', 'children': ['48']}; {'id': '48', 'type': 'assignment', 'children': ['49', '52']}, {'id': '49', 'type': 'pattern_list', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'username'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'host'}; {'id': '52', 'type': 'call', 'children': ['53', '56']}, {'id': '53', 'type': 'attribute', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '56', 'type': 'argument_list', 'children': ['57']}, {'id': '57', 'type': 'string', 'children': [], 'value': "'@'"}; {'id': '58', 'type': 'if_statement', 'children': ['59', '62']}, {'id': '59', 'type': 'comparison_operator', 'children': ['60', '61'], 'value': 'in'}; {'id': '60', 'type': 'string', 'children': [], 'value': "':'"}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'username'}; {'id': '62', 'type': 'block', 'children': ['63', '74']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '68']}, {'id': '65', 'type': 'pattern_list', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'username'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'password'}; {'id': '68', 'type': 'call', 'children': ['69', '72']}, {'id': '69', 'type': 'attribute', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'username'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '72', 'type': 'argument_list', 'children': ['73']}, {'id': '73', 'type': 'string', 'children': [], 'value': "':'"}; {'id': '74', 'type': 'expression_statement', 'children': ['75']}, {'id': '75', 'type': 'assignment', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'password'}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'unquote'}, {'id': '79', 'type': 'argument_list', 'children': ['80']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'password'}, {'id': '81', 'type': 'expression_statement', 'children': ['82']}; {'id': '82', 'type': 'assignment', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'username'}; {'id': '84', 'type': 'call', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'unquote'}; {'id': '86', 'type': 'argument_list', 'children': ['87']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'username'}; {'id': '88', 'type': 'if_statement', 'children': ['89', '92']}, {'id': '89', 'type': 'comparison_operator', 'children': ['90', '91'], 'value': 'in'}; {'id': '90', 'type': 'string', 'children': [], 'value': "':'"}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'host'}; {'id': '92', 'type': 'block', 'children': ['93', '104']}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '98']}, {'id': '95', 'type': 'pattern_list', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'port'}; {'id': '98', 'type': 'call', 'children': ['99', '102']}, {'id': '99', 'type': 'attribute', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'string', 'children': [], 'value': "':'"}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'assignment', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'port'}, {'id': '107', 'type': 'call', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'int'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'port'}, {'id': '111', 'type': 'expression_statement', 'children': ['112']}; {'id': '112', 'type': 'assignment', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'database'}; {'id': '114', 'type': 'subscript', 'children': ['115', '125']}, {'id': '115', 'type': 'subscript', 'children': ['116', '124']}; {'id': '116', 'type': 'call', 'children': ['117', '122']}, {'id': '117', 'type': 'attribute', 'children': ['118', '121']}; {'id': '118', 'type': 'attribute', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'dsn'}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'string', 'children': [], 'value': "'?'"}; {'id': '124', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '125', 'type': 'slice', 'children': ['126', '127']}; {'id': '126', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '127', 'type': 'colon', 'children': []}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'assignment', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '131', 'type': 'conditional_expression', 'children': ['132', '142', '147'], 'value': 'if'}; {'id': '132', 'type': 'subscript', 'children': ['133', '141']}, {'id': '133', 'type': 'call', 'children': ['134', '139']}; {'id': '134', 'type': 'attribute', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'dsn'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '139', 'type': 'argument_list', 'children': ['140']}; {'id': '140', 'type': 'string', 'children': [], 'value': "'?'"}, {'id': '141', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '142', 'type': 'comparison_operator', 'children': ['143', '144'], 'value': 'in'}, {'id': '143', 'type': 'string', 'children': [], 'value': "'?'"}; {'id': '144', 'type': 'attribute', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'dsn'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '147', 'type': 'attribute', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'dsn'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'assignment', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '153', 'type': 'call', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '155', 'type': 'argument_list', 'children': ['156']}; {'id': '156', 'type': 'call', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'parse_qsl'}; {'id': '158', 'type': 'argument_list', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '160', 'type': 'True', 'children': []}, {'id': '161', 'type': 'if_statement', 'children': ['162', '165', '174', '225', '292']}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '164'], 'value': '=='}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'scheme'}; {'id': '164', 'type': 'string', 'children': [], 'value': "'sqlite'"}, {'id': '165', 'type': 'block', 'children': ['166']}; {'id': '166', 'type': 'return_statement', 'children': ['167']}, {'id': '167', 'type': 'expression_list', 'children': ['168', '169', '173']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'SQLiteDriver'}, {'id': '169', 'type': 'list', 'children': ['170'], 'value': '[dsn.path]'}; {'id': '170', 'type': 'attribute', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'dsn'}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '173', 'type': 'dictionary', 'children': []}; {'id': '174', 'type': 'elif_clause', 'children': ['175', '178']}, {'id': '175', 'type': 'comparison_operator', 'children': ['176', '177'], 'value': '=='}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'scheme'}, {'id': '177', 'type': 'string', 'children': [], 'value': "'mysql'"}; {'id': '178', 'type': 'block', 'children': ['179', '187', '193', '202', '211', '220']}, {'id': '179', 'type': 'expression_statement', 'children': ['180']}; {'id': '180', 'type': 'assignment', 'children': ['181', '184']}, {'id': '181', 'type': 'subscript', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '183', 'type': 'string', 'children': [], 'value': "'user'"}; {'id': '184', 'type': 'boolean_operator', 'children': ['185', '186'], 'value': 'or'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'username'}; {'id': '186', 'type': 'string', 'children': [], 'value': "'root'"}, {'id': '187', 'type': 'expression_statement', 'children': ['188']}; {'id': '188', 'type': 'assignment', 'children': ['189', '192']}, {'id': '189', 'type': 'subscript', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '191', 'type': 'string', 'children': [], 'value': "'db'"}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'database'}, {'id': '193', 'type': 'if_statement', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'port'}, {'id': '195', 'type': 'block', 'children': ['196']}; {'id': '196', 'type': 'expression_statement', 'children': ['197']}, {'id': '197', 'type': 'assignment', 'children': ['198', '201']}; {'id': '198', 'type': 'subscript', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '200', 'type': 'string', 'children': [], 'value': "'port'"}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'port'}; {'id': '202', 'type': 'if_statement', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'host'}; {'id': '204', 'type': 'block', 'children': ['205']}, {'id': '205', 'type': 'expression_statement', 'children': ['206']}; {'id': '206', 'type': 'assignment', 'children': ['207', '210']}, {'id': '207', 'type': 'subscript', 'children': ['208', '209']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '209', 'type': 'string', 'children': [], 'value': "'host'"}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '211', 'type': 'if_statement', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'password'}, {'id': '213', 'type': 'block', 'children': ['214']}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'assignment', 'children': ['216', '219']}; {'id': '216', 'type': 'subscript', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '218', 'type': 'string', 'children': [], 'value': "'passwd'"}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'password'}; {'id': '220', 'type': 'return_statement', 'children': ['221']}, {'id': '221', 'type': 'expression_list', 'children': ['222', '223', '224']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'MySQLDriver'}, {'id': '223', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '225', 'type': 'elif_clause', 'children': ['226', '229']}; {'id': '226', 'type': 'comparison_operator', 'children': ['227', '228'], 'value': '=='}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'scheme'}; {'id': '228', 'type': 'string', 'children': [], 'value': "'postgresql'"}, {'id': '229', 'type': 'block', 'children': ['230', '238', '244', '253', '278', '287']}; {'id': '230', 'type': 'expression_statement', 'children': ['231']}, {'id': '231', 'type': 'assignment', 'children': ['232', '235']}; {'id': '232', 'type': 'subscript', 'children': ['233', '234']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '234', 'type': 'string', 'children': [], 'value': "'user'"}, {'id': '235', 'type': 'boolean_operator', 'children': ['236', '237'], 'value': 'or'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'username'}, {'id': '237', 'type': 'string', 'children': [], 'value': "'postgres'"}; {'id': '238', 'type': 'expression_statement', 'children': ['239']}, {'id': '239', 'type': 'assignment', 'children': ['240', '243']}; {'id': '240', 'type': 'subscript', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '242', 'type': 'string', 'children': [], 'value': "'database'"}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'database'}; {'id': '244', 'type': 'if_statement', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'port'}; {'id': '246', 'type': 'block', 'children': ['247']}, {'id': '247', 'type': 'expression_statement', 'children': ['248']}; {'id': '248', 'type': 'assignment', 'children': ['249', '252']}, {'id': '249', 'type': 'subscript', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '251', 'type': 'string', 'children': [], 'value': "'port'"}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'port'}, {'id': '253', 'type': 'if_statement', 'children': ['254', '257', '269']}; {'id': '254', 'type': 'comparison_operator', 'children': ['255', '256'], 'value': 'in'}, {'id': '255', 'type': 'string', 'children': [], 'value': "'unix_socket'"}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '257', 'type': 'block', 'children': ['258']}; {'id': '258', 'type': 'expression_statement', 'children': ['259']}, {'id': '259', 'type': 'assignment', 'children': ['260', '263']}; {'id': '260', 'type': 'subscript', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '262', 'type': 'string', 'children': [], 'value': "'host'"}, {'id': '263', 'type': 'call', 'children': ['264', '267']}; {'id': '264', 'type': 'attribute', 'children': ['265', '266']}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '267', 'type': 'argument_list', 'children': ['268']}; {'id': '268', 'type': 'string', 'children': [], 'value': "'unix_socket'"}, {'id': '269', 'type': 'elif_clause', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'host'}, {'id': '271', 'type': 'block', 'children': ['272']}; {'id': '272', 'type': 'expression_statement', 'children': ['273']}, {'id': '273', 'type': 'assignment', 'children': ['274', '277']}; {'id': '274', 'type': 'subscript', 'children': ['275', '276']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '276', 'type': 'string', 'children': [], 'value': "'host'"}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'host'}; {'id': '278', 'type': 'if_statement', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'password'}; {'id': '280', 'type': 'block', 'children': ['281']}, {'id': '281', 'type': 'expression_statement', 'children': ['282']}; {'id': '282', 'type': 'assignment', 'children': ['283', '286']}, {'id': '283', 'type': 'subscript', 'children': ['284', '285']}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '285', 'type': 'string', 'children': [], 'value': "'password'"}; {'id': '286', 'type': 'identifier', 'children': [], 'value': 'password'}, {'id': '287', 'type': 'return_statement', 'children': ['288']}; {'id': '288', 'type': 'expression_list', 'children': ['289', '290', '291']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'PostgreSQLDriver'}; {'id': '290', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '292', 'type': 'else_clause', 'children': ['293']}, {'id': '293', 'type': 'block', 'children': ['294']}; {'id': '294', 'type': 'raise_statement', 'children': ['295']}, {'id': '295', 'type': 'call', 'children': ['296', '297']}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '297', 'type': 'argument_list', 'children': ['298']}; {'id': '298', 'type': 'binary_operator', 'children': ['299', '300'], 'value': '%'}, {'id': '299', 'type': 'string', 'children': [], 'value': "'Unknown driver %s'"} | def parse_dsn(dsn_string):
dsn = urlparse(dsn_string)
scheme = dsn.scheme.split('+')[0]
username = password = host = port = None
host = dsn.netloc
if '@' in host:
username, host = host.split('@')
if ':' in username:
username, password = username.split(':')
password = unquote(password)
username = unquote(username)
if ':' in host:
host, port = host.split(':')
port = int(port)
database = dsn.path.split('?')[0][1:]
query = dsn.path.split('?')[1] if '?' in dsn.path else dsn.query
kwargs = dict(parse_qsl(query, True))
if scheme == 'sqlite':
return SQLiteDriver, [dsn.path], {}
elif scheme == 'mysql':
kwargs['user'] = username or 'root'
kwargs['db'] = database
if port:
kwargs['port'] = port
if host:
kwargs['host'] = host
if password:
kwargs['passwd'] = password
return MySQLDriver, [], kwargs
elif scheme == 'postgresql':
kwargs['user'] = username or 'postgres'
kwargs['database'] = database
if port:
kwargs['port'] = port
if 'unix_socket' in kwargs:
kwargs['host'] = kwargs.pop('unix_socket')
elif host:
kwargs['host'] = host
if password:
kwargs['password'] = password
return PostgreSQLDriver, [], kwargs
else:
raise ValueError('Unknown driver %s' % dsn_string) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '31']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'numpy_str'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '14', '17', '20', '23', '26', '29']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'arr'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'strvals'}, {'id': '7', 'type': 'False', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'precision'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'pr'}, {'id': '13', 'type': 'None', 'children': []}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'force_dtype'}; {'id': '16', 'type': 'False', 'children': []}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'with_dtype'}, {'id': '19', 'type': 'None', 'children': []}; {'id': '20', 'type': 'default_parameter', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'suppress_small'}; {'id': '22', 'type': 'None', 'children': []}, {'id': '23', 'type': 'default_parameter', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'max_line_width'}, {'id': '25', 'type': 'None', 'children': []}; {'id': '26', 'type': 'default_parameter', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'threshold'}; {'id': '28', 'type': 'None', 'children': []}, {'id': '29', 'type': 'dictionary_splat_pattern', 'children': ['30']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '31', 'type': 'block', 'children': ['32', '42', '58', '62', '68', '172', '237', '252', '260']}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'itemsep'}, {'id': '35', 'type': 'call', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '39', 'type': 'argument_list', 'children': ['40', '41']}; {'id': '40', 'type': 'string', 'children': [], 'value': "'itemsep'"}, {'id': '41', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'newlines'}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '49', 'type': 'argument_list', 'children': ['50', '51']}; {'id': '50', 'type': 'string', 'children': [], 'value': "'nl'"}, {'id': '51', 'type': 'call', 'children': ['52', '55']}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '55', 'type': 'argument_list', 'children': ['56', '57']}; {'id': '56', 'type': 'string', 'children': [], 'value': "'newlines'"}, {'id': '57', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '58', 'type': 'expression_statement', 'children': ['59']}, {'id': '59', 'type': 'assignment', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'arr'}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '65', 'type': 'binary_operator', 'children': ['66', '67'], 'value': '+'}; {'id': '66', 'type': 'string', 'children': [], 'value': "','"}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'itemsep'}; {'id': '68', 'type': 'if_statement', 'children': ['69', '70', '79']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'strvals'}; {'id': '70', 'type': 'block', 'children': ['71', '75']}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '74', 'type': 'string', 'children': [], 'value': "''"}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'suffix'}; {'id': '78', 'type': 'string', 'children': [], 'value': "''"}, {'id': '79', 'type': 'else_clause', 'children': ['80']}; {'id': '80', 'type': 'block', 'children': ['81', '90', '94', '105', '116', '125', '134', '144']}, {'id': '81', 'type': 'expression_statement', 'children': ['82']}; {'id': '82', 'type': 'assignment', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '84', 'type': 'attribute', 'children': ['85', '89']}, {'id': '85', 'type': 'call', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'type'}, {'id': '87', 'type': 'argument_list', 'children': ['88']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': '__module__'}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'np_nice'}, {'id': '93', 'type': 'string', 'children': [], 'value': "'np'"}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'modname'}, {'id': '97', 'type': 'call', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 're'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'sub'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '103', '104']}; {'id': '102', 'type': 'string', 'children': [], 'value': "'\\\\bnumpy\\\\b'"}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'np_nice'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'modname'}, {'id': '105', 'type': 'expression_statement', 'children': ['106']}; {'id': '106', 'type': 'assignment', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '108', 'type': 'call', 'children': ['109', '112']}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'sub'}; {'id': '112', 'type': 'argument_list', 'children': ['113', '114', '115']}, {'id': '113', 'type': 'string', 'children': [], 'value': "'\\\\bma.core\\\\b'"}; {'id': '114', 'type': 'string', 'children': [], 'value': "'ma'"}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '116', 'type': 'expression_statement', 'children': ['117']}, {'id': '117', 'type': 'assignment', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'class_name'}, {'id': '119', 'type': 'attribute', 'children': ['120', '124']}; {'id': '120', 'type': 'call', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'type'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': '__name__'}, {'id': '125', 'type': 'if_statement', 'children': ['126', '129']}; {'id': '126', 'type': 'comparison_operator', 'children': ['127', '128'], 'value': '=='}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'class_name'}; {'id': '128', 'type': 'string', 'children': [], 'value': "'ndarray'"}, {'id': '129', 'type': 'block', 'children': ['130']}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'assignment', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'class_name'}, {'id': '133', 'type': 'string', 'children': [], 'value': "'array'"}; {'id': '134', 'type': 'expression_statement', 'children': ['135']}, {'id': '135', 'type': 'assignment', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'prefix'}, {'id': '137', 'type': 'binary_operator', 'children': ['138', '143'], 'value': '+'}; {'id': '138', 'type': 'binary_operator', 'children': ['139', '142'], 'value': '+'}, {'id': '139', 'type': 'binary_operator', 'children': ['140', '141'], 'value': '+'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'modname'}, {'id': '141', 'type': 'string', 'children': [], 'value': "'.'"}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'class_name'}, {'id': '143', 'type': 'string', 'children': [], 'value': "'('"}; {'id': '144', 'type': 'if_statement', 'children': ['145', '146', '166']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'with_dtype'}; {'id': '146', 'type': 'block', 'children': ['147', '155']}, {'id': '147', 'type': 'expression_statement', 'children': ['148']}; {'id': '148', 'type': 'assignment', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'dtype_repr'}; {'id': '150', 'type': 'attribute', 'children': ['151', '154']}, {'id': '151', 'type': 'attribute', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'dtype'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'assignment', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'suffix'}; {'id': '158', 'type': 'call', 'children': ['159', '162']}, {'id': '159', 'type': 'attribute', 'children': ['160', '161']}; {'id': '160', 'type': 'string', 'children': [], 'value': "',{}dtype={}.{})'"}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '162', 'type': 'argument_list', 'children': ['163', '164', '165']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'itemsep'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'np_nice'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'dtype_repr'}; {'id': '166', 'type': 'else_clause', 'children': ['167']}, {'id': '167', 'type': 'block', 'children': ['168']}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'suffix'}, {'id': '171', 'type': 'string', 'children': [], 'value': "')'"}; {'id': '172', 'type': 'if_statement', 'children': ['173', '188', '211']}, {'id': '173', 'type': 'boolean_operator', 'children': ['174', '182'], 'value': 'and'}; {'id': '174', 'type': 'boolean_operator', 'children': ['175', '177'], 'value': 'and'}, {'id': '175', 'type': 'not_operator', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'strvals'}, {'id': '177', 'type': 'comparison_operator', 'children': ['178', '181'], 'value': '=='}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'size'}, {'id': '181', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '182', 'type': 'comparison_operator', 'children': ['183', '186'], 'value': '!='}, {'id': '183', 'type': 'attribute', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'shape'}; {'id': '186', 'type': 'tuple', 'children': ['187']}, {'id': '187', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '188', 'type': 'block', 'children': ['189', '195']}, {'id': '189', 'type': 'expression_statement', 'children': ['190']}; {'id': '190', 'type': 'assignment', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '192', 'type': 'binary_operator', 'children': ['193', '194'], 'value': '+'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '194', 'type': 'string', 'children': [], 'value': "'.empty('"}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'assignment', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'body'}; {'id': '198', 'type': 'call', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'repr'}; {'id': '200', 'type': 'argument_list', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '203', 'type': 'argument_list', 'children': ['204']}; {'id': '204', 'type': 'call', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'map'}; {'id': '206', 'type': 'argument_list', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '208', 'type': 'attribute', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'shape'}, {'id': '211', 'type': 'else_clause', 'children': ['212']}; {'id': '212', 'type': 'block', 'children': ['213']}, {'id': '213', 'type': 'expression_statement', 'children': ['214']}; {'id': '214', 'type': 'assignment', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'body'}; {'id': '216', 'type': 'call', 'children': ['217', '220']}, {'id': '217', 'type': 'attribute', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'array2string'}; {'id': '220', 'type': 'argument_list', 'children': ['221', '222', '225', '228', '231', '234']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '222', 'type': 'keyword_argument', 'children': ['223', '224']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'precision'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'precision'}, {'id': '225', 'type': 'keyword_argument', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'separator'}; {'id': '228', 'type': 'keyword_argument', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'suppress_small'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'suppress_small'}, {'id': '231', 'type': 'keyword_argument', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'prefix'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '234', 'type': 'keyword_argument', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'max_line_width'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'max_line_width'}, {'id': '237', 'type': 'if_statement', 'children': ['238', '240']}; {'id': '238', 'type': 'not_operator', 'children': ['239']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'newlines'}; {'id': '240', 'type': 'block', 'children': ['241']}, {'id': '241', 'type': 'expression_statement', 'children': ['242']}; {'id': '242', 'type': 'assignment', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'body'}; {'id': '244', 'type': 'call', 'children': ['245', '248']}, {'id': '245', 'type': 'attribute', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'sub'}; {'id': '248', 'type': 'argument_list', 'children': ['249', '250', '251']}, {'id': '249', 'type': 'string', 'children': [], 'value': "'\\n *'"}; {'id': '250', 'type': 'string', 'children': [], 'value': "''"}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'body'}; {'id': '252', 'type': 'expression_statement', 'children': ['253']}, {'id': '253', 'type': 'assignment', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'formatted'}, {'id': '255', 'type': 'binary_operator', 'children': ['256', '259'], 'value': '+'}; {'id': '256', 'type': 'binary_operator', 'children': ['257', '258'], 'value': '+'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'body'}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'suffix'}; {'id': '260', 'type': 'return_statement', 'children': ['261']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'formatted'} | def numpy_str(arr, strvals=False, precision=None, pr=None,
force_dtype=False,
with_dtype=None, suppress_small=None, max_line_width=None,
threshold=None, **kwargs):
itemsep = kwargs.get('itemsep', ' ')
newlines = kwargs.pop('nl', kwargs.pop('newlines', 1))
data = arr
separator = ',' + itemsep
if strvals:
prefix = ''
suffix = ''
else:
modname = type(data).__module__
np_nice = 'np'
modname = re.sub('\\bnumpy\\b', np_nice, modname)
modname = re.sub('\\bma.core\\b', 'ma', modname)
class_name = type(data).__name__
if class_name == 'ndarray':
class_name = 'array'
prefix = modname + '.' + class_name + '('
if with_dtype:
dtype_repr = data.dtype.name
suffix = ',{}dtype={}.{})'.format(itemsep, np_nice, dtype_repr)
else:
suffix = ')'
if not strvals and data.size == 0 and data.shape != (0,):
prefix = modname + '.empty('
body = repr(tuple(map(int, data.shape)))
else:
body = np.array2string(data, precision=precision,
separator=separator,
suppress_small=suppress_small,
prefix=prefix,
max_line_width=max_line_width)
if not newlines:
body = re.sub('\n *', '', body)
formatted = prefix + body + suffix
return formatted |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '7']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'list_str'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '5', 'type': 'dictionary_splat_pattern', 'children': ['6']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '7', 'type': 'block', 'children': ['8', '10', '15', '31', '41', '51', '60', '69', '78', '94', '104', '114', '118', '127', '135', '148', '164', '207', '219', '392', '407', '429']}; {'id': '8', 'type': 'expression_statement', 'children': ['9']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '10', 'type': 'import_statement', 'children': ['11']}, {'id': '11', 'type': 'aliased_import', 'children': ['12', '14']}; {'id': '12', 'type': 'dotted_name', 'children': ['13']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'newlines'}; {'id': '18', 'type': 'call', 'children': ['19', '22']}, {'id': '19', 'type': 'attribute', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '22', 'type': 'argument_list', 'children': ['23', '24']}, {'id': '23', 'type': 'string', 'children': [], 'value': "'nl'"}; {'id': '24', 'type': 'call', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '28', 'type': 'argument_list', 'children': ['29', '30']}, {'id': '29', 'type': 'string', 'children': [], 'value': "'newlines'"}; {'id': '30', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'packed'}; {'id': '34', 'type': 'call', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '38', 'type': 'argument_list', 'children': ['39', '40']}, {'id': '39', 'type': 'string', 'children': [], 'value': "'packed'"}; {'id': '40', 'type': 'False', 'children': []}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'truncate'}; {'id': '44', 'type': 'call', 'children': ['45', '48']}, {'id': '45', 'type': 'attribute', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '48', 'type': 'argument_list', 'children': ['49', '50']}, {'id': '49', 'type': 'string', 'children': [], 'value': "'truncate'"}; {'id': '50', 'type': 'False', 'children': []}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '56']}, {'id': '53', 'type': 'subscript', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '55', 'type': 'string', 'children': [], 'value': "'nl'"}; {'id': '56', 'type': 'call', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': '_rectify_countdown_or_bool'}; {'id': '58', 'type': 'argument_list', 'children': ['59']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'newlines'}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '65']}; {'id': '62', 'type': 'subscript', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '64', 'type': 'string', 'children': [], 'value': "'truncate'"}, {'id': '65', 'type': 'call', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': '_rectify_countdown_or_bool'}, {'id': '67', 'type': 'argument_list', 'children': ['68']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'truncate'}, {'id': '69', 'type': 'expression_statement', 'children': ['70']}; {'id': '70', 'type': 'assignment', 'children': ['71', '74']}, {'id': '71', 'type': 'subscript', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '73', 'type': 'string', 'children': [], 'value': "'packed'"}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': '_rectify_countdown_or_bool'}; {'id': '76', 'type': 'argument_list', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'packed'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'nobraces'}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '85', 'type': 'argument_list', 'children': ['86', '87']}; {'id': '86', 'type': 'string', 'children': [], 'value': "'nobr'"}, {'id': '87', 'type': 'call', 'children': ['88', '91']}; {'id': '88', 'type': 'attribute', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '91', 'type': 'argument_list', 'children': ['92', '93']}; {'id': '92', 'type': 'string', 'children': [], 'value': "'nobraces'"}, {'id': '93', 'type': 'False', 'children': []}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'itemsep'}, {'id': '97', 'type': 'call', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '103']}; {'id': '102', 'type': 'string', 'children': [], 'value': "'itemsep'"}, {'id': '103', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'assignment', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'trailing_sep'}, {'id': '107', 'type': 'call', 'children': ['108', '111']}; {'id': '108', 'type': 'attribute', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '111', 'type': 'argument_list', 'children': ['112', '113']}; {'id': '112', 'type': 'string', 'children': [], 'value': "'trailing_sep'"}, {'id': '113', 'type': 'True', 'children': []}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'assignment', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'with_comma'}, {'id': '117', 'type': 'True', 'children': []}; {'id': '118', 'type': 'expression_statement', 'children': ['119']}, {'id': '119', 'type': 'assignment', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'get_itemstr_list'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '125', 'type': 'dictionary_splat', 'children': ['126']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'listkw'}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'assignment', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'is_tuple'}; {'id': '130', 'type': 'call', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '132', 'type': 'argument_list', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'list_'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '135', 'type': 'expression_statement', 'children': ['136']}; {'id': '136', 'type': 'assignment', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'is_set'}; {'id': '138', 'type': 'call', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '140', 'type': 'argument_list', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'list_'}; {'id': '142', 'type': 'tuple', 'children': ['143', '144', '145']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'frozenset'}, {'id': '145', 'type': 'attribute', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'oset'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'is_onetup'}, {'id': '151', 'type': 'boolean_operator', 'children': ['152', '158'], 'value': 'and'}; {'id': '152', 'type': 'call', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '154', 'type': 'argument_list', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'list_'}; {'id': '156', 'type': 'parenthesized_expression', 'children': ['157']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '158', 'type': 'comparison_operator', 'children': ['159', '163'], 'value': '<='}, {'id': '159', 'type': 'call', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '161', 'type': 'argument_list', 'children': ['162']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'list_'}, {'id': '163', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '164', 'type': 'if_statement', 'children': ['165', '166', '175', '186', '197']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'nobraces'}; {'id': '166', 'type': 'block', 'children': ['167']}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'assignment', 'children': ['169', '172']}, {'id': '169', 'type': 'pattern_list', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'lbr'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '172', 'type': 'expression_list', 'children': ['173', '174']}, {'id': '173', 'type': 'string', 'children': [], 'value': "''"}; {'id': '174', 'type': 'string', 'children': [], 'value': "''"}, {'id': '175', 'type': 'elif_clause', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'is_tuple'}, {'id': '177', 'type': 'block', 'children': ['178']}; {'id': '178', 'type': 'expression_statement', 'children': ['179']}, {'id': '179', 'type': 'assignment', 'children': ['180', '183']}; {'id': '180', 'type': 'pattern_list', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'lbr'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'rbr'}, {'id': '183', 'type': 'expression_list', 'children': ['184', '185']}; {'id': '184', 'type': 'string', 'children': [], 'value': "'('"}, {'id': '185', 'type': 'string', 'children': [], 'value': "')'"}; {'id': '186', 'type': 'elif_clause', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'is_set'}; {'id': '188', 'type': 'block', 'children': ['189']}, {'id': '189', 'type': 'expression_statement', 'children': ['190']}; {'id': '190', 'type': 'assignment', 'children': ['191', '194']}, {'id': '191', 'type': 'pattern_list', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'lbr'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '194', 'type': 'expression_list', 'children': ['195', '196']}, {'id': '195', 'type': 'string', 'children': [], 'value': "'{'"}; {'id': '196', 'type': 'string', 'children': [], 'value': "'}'"}, {'id': '197', 'type': 'else_clause', 'children': ['198']}; {'id': '198', 'type': 'block', 'children': ['199']}, {'id': '199', 'type': 'expression_statement', 'children': ['200']}; {'id': '200', 'type': 'assignment', 'children': ['201', '204']}, {'id': '201', 'type': 'pattern_list', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'lbr'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '204', 'type': 'expression_list', 'children': ['205', '206']}, {'id': '205', 'type': 'string', 'children': [], 'value': "'['"}; {'id': '206', 'type': 'string', 'children': [], 'value': "']'"}, {'id': '207', 'type': 'if_statement', 'children': ['208', '214']}; {'id': '208', 'type': 'comparison_operator', 'children': ['209', '213'], 'value': '=='}, {'id': '209', 'type': 'call', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}, {'id': '213', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '214', 'type': 'block', 'children': ['215']}, {'id': '215', 'type': 'expression_statement', 'children': ['216']}; {'id': '216', 'type': 'assignment', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'newlines'}; {'id': '218', 'type': 'False', 'children': []}, {'id': '219', 'type': 'if_statement', 'children': ['220', '232', '356']}; {'id': '220', 'type': 'boolean_operator', 'children': ['221', '224'], 'value': 'and'}, {'id': '221', 'type': 'comparison_operator', 'children': ['222', '223'], 'value': 'is'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'newlines'}, {'id': '223', 'type': 'False', 'children': []}; {'id': '224', 'type': 'parenthesized_expression', 'children': ['225']}, {'id': '225', 'type': 'boolean_operator', 'children': ['226', '229'], 'value': 'or'}; {'id': '226', 'type': 'comparison_operator', 'children': ['227', '228'], 'value': 'is'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'newlines'}; {'id': '228', 'type': 'True', 'children': []}, {'id': '229', 'type': 'comparison_operator', 'children': ['230', '231'], 'value': '>'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'newlines'}, {'id': '231', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '232', 'type': 'block', 'children': ['233', '240']}, {'id': '233', 'type': 'expression_statement', 'children': ['234']}; {'id': '234', 'type': 'assignment', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'sep'}; {'id': '236', 'type': 'conditional_expression', 'children': ['237', '238', '239'], 'value': 'if'}, {'id': '237', 'type': 'string', 'children': [], 'value': "',\\n'"}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'with_comma'}, {'id': '239', 'type': 'string', 'children': [], 'value': "'\\n'"}; {'id': '240', 'type': 'if_statement', 'children': ['241', '242', '263']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'nobraces'}; {'id': '242', 'type': 'block', 'children': ['243', '252', '259']}, {'id': '243', 'type': 'expression_statement', 'children': ['244']}; {'id': '244', 'type': 'assignment', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '246', 'type': 'call', 'children': ['247', '250']}, {'id': '247', 'type': 'attribute', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'sep'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '250', 'type': 'argument_list', 'children': ['251']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}; {'id': '252', 'type': 'if_statement', 'children': ['253', '254']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'trailing_sep'}; {'id': '254', 'type': 'block', 'children': ['255']}, {'id': '255', 'type': 'expression_statement', 'children': ['256']}; {'id': '256', 'type': 'augmented_assignment', 'children': ['257', '258'], 'value': '+='}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '258', 'type': 'string', 'children': [], 'value': "','"}, {'id': '259', 'type': 'expression_statement', 'children': ['260']}; {'id': '260', 'type': 'assignment', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'retstr'}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'body_str'}, {'id': '263', 'type': 'else_clause', 'children': ['264']}; {'id': '264', 'type': 'block', 'children': ['265', '352']}, {'id': '265', 'type': 'if_statement', 'children': ['266', '267', '312']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'packed'}, {'id': '267', 'type': 'block', 'children': ['268', '279', '292', '299']}; {'id': '268', 'type': 'expression_statement', 'children': ['269']}, {'id': '269', 'type': 'assignment', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'joinstr'}, {'id': '271', 'type': 'binary_operator', 'children': ['272', '273'], 'value': '+'}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'sep'}, {'id': '273', 'type': 'binary_operator', 'children': ['274', '275'], 'value': '*'}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'itemsep'}, {'id': '275', 'type': 'call', 'children': ['276', '277']}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '277', 'type': 'argument_list', 'children': ['278']}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'lbr'}, {'id': '279', 'type': 'expression_statement', 'children': ['280']}; {'id': '280', 'type': 'assignment', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '282', 'type': 'call', 'children': ['283', '286']}, {'id': '283', 'type': 'attribute', 'children': ['284', '285']}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'joinstr'}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '286', 'type': 'argument_list', 'children': ['287']}, {'id': '287', 'type': 'list_comprehension', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'itemstr'}, {'id': '289', 'type': 'for_in_clause', 'children': ['290', '291']}; {'id': '290', 'type': 'identifier', 'children': [], 'value': 'itemstr'}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}; {'id': '292', 'type': 'if_statement', 'children': ['293', '294']}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'trailing_sep'}; {'id': '294', 'type': 'block', 'children': ['295']}, {'id': '295', 'type': 'expression_statement', 'children': ['296']}; {'id': '296', 'type': 'augmented_assignment', 'children': ['297', '298'], 'value': '+='}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '298', 'type': 'string', 'children': [], 'value': "','"}, {'id': '299', 'type': 'expression_statement', 'children': ['300']}; {'id': '300', 'type': 'assignment', 'children': ['301', '302']}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'braced_body_str'}; {'id': '302', 'type': 'parenthesized_expression', 'children': ['303']}, {'id': '303', 'type': 'binary_operator', 'children': ['304', '311'], 'value': '+'}; {'id': '304', 'type': 'binary_operator', 'children': ['305', '310'], 'value': '+'}, {'id': '305', 'type': 'binary_operator', 'children': ['306', '309'], 'value': '+'}; {'id': '306', 'type': 'binary_operator', 'children': ['307', '308'], 'value': '+'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'lbr'}; {'id': '308', 'type': 'string', 'children': [], 'value': "''"}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '310', 'type': 'string', 'children': [], 'value': "''"}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '312', 'type': 'else_clause', 'children': ['313']}, {'id': '313', 'type': 'block', 'children': ['314', '332', '339']}; {'id': '314', 'type': 'expression_statement', 'children': ['315']}, {'id': '315', 'type': 'assignment', 'children': ['316', '317']}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'body_str'}, {'id': '317', 'type': 'call', 'children': ['318', '321']}; {'id': '318', 'type': 'attribute', 'children': ['319', '320']}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'sep'}; {'id': '320', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '321', 'type': 'argument_list', 'children': ['322']}; {'id': '322', 'type': 'list_comprehension', 'children': ['323', '329']}, {'id': '323', 'type': 'call', 'children': ['324', '327']}; {'id': '324', 'type': 'attribute', 'children': ['325', '326']}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '326', 'type': 'identifier', 'children': [], 'value': 'indent'}, {'id': '327', 'type': 'argument_list', 'children': ['328']}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'itemstr'}, {'id': '329', 'type': 'for_in_clause', 'children': ['330', '331']}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'itemstr'}, {'id': '331', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}; {'id': '332', 'type': 'if_statement', 'children': ['333', '334']}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'trailing_sep'}; {'id': '334', 'type': 'block', 'children': ['335']}, {'id': '335', 'type': 'expression_statement', 'children': ['336']}; {'id': '336', 'type': 'augmented_assignment', 'children': ['337', '338'], 'value': '+='}, {'id': '337', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '338', 'type': 'string', 'children': [], 'value': "','"}, {'id': '339', 'type': 'expression_statement', 'children': ['340']}; {'id': '340', 'type': 'assignment', 'children': ['341', '342']}, {'id': '341', 'type': 'identifier', 'children': [], 'value': 'braced_body_str'}; {'id': '342', 'type': 'parenthesized_expression', 'children': ['343']}, {'id': '343', 'type': 'binary_operator', 'children': ['344', '351'], 'value': '+'}; {'id': '344', 'type': 'binary_operator', 'children': ['345', '350'], 'value': '+'}, {'id': '345', 'type': 'binary_operator', 'children': ['346', '349'], 'value': '+'}; {'id': '346', 'type': 'binary_operator', 'children': ['347', '348'], 'value': '+'}, {'id': '347', 'type': 'identifier', 'children': [], 'value': 'lbr'}; {'id': '348', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '349', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '350', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '351', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '352', 'type': 'expression_statement', 'children': ['353']}, {'id': '353', 'type': 'assignment', 'children': ['354', '355']}; {'id': '354', 'type': 'identifier', 'children': [], 'value': 'retstr'}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'braced_body_str'}; {'id': '356', 'type': 'else_clause', 'children': ['357']}, {'id': '357', 'type': 'block', 'children': ['358', '367', '376', '383']}; {'id': '358', 'type': 'expression_statement', 'children': ['359']}, {'id': '359', 'type': 'assignment', 'children': ['360', '361']}; {'id': '360', 'type': 'identifier', 'children': [], 'value': 'sep'}, {'id': '361', 'type': 'conditional_expression', 'children': ['362', '365', '366'], 'value': 'if'}; {'id': '362', 'type': 'binary_operator', 'children': ['363', '364'], 'value': '+'}, {'id': '363', 'type': 'string', 'children': [], 'value': "','"}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'itemsep'}, {'id': '365', 'type': 'identifier', 'children': [], 'value': 'with_comma'}; {'id': '366', 'type': 'identifier', 'children': [], 'value': 'itemsep'}, {'id': '367', 'type': 'expression_statement', 'children': ['368']}; {'id': '368', 'type': 'assignment', 'children': ['369', '370']}, {'id': '369', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '370', 'type': 'call', 'children': ['371', '374']}, {'id': '371', 'type': 'attribute', 'children': ['372', '373']}; {'id': '372', 'type': 'identifier', 'children': [], 'value': 'sep'}, {'id': '373', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '374', 'type': 'argument_list', 'children': ['375']}, {'id': '375', 'type': 'identifier', 'children': [], 'value': 'itemstr_list'}; {'id': '376', 'type': 'if_statement', 'children': ['377', '378']}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'is_onetup'}; {'id': '378', 'type': 'block', 'children': ['379']}, {'id': '379', 'type': 'expression_statement', 'children': ['380']}; {'id': '380', 'type': 'augmented_assignment', 'children': ['381', '382'], 'value': '+='}, {'id': '381', 'type': 'identifier', 'children': [], 'value': 'body_str'}; {'id': '382', 'type': 'string', 'children': [], 'value': "','"}, {'id': '383', 'type': 'expression_statement', 'children': ['384']}; {'id': '384', 'type': 'assignment', 'children': ['385', '386']}, {'id': '385', 'type': 'identifier', 'children': [], 'value': 'retstr'}; {'id': '386', 'type': 'parenthesized_expression', 'children': ['387']}, {'id': '387', 'type': 'binary_operator', 'children': ['388', '391'], 'value': '+'}; {'id': '388', 'type': 'binary_operator', 'children': ['389', '390'], 'value': '+'}, {'id': '389', 'type': 'identifier', 'children': [], 'value': 'lbr'}; {'id': '390', 'type': 'identifier', 'children': [], 'value': 'body_str'}, {'id': '391', 'type': 'identifier', 'children': [], 'value': 'rbr'}; {'id': '392', 'type': 'expression_statement', 'children': ['393']}, {'id': '393', 'type': 'assignment', 'children': ['394', '395']}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'do_truncate'}, {'id': '395', 'type': 'boolean_operator', 'children': ['396', '399'], 'value': 'and'}; {'id': '396', 'type': 'comparison_operator', 'children': ['397', '398'], 'value': 'is'}, {'id': '397', 'type': 'identifier', 'children': [], 'value': 'truncate'}; {'id': '398', 'type': 'False', 'children': []}, {'id': '399', 'type': 'parenthesized_expression', 'children': ['400']}; {'id': '400', 'type': 'boolean_operator', 'children': ['401', '404'], 'value': 'or'}, {'id': '401', 'type': 'comparison_operator', 'children': ['402', '403'], 'value': 'is'}; {'id': '402', 'type': 'identifier', 'children': [], 'value': 'truncate'}, {'id': '403', 'type': 'True', 'children': []}; {'id': '404', 'type': 'comparison_operator', 'children': ['405', '406'], 'value': '=='}, {'id': '405', 'type': 'identifier', 'children': [], 'value': 'truncate'}; {'id': '406', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '407', 'type': 'if_statement', 'children': ['408', '409']}; {'id': '408', 'type': 'identifier', 'children': [], 'value': 'do_truncate'}, {'id': '409', 'type': 'block', 'children': ['410', '420']}; {'id': '410', 'type': 'expression_statement', 'children': ['411']}, {'id': '411', 'type': 'assignment', 'children': ['412', '413']}; {'id': '412', 'type': 'identifier', 'children': [], 'value': 'truncatekw'}, {'id': '413', 'type': 'call', 'children': ['414', '417']}; {'id': '414', 'type': 'attribute', 'children': ['415', '416']}, {'id': '415', 'type': 'identifier', 'children': [], 'value': 'listkw'}; {'id': '416', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '417', 'type': 'argument_list', 'children': ['418', '419']}; {'id': '418', 'type': 'string', 'children': [], 'value': "'truncatekw'"}, {'id': '419', 'type': 'dictionary', 'children': []}; {'id': '420', 'type': 'expression_statement', 'children': ['421']}, {'id': '421', 'type': 'assignment', 'children': ['422', '423']}; {'id': '422', 'type': 'identifier', 'children': [], 'value': 'retstr'}, {'id': '423', 'type': 'call', 'children': ['424', '425']}; {'id': '424', 'type': 'identifier', 'children': [], 'value': 'truncate_str'}, {'id': '425', 'type': 'argument_list', 'children': ['426', '427']}; {'id': '426', 'type': 'identifier', 'children': [], 'value': 'retstr'}, {'id': '427', 'type': 'dictionary_splat', 'children': ['428']}; {'id': '428', 'type': 'identifier', 'children': [], 'value': 'truncatekw'}, {'id': '429', 'type': 'return_statement', 'children': ['430']} | def list_str(list_, **listkw):
r
import utool as ut
newlines = listkw.pop('nl', listkw.pop('newlines', 1))
packed = listkw.pop('packed', False)
truncate = listkw.pop('truncate', False)
listkw['nl'] = _rectify_countdown_or_bool(newlines)
listkw['truncate'] = _rectify_countdown_or_bool(truncate)
listkw['packed'] = _rectify_countdown_or_bool(packed)
nobraces = listkw.pop('nobr', listkw.pop('nobraces', False))
itemsep = listkw.get('itemsep', ' ')
trailing_sep = listkw.get('trailing_sep', True)
with_comma = True
itemstr_list = get_itemstr_list(list_, **listkw)
is_tuple = isinstance(list_, tuple)
is_set = isinstance(list_, (set, frozenset, ut.oset))
is_onetup = isinstance(list_, (tuple)) and len(list_) <= 1
if nobraces:
lbr, rbr = '', ''
elif is_tuple:
lbr, rbr = '(', ')'
elif is_set:
lbr, rbr = '{', '}'
else:
lbr, rbr = '[', ']'
if len(itemstr_list) == 0:
newlines = False
if newlines is not False and (newlines is True or newlines > 0):
sep = ',\n' if with_comma else '\n'
if nobraces:
body_str = sep.join(itemstr_list)
if trailing_sep:
body_str += ','
retstr = body_str
else:
if packed:
joinstr = sep + itemsep * len(lbr)
body_str = joinstr.join([itemstr for itemstr in itemstr_list])
if trailing_sep:
body_str += ','
braced_body_str = (lbr + '' + body_str + '' + rbr)
else:
body_str = sep.join([
ut.indent(itemstr) for itemstr in itemstr_list])
if trailing_sep:
body_str += ','
braced_body_str = (lbr + '\n' + body_str + '\n' + rbr)
retstr = braced_body_str
else:
sep = ',' + itemsep if with_comma else itemsep
body_str = sep.join(itemstr_list)
if is_onetup:
body_str += ','
retstr = (lbr + body_str + rbr)
do_truncate = truncate is not False and (truncate is True or truncate == 0)
if do_truncate:
truncatekw = listkw.get('truncatekw', {})
retstr = truncate_str(retstr, **truncatekw)
return retstr |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'horiz_string'}, {'id': '3', 'type': 'parameters', 'children': ['4', '6']}; {'id': '4', 'type': 'list_splat_pattern', 'children': ['5']}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '6', 'type': 'dictionary_splat_pattern', 'children': ['7']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '8', 'type': 'block', 'children': ['9', '12', '22', '32', '63', '80', '84', '88', '258', '271', '280']}, {'id': '9', 'type': 'import_statement', 'children': ['10']}; {'id': '10', 'type': 'dotted_name', 'children': ['11']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'unicodedata'}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'precision'}, {'id': '15', 'type': 'call', 'children': ['16', '19']}; {'id': '16', 'type': 'attribute', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '19', 'type': 'argument_list', 'children': ['20', '21']}; {'id': '20', 'type': 'string', 'children': [], 'value': "'precision'"}, {'id': '21', 'type': 'None', 'children': []}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'sep'}, {'id': '25', 'type': 'call', 'children': ['26', '29']}; {'id': '26', 'type': 'attribute', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '29', 'type': 'argument_list', 'children': ['30', '31']}; {'id': '30', 'type': 'string', 'children': [], 'value': "'sep'"}, {'id': '31', 'type': 'string', 'children': [], 'value': "''"}; {'id': '32', 'type': 'if_statement', 'children': ['33', '50', '57']}, {'id': '33', 'type': 'boolean_operator', 'children': ['34', '40'], 'value': 'and'}; {'id': '34', 'type': 'comparison_operator', 'children': ['35', '39'], 'value': '=='}, {'id': '35', 'type': 'call', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '39', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '40', 'type': 'not_operator', 'children': ['41']}, {'id': '41', 'type': 'call', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '43', 'type': 'argument_list', 'children': ['44', '47']}; {'id': '44', 'type': 'subscript', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '46', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '47', 'type': 'attribute', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'string_types'}; {'id': '50', 'type': 'block', 'children': ['51']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '54', 'type': 'subscript', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '56', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '57', 'type': 'else_clause', 'children': ['58']}; {'id': '58', 'type': 'block', 'children': ['59']}, {'id': '59', 'type': 'expression_statement', 'children': ['60']}; {'id': '60', 'type': 'assignment', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '66', 'type': 'list_comprehension', 'children': ['67', '77']}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'unicodedata'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'normalize'}, {'id': '71', 'type': 'argument_list', 'children': ['72', '73']}; {'id': '72', 'type': 'string', 'children': [], 'value': "'NFC'"}, {'id': '73', 'type': 'call', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'ensure_unicode'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '77', 'type': 'for_in_clause', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'val_list'}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '83', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'hpos'}, {'id': '87', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '88', 'type': 'for_statement', 'children': ['89', '90', '97']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'sx'}; {'id': '90', 'type': 'call', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'call', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '97', 'type': 'block', 'children': ['98', '104', '108', '148', '164', '173', '185', '199', '227']}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'assignment', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '101', 'type': 'subscript', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'sx'}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'assignment', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'str_'}, {'id': '107', 'type': 'None', 'children': []}; {'id': '108', 'type': 'if_statement', 'children': ['109', '112']}, {'id': '109', 'type': 'comparison_operator', 'children': ['110', '111'], 'value': 'is'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'precision'}, {'id': '111', 'type': 'None', 'children': []}; {'id': '112', 'type': 'block', 'children': ['113']}, {'id': '113', 'type': 'if_statement', 'children': ['114', '117']}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'util_type'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'HAVE_NUMPY'}, {'id': '117', 'type': 'block', 'children': ['118']}; {'id': '118', 'type': 'try_statement', 'children': ['119', '144']}, {'id': '119', 'type': 'block', 'children': ['120']}; {'id': '120', 'type': 'if_statement', 'children': ['121', '128']}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'ndarray'}; {'id': '128', 'type': 'block', 'children': ['129']}, {'id': '129', 'type': 'expression_statement', 'children': ['130']}; {'id': '130', 'type': 'assignment', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'str_'}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'array_str'}; {'id': '136', 'type': 'argument_list', 'children': ['137', '138', '141']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '138', 'type': 'keyword_argument', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'precision'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'precision'}, {'id': '141', 'type': 'keyword_argument', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'suppress_small'}, {'id': '143', 'type': 'True', 'children': []}; {'id': '144', 'type': 'except_clause', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'ImportError'}; {'id': '146', 'type': 'block', 'children': ['147']}, {'id': '147', 'type': 'pass_statement', 'children': []}; {'id': '148', 'type': 'if_statement', 'children': ['149', '152']}, {'id': '149', 'type': 'comparison_operator', 'children': ['150', '151'], 'value': 'is'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'str_'}, {'id': '151', 'type': 'None', 'children': []}; {'id': '152', 'type': 'block', 'children': ['153']}, {'id': '153', 'type': 'expression_statement', 'children': ['154']}; {'id': '154', 'type': 'assignment', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'str_'}; {'id': '156', 'type': 'call', 'children': ['157', '160']}, {'id': '157', 'type': 'attribute', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'text_type'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'subscript', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'val_list'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'sx'}; {'id': '164', 'type': 'expression_statement', 'children': ['165']}, {'id': '165', 'type': 'assignment', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'lines'}, {'id': '167', 'type': 'call', 'children': ['168', '171']}; {'id': '168', 'type': 'attribute', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'str_'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '171', 'type': 'argument_list', 'children': ['172']}; {'id': '172', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '173', 'type': 'expression_statement', 'children': ['174']}; {'id': '174', 'type': 'assignment', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'line_diff'}; {'id': '176', 'type': 'binary_operator', 'children': ['177', '181'], 'value': '-'}, {'id': '177', 'type': 'call', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '179', 'type': 'argument_list', 'children': ['180']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'lines'}, {'id': '181', 'type': 'call', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '183', 'type': 'argument_list', 'children': ['184']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '185', 'type': 'if_statement', 'children': ['186', '189']}; {'id': '186', 'type': 'comparison_operator', 'children': ['187', '188'], 'value': '>'}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'line_diff'}; {'id': '188', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '189', 'type': 'block', 'children': ['190']}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'augmented_assignment', 'children': ['192', '193'], 'value': '+='}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '193', 'type': 'binary_operator', 'children': ['194', '198'], 'value': '*'}; {'id': '194', 'type': 'list', 'children': ['195'], 'value': "[' ' * hpos]"}, {'id': '195', 'type': 'binary_operator', 'children': ['196', '197'], 'value': '*'}; {'id': '196', 'type': 'string', 'children': [], 'value': "' '"}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'hpos'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'line_diff'}, {'id': '199', 'type': 'for_statement', 'children': ['200', '203', '207']}; {'id': '200', 'type': 'pattern_list', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'lx'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '203', 'type': 'call', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'enumerate'}, {'id': '205', 'type': 'argument_list', 'children': ['206']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'lines'}, {'id': '207', 'type': 'block', 'children': ['208', '214']}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'augmented_assignment', 'children': ['210', '213'], 'value': '+='}; {'id': '210', 'type': 'subscript', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'all_lines'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'lx'}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'assignment', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'hpos'}, {'id': '217', 'type': 'call', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'max'}, {'id': '219', 'type': 'argument_list', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'hpos'}, {'id': '221', 'type': 'call', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '223', 'type': 'argument_list', 'children': ['224']}; {'id': '224', 'type': 'subscript', 'children': ['225', '226']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'all_lines'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'lx'}, {'id': '227', 'type': 'for_statement', 'children': ['228', '229', '236']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'lx'}, {'id': '229', 'type': 'call', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '231', 'type': 'argument_list', 'children': ['232']}; {'id': '232', 'type': 'call', 'children': ['233', '234']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '234', 'type': 'argument_list', 'children': ['235']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'all_lines'}; {'id': '236', 'type': 'block', 'children': ['237', '248']}, {'id': '237', 'type': 'expression_statement', 'children': ['238']}; {'id': '238', 'type': 'assignment', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'hpos_diff'}; {'id': '240', 'type': 'binary_operator', 'children': ['241', '242'], 'value': '-'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'hpos'}; {'id': '242', 'type': 'call', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '244', 'type': 'argument_list', 'children': ['245']}, {'id': '245', 'type': 'subscript', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'lx'}; {'id': '248', 'type': 'expression_statement', 'children': ['249']}, {'id': '249', 'type': 'augmented_assignment', 'children': ['250', '253'], 'value': '+='}; {'id': '250', 'type': 'subscript', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'all_lines'}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'lx'}, {'id': '253', 'type': 'binary_operator', 'children': ['254', '257'], 'value': '+'}; {'id': '254', 'type': 'binary_operator', 'children': ['255', '256'], 'value': '*'}, {'id': '255', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'hpos_diff'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'sep'}; {'id': '258', 'type': 'expression_statement', 'children': ['259']}, {'id': '259', 'type': 'assignment', 'children': ['260', '261']}; {'id': '260', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '261', 'type': 'list_comprehension', 'children': ['262', '268']}; {'id': '262', 'type': 'call', 'children': ['263', '266']}, {'id': '263', 'type': 'attribute', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'rstrip'}; {'id': '266', 'type': 'argument_list', 'children': ['267']}, {'id': '267', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '268', 'type': 'for_in_clause', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'all_lines'}, {'id': '271', 'type': 'expression_statement', 'children': ['272']}; {'id': '272', 'type': 'assignment', 'children': ['273', '274']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'ret'}; {'id': '274', 'type': 'call', 'children': ['275', '278']}, {'id': '275', 'type': 'attribute', 'children': ['276', '277']}; {'id': '276', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '278', 'type': 'argument_list', 'children': ['279']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'all_lines'}; {'id': '280', 'type': 'return_statement', 'children': ['281']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'ret'} | def horiz_string(*args, **kwargs):
import unicodedata
precision = kwargs.get('precision', None)
sep = kwargs.get('sep', '')
if len(args) == 1 and not isinstance(args[0], six.string_types):
val_list = args[0]
else:
val_list = args
val_list = [unicodedata.normalize('NFC', ensure_unicode(val))
for val in val_list]
all_lines = []
hpos = 0
for sx in range(len(val_list)):
val = val_list[sx]
str_ = None
if precision is not None:
if util_type.HAVE_NUMPY:
try:
if isinstance(val, np.ndarray):
str_ = np.array_str(val, precision=precision,
suppress_small=True)
except ImportError:
pass
if str_ is None:
str_ = six.text_type(val_list[sx])
lines = str_.split('\n')
line_diff = len(lines) - len(all_lines)
if line_diff > 0:
all_lines += [' ' * hpos] * line_diff
for lx, line in enumerate(lines):
all_lines[lx] += line
hpos = max(hpos, len(all_lines[lx]))
for lx in range(len(all_lines)):
hpos_diff = hpos - len(all_lines[lx])
all_lines[lx] += ' ' * hpos_diff + sep
all_lines = [line.rstrip(' ') for line in all_lines]
ret = '\n'.join(all_lines)
return ret |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '12']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_textdiff'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'text1'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'text2'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'num_context_lines'}; {'id': '8', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'ignore_whitespace'}, {'id': '11', 'type': 'False', 'children': []}; {'id': '12', 'type': 'block', 'children': ['13', '15', '18', '25', '32', '40', '48', '97', '112', '279']}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '15', 'type': 'import_statement', 'children': ['16']}; {'id': '16', 'type': 'dotted_name', 'children': ['17']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'difflib'}; {'id': '18', 'type': 'expression_statement', 'children': ['19']}, {'id': '19', 'type': 'assignment', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'text1'}, {'id': '21', 'type': 'call', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'ensure_unicode'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'text1'}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'text2'}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'ensure_unicode'}; {'id': '30', 'type': 'argument_list', 'children': ['31']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'text2'}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'text1_lines'}, {'id': '35', 'type': 'call', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'text1'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'splitlines'}, {'id': '39', 'type': 'argument_list', 'children': []}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'text2_lines'}, {'id': '43', 'type': 'call', 'children': ['44', '47']}; {'id': '44', 'type': 'attribute', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'text2'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'splitlines'}, {'id': '47', 'type': 'argument_list', 'children': []}; {'id': '48', 'type': 'if_statement', 'children': ['49', '50', '91']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'ignore_whitespace'}; {'id': '50', 'type': 'block', 'children': ['51', '63', '75']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'text1_lines'}; {'id': '54', 'type': 'list_comprehension', 'children': ['55', '60']}, {'id': '55', 'type': 'call', 'children': ['56', '59']}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'rstrip'}, {'id': '59', 'type': 'argument_list', 'children': []}; {'id': '60', 'type': 'for_in_clause', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'text1_lines'}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'text2_lines'}; {'id': '66', 'type': 'list_comprehension', 'children': ['67', '72']}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'rstrip'}, {'id': '71', 'type': 'argument_list', 'children': []}; {'id': '72', 'type': 'for_in_clause', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'text2_lines'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'ndiff_kw'}; {'id': '78', 'type': 'call', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '80', 'type': 'argument_list', 'children': ['81', '86']}, {'id': '81', 'type': 'keyword_argument', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'linejunk'}, {'id': '83', 'type': 'attribute', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'difflib'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'IS_LINE_JUNK'}; {'id': '86', 'type': 'keyword_argument', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'charjunk'}; {'id': '88', 'type': 'attribute', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'difflib'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'IS_CHARACTER_JUNK'}, {'id': '91', 'type': 'else_clause', 'children': ['92']}; {'id': '92', 'type': 'block', 'children': ['93']}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'ndiff_kw'}; {'id': '96', 'type': 'dictionary', 'children': []}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'all_diff_lines'}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'call', 'children': ['104', '107']}; {'id': '104', 'type': 'attribute', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'difflib'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'ndiff'}, {'id': '107', 'type': 'argument_list', 'children': ['108', '109', '110']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'text1_lines'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'text2_lines'}; {'id': '110', 'type': 'dictionary_splat', 'children': ['111']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'ndiff_kw'}; {'id': '112', 'type': 'if_statement', 'children': ['113', '116', '121']}, {'id': '113', 'type': 'comparison_operator', 'children': ['114', '115'], 'value': 'is'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'num_context_lines'}, {'id': '115', 'type': 'None', 'children': []}; {'id': '116', 'type': 'block', 'children': ['117']}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'diff_lines'}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'all_diff_lines'}, {'id': '121', 'type': 'else_clause', 'children': ['122']}; {'id': '122', 'type': 'block', 'children': ['123', '128', '147', '154', '211', '215']}, {'id': '123', 'type': 'import_from_statement', 'children': ['124', '126']}; {'id': '124', 'type': 'dotted_name', 'children': ['125']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '126', 'type': 'dotted_name', 'children': ['127']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'util_list'}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'assignment', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'ismarked_list'}, {'id': '131', 'type': 'list_comprehension', 'children': ['132', '144']}; {'id': '132', 'type': 'boolean_operator', 'children': ['133', '139'], 'value': 'and'}, {'id': '133', 'type': 'comparison_operator', 'children': ['134', '138'], 'value': '>'}; {'id': '134', 'type': 'call', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '138', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '139', 'type': 'comparison_operator', 'children': ['140', '143'], 'value': 'in'}; {'id': '140', 'type': 'subscript', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '142', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '143', 'type': 'string', 'children': [], 'value': "'+-?'"}; {'id': '144', 'type': 'for_in_clause', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'all_diff_lines'}, {'id': '147', 'type': 'expression_statement', 'children': ['148']}; {'id': '148', 'type': 'assignment', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}; {'id': '150', 'type': 'subscript', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'ismarked_list'}; {'id': '152', 'type': 'slice', 'children': ['153']}, {'id': '153', 'type': 'colon', 'children': []}; {'id': '154', 'type': 'for_statement', 'children': ['155', '156', '163']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '156', 'type': 'call', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '158', 'type': 'argument_list', 'children': ['159', '160']}, {'id': '159', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '160', 'type': 'binary_operator', 'children': ['161', '162'], 'value': '+'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'num_context_lines'}; {'id': '162', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '163', 'type': 'block', 'children': ['164', '188']}; {'id': '164', 'type': 'expression_statement', 'children': ['165']}, {'id': '165', 'type': 'assignment', 'children': ['166', '172']}; {'id': '166', 'type': 'subscript', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}; {'id': '168', 'type': 'slice', 'children': ['169', '170']}, {'id': '169', 'type': 'colon', 'children': []}; {'id': '170', 'type': 'unary_operator', 'children': ['171'], 'value': '-'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '172', 'type': 'call', 'children': ['173', '176']}, {'id': '173', 'type': 'attribute', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'util_list'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'or_lists'}; {'id': '176', 'type': 'argument_list', 'children': ['177', '183']}, {'id': '177', 'type': 'subscript', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}, {'id': '179', 'type': 'slice', 'children': ['180', '181']}; {'id': '180', 'type': 'colon', 'children': []}, {'id': '181', 'type': 'unary_operator', 'children': ['182'], 'value': '-'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '183', 'type': 'subscript', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'ismarked_list'}, {'id': '185', 'type': 'slice', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '187', 'type': 'colon', 'children': []}; {'id': '188', 'type': 'expression_statement', 'children': ['189']}, {'id': '189', 'type': 'assignment', 'children': ['190', '195']}; {'id': '190', 'type': 'subscript', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}; {'id': '192', 'type': 'slice', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '194', 'type': 'colon', 'children': []}, {'id': '195', 'type': 'call', 'children': ['196', '199']}; {'id': '196', 'type': 'attribute', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'util_list'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'or_lists'}, {'id': '199', 'type': 'argument_list', 'children': ['200', '205']}; {'id': '200', 'type': 'subscript', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}; {'id': '202', 'type': 'slice', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '204', 'type': 'colon', 'children': []}, {'id': '205', 'type': 'subscript', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'ismarked_list'}, {'id': '207', 'type': 'slice', 'children': ['208', '209']}; {'id': '208', 'type': 'colon', 'children': []}, {'id': '209', 'type': 'unary_operator', 'children': ['210'], 'value': '-'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '211', 'type': 'expression_statement', 'children': ['212']}; {'id': '212', 'type': 'assignment', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'USE_BREAK_LINE'}; {'id': '214', 'type': 'True', 'children': []}, {'id': '215', 'type': 'if_statement', 'children': ['216', '217', '267']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'USE_BREAK_LINE'}, {'id': '217', 'type': 'block', 'children': ['218', '222', '226', '230']}; {'id': '218', 'type': 'expression_statement', 'children': ['219']}, {'id': '219', 'type': 'assignment', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'diff_lines'}, {'id': '221', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '222', 'type': 'expression_statement', 'children': ['223']}, {'id': '223', 'type': 'assignment', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'prev'}, {'id': '225', 'type': 'False', 'children': []}; {'id': '226', 'type': 'expression_statement', 'children': ['227']}, {'id': '227', 'type': 'assignment', 'children': ['228', '229']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'visual_break'}, {'id': '229', 'type': 'string', 'children': [], 'value': "'\\n <... FILTERED CONTEXT ...> \\n'"}; {'id': '230', 'type': 'for_statement', 'children': ['231', '234', '239']}, {'id': '231', 'type': 'pattern_list', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'valid'}; {'id': '234', 'type': 'call', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '236', 'type': 'argument_list', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'all_diff_lines'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}, {'id': '239', 'type': 'block', 'children': ['240', '263']}; {'id': '240', 'type': 'if_statement', 'children': ['241', '242', '250']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'valid'}; {'id': '242', 'type': 'block', 'children': ['243']}, {'id': '243', 'type': 'expression_statement', 'children': ['244']}; {'id': '244', 'type': 'call', 'children': ['245', '248']}, {'id': '245', 'type': 'attribute', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'diff_lines'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '248', 'type': 'argument_list', 'children': ['249']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '250', 'type': 'elif_clause', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'prev'}; {'id': '252', 'type': 'block', 'children': ['253']}, {'id': '253', 'type': 'if_statement', 'children': ['254', '255']}; {'id': '254', 'type': 'False', 'children': []}, {'id': '255', 'type': 'block', 'children': ['256']}; {'id': '256', 'type': 'expression_statement', 'children': ['257']}, {'id': '257', 'type': 'call', 'children': ['258', '261']}; {'id': '258', 'type': 'attribute', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'diff_lines'}; {'id': '260', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '261', 'type': 'argument_list', 'children': ['262']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'visual_break'}, {'id': '263', 'type': 'expression_statement', 'children': ['264']}; {'id': '264', 'type': 'assignment', 'children': ['265', '266']}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'prev'}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'valid'}, {'id': '267', 'type': 'else_clause', 'children': ['268']}; {'id': '268', 'type': 'block', 'children': ['269']}, {'id': '269', 'type': 'expression_statement', 'children': ['270']}; {'id': '270', 'type': 'assignment', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'diff_lines'}; {'id': '272', 'type': 'call', 'children': ['273', '276']}, {'id': '273', 'type': 'attribute', 'children': ['274', '275']}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'util_list'}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'compress'}; {'id': '276', 'type': 'argument_list', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'all_diff_lines'}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'isvalid_list'}, {'id': '279', 'type': 'return_statement', 'children': ['280']}; {'id': '280', 'type': 'call', 'children': ['281', '284']}, {'id': '281', 'type': 'attribute', 'children': ['282', '283']}; {'id': '282', 'type': 'string', 'children': [], 'value': "'\\n'"}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '284', 'type': 'argument_list', 'children': ['285']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'diff_lines'} | def get_textdiff(text1, text2, num_context_lines=0, ignore_whitespace=False):
r
import difflib
text1 = ensure_unicode(text1)
text2 = ensure_unicode(text2)
text1_lines = text1.splitlines()
text2_lines = text2.splitlines()
if ignore_whitespace:
text1_lines = [t.rstrip() for t in text1_lines]
text2_lines = [t.rstrip() for t in text2_lines]
ndiff_kw = dict(linejunk=difflib.IS_LINE_JUNK,
charjunk=difflib.IS_CHARACTER_JUNK)
else:
ndiff_kw = {}
all_diff_lines = list(difflib.ndiff(text1_lines, text2_lines, **ndiff_kw))
if num_context_lines is None:
diff_lines = all_diff_lines
else:
from utool import util_list
ismarked_list = [len(line) > 0 and line[0] in '+-?'
for line in all_diff_lines]
isvalid_list = ismarked_list[:]
for i in range(1, num_context_lines + 1):
isvalid_list[:-i] = util_list.or_lists(isvalid_list[:-i],
ismarked_list[i:])
isvalid_list[i:] = util_list.or_lists(isvalid_list[i:],
ismarked_list[:-i])
USE_BREAK_LINE = True
if USE_BREAK_LINE:
diff_lines = []
prev = False
visual_break = '\n <... FILTERED CONTEXT ...> \n'
for line, valid in zip(all_diff_lines, isvalid_list):
if valid:
diff_lines.append(line)
elif prev:
if False:
diff_lines.append(visual_break)
prev = valid
else:
diff_lines = util_list.compress(all_diff_lines, isvalid_list)
return '\n'.join(diff_lines) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '18']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'add'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '12', '15']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '11', 'type': 'None', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'namespace'}; {'id': '14', 'type': 'None', 'children': []}, {'id': '15', 'type': 'default_parameter', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '17', 'type': 'False', 'children': []}; {'id': '18', 'type': 'block', 'children': ['19', '43', '49', '56', '72', '78', '84', '90', '101', '112', '175', '191', '203', '255', '259', '267', '273', '283', '293', '484', '490']}, {'id': '19', 'type': 'if_statement', 'children': ['20', '23']}; {'id': '20', 'type': 'comparison_operator', 'children': ['21', '22'], 'value': 'is'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '22', 'type': 'None', 'children': []}, {'id': '23', 'type': 'block', 'children': ['24']}; {'id': '24', 'type': 'return_statement', 'children': ['25']}, {'id': '25', 'type': 'call', 'children': ['26', '29']}; {'id': '26', 'type': 'attribute', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '29', 'type': 'argument_list', 'children': ['30', '34', '37', '40']}; {'id': '30', 'type': 'tuple', 'children': ['31']}, {'id': '31', 'type': 'tuple', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '34', 'type': 'keyword_argument', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'timestamp'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '37', 'type': 'keyword_argument', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'namespace'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'namespace'}; {'id': '40', 'type': 'keyword_argument', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'assignment', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'writer'}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'writer'}, {'id': '49', 'type': 'if_statement', 'children': ['50', '53']}; {'id': '50', 'type': 'comparison_operator', 'children': ['51', '52'], 'value': 'is'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'writer'}; {'id': '52', 'type': 'None', 'children': []}, {'id': '53', 'type': 'block', 'children': ['54']}; {'id': '54', 'type': 'raise_statement', 'children': ['55']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'GaugedUseAfterFreeError'}; {'id': '56', 'type': 'if_statement', 'children': ['57', '60']}, {'id': '57', 'type': 'comparison_operator', 'children': ['58', '59'], 'value': 'is'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '59', 'type': 'None', 'children': []}; {'id': '60', 'type': 'block', 'children': ['61']}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'timestamp'}; {'id': '64', 'type': 'call', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'long'}; {'id': '66', 'type': 'argument_list', 'children': ['67']}, {'id': '67', 'type': 'binary_operator', 'children': ['68', '71'], 'value': '*'}; {'id': '68', 'type': 'call', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'time'}; {'id': '70', 'type': 'argument_list', 'children': []}, {'id': '71', 'type': 'integer', 'children': [], 'value': '1000'}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '75', 'type': 'attribute', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'block_size'}, {'id': '81', 'type': 'attribute', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'block_size'}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'this_block'}, {'id': '87', 'type': 'binary_operator', 'children': ['88', '89'], 'value': '//'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'block_size'}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'this_array'}, {'id': '93', 'type': 'binary_operator', 'children': ['94', '98'], 'value': '//'}; {'id': '94', 'type': 'parenthesized_expression', 'children': ['95']}, {'id': '95', 'type': 'binary_operator', 'children': ['96', '97'], 'value': '%'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'block_size'}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'resolution'}, {'id': '101', 'type': 'if_statement', 'children': ['102', '105']}; {'id': '102', 'type': 'comparison_operator', 'children': ['103', '104'], 'value': 'is'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'namespace'}; {'id': '104', 'type': 'None', 'children': []}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'namespace'}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'namespace'}; {'id': '112', 'type': 'if_statement', 'children': ['113', '132']}, {'id': '113', 'type': 'boolean_operator', 'children': ['114', '119', '120'], 'value': 'or'}; {'id': '114', 'type': 'comparison_operator', 'children': ['115', '116'], 'value': '<'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'this_block'}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'current_block'}, {'id': '119', 'type': 'line_continuation', 'children': [], 'value': '\\'}; {'id': '120', 'type': 'parenthesized_expression', 'children': ['121']}, {'id': '121', 'type': 'boolean_operator', 'children': ['122', '127'], 'value': 'and'}; {'id': '122', 'type': 'comparison_operator', 'children': ['123', '124'], 'value': '=='}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'this_block'}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'current_block'}, {'id': '127', 'type': 'comparison_operator', 'children': ['128', '129'], 'value': '<'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'this_array'}, {'id': '129', 'type': 'attribute', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'current_array'}; {'id': '132', 'type': 'block', 'children': ['133']}, {'id': '133', 'type': 'if_statement', 'children': ['134', '141', '151', '172']}; {'id': '134', 'type': 'comparison_operator', 'children': ['135', '138'], 'value': '=='}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'append_only_violation'}; {'id': '138', 'type': 'attribute', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'Writer'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'ERROR'}, {'id': '141', 'type': 'block', 'children': ['142', '146']}; {'id': '142', 'type': 'expression_statement', 'children': ['143']}, {'id': '143', 'type': 'assignment', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '145', 'type': 'string', 'children': [], 'value': "'Gauged is append-only; timestamps must be increasing'"}; {'id': '146', 'type': 'raise_statement', 'children': ['147']}, {'id': '147', 'type': 'call', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'GaugedAppendOnlyError'}, {'id': '149', 'type': 'argument_list', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '151', 'type': 'elif_clause', 'children': ['152', '159']}; {'id': '152', 'type': 'comparison_operator', 'children': ['153', '156'], 'value': '=='}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'append_only_violation'}; {'id': '156', 'type': 'attribute', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'Writer'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'REWRITE'}, {'id': '159', 'type': 'block', 'children': ['160', '166']}; {'id': '160', 'type': 'expression_statement', 'children': ['161']}, {'id': '161', 'type': 'assignment', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'this_block'}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'current_block'}; {'id': '166', 'type': 'expression_statement', 'children': ['167']}, {'id': '167', 'type': 'assignment', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'this_array'}, {'id': '169', 'type': 'attribute', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'current_array'}; {'id': '172', 'type': 'else_clause', 'children': ['173']}, {'id': '173', 'type': 'block', 'children': ['174']}; {'id': '174', 'type': 'return_statement', 'children': []}, {'id': '175', 'type': 'if_statement', 'children': ['176', '181']}; {'id': '176', 'type': 'call', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '178', 'type': 'argument_list', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'unicode'}, {'id': '181', 'type': 'block', 'children': ['182']}; {'id': '182', 'type': 'expression_statement', 'children': ['183']}, {'id': '183', 'type': 'assignment', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '185', 'type': 'call', 'children': ['186', '189']}; {'id': '186', 'type': 'attribute', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'encode'}, {'id': '189', 'type': 'argument_list', 'children': ['190']}; {'id': '190', 'type': 'string', 'children': [], 'value': "'utf8'"}, {'id': '191', 'type': 'if_statement', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '193', 'type': 'block', 'children': ['194']}; {'id': '194', 'type': 'return_statement', 'children': ['195']}, {'id': '195', 'type': 'call', 'children': ['196', '199']}; {'id': '196', 'type': 'attribute', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '199', 'type': 'argument_list', 'children': ['200', '201', '202']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'timestamp'}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'namespace'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '203', 'type': 'if_statement', 'children': ['204', '209', '228']}; {'id': '204', 'type': 'comparison_operator', 'children': ['205', '206'], 'value': '>'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'this_block'}; {'id': '206', 'type': 'attribute', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'current_block'}, {'id': '209', 'type': 'block', 'children': ['210', '216', '222']}; {'id': '210', 'type': 'expression_statement', 'children': ['211']}, {'id': '211', 'type': 'call', 'children': ['212', '215']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'flush_blocks'}, {'id': '215', 'type': 'argument_list', 'children': []}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '221']}; {'id': '218', 'type': 'attribute', 'children': ['219', '220']}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'current_block'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'this_block'}; {'id': '222', 'type': 'expression_statement', 'children': ['223']}, {'id': '223', 'type': 'assignment', 'children': ['224', '227']}; {'id': '224', 'type': 'attribute', 'children': ['225', '226']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'current_array'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'this_array'}; {'id': '228', 'type': 'elif_clause', 'children': ['229', '234']}, {'id': '229', 'type': 'comparison_operator', 'children': ['230', '231'], 'value': '>'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'this_array'}, {'id': '231', 'type': 'attribute', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'current_array'}; {'id': '234', 'type': 'block', 'children': ['235', '249']}, {'id': '235', 'type': 'if_statement', 'children': ['236', '246']}; {'id': '236', 'type': 'not_operator', 'children': ['237']}, {'id': '237', 'type': 'call', 'children': ['238', '241']}; {'id': '238', 'type': 'attribute', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'Gauged'}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'writer_flush_arrays'}, {'id': '241', 'type': 'argument_list', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'writer'}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'current_array'}; {'id': '246', 'type': 'block', 'children': ['247']}, {'id': '247', 'type': 'raise_statement', 'children': ['248']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'MemoryError'}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'assignment', 'children': ['251', '254']}, {'id': '251', 'type': 'attribute', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'current_array'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'this_array'}, {'id': '255', 'type': 'expression_statement', 'children': ['256']}; {'id': '256', 'type': 'assignment', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '258', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '259', 'type': 'expression_statement', 'children': ['260']}; {'id': '260', 'type': 'assignment', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'namespace_statistics'}; {'id': '262', 'type': 'subscript', 'children': ['263', '266']}, {'id': '263', 'type': 'attribute', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'statistics'}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'namespace'}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'assignment', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'whitelist'}; {'id': '270', 'type': 'attribute', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'key_whitelist'}, {'id': '273', 'type': 'expression_statement', 'children': ['274']}; {'id': '274', 'type': 'assignment', 'children': ['275', '276']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'skip_long_keys'}; {'id': '276', 'type': 'comparison_operator', 'children': ['277', '280'], 'value': '=='}, {'id': '277', 'type': 'attribute', 'children': ['278', '279']}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'key_overflow'}; {'id': '280', 'type': 'attribute', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'Writer'}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'IGNORE'}, {'id': '283', 'type': 'expression_statement', 'children': ['284']}; {'id': '284', 'type': 'assignment', 'children': ['285', '286']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'skip_gauge_nan'}; {'id': '286', 'type': 'comparison_operator', 'children': ['287', '290'], 'value': '=='}, {'id': '287', 'type': 'attribute', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'gauge_nan'}; {'id': '290', 'type': 'attribute', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'Writer'}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'IGNORE'}, {'id': '293', 'type': 'if_statement', 'children': ['294', '308', '339']}; {'id': '294', 'type': 'boolean_operator', 'children': ['295', '305'], 'value': 'and'}, {'id': '295', 'type': 'boolean_operator', 'children': ['296', '303', '304'], 'value': 'and'}; {'id': '296', 'type': 'boolean_operator', 'children': ['297', '302'], 'value': 'and'}, {'id': '297', 'type': 'call', 'children': ['298', '299']}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '299', 'type': 'argument_list', 'children': ['300', '301']}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '302', 'type': 'identifier', 'children': [], 'value': 'skip_gauge_nan'}, {'id': '303', 'type': 'line_continuation', 'children': [], 'value': '\\'}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'skip_long_keys'}, {'id': '305', 'type': 'comparison_operator', 'children': ['306', '307'], 'value': 'is'}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'whitelist'}, {'id': '307', 'type': 'None', 'children': []}; {'id': '308', 'type': 'block', 'children': ['309', '316', '333']}, {'id': '309', 'type': 'expression_statement', 'children': ['310']}; {'id': '310', 'type': 'assignment', 'children': ['311', '312']}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '312', 'type': 'call', 'children': ['313', '314']}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'c_uint32'}; {'id': '314', 'type': 'argument_list', 'children': ['315']}, {'id': '315', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '316', 'type': 'if_statement', 'children': ['317', '330']}, {'id': '317', 'type': 'not_operator', 'children': ['318']}; {'id': '318', 'type': 'call', 'children': ['319', '322']}, {'id': '319', 'type': 'attribute', 'children': ['320', '321']}; {'id': '320', 'type': 'identifier', 'children': [], 'value': 'Gauged'}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'writer_emit_pairs'}; {'id': '322', 'type': 'argument_list', 'children': ['323', '324', '325', '326']}, {'id': '323', 'type': 'identifier', 'children': [], 'value': 'writer'}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'namespace'}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '326', 'type': 'call', 'children': ['327', '328']}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'byref'}; {'id': '328', 'type': 'argument_list', 'children': ['329']}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '330', 'type': 'block', 'children': ['331']}, {'id': '331', 'type': 'raise_statement', 'children': ['332']}; {'id': '332', 'type': 'identifier', 'children': [], 'value': 'MemoryError'}, {'id': '333', 'type': 'expression_statement', 'children': ['334']}; {'id': '334', 'type': 'assignment', 'children': ['335', '336']}, {'id': '335', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '336', 'type': 'attribute', 'children': ['337', '338']}, {'id': '337', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '339', 'type': 'else_clause', 'children': ['340']}; {'id': '340', 'type': 'block', 'children': ['341', '372', '378']}, {'id': '341', 'type': 'if_statement', 'children': ['342', '347', '356']}; {'id': '342', 'type': 'call', 'children': ['343', '344']}, {'id': '343', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '344', 'type': 'argument_list', 'children': ['345', '346']}, {'id': '345', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '346', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '347', 'type': 'block', 'children': ['348']}; {'id': '348', 'type': 'expression_statement', 'children': ['349']}, {'id': '349', 'type': 'assignment', 'children': ['350', '351']}; {'id': '350', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '351', 'type': 'call', 'children': ['352', '355']}; {'id': '352', 'type': 'attribute', 'children': ['353', '354']}, {'id': '353', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '354', 'type': 'identifier', 'children': [], 'value': 'iteritems'}, {'id': '355', 'type': 'argument_list', 'children': []}; {'id': '356', 'type': 'elif_clause', 'children': ['357', '362']}, {'id': '357', 'type': 'call', 'children': ['358', '359']}; {'id': '358', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '359', 'type': 'argument_list', 'children': ['360', '361']}; {'id': '360', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '361', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '362', 'type': 'block', 'children': ['363']}, {'id': '363', 'type': 'expression_statement', 'children': ['364']}; {'id': '364', 'type': 'assignment', 'children': ['365', '366']}, {'id': '365', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '366', 'type': 'call', 'children': ['367', '370']}, {'id': '367', 'type': 'attribute', 'children': ['368', '369']}; {'id': '368', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '369', 'type': 'identifier', 'children': [], 'value': 'parse_query'}; {'id': '370', 'type': 'argument_list', 'children': ['371']}, {'id': '371', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '372', 'type': 'expression_statement', 'children': ['373']}, {'id': '373', 'type': 'assignment', 'children': ['374', '375']}; {'id': '374', 'type': 'identifier', 'children': [], 'value': 'emit'}, {'id': '375', 'type': 'attribute', 'children': ['376', '377']}; {'id': '376', 'type': 'identifier', 'children': [], 'value': 'Gauged'}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'writer_emit'}; {'id': '378', 'type': 'for_statement', 'children': ['379', '382', '383']}, {'id': '379', 'type': 'pattern_list', 'children': ['380', '381']}; {'id': '380', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '381', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '382', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '383', 'type': 'block', 'children': ['384', '391', '401', '420', '431', '444', '480']}; {'id': '384', 'type': 'expression_statement', 'children': ['385']}, {'id': '385', 'type': 'assignment', 'children': ['386', '387']}; {'id': '386', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '387', 'type': 'call', 'children': ['388', '389']}; {'id': '388', 'type': 'identifier', 'children': [], 'value': 'to_bytes'}, {'id': '389', 'type': 'argument_list', 'children': ['390']}; {'id': '390', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '391', 'type': 'if_statement', 'children': ['392', '399']}; {'id': '392', 'type': 'boolean_operator', 'children': ['393', '396'], 'value': 'and'}, {'id': '393', 'type': 'comparison_operator', 'children': ['394', '395'], 'value': 'is'}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'whitelist'}, {'id': '395', 'type': 'None', 'children': []}; {'id': '396', 'type': 'comparison_operator', 'children': ['397', '398'], 'value': 'not'}, {'id': '397', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '398', 'type': 'identifier', 'children': [], 'value': 'whitelist'}, {'id': '399', 'type': 'block', 'children': ['400']}; {'id': '400', 'type': 'continue_statement', 'children': []}, {'id': '401', 'type': 'try_statement', 'children': ['402', '410']}; {'id': '402', 'type': 'block', 'children': ['403']}, {'id': '403', 'type': 'expression_statement', 'children': ['404']}; {'id': '404', 'type': 'assignment', 'children': ['405', '406']}, {'id': '405', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '406', 'type': 'call', 'children': ['407', '408']}, {'id': '407', 'type': 'identifier', 'children': [], 'value': 'float'}; {'id': '408', 'type': 'argument_list', 'children': ['409']}, {'id': '409', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '410', 'type': 'except_clause', 'children': ['411', '412']}, {'id': '411', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '412', 'type': 'block', 'children': ['413']}, {'id': '413', 'type': 'expression_statement', 'children': ['414']}; {'id': '414', 'type': 'assignment', 'children': ['415', '416']}, {'id': '415', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '416', 'type': 'call', 'children': ['417', '418']}, {'id': '417', 'type': 'identifier', 'children': [], 'value': 'float'}; {'id': '418', 'type': 'argument_list', 'children': ['419']}, {'id': '419', 'type': 'string', 'children': [], 'value': "'nan'"}; {'id': '420', 'type': 'if_statement', 'children': ['421', '424']}, {'id': '421', 'type': 'comparison_operator', 'children': ['422', '423'], 'value': '!='}; {'id': '422', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '423', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '424', 'type': 'block', 'children': ['425', '429']}, {'id': '425', 'type': 'if_statement', 'children': ['426', '427']}; {'id': '426', 'type': 'identifier', 'children': [], 'value': 'skip_gauge_nan'}, {'id': '427', 'type': 'block', 'children': ['428']}; {'id': '428', 'type': 'continue_statement', 'children': []}, {'id': '429', 'type': 'raise_statement', 'children': ['430']}; {'id': '430', 'type': 'identifier', 'children': [], 'value': 'GaugedNaNError'}, {'id': '431', 'type': 'expression_statement', 'children': ['432']}; {'id': '432', 'type': 'assignment', 'children': ['433', '434']}, {'id': '433', 'type': 'identifier', 'children': [], 'value': 'success'}; {'id': '434', 'type': 'call', 'children': ['435', '436']}, {'id': '435', 'type': 'identifier', 'children': [], 'value': 'emit'}; {'id': '436', 'type': 'argument_list', 'children': ['437', '438', '439', '440']}, {'id': '437', 'type': 'identifier', 'children': [], 'value': 'writer'}; {'id': '438', 'type': 'identifier', 'children': [], 'value': 'namespace'}, {'id': '439', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '440', 'type': 'call', 'children': ['441', '442']}, {'id': '441', 'type': 'identifier', 'children': [], 'value': 'c_float'}; {'id': '442', 'type': 'argument_list', 'children': ['443']}, {'id': '443', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '444', 'type': 'if_statement', 'children': ['445', '448']}, {'id': '445', 'type': 'comparison_operator', 'children': ['446', '447'], 'value': '!='}; {'id': '446', 'type': 'identifier', 'children': [], 'value': 'success'}, {'id': '447', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '448', 'type': 'block', 'children': ['449']}, {'id': '449', 'type': 'if_statement', 'children': ['450', '452', '455']}; {'id': '450', 'type': 'not_operator', 'children': ['451']}, {'id': '451', 'type': 'identifier', 'children': [], 'value': 'success'}; {'id': '452', 'type': 'block', 'children': ['453']}, {'id': '453', 'type': 'raise_statement', 'children': ['454']}; {'id': '454', 'type': 'identifier', 'children': [], 'value': 'MemoryError'}, {'id': '455', 'type': 'elif_clause', 'children': ['456', '464']}; {'id': '456', 'type': 'boolean_operator', 'children': ['457', '462'], 'value': 'and'}, {'id': '457', 'type': 'comparison_operator', 'children': ['458', '459'], 'value': '=='}; {'id': '458', 'type': 'identifier', 'children': [], 'value': 'success'}, {'id': '459', 'type': 'attribute', 'children': ['460', '461']}; {'id': '460', 'type': 'identifier', 'children': [], 'value': 'Writer'}, {'id': '461', 'type': 'identifier', 'children': [], 'value': 'KEY_OVERFLOW'}; {'id': '462', 'type': 'not_operator', 'children': ['463']}, {'id': '463', 'type': 'identifier', 'children': [], 'value': 'skip_long_keys'}; {'id': '464', 'type': 'block', 'children': ['465', '469', '475']}, {'id': '465', 'type': 'expression_statement', 'children': ['466']}; {'id': '466', 'type': 'assignment', 'children': ['467', '468']}, {'id': '467', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '468', 'type': 'string', 'children': [], 'value': "'Key is larger than the driver allows '"}, {'id': '469', 'type': 'expression_statement', 'children': ['470']}; {'id': '470', 'type': 'augmented_assignment', 'children': ['471', '472'], 'value': '+='}, {'id': '471', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '472', 'type': 'binary_operator', 'children': ['473', '474'], 'value': '%'}, {'id': '473', 'type': 'string', 'children': [], 'value': "'(%s)'"}; {'id': '474', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '475', 'type': 'raise_statement', 'children': ['476']}; {'id': '476', 'type': 'call', 'children': ['477', '478']}, {'id': '477', 'type': 'identifier', 'children': [], 'value': 'GaugedKeyOverflowError'}; {'id': '478', 'type': 'argument_list', 'children': ['479']}, {'id': '479', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '480', 'type': 'expression_statement', 'children': ['481']}, {'id': '481', 'type': 'augmented_assignment', 'children': ['482', '483'], 'value': '+='}; {'id': '482', 'type': 'identifier', 'children': [], 'value': 'data_points'}, {'id': '483', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '484', 'type': 'expression_statement', 'children': ['485']}, {'id': '485', 'type': 'augmented_assignment', 'children': ['486', '489'], 'value': '+='}; {'id': '486', 'type': 'attribute', 'children': ['487', '488']}, {'id': '487', 'type': 'identifier', 'children': [], 'value': 'namespace_statistics'}; {'id': '488', 'type': 'identifier', 'children': [], 'value': 'data_points'}, {'id': '489', 'type': 'identifier', 'children': [], 'value': 'data_points'}; {'id': '490', 'type': 'if_statement', 'children': ['491', '494']}, {'id': '491', 'type': 'attribute', 'children': ['492', '493']}; {'id': '492', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '493', 'type': 'identifier', 'children': [], 'value': 'flush_now'}; {'id': '494', 'type': 'block', 'children': ['495']}, {'id': '495', 'type': 'expression_statement', 'children': ['496']}; {'id': '496', 'type': 'call', 'children': ['497', '500']}, {'id': '497', 'type': 'attribute', 'children': ['498', '499']}; {'id': '498', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '499', 'type': 'identifier', 'children': [], 'value': 'flush'} | def add(self, data, value=None, timestamp=None, namespace=None,
debug=False):
if value is not None:
return self.add(((data, value),), timestamp=timestamp,
namespace=namespace, debug=debug)
writer = self.writer
if writer is None:
raise GaugedUseAfterFreeError
if timestamp is None:
timestamp = long(time() * 1000)
config = self.config
block_size = config.block_size
this_block = timestamp // block_size
this_array = (timestamp % block_size) // config.resolution
if namespace is None:
namespace = config.namespace
if this_block < self.current_block or \
(this_block == self.current_block and
this_array < self.current_array):
if config.append_only_violation == Writer.ERROR:
msg = 'Gauged is append-only; timestamps must be increasing'
raise GaugedAppendOnlyError(msg)
elif config.append_only_violation == Writer.REWRITE:
this_block = self.current_block
this_array = self.current_array
else:
return
if isinstance(data, unicode):
data = data.encode('utf8')
if debug:
return self.debug(timestamp, namespace, data)
if this_block > self.current_block:
self.flush_blocks()
self.current_block = this_block
self.current_array = this_array
elif this_array > self.current_array:
if not Gauged.writer_flush_arrays(writer, self.current_array):
raise MemoryError
self.current_array = this_array
data_points = 0
namespace_statistics = self.statistics[namespace]
whitelist = config.key_whitelist
skip_long_keys = config.key_overflow == Writer.IGNORE
skip_gauge_nan = config.gauge_nan == Writer.IGNORE
if isinstance(data, str) and skip_gauge_nan \
and skip_long_keys and whitelist is None:
data_points = c_uint32(0)
if not Gauged.writer_emit_pairs(writer, namespace, data,
byref(data_points)):
raise MemoryError
data_points = data_points.value
else:
if isinstance(data, dict):
data = data.iteritems()
elif isinstance(data, str):
data = self.parse_query(data)
emit = Gauged.writer_emit
for key, value in data:
key = to_bytes(key)
if whitelist is not None and key not in whitelist:
continue
try:
value = float(value)
except ValueError:
value = float('nan')
if value != value:
if skip_gauge_nan:
continue
raise GaugedNaNError
success = emit(writer, namespace, key, c_float(value))
if success != 1:
if not success:
raise MemoryError
elif success == Writer.KEY_OVERFLOW and not skip_long_keys:
msg = 'Key is larger than the driver allows '
msg += '(%s)' % key
raise GaugedKeyOverflowError(msg)
data_points += 1
namespace_statistics.data_points += data_points
if self.flush_now:
self.flush() |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '14']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'generate_psms_quanted'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8', '11']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'quantdb'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'tsvfn'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'isob_header'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'oldheader'}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'isobaric'}; {'id': '10', 'type': 'False', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'precursor'}, {'id': '13', 'type': 'False', 'children': []}; {'id': '14', 'type': 'block', 'children': ['15', '27', '34']}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '20']}, {'id': '17', 'type': 'pattern_list', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'allquants'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'sqlfields'}; {'id': '20', 'type': 'call', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'quantdb'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'select_all_psm_quants'}; {'id': '24', 'type': 'argument_list', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'isobaric'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'precursor'}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'quant'}; {'id': '30', 'type': 'call', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'next'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'allquants'}; {'id': '34', 'type': 'for_statement', 'children': ['35', '38', '48']}, {'id': '35', 'type': 'pattern_list', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'rownr'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'psm'}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'enumerate'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'call', 'children': ['42', '45']}; {'id': '42', 'type': 'attribute', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'readers'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'generate_tsv_psms'}, {'id': '45', 'type': 'argument_list', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'tsvfn'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'oldheader'}; {'id': '48', 'type': 'block', 'children': ['49', '65', '100', '177']}, {'id': '49', 'type': 'expression_statement', 'children': ['50']}; {'id': '50', 'type': 'assignment', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'outpsm'}; {'id': '52', 'type': 'dictionary_comprehension', 'children': ['53', '56']}, {'id': '53', 'type': 'pair', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '56', 'type': 'for_in_clause', 'children': ['57', '60']}, {'id': '57', 'type': 'pattern_list', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '60', 'type': 'call', 'children': ['61', '64']}, {'id': '61', 'type': 'attribute', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'psm'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '64', 'type': 'argument_list', 'children': []}, {'id': '65', 'type': 'if_statement', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'precursor'}, {'id': '67', 'type': 'block', 'children': ['68', '76', '85']}; {'id': '68', 'type': 'expression_statement', 'children': ['69']}, {'id': '69', 'type': 'assignment', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'pquant'}, {'id': '71', 'type': 'subscript', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'quant'}, {'id': '73', 'type': 'subscript', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'sqlfields'}, {'id': '75', 'type': 'string', 'children': [], 'value': "'precursor'"}; {'id': '76', 'type': 'if_statement', 'children': ['77', '80']}, {'id': '77', 'type': 'comparison_operator', 'children': ['78', '79'], 'value': 'is'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'pquant'}, {'id': '79', 'type': 'None', 'children': []}; {'id': '80', 'type': 'block', 'children': ['81']}, {'id': '81', 'type': 'expression_statement', 'children': ['82']}; {'id': '82', 'type': 'assignment', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'pquant'}; {'id': '84', 'type': 'string', 'children': [], 'value': "'NA'"}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'call', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'outpsm'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'update'}; {'id': '90', 'type': 'argument_list', 'children': ['91']}, {'id': '91', 'type': 'dictionary', 'children': ['92']}; {'id': '92', 'type': 'pair', 'children': ['93', '96']}, {'id': '93', 'type': 'attribute', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'mzidtsvdata'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'HEADER_PRECURSOR_QUANT'}; {'id': '96', 'type': 'call', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '98', 'type': 'argument_list', 'children': ['99']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'pquant'}; {'id': '100', 'type': 'if_statement', 'children': ['101', '102', '159']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'isobaric'}; {'id': '102', 'type': 'block', 'children': ['103', '107', '148']}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'assignment', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'isoquants'}; {'id': '106', 'type': 'dictionary', 'children': []}, {'id': '107', 'type': 'while_statement', 'children': ['108', '113']}; {'id': '108', 'type': 'comparison_operator', 'children': ['109', '112'], 'value': '=='}, {'id': '109', 'type': 'subscript', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'quant'}, {'id': '111', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'rownr'}, {'id': '113', 'type': 'block', 'children': ['114', '135']}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'call', 'children': ['116', '119']}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'isoquants'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'update'}, {'id': '119', 'type': 'argument_list', 'children': ['120']}; {'id': '120', 'type': 'dictionary', 'children': ['121']}, {'id': '121', 'type': 'pair', 'children': ['122', '127']}; {'id': '122', 'type': 'subscript', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'quant'}; {'id': '124', 'type': 'subscript', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'sqlfields'}; {'id': '126', 'type': 'string', 'children': [], 'value': "'isochan'"}, {'id': '127', 'type': 'call', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'subscript', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'quant'}; {'id': '132', 'type': 'subscript', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'sqlfields'}; {'id': '134', 'type': 'string', 'children': [], 'value': "'isoquant'"}, {'id': '135', 'type': 'try_statement', 'children': ['136', '144']}; {'id': '136', 'type': 'block', 'children': ['137']}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'assignment', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'quant'}; {'id': '140', 'type': 'call', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'next'}; {'id': '142', 'type': 'argument_list', 'children': ['143']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'allquants'}; {'id': '144', 'type': 'except_clause', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'StopIteration'}; {'id': '146', 'type': 'block', 'children': ['147']}, {'id': '147', 'type': 'break_statement', 'children': []}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'call', 'children': ['150', '153']}; {'id': '150', 'type': 'attribute', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'outpsm'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'update'}, {'id': '153', 'type': 'argument_list', 'children': ['154']}; {'id': '154', 'type': 'call', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'get_quant_NAs'}; {'id': '156', 'type': 'argument_list', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'isoquants'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'isob_header'}, {'id': '159', 'type': 'else_clause', 'children': ['160']}; {'id': '160', 'type': 'block', 'children': ['161']}, {'id': '161', 'type': 'try_statement', 'children': ['162', '170']}; {'id': '162', 'type': 'block', 'children': ['163']}, {'id': '163', 'type': 'expression_statement', 'children': ['164']}; {'id': '164', 'type': 'assignment', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'quant'}; {'id': '166', 'type': 'call', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'next'}; {'id': '168', 'type': 'argument_list', 'children': ['169']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'allquants'}; {'id': '170', 'type': 'except_clause', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'StopIteration'}; {'id': '172', 'type': 'block', 'children': ['173', '176']}, {'id': '173', 'type': 'expression_statement', 'children': ['174']}; {'id': '174', 'type': 'yield', 'children': ['175']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'outpsm'}; {'id': '176', 'type': 'break_statement', 'children': []}, {'id': '177', 'type': 'expression_statement', 'children': ['178']}; {'id': '178', 'type': 'yield', 'children': ['179']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'outpsm'} | def generate_psms_quanted(quantdb, tsvfn, isob_header, oldheader,
isobaric=False, precursor=False):
allquants, sqlfields = quantdb.select_all_psm_quants(isobaric, precursor)
quant = next(allquants)
for rownr, psm in enumerate(readers.generate_tsv_psms(tsvfn, oldheader)):
outpsm = {x: y for x, y in psm.items()}
if precursor:
pquant = quant[sqlfields['precursor']]
if pquant is None:
pquant = 'NA'
outpsm.update({mzidtsvdata.HEADER_PRECURSOR_QUANT: str(pquant)})
if isobaric:
isoquants = {}
while quant[0] == rownr:
isoquants.update({quant[sqlfields['isochan']]:
str(quant[sqlfields['isoquant']])})
try:
quant = next(allquants)
except StopIteration:
break
outpsm.update(get_quant_NAs(isoquants, isob_header))
else:
try:
quant = next(allquants)
except StopIteration:
yield outpsm
break
yield outpsm |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'total_purge_developed_repo'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'repodir'}, {'id': '5', 'type': 'block', 'children': ['6', '8', '12', '17', '20', '33', '41', '86', '106', '111', '121', '140', '148', '156', '201', '216', '235', '357', '372', '375', '383']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '8', 'type': 'assert_statement', 'children': ['9']}, {'id': '9', 'type': 'comparison_operator', 'children': ['10', '11'], 'value': 'is'}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'repodir'}, {'id': '11', 'type': 'None', 'children': []}; {'id': '12', 'type': 'import_statement', 'children': ['13']}, {'id': '13', 'type': 'aliased_import', 'children': ['14', '16']}; {'id': '14', 'type': 'dotted_name', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '17', 'type': 'import_statement', 'children': ['18']}; {'id': '18', 'type': 'dotted_name', 'children': ['19']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'os'}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'assignment', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'repo'}, {'id': '23', 'type': 'call', 'children': ['24', '29']}; {'id': '24', 'type': 'attribute', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'util_git'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'Repo'}, {'id': '29', 'type': 'argument_list', 'children': ['30']}; {'id': '30', 'type': 'keyword_argument', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'dpath'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'repodir'}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'user'}; {'id': '36', 'type': 'subscript', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'os'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'environ'}; {'id': '40', 'type': 'string', 'children': [], 'value': "'USER'"}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'fmtdict'}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '46', 'type': 'argument_list', 'children': ['47', '50', '55', '60', '65', '72', '79']}, {'id': '47', 'type': 'keyword_argument', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'user'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'user'}; {'id': '50', 'type': 'keyword_argument', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'repo'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'modname'}, {'id': '55', 'type': 'keyword_argument', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'reponame'}, {'id': '57', 'type': 'attribute', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'repo'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'reponame'}; {'id': '60', 'type': 'keyword_argument', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'dpath'}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'repo'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'dpath'}, {'id': '65', 'type': 'keyword_argument', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'global_site_pkgs'}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'get_global_dist_packages_dir'}, {'id': '71', 'type': 'argument_list', 'children': []}; {'id': '72', 'type': 'keyword_argument', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'local_site_pkgs'}; {'id': '74', 'type': 'call', 'children': ['75', '78']}, {'id': '75', 'type': 'attribute', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'get_local_dist_packages_dir'}; {'id': '78', 'type': 'argument_list', 'children': []}, {'id': '79', 'type': 'keyword_argument', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'venv_site_pkgs'}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'get_site_packages_dir'}, {'id': '85', 'type': 'argument_list', 'children': []}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'commands'}, {'id': '89', 'type': 'list_comprehension', 'children': ['90', '97']}; {'id': '90', 'type': 'call', 'children': ['91', '94']}, {'id': '91', 'type': 'attribute', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '94', 'type': 'argument_list', 'children': ['95']}, {'id': '95', 'type': 'dictionary_splat', 'children': ['96']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'fmtdict'}, {'id': '97', 'type': 'for_in_clause', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '99', 'type': 'list', 'children': ['100', '101', '102', '103', '104', '105'], 'value': "[\n 'pip uninstall {modname}',\n 'sudo -H pip uninstall {modname}',\n 'sudo pip uninstall {modname}',\n 'easy_install -m {modname}',\n 'cd {dpath} && python setup.py develop --uninstall',\n 'sudo chown -R {user}:{user} {dpath}',\n ]"}; {'id': '100', 'type': 'string', 'children': [], 'value': "'pip uninstall {modname}'"}, {'id': '101', 'type': 'string', 'children': [], 'value': "'sudo -H pip uninstall {modname}'"}; {'id': '102', 'type': 'string', 'children': [], 'value': "'sudo pip uninstall {modname}'"}, {'id': '103', 'type': 'string', 'children': [], 'value': "'easy_install -m {modname}'"}; {'id': '104', 'type': 'string', 'children': [], 'value': "'cd {dpath} && python setup.py develop --uninstall'"}, {'id': '105', 'type': 'string', 'children': [], 'value': "'sudo chown -R {user}:{user} {dpath}'"}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'call', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'string', 'children': [], 'value': "'Normal uninstall commands'"}, {'id': '111', 'type': 'expression_statement', 'children': ['112']}; {'id': '112', 'type': 'call', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'call', 'children': ['116', '119']}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'string', 'children': [], 'value': "'\\n'"}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '119', 'type': 'argument_list', 'children': ['120']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'commands'}, {'id': '121', 'type': 'expression_statement', 'children': ['122']}; {'id': '122', 'type': 'assignment', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'possible_link_paths'}; {'id': '124', 'type': 'list_comprehension', 'children': ['125', '132']}, {'id': '125', 'type': 'call', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'dictionary_splat', 'children': ['131']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'fmtdict'}; {'id': '132', 'type': 'for_in_clause', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '134', 'type': 'list', 'children': ['135', '136', '137', '138', '139'], 'value': "[\n '{dpath}/{modname}.egg-info',\n '{dpath}/build',\n '{venv_site_pkgs}/{reponame}.egg-info',\n '{local_site_pkgs}/{reponame}.egg-info',\n '{venv_site_pkgs}/{reponame}.egg-info',\n ]"}, {'id': '135', 'type': 'string', 'children': [], 'value': "'{dpath}/{modname}.egg-info'"}; {'id': '136', 'type': 'string', 'children': [], 'value': "'{dpath}/build'"}, {'id': '137', 'type': 'string', 'children': [], 'value': "'{venv_site_pkgs}/{reponame}.egg-info'"}; {'id': '138', 'type': 'string', 'children': [], 'value': "'{local_site_pkgs}/{reponame}.egg-info'"}, {'id': '139', 'type': 'string', 'children': [], 'value': "'{venv_site_pkgs}/{reponame}.egg-info'"}; {'id': '140', 'type': 'import_from_statement', 'children': ['141', '144', '146']}, {'id': '141', 'type': 'dotted_name', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'os'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '144', 'type': 'dotted_name', 'children': ['145']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'exists'}; {'id': '146', 'type': 'dotted_name', 'children': ['147']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'basename'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'existing_link_paths'}, {'id': '151', 'type': 'list_comprehension', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '153', 'type': 'for_in_clause', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'possible_link_paths'}; {'id': '156', 'type': 'expression_statement', 'children': ['157']}, {'id': '157', 'type': 'assignment', 'children': ['158', '159', '165', '167']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '159', 'type': 'ERROR', 'children': ['160', '162']}; {'id': '160', 'type': 'ERROR', 'children': ['161']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'for'}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '164'], 'value': 'in'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'existing_link_paths'}, {'id': '165', 'type': 'ERROR', 'children': ['166']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'if'}, {'id': '167', 'type': 'type', 'children': ['168']}; {'id': '168', 'type': 'constrained_type', 'children': ['169', '174', '176']}, {'id': '169', 'type': 'type', 'children': ['170']}; {'id': '170', 'type': 'call', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'exists'}; {'id': '172', 'type': 'argument_list', 'children': ['173']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '174', 'type': 'ERROR', 'children': ['175']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'if'}; {'id': '176', 'type': 'type', 'children': ['177']}, {'id': '177', 'type': 'constrained_type', 'children': ['178', '189']}; {'id': '178', 'type': 'type', 'children': ['179']}, {'id': '179', 'type': 'comparison_operator', 'children': ['180', '188'], 'value': '!='}; {'id': '180', 'type': 'subscript', 'children': ['181', '187']}, {'id': '181', 'type': 'call', 'children': ['182', '185']}; {'id': '182', 'type': 'attribute', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'get_file_info'}, {'id': '185', 'type': 'argument_list', 'children': ['186']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '187', 'type': 'string', 'children': [], 'value': "'owner'"}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'user'}, {'id': '189', 'type': 'type', 'children': ['190']}; {'id': '190', 'type': 'call', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '192', 'type': 'argument_list', 'children': ['193']}, {'id': '193', 'type': 'call', 'children': ['194', '197']}; {'id': '194', 'type': 'attribute', 'children': ['195', '196']}, {'id': '195', 'type': 'string', 'children': [], 'value': "'sudo /bin/rm -rf {path}'"}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '197', 'type': 'argument_list', 'children': ['198']}; {'id': '198', 'type': 'keyword_argument', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '201', 'type': 'expression_statement', 'children': ['202']}; {'id': '202', 'type': 'assignment', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'else'}; {'id': '204', 'type': 'type', 'children': ['205']}, {'id': '205', 'type': 'call', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '207', 'type': 'argument_list', 'children': ['208']}; {'id': '208', 'type': 'call', 'children': ['209', '212']}, {'id': '209', 'type': 'attribute', 'children': ['210', '211']}; {'id': '210', 'type': 'string', 'children': [], 'value': "'/bin/rm -rf {path}'"}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '212', 'type': 'argument_list', 'children': ['213']}, {'id': '213', 'type': 'keyword_argument', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '219', '221']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '219', 'type': 'ERROR', 'children': ['220']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'easyinstall_paths'}, {'id': '221', 'type': 'list_comprehension', 'children': ['222', '229']}; {'id': '222', 'type': 'call', 'children': ['223', '226']}, {'id': '223', 'type': 'attribute', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '226', 'type': 'argument_list', 'children': ['227']}, {'id': '227', 'type': 'dictionary_splat', 'children': ['228']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'fmtdict'}, {'id': '229', 'type': 'for_in_clause', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '231', 'type': 'list', 'children': ['232', '233', '234'], 'value': "[\n '{venv_site_pkgs}/easy-install.pth',\n '{local_site_pkgs}/easy-install.pth',\n '{venv_site_pkgs}/easy-install.pth',\n ]"}; {'id': '232', 'type': 'string', 'children': [], 'value': "'{venv_site_pkgs}/easy-install.pth'"}, {'id': '233', 'type': 'string', 'children': [], 'value': "'{local_site_pkgs}/easy-install.pth'"}; {'id': '234', 'type': 'string', 'children': [], 'value': "'{venv_site_pkgs}/easy-install.pth'"}, {'id': '235', 'type': 'for_statement', 'children': ['236', '237', '238']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'easyinstall_paths'}; {'id': '238', 'type': 'block', 'children': ['239']}, {'id': '239', 'type': 'if_statement', 'children': ['240', '244']}; {'id': '240', 'type': 'call', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'exists'}; {'id': '242', 'type': 'argument_list', 'children': ['243']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '244', 'type': 'block', 'children': ['245', '266', '277', '289', '301']}, {'id': '245', 'type': 'expression_statement', 'children': ['246']}; {'id': '246', 'type': 'assignment', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'easy_install_list'}; {'id': '248', 'type': 'call', 'children': ['249', '264']}, {'id': '249', 'type': 'attribute', 'children': ['250', '263']}; {'id': '250', 'type': 'call', 'children': ['251', '262']}, {'id': '251', 'type': 'attribute', 'children': ['252', '261']}; {'id': '252', 'type': 'call', 'children': ['253', '256']}, {'id': '253', 'type': 'attribute', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'readfrom'}; {'id': '256', 'type': 'argument_list', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '258', 'type': 'keyword_argument', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '260', 'type': 'False', 'children': []}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'strip'}; {'id': '262', 'type': 'argument_list', 'children': []}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '264', 'type': 'argument_list', 'children': ['265']}, {'id': '265', 'type': 'string', 'children': [], 'value': "'\\n'"}; {'id': '266', 'type': 'expression_statement', 'children': ['267']}, {'id': '267', 'type': 'assignment', 'children': ['268', '269']}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'easy_install_list_'}, {'id': '269', 'type': 'list_comprehension', 'children': ['270', '274']}; {'id': '270', 'type': 'call', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'basename'}; {'id': '272', 'type': 'argument_list', 'children': ['273']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'p'}; {'id': '274', 'type': 'for_in_clause', 'children': ['275', '276']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'p'}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'easy_install_list'}, {'id': '277', 'type': 'expression_statement', 'children': ['278']}; {'id': '278', 'type': 'assignment', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'index1'}; {'id': '280', 'type': 'call', 'children': ['281', '284']}, {'id': '281', 'type': 'attribute', 'children': ['282', '283']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'listfind'}; {'id': '284', 'type': 'argument_list', 'children': ['285', '286']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'easy_install_list_'}; {'id': '286', 'type': 'attribute', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'repo'}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'reponame'}, {'id': '289', 'type': 'expression_statement', 'children': ['290']}; {'id': '290', 'type': 'assignment', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'index2'}; {'id': '292', 'type': 'call', 'children': ['293', '296']}, {'id': '293', 'type': 'attribute', 'children': ['294', '295']}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'listfind'}; {'id': '296', 'type': 'argument_list', 'children': ['297', '298']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'easy_install_list_'}; {'id': '298', 'type': 'attribute', 'children': ['299', '300']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'repo'}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'modname'}, {'id': '301', 'type': 'if_statement', 'children': ['302', '309']}; {'id': '302', 'type': 'boolean_operator', 'children': ['303', '306'], 'value': 'or'}, {'id': '303', 'type': 'comparison_operator', 'children': ['304', '305'], 'value': 'is'}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'index1'}, {'id': '305', 'type': 'None', 'children': []}; {'id': '306', 'type': 'comparison_operator', 'children': ['307', '308'], 'value': 'is'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'index2'}; {'id': '308', 'type': 'None', 'children': []}, {'id': '309', 'type': 'block', 'children': ['310', '319']}; {'id': '310', 'type': 'expression_statement', 'children': ['311']}, {'id': '311', 'type': 'call', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '313', 'type': 'argument_list', 'children': ['314']}; {'id': '314', 'type': 'binary_operator', 'children': ['315', '316'], 'value': '%'}, {'id': '315', 'type': 'string', 'children': [], 'value': "'Found at index1=%r, index=%r'"}; {'id': '316', 'type': 'tuple', 'children': ['317', '318']}, {'id': '317', 'type': 'identifier', 'children': [], 'value': 'index1'}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'index2'}, {'id': '319', 'type': 'if_statement', 'children': ['320', '330', '343']}; {'id': '320', 'type': 'comparison_operator', 'children': ['321', '329'], 'value': '!='}, {'id': '321', 'type': 'subscript', 'children': ['322', '328']}; {'id': '322', 'type': 'call', 'children': ['323', '326']}, {'id': '323', 'type': 'attribute', 'children': ['324', '325']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'get_file_info'}; {'id': '326', 'type': 'argument_list', 'children': ['327']}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '328', 'type': 'string', 'children': [], 'value': "'owner'"}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'user'}; {'id': '330', 'type': 'block', 'children': ['331']}, {'id': '331', 'type': 'expression_statement', 'children': ['332']}; {'id': '332', 'type': 'call', 'children': ['333', '334']}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '334', 'type': 'argument_list', 'children': ['335']}, {'id': '335', 'type': 'call', 'children': ['336', '339']}; {'id': '336', 'type': 'attribute', 'children': ['337', '338']}, {'id': '337', 'type': 'string', 'children': [], 'value': "'sudo gvim {path}'"}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '339', 'type': 'argument_list', 'children': ['340']}; {'id': '340', 'type': 'keyword_argument', 'children': ['341', '342']}, {'id': '341', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '342', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '343', 'type': 'else_clause', 'children': ['344']}; {'id': '344', 'type': 'block', 'children': ['345']}, {'id': '345', 'type': 'expression_statement', 'children': ['346']}; {'id': '346', 'type': 'call', 'children': ['347', '348']}, {'id': '347', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '348', 'type': 'argument_list', 'children': ['349']}, {'id': '349', 'type': 'call', 'children': ['350', '353']}; {'id': '350', 'type': 'attribute', 'children': ['351', '352']}, {'id': '351', 'type': 'string', 'children': [], 'value': "'gvim {path}'"}; {'id': '352', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '353', 'type': 'argument_list', 'children': ['354']}; {'id': '354', 'type': 'keyword_argument', 'children': ['355', '356']}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '356', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '357', 'type': 'expression_statement', 'children': ['358']}; {'id': '358', 'type': 'assignment', 'children': ['359', '360']}, {'id': '359', 'type': 'identifier', 'children': [], 'value': 'checkcmds'}; {'id': '360', 'type': 'list_comprehension', 'children': ['361', '368']}, {'id': '361', 'type': 'call', 'children': ['362', '365']}; {'id': '362', 'type': 'attribute', 'children': ['363', '364']}, {'id': '363', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '365', 'type': 'argument_list', 'children': ['366']}; {'id': '366', 'type': 'dictionary_splat', 'children': ['367']}, {'id': '367', 'type': 'identifier', 'children': [], 'value': 'fmtdict'}; {'id': '368', 'type': 'for_in_clause', 'children': ['369', '370']}, {'id': '369', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '370', 'type': 'list', 'children': ['371'], 'value': '[\n \'python -c "import {modname}; print({modname}.__file__)"\'\n ]'}, {'id': '371', 'type': 'string', 'children': [], 'value': '\'python -c "import {modname}; print({modname}.__file__)"\''}; {'id': '372', 'type': 'import_statement', 'children': ['373']}, {'id': '373', 'type': 'dotted_name', 'children': ['374']}; {'id': '374', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '375', 'type': 'assert_statement', 'children': ['376']}; {'id': '376', 'type': 'comparison_operator', 'children': ['377', '380'], 'value': 'not'}, {'id': '377', 'type': 'attribute', 'children': ['378', '379']}; {'id': '378', 'type': 'identifier', 'children': [], 'value': 'repo'}, {'id': '379', 'type': 'identifier', 'children': [], 'value': 'modname'}; {'id': '380', 'type': 'attribute', 'children': ['381', '382']}, {'id': '381', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '382', 'type': 'identifier', 'children': [], 'value': 'modules'}, {'id': '383', 'type': 'expression_statement', 'children': ['384']}; {'id': '384', 'type': 'assignment', 'children': ['385', '386', '392']}, {'id': '385', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '386', 'type': 'ERROR', 'children': ['387', '389']}, {'id': '387', 'type': 'ERROR', 'children': ['388']}; {'id': '388', 'type': 'identifier', 'children': [], 'value': 'for'}, {'id': '389', 'type': 'comparison_operator', 'children': ['390', '391'], 'value': 'in'}; {'id': '390', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '391', 'type': 'identifier', 'children': [], 'value': 'checkcmds'}; {'id': '392', 'type': 'type', 'children': ['393']}, {'id': '393', 'type': 'call', 'children': ['394', '395']}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '395', 'type': 'argument_list', 'children': ['396']} | def total_purge_developed_repo(repodir):
r
assert repodir is not None
import utool as ut
import os
repo = ut.util_git.Repo(dpath=repodir)
user = os.environ['USER']
fmtdict = dict(
user=user,
modname=repo.modname,
reponame=repo.reponame,
dpath=repo.dpath,
global_site_pkgs=ut.get_global_dist_packages_dir(),
local_site_pkgs=ut.get_local_dist_packages_dir(),
venv_site_pkgs=ut.get_site_packages_dir(),
)
commands = [_.format(**fmtdict) for _ in [
'pip uninstall {modname}',
'sudo -H pip uninstall {modname}',
'sudo pip uninstall {modname}',
'easy_install -m {modname}',
'cd {dpath} && python setup.py develop --uninstall',
'sudo chown -R {user}:{user} {dpath}',
]]
print('Normal uninstall commands')
print('\n'.join(commands))
possible_link_paths = [_.format(**fmtdict) for _ in [
'{dpath}/{modname}.egg-info',
'{dpath}/build',
'{venv_site_pkgs}/{reponame}.egg-info',
'{local_site_pkgs}/{reponame}.egg-info',
'{venv_site_pkgs}/{reponame}.egg-info',
]]
from os.path import exists, basename
existing_link_paths = [path for path in possible_link_paths]
print('
for path in existing_link_paths:
if exists(path):
if ut.get_file_info(path)['owner'] != user:
print('sudo /bin/rm -rf {path}'.format(path=path))
else:
print('/bin/rm -rf {path}'.format(path=path))
print('
easyinstall_paths = [_.format(**fmtdict) for _ in [
'{venv_site_pkgs}/easy-install.pth',
'{local_site_pkgs}/easy-install.pth',
'{venv_site_pkgs}/easy-install.pth',
]]
for path in easyinstall_paths:
if exists(path):
easy_install_list = ut.readfrom(path, verbose=False).strip().split('\n')
easy_install_list_ = [basename(p) for p in easy_install_list]
index1 = ut.listfind(easy_install_list_, repo.reponame)
index2 = ut.listfind(easy_install_list_, repo.modname)
if index1 is not None or index2 is not None:
print('Found at index1=%r, index=%r' % (index1, index2))
if ut.get_file_info(path)['owner'] != user:
print('sudo gvim {path}'.format(path=path))
else:
print('gvim {path}'.format(path=path))
checkcmds = [_.format(**fmtdict) for _ in [
'python -c "import {modname}; print({modname}.__file__)"'
]]
import sys
assert repo.modname not in sys.modules
print("
for cmd in checkcmds:
print(cmd) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'nx_dag_node_rank'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '14', '28', '50', '60']}, {'id': '9', 'type': 'import_statement', 'children': ['10']}; {'id': '10', 'type': 'aliased_import', 'children': ['11', '13']}, {'id': '11', 'type': 'dotted_name', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '14', 'type': 'expression_statement', 'children': ['15']}, {'id': '15', 'type': 'assignment', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '17', 'type': 'subscript', 'children': ['18', '27']}; {'id': '18', 'type': 'call', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '20', 'type': 'argument_list', 'children': ['21']}, {'id': '21', 'type': 'call', 'children': ['22', '25']}; {'id': '22', 'type': 'attribute', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'nx_source_nodes'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '27', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '28', 'type': 'expression_statement', 'children': ['29']}, {'id': '29', 'type': 'assignment', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'longest_paths'}, {'id': '31', 'type': 'call', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '33', 'type': 'argument_list', 'children': ['34']}; {'id': '34', 'type': 'list_comprehension', 'children': ['35', '43']}, {'id': '35', 'type': 'tuple', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'dag_longest_path'}, {'id': '39', 'type': 'argument_list', 'children': ['40', '41', '42']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '43', 'type': 'for_in_clause', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '49', 'type': 'argument_list', 'children': []}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'node_to_rank'}, {'id': '53', 'type': 'call', 'children': ['54', '57']}; {'id': '54', 'type': 'attribute', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'map_dict_vals'}, {'id': '57', 'type': 'argument_list', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'longest_paths'}; {'id': '60', 'type': 'if_statement', 'children': ['61', '64', '67']}, {'id': '61', 'type': 'comparison_operator', 'children': ['62', '63'], 'value': 'is'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '63', 'type': 'None', 'children': []}; {'id': '64', 'type': 'block', 'children': ['65']}, {'id': '65', 'type': 'return_statement', 'children': ['66']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'node_to_rank'}, {'id': '67', 'type': 'else_clause', 'children': ['68']}; {'id': '68', 'type': 'block', 'children': ['69', '79']}, {'id': '69', 'type': 'expression_statement', 'children': ['70']}; {'id': '70', 'type': 'assignment', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'ranks'}; {'id': '72', 'type': 'call', 'children': ['73', '76']}, {'id': '73', 'type': 'attribute', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'dict_take'}; {'id': '76', 'type': 'argument_list', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'node_to_rank'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '79', 'type': 'return_statement', 'children': ['80']} | def nx_dag_node_rank(graph, nodes=None):
import utool as ut
source = list(ut.nx_source_nodes(graph))[0]
longest_paths = dict([(target, dag_longest_path(graph, source, target))
for target in graph.nodes()])
node_to_rank = ut.map_dict_vals(len, longest_paths)
if nodes is None:
return node_to_rank
else:
ranks = ut.dict_take(node_to_rank, nodes)
return ranks |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '16']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'nx_all_simple_edge_paths'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '10', '13']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'cutoff'}, {'id': '9', 'type': 'None', 'children': []}; {'id': '10', 'type': 'default_parameter', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '12', 'type': 'False', 'children': []}, {'id': '13', 'type': 'default_parameter', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '15', 'type': 'False', 'children': []}; {'id': '16', 'type': 'block', 'children': ['17', '31', '37', '42', '45', '50', '54', '94', '105']}, {'id': '17', 'type': 'if_statement', 'children': ['18', '21']}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': 'is'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'cutoff'}; {'id': '20', 'type': 'None', 'children': []}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'cutoff'}, {'id': '25', 'type': 'binary_operator', 'children': ['26', '30'], 'value': '-'}; {'id': '26', 'type': 'call', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '28', 'type': 'argument_list', 'children': ['29']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '30', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '31', 'type': 'if_statement', 'children': ['32', '35']}; {'id': '32', 'type': 'comparison_operator', 'children': ['33', '34'], 'value': '<'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'cutoff'}; {'id': '34', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '35', 'type': 'block', 'children': ['36']}; {'id': '36', 'type': 'return_statement', 'children': []}, {'id': '37', 'type': 'import_statement', 'children': ['38']}; {'id': '38', 'type': 'aliased_import', 'children': ['39', '41']}, {'id': '39', 'type': 'dotted_name', 'children': ['40']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '42', 'type': 'import_statement', 'children': ['43']}, {'id': '43', 'type': 'dotted_name', 'children': ['44']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '48', 'type': 'list', 'children': ['49'], 'value': '[source]'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}, {'id': '53', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '54', 'type': 'if_statement', 'children': ['55', '60', '78']}, {'id': '55', 'type': 'call', 'children': ['56', '59']}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'is_multigraph'}, {'id': '59', 'type': 'argument_list', 'children': []}; {'id': '60', 'type': 'block', 'children': ['61']}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'get_neighbs'}; {'id': '64', 'type': 'call', 'children': ['65', '68']}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'partial'}; {'id': '68', 'type': 'argument_list', 'children': ['69', '72', '75']}, {'id': '69', 'type': 'attribute', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '72', 'type': 'keyword_argument', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '75', 'type': 'keyword_argument', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '78', 'type': 'else_clause', 'children': ['79']}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'get_neighbs'}, {'id': '83', 'type': 'call', 'children': ['84', '87']}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'partial'}, {'id': '87', 'type': 'argument_list', 'children': ['88', '91']}; {'id': '88', 'type': 'attribute', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '91', 'type': 'keyword_argument', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}, {'id': '97', 'type': 'list', 'children': ['98'], 'value': '[iter(get_neighbs(source))]'}; {'id': '98', 'type': 'call', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'iter'}; {'id': '100', 'type': 'argument_list', 'children': ['101']}, {'id': '101', 'type': 'call', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'get_neighbs'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '105', 'type': 'while_statement', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}, {'id': '107', 'type': 'block', 'children': ['108', '115', '125']}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'children_edges'}, {'id': '111', 'type': 'subscript', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}, {'id': '113', 'type': 'unary_operator', 'children': ['114'], 'value': '-'}; {'id': '114', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '115', 'type': 'expression_statement', 'children': ['116']}; {'id': '116', 'type': 'assignment', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'child_edge'}; {'id': '118', 'type': 'call', 'children': ['119', '122']}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'six'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'next'}; {'id': '122', 'type': 'argument_list', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'children_edges'}; {'id': '124', 'type': 'None', 'children': []}, {'id': '125', 'type': 'if_statement', 'children': ['126', '129', '156', '213']}; {'id': '126', 'type': 'comparison_operator', 'children': ['127', '128'], 'value': 'is'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'child_edge'}; {'id': '128', 'type': 'None', 'children': []}, {'id': '129', 'type': 'block', 'children': ['130', '136', '142']}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'call', 'children': ['132', '135']}; {'id': '132', 'type': 'attribute', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '135', 'type': 'argument_list', 'children': []}; {'id': '136', 'type': 'expression_statement', 'children': ['137']}, {'id': '137', 'type': 'call', 'children': ['138', '141']}; {'id': '138', 'type': 'attribute', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '141', 'type': 'argument_list', 'children': []}; {'id': '142', 'type': 'if_statement', 'children': ['143', '149']}, {'id': '143', 'type': 'comparison_operator', 'children': ['144', '148'], 'value': '>'}; {'id': '144', 'type': 'call', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '146', 'type': 'argument_list', 'children': ['147']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}; {'id': '148', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '149', 'type': 'block', 'children': ['150']}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'call', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '155', 'type': 'argument_list', 'children': []}; {'id': '156', 'type': 'elif_clause', 'children': ['157', '163']}, {'id': '157', 'type': 'comparison_operator', 'children': ['158', '162'], 'value': '<'}; {'id': '158', 'type': 'call', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'cutoff'}, {'id': '163', 'type': 'block', 'children': ['164', '170']}; {'id': '164', 'type': 'expression_statement', 'children': ['165']}, {'id': '165', 'type': 'assignment', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '167', 'type': 'subscript', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'child_edge'}, {'id': '169', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '170', 'type': 'if_statement', 'children': ['171', '174', '181']}, {'id': '171', 'type': 'comparison_operator', 'children': ['172', '173'], 'value': '=='}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'target'}; {'id': '174', 'type': 'block', 'children': ['175']}, {'id': '175', 'type': 'expression_statement', 'children': ['176']}; {'id': '176', 'type': 'yield', 'children': ['177']}, {'id': '177', 'type': 'binary_operator', 'children': ['178', '179'], 'value': '+'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}, {'id': '179', 'type': 'list', 'children': ['180'], 'value': '[child_edge]'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'child_edge'}, {'id': '181', 'type': 'elif_clause', 'children': ['182', '185']}; {'id': '182', 'type': 'comparison_operator', 'children': ['183', '184'], 'value': 'not'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'child_node'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}, {'id': '185', 'type': 'block', 'children': ['186', '193', '200']}; {'id': '186', 'type': 'expression_statement', 'children': ['187']}, {'id': '187', 'type': 'call', 'children': ['188', '191']}; {'id': '188', 'type': 'attribute', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '193', 'type': 'expression_statement', 'children': ['194']}; {'id': '194', 'type': 'call', 'children': ['195', '198']}, {'id': '195', 'type': 'attribute', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '198', 'type': 'argument_list', 'children': ['199']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'child_edge'}; {'id': '200', 'type': 'expression_statement', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '205']}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '205', 'type': 'argument_list', 'children': ['206']}; {'id': '206', 'type': 'call', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'iter'}; {'id': '208', 'type': 'argument_list', 'children': ['209']}, {'id': '209', 'type': 'call', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'get_neighbs'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '213', 'type': 'else_clause', 'children': ['214']}; {'id': '214', 'type': 'block', 'children': ['215', '238', '244', '250']}, {'id': '215', 'type': 'for_statement', 'children': ['216', '217', '224']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '217', 'type': 'binary_operator', 'children': ['218', '220'], 'value': '+'}; {'id': '218', 'type': 'list', 'children': ['219'], 'value': '[child_edge]'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'child_edge'}; {'id': '220', 'type': 'call', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '222', 'type': 'argument_list', 'children': ['223']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'children_edges'}; {'id': '224', 'type': 'block', 'children': ['225']}, {'id': '225', 'type': 'if_statement', 'children': ['226', '231']}; {'id': '226', 'type': 'comparison_operator', 'children': ['227', '230'], 'value': '=='}, {'id': '227', 'type': 'subscript', 'children': ['228', '229']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '229', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '231', 'type': 'block', 'children': ['232']}; {'id': '232', 'type': 'expression_statement', 'children': ['233']}, {'id': '233', 'type': 'yield', 'children': ['234']}; {'id': '234', 'type': 'binary_operator', 'children': ['235', '236'], 'value': '+'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}; {'id': '236', 'type': 'list', 'children': ['237'], 'value': '[edge]'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '238', 'type': 'expression_statement', 'children': ['239']}, {'id': '239', 'type': 'call', 'children': ['240', '243']}; {'id': '240', 'type': 'attribute', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'edge_stack'}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '243', 'type': 'argument_list', 'children': []}; {'id': '244', 'type': 'expression_statement', 'children': ['245']}, {'id': '245', 'type': 'call', 'children': ['246', '249']}; {'id': '246', 'type': 'attribute', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '249', 'type': 'argument_list', 'children': []}; {'id': '250', 'type': 'if_statement', 'children': ['251', '257']}, {'id': '251', 'type': 'comparison_operator', 'children': ['252', '256'], 'value': '>'}; {'id': '252', 'type': 'call', 'children': ['253', '254']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '254', 'type': 'argument_list', 'children': ['255']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}; {'id': '256', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '257', 'type': 'block', 'children': ['258']}; {'id': '258', 'type': 'expression_statement', 'children': ['259']}, {'id': '259', 'type': 'call', 'children': ['260', '263']}; {'id': '260', 'type': 'attribute', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'visited_edges'}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '263', 'type': 'argument_list', 'children': []} | def nx_all_simple_edge_paths(G, source, target, cutoff=None, keys=False,
data=False):
if cutoff is None:
cutoff = len(G) - 1
if cutoff < 1:
return
import utool as ut
import six
visited_nodes = [source]
visited_edges = []
if G.is_multigraph():
get_neighbs = ut.partial(G.edges, keys=keys, data=data)
else:
get_neighbs = ut.partial(G.edges, data=data)
edge_stack = [iter(get_neighbs(source))]
while edge_stack:
children_edges = edge_stack[-1]
child_edge = six.next(children_edges, None)
if child_edge is None:
edge_stack.pop()
visited_nodes.pop()
if len(visited_edges) > 0:
visited_edges.pop()
elif len(visited_nodes) < cutoff:
child_node = child_edge[1]
if child_node == target:
yield visited_edges + [child_edge]
elif child_node not in visited_nodes:
visited_nodes.append(child_node)
visited_edges.append(child_edge)
edge_stack.append(iter(get_neighbs(child_node)))
else:
for edge in [child_edge] + list(children_edges):
if edge[1] == target:
yield visited_edges + [edge]
edge_stack.pop()
visited_nodes.pop()
if len(visited_edges) > 0:
visited_edges.pop() |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '20']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'nx_gen_node_attrs'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '14', '17']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'default'}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'util_const'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'NoParam'}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '16', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '19', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '20', 'type': 'block', 'children': ['21', '30', '45', '58', '65', '136', '213']}, {'id': '21', 'type': 'if_statement', 'children': ['22', '25']}; {'id': '22', 'type': 'comparison_operator', 'children': ['23', '24'], 'value': 'is'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '24', 'type': 'None', 'children': []}, {'id': '25', 'type': 'block', 'children': ['26']}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'assignment', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '29', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '30', 'type': 'if_statement', 'children': ['31', '40']}, {'id': '31', 'type': 'boolean_operator', 'children': ['32', '37'], 'value': 'and'}; {'id': '32', 'type': 'comparison_operator', 'children': ['33', '34'], 'value': 'is'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '34', 'type': 'attribute', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'util_const'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'NoParam'}, {'id': '37', 'type': 'comparison_operator', 'children': ['38', '39'], 'value': '=='}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '39', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '44', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '45', 'type': 'if_statement', 'children': ['46', '49']}; {'id': '46', 'type': 'comparison_operator', 'children': ['47', '48'], 'value': 'is'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '48', 'type': 'None', 'children': []}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '53', 'type': 'call', 'children': ['54', '57']}; {'id': '54', 'type': 'attribute', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '57', 'type': 'argument_list', 'children': []}; {'id': '58', 'type': 'expression_statement', 'children': ['59']}, {'id': '59', 'type': 'assignment', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'node_dict'}, {'id': '61', 'type': 'call', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'nx_node_dict'}, {'id': '63', 'type': 'argument_list', 'children': ['64']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '65', 'type': 'if_statement', 'children': ['66', '69', '82', '103', '124']}; {'id': '66', 'type': 'comparison_operator', 'children': ['67', '68'], 'value': '=='}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '68', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'assignment', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'node_data'}, {'id': '73', 'type': 'generator_expression', 'children': ['74', '79']}; {'id': '74', 'type': 'tuple', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '76', 'type': 'subscript', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'node_dict'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '79', 'type': 'for_in_clause', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '82', 'type': 'elif_clause', 'children': ['83', '86']}, {'id': '83', 'type': 'comparison_operator', 'children': ['84', '85'], 'value': '=='}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '85', 'type': 'string', 'children': [], 'value': "'filter'"}; {'id': '86', 'type': 'block', 'children': ['87']}, {'id': '87', 'type': 'expression_statement', 'children': ['88']}; {'id': '88', 'type': 'assignment', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'node_data'}; {'id': '90', 'type': 'generator_expression', 'children': ['91', '96', '99']}, {'id': '91', 'type': 'tuple', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '93', 'type': 'subscript', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'node_dict'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '96', 'type': 'for_in_clause', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '99', 'type': 'if_clause', 'children': ['100']}; {'id': '100', 'type': 'comparison_operator', 'children': ['101', '102'], 'value': 'in'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '103', 'type': 'elif_clause', 'children': ['104', '107']}; {'id': '104', 'type': 'comparison_operator', 'children': ['105', '106'], 'value': '=='}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '106', 'type': 'string', 'children': [], 'value': "'default'"}, {'id': '107', 'type': 'block', 'children': ['108']}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'node_data'}, {'id': '111', 'type': 'generator_expression', 'children': ['112', '121']}; {'id': '112', 'type': 'tuple', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '114', 'type': 'call', 'children': ['115', '118']}, {'id': '115', 'type': 'attribute', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'node_dict'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '118', 'type': 'argument_list', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '120', 'type': 'dictionary', 'children': []}, {'id': '121', 'type': 'for_in_clause', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '124', 'type': 'else_clause', 'children': ['125']}, {'id': '125', 'type': 'block', 'children': ['126']}; {'id': '126', 'type': 'raise_statement', 'children': ['127']}, {'id': '127', 'type': 'call', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'call', 'children': ['131', '134']}, {'id': '131', 'type': 'attribute', 'children': ['132', '133']}; {'id': '132', 'type': 'string', 'children': [], 'value': "'on_missing={} must be error, filter or default'"}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '134', 'type': 'argument_list', 'children': ['135']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '136', 'type': 'if_statement', 'children': ['137', '140', '155', '178', '201']}, {'id': '137', 'type': 'comparison_operator', 'children': ['138', '139'], 'value': '=='}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '139', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '140', 'type': 'block', 'children': ['141']}, {'id': '141', 'type': 'expression_statement', 'children': ['142']}; {'id': '142', 'type': 'assignment', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'node_attrs'}; {'id': '144', 'type': 'generator_expression', 'children': ['145', '150']}, {'id': '145', 'type': 'tuple', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '147', 'type': 'subscript', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '150', 'type': 'for_in_clause', 'children': ['151', '154']}, {'id': '151', 'type': 'pattern_list', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'node_data'}, {'id': '155', 'type': 'elif_clause', 'children': ['156', '159']}; {'id': '156', 'type': 'comparison_operator', 'children': ['157', '158'], 'value': '=='}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '158', 'type': 'string', 'children': [], 'value': "'filter'"}, {'id': '159', 'type': 'block', 'children': ['160']}; {'id': '160', 'type': 'expression_statement', 'children': ['161']}, {'id': '161', 'type': 'assignment', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'node_attrs'}, {'id': '163', 'type': 'generator_expression', 'children': ['164', '169', '174']}; {'id': '164', 'type': 'tuple', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '166', 'type': 'subscript', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '169', 'type': 'for_in_clause', 'children': ['170', '173']}; {'id': '170', 'type': 'pattern_list', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'node_data'}; {'id': '174', 'type': 'if_clause', 'children': ['175']}, {'id': '175', 'type': 'comparison_operator', 'children': ['176', '177'], 'value': 'in'}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '178', 'type': 'elif_clause', 'children': ['179', '182']}, {'id': '179', 'type': 'comparison_operator', 'children': ['180', '181'], 'value': '=='}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '181', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '182', 'type': 'block', 'children': ['183']}, {'id': '183', 'type': 'expression_statement', 'children': ['184']}; {'id': '184', 'type': 'assignment', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'node_attrs'}; {'id': '186', 'type': 'generator_expression', 'children': ['187', '196']}, {'id': '187', 'type': 'tuple', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '189', 'type': 'call', 'children': ['190', '193']}; {'id': '190', 'type': 'attribute', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '193', 'type': 'argument_list', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '196', 'type': 'for_in_clause', 'children': ['197', '200']}, {'id': '197', 'type': 'pattern_list', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'node_data'}, {'id': '201', 'type': 'else_clause', 'children': ['202']}; {'id': '202', 'type': 'block', 'children': ['203']}, {'id': '203', 'type': 'raise_statement', 'children': ['204']}; {'id': '204', 'type': 'call', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'KeyError'}; {'id': '206', 'type': 'argument_list', 'children': ['207']}, {'id': '207', 'type': 'call', 'children': ['208', '211']}; {'id': '208', 'type': 'attribute', 'children': ['209', '210']}, {'id': '209', 'type': 'string', 'children': [], 'value': "'on_keyerr={} must be error filter or default'"}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '213', 'type': 'return_statement', 'children': ['214']} | def nx_gen_node_attrs(G, key, nodes=None, default=util_const.NoParam,
on_missing='error', on_keyerr='default'):
if on_missing is None:
on_missing = 'error'
if default is util_const.NoParam and on_keyerr == 'default':
on_keyerr = 'error'
if nodes is None:
nodes = G.nodes()
node_dict = nx_node_dict(G)
if on_missing == 'error':
node_data = ((n, node_dict[n]) for n in nodes)
elif on_missing == 'filter':
node_data = ((n, node_dict[n]) for n in nodes if n in G)
elif on_missing == 'default':
node_data = ((n, node_dict.get(n, {})) for n in nodes)
else:
raise KeyError('on_missing={} must be error, filter or default'.format(
on_missing))
if on_keyerr == 'error':
node_attrs = ((n, d[key]) for n, d in node_data)
elif on_keyerr == 'filter':
node_attrs = ((n, d[key]) for n, d in node_data if key in d)
elif on_keyerr == 'default':
node_attrs = ((n, d.get(key, default)) for n, d in node_data)
else:
raise KeyError('on_keyerr={} must be error filter or default'.format(on_keyerr))
return node_attrs |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '20']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'nx_gen_edge_values'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '14', '17']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'default'}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'util_const'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'NoParam'}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '16', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '19', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '20', 'type': 'block', 'children': ['21', '34', '43', '52', '67', '130', '176']}, {'id': '21', 'type': 'if_statement', 'children': ['22', '25']}; {'id': '22', 'type': 'comparison_operator', 'children': ['23', '24'], 'value': 'is'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '24', 'type': 'None', 'children': []}, {'id': '25', 'type': 'block', 'children': ['26']}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'assignment', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '29', 'type': 'call', 'children': ['30', '33']}; {'id': '30', 'type': 'attribute', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '33', 'type': 'argument_list', 'children': []}; {'id': '34', 'type': 'if_statement', 'children': ['35', '38']}, {'id': '35', 'type': 'comparison_operator', 'children': ['36', '37'], 'value': 'is'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '37', 'type': 'None', 'children': []}; {'id': '38', 'type': 'block', 'children': ['39']}, {'id': '39', 'type': 'expression_statement', 'children': ['40']}; {'id': '40', 'type': 'assignment', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '42', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '43', 'type': 'if_statement', 'children': ['44', '47']}; {'id': '44', 'type': 'comparison_operator', 'children': ['45', '46'], 'value': 'is'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '46', 'type': 'None', 'children': []}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '51', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '52', 'type': 'if_statement', 'children': ['53', '62']}, {'id': '53', 'type': 'boolean_operator', 'children': ['54', '59'], 'value': 'and'}; {'id': '54', 'type': 'comparison_operator', 'children': ['55', '56'], 'value': 'is'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'util_const'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'NoParam'}, {'id': '59', 'type': 'comparison_operator', 'children': ['60', '61'], 'value': '=='}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '61', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '62', 'type': 'block', 'children': ['63']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '66', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '67', 'type': 'if_statement', 'children': ['68', '71', '88', '118']}; {'id': '68', 'type': 'comparison_operator', 'children': ['69', '70'], 'value': '=='}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '70', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '71', 'type': 'block', 'children': ['72']}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'data_iter'}, {'id': '75', 'type': 'generator_expression', 'children': ['76', '83']}; {'id': '76', 'type': 'subscript', 'children': ['77', '82']}, {'id': '77', 'type': 'subscript', 'children': ['78', '81']}; {'id': '78', 'type': 'attribute', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'adj'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '83', 'type': 'for_in_clause', 'children': ['84', '87']}; {'id': '84', 'type': 'pattern_list', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '88', 'type': 'elif_clause', 'children': ['89', '92']}, {'id': '89', 'type': 'comparison_operator', 'children': ['90', '91'], 'value': '=='}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '91', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '92', 'type': 'block', 'children': ['93']}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'data_iter'}; {'id': '96', 'type': 'generator_expression', 'children': ['97', '113']}, {'id': '97', 'type': 'conditional_expression', 'children': ['98', '105', '112'], 'value': 'if'}; {'id': '98', 'type': 'subscript', 'children': ['99', '104']}, {'id': '99', 'type': 'subscript', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'adj'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'has_edge'}, {'id': '109', 'type': 'argument_list', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '112', 'type': 'dictionary', 'children': []}, {'id': '113', 'type': 'for_in_clause', 'children': ['114', '117']}; {'id': '114', 'type': 'pattern_list', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '118', 'type': 'else_clause', 'children': ['119']}, {'id': '119', 'type': 'block', 'children': ['120']}; {'id': '120', 'type': 'raise_statement', 'children': ['121']}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '123', 'type': 'argument_list', 'children': ['124']}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'string', 'children': [], 'value': "'on_missing={} must be error, filter or default'"}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '128', 'type': 'argument_list', 'children': ['129']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '130', 'type': 'if_statement', 'children': ['131', '134', '145', '164']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '133'], 'value': '=='}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '133', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '134', 'type': 'block', 'children': ['135']}, {'id': '135', 'type': 'expression_statement', 'children': ['136']}; {'id': '136', 'type': 'assignment', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'value_iter'}; {'id': '138', 'type': 'generator_expression', 'children': ['139', '142']}, {'id': '139', 'type': 'subscript', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '142', 'type': 'for_in_clause', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'data_iter'}, {'id': '145', 'type': 'elif_clause', 'children': ['146', '149']}; {'id': '146', 'type': 'comparison_operator', 'children': ['147', '148'], 'value': '=='}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '148', 'type': 'string', 'children': [], 'value': "'default'"}, {'id': '149', 'type': 'block', 'children': ['150']}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'assignment', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'value_iter'}, {'id': '153', 'type': 'generator_expression', 'children': ['154', '161']}; {'id': '154', 'type': 'call', 'children': ['155', '158']}, {'id': '155', 'type': 'attribute', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '158', 'type': 'argument_list', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'default'}, {'id': '161', 'type': 'for_in_clause', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'data_iter'}; {'id': '164', 'type': 'else_clause', 'children': ['165']}, {'id': '165', 'type': 'block', 'children': ['166']}; {'id': '166', 'type': 'raise_statement', 'children': ['167']}, {'id': '167', 'type': 'call', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '169', 'type': 'argument_list', 'children': ['170']}; {'id': '170', 'type': 'call', 'children': ['171', '174']}, {'id': '171', 'type': 'attribute', 'children': ['172', '173']}; {'id': '172', 'type': 'string', 'children': [], 'value': "'on_keyerr={} must be error or default'"}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '174', 'type': 'argument_list', 'children': ['175']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '176', 'type': 'return_statement', 'children': ['177']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'value_iter'} | def nx_gen_edge_values(G, key, edges=None, default=util_const.NoParam,
on_missing='error', on_keyerr='default'):
if edges is None:
edges = G.edges()
if on_missing is None:
on_missing = 'error'
if on_keyerr is None:
on_keyerr = 'default'
if default is util_const.NoParam and on_keyerr == 'default':
on_keyerr = 'error'
if on_missing == 'error':
data_iter = (G.adj[u][v] for u, v in edges)
elif on_missing == 'default':
data_iter = (G.adj[u][v] if G.has_edge(u, v) else {}
for u, v in edges)
else:
raise KeyError('on_missing={} must be error, filter or default'.format(
on_missing))
if on_keyerr == 'error':
value_iter = (d[key] for d in data_iter)
elif on_keyerr == 'default':
value_iter = (d.get(key, default) for d in data_iter)
else:
raise KeyError('on_keyerr={} must be error or default'.format(on_keyerr))
return value_iter |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '20']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'nx_gen_edge_attrs'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '14', '17']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'default'}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'util_const'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'NoParam'}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '16', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '19', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '20', 'type': 'block', 'children': ['21', '30', '45', '72', '180', '257']}, {'id': '21', 'type': 'if_statement', 'children': ['22', '25']}; {'id': '22', 'type': 'comparison_operator', 'children': ['23', '24'], 'value': 'is'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '24', 'type': 'None', 'children': []}, {'id': '25', 'type': 'block', 'children': ['26']}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'assignment', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '29', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '30', 'type': 'if_statement', 'children': ['31', '40']}, {'id': '31', 'type': 'boolean_operator', 'children': ['32', '37'], 'value': 'and'}; {'id': '32', 'type': 'comparison_operator', 'children': ['33', '34'], 'value': 'is'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '34', 'type': 'attribute', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'util_const'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'NoParam'}, {'id': '37', 'type': 'comparison_operator', 'children': ['38', '39'], 'value': '=='}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '39', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '44', 'type': 'string', 'children': [], 'value': "'error'"}, {'id': '45', 'type': 'if_statement', 'children': ['46', '49']}; {'id': '46', 'type': 'comparison_operator', 'children': ['47', '48'], 'value': 'is'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '48', 'type': 'None', 'children': []}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'if_statement', 'children': ['51', '56', '62']}, {'id': '51', 'type': 'call', 'children': ['52', '55']}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'is_multigraph'}, {'id': '55', 'type': 'argument_list', 'children': []}; {'id': '56', 'type': 'block', 'children': ['57']}, {'id': '57', 'type': 'raise_statement', 'children': ['58']}; {'id': '58', 'type': 'call', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'NotImplementedError'}; {'id': '60', 'type': 'argument_list', 'children': ['61']}, {'id': '61', 'type': 'string', 'children': [], 'value': "''"}; {'id': '62', 'type': 'else_clause', 'children': ['63']}, {'id': '63', 'type': 'block', 'children': ['64']}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '71', 'type': 'argument_list', 'children': []}; {'id': '72', 'type': 'if_statement', 'children': ['73', '76', '97', '130', '168']}, {'id': '73', 'type': 'comparison_operator', 'children': ['74', '75'], 'value': '=='}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '75', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '76', 'type': 'block', 'children': ['77']}, {'id': '77', 'type': 'expression_statement', 'children': ['78']}; {'id': '78', 'type': 'assignment', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'edge_data'}; {'id': '80', 'type': 'generator_expression', 'children': ['81', '92']}, {'id': '81', 'type': 'tuple', 'children': ['82', '85']}; {'id': '82', 'type': 'tuple', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '85', 'type': 'subscript', 'children': ['86', '91']}; {'id': '86', 'type': 'subscript', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'adj'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '92', 'type': 'for_in_clause', 'children': ['93', '96']}, {'id': '93', 'type': 'pattern_list', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '97', 'type': 'elif_clause', 'children': ['98', '101']}; {'id': '98', 'type': 'comparison_operator', 'children': ['99', '100'], 'value': '=='}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '100', 'type': 'string', 'children': [], 'value': "'filter'"}, {'id': '101', 'type': 'block', 'children': ['102']}; {'id': '102', 'type': 'expression_statement', 'children': ['103']}, {'id': '103', 'type': 'assignment', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'edge_data'}, {'id': '105', 'type': 'generator_expression', 'children': ['106', '117', '122']}; {'id': '106', 'type': 'tuple', 'children': ['107', '110']}, {'id': '107', 'type': 'tuple', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '110', 'type': 'subscript', 'children': ['111', '116']}, {'id': '111', 'type': 'subscript', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'adj'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '117', 'type': 'for_in_clause', 'children': ['118', '121']}; {'id': '118', 'type': 'pattern_list', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '122', 'type': 'if_clause', 'children': ['123']}, {'id': '123', 'type': 'call', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'has_edge'}, {'id': '127', 'type': 'argument_list', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '130', 'type': 'elif_clause', 'children': ['131', '134']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '133'], 'value': '=='}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'on_missing'}, {'id': '133', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '134', 'type': 'block', 'children': ['135']}, {'id': '135', 'type': 'expression_statement', 'children': ['136']}; {'id': '136', 'type': 'assignment', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'edge_data'}; {'id': '138', 'type': 'generator_expression', 'children': ['139', '163']}, {'id': '139', 'type': 'conditional_expression', 'children': ['140', '151', '158'], 'value': 'if'}; {'id': '140', 'type': 'tuple', 'children': ['141', '144']}, {'id': '141', 'type': 'tuple', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '144', 'type': 'subscript', 'children': ['145', '150']}, {'id': '145', 'type': 'subscript', 'children': ['146', '149']}; {'id': '146', 'type': 'attribute', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'adj'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '151', 'type': 'call', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'has_edge'}, {'id': '155', 'type': 'argument_list', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '158', 'type': 'tuple', 'children': ['159', '162']}, {'id': '159', 'type': 'tuple', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '162', 'type': 'dictionary', 'children': []}, {'id': '163', 'type': 'for_in_clause', 'children': ['164', '167']}; {'id': '164', 'type': 'pattern_list', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '168', 'type': 'else_clause', 'children': ['169']}, {'id': '169', 'type': 'block', 'children': ['170']}; {'id': '170', 'type': 'raise_statement', 'children': ['171']}, {'id': '171', 'type': 'call', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '173', 'type': 'argument_list', 'children': ['174']}; {'id': '174', 'type': 'call', 'children': ['175', '178']}, {'id': '175', 'type': 'attribute', 'children': ['176', '177']}; {'id': '176', 'type': 'string', 'children': [], 'value': "'on_missing={}'"}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '178', 'type': 'argument_list', 'children': ['179']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'on_missing'}; {'id': '180', 'type': 'if_statement', 'children': ['181', '184', '199', '222', '245']}, {'id': '181', 'type': 'comparison_operator', 'children': ['182', '183'], 'value': '=='}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '183', 'type': 'string', 'children': [], 'value': "'error'"}; {'id': '184', 'type': 'block', 'children': ['185']}, {'id': '185', 'type': 'expression_statement', 'children': ['186']}; {'id': '186', 'type': 'assignment', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'edge_attrs'}; {'id': '188', 'type': 'generator_expression', 'children': ['189', '194']}, {'id': '189', 'type': 'tuple', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '191', 'type': 'subscript', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '194', 'type': 'for_in_clause', 'children': ['195', '198']}, {'id': '195', 'type': 'pattern_list', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'edge_data'}, {'id': '199', 'type': 'elif_clause', 'children': ['200', '203']}; {'id': '200', 'type': 'comparison_operator', 'children': ['201', '202'], 'value': '=='}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}; {'id': '202', 'type': 'string', 'children': [], 'value': "'filter'"}, {'id': '203', 'type': 'block', 'children': ['204']}; {'id': '204', 'type': 'expression_statement', 'children': ['205']}, {'id': '205', 'type': 'assignment', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'edge_attrs'}, {'id': '207', 'type': 'generator_expression', 'children': ['208', '213', '218']}; {'id': '208', 'type': 'tuple', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '210', 'type': 'subscript', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '213', 'type': 'for_in_clause', 'children': ['214', '217']}; {'id': '214', 'type': 'pattern_list', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'edge_data'}; {'id': '218', 'type': 'if_clause', 'children': ['219']}, {'id': '219', 'type': 'comparison_operator', 'children': ['220', '221'], 'value': 'in'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '222', 'type': 'elif_clause', 'children': ['223', '226']}, {'id': '223', 'type': 'comparison_operator', 'children': ['224', '225'], 'value': '=='}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '225', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '226', 'type': 'block', 'children': ['227']}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'edge_attrs'}; {'id': '230', 'type': 'generator_expression', 'children': ['231', '240']}, {'id': '231', 'type': 'tuple', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '233', 'type': 'call', 'children': ['234', '237']}; {'id': '234', 'type': 'attribute', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '237', 'type': 'argument_list', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '240', 'type': 'for_in_clause', 'children': ['241', '244']}, {'id': '241', 'type': 'pattern_list', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'edge_data'}, {'id': '245', 'type': 'else_clause', 'children': ['246']}; {'id': '246', 'type': 'block', 'children': ['247']}, {'id': '247', 'type': 'raise_statement', 'children': ['248']}; {'id': '248', 'type': 'call', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'KeyError'}; {'id': '250', 'type': 'argument_list', 'children': ['251']}, {'id': '251', 'type': 'call', 'children': ['252', '255']}; {'id': '252', 'type': 'attribute', 'children': ['253', '254']}, {'id': '253', 'type': 'string', 'children': [], 'value': "'on_keyerr={}'"}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '255', 'type': 'argument_list', 'children': ['256']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'on_keyerr'}, {'id': '257', 'type': 'return_statement', 'children': ['258']} | def nx_gen_edge_attrs(G, key, edges=None, default=util_const.NoParam,
on_missing='error', on_keyerr='default'):
if on_missing is None:
on_missing = 'error'
if default is util_const.NoParam and on_keyerr == 'default':
on_keyerr = 'error'
if edges is None:
if G.is_multigraph():
raise NotImplementedError('')
else:
edges = G.edges()
if on_missing == 'error':
edge_data = (((u, v), G.adj[u][v]) for u, v in edges)
elif on_missing == 'filter':
edge_data = (((u, v), G.adj[u][v]) for u, v in edges if G.has_edge(u, v))
elif on_missing == 'default':
edge_data = (((u, v), G.adj[u][v])
if G.has_edge(u, v) else ((u, v), {})
for u, v in edges)
else:
raise KeyError('on_missing={}'.format(on_missing))
if on_keyerr == 'error':
edge_attrs = ((e, d[key]) for e, d in edge_data)
elif on_keyerr == 'filter':
edge_attrs = ((e, d[key]) for e, d in edge_data if key in d)
elif on_keyerr == 'default':
edge_attrs = ((e, d.get(key, default)) for e, d in edge_data)
else:
raise KeyError('on_keyerr={}'.format(on_keyerr))
return edge_attrs |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'ERROR', 'children': ['2', '3', '5', '10', '15', '16', '18', '188', '199']}; {'id': '2', 'type': 'identifier', 'children': [], 'value': 'nx_ensure_agraph_color'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '5', 'type': 'import_from_statement', 'children': ['6', '8']}; {'id': '6', 'type': 'dotted_name', 'children': ['7']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'plottool'}; {'id': '8', 'type': 'dotted_name', 'children': ['9']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'color_funcs'}; {'id': '10', 'type': 'import_statement', 'children': ['11']}, {'id': '11', 'type': 'aliased_import', 'children': ['12', '14']}; {'id': '12', 'type': 'dotted_name', 'children': ['13']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'plottool'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'pt'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': '_fix_agraph_color'}; {'id': '16', 'type': 'parameters', 'children': ['17']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '18', 'type': 'ERROR', 'children': ['19', '176']}, {'id': '19', 'type': 'ERROR', 'children': ['20', '174', '175']}; {'id': '20', 'type': 'block', 'children': ['21', '31', '41', '45', '61', '155', '160', '173']}, {'id': '21', 'type': 'expression_statement', 'children': ['22']}; {'id': '22', 'type': 'assignment', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'orig_color'}; {'id': '24', 'type': 'call', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '28', 'type': 'argument_list', 'children': ['29', '30']}, {'id': '29', 'type': 'string', 'children': [], 'value': "'color'"}; {'id': '30', 'type': 'None', 'children': []}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'alpha'}; {'id': '34', 'type': 'call', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '38', 'type': 'argument_list', 'children': ['39', '40']}, {'id': '39', 'type': 'string', 'children': [], 'value': "'alpha'"}; {'id': '40', 'type': 'None', 'children': []}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'orig_color'}, {'id': '45', 'type': 'if_statement', 'children': ['46', '53']}; {'id': '46', 'type': 'boolean_operator', 'children': ['47', '50'], 'value': 'and'}, {'id': '47', 'type': 'comparison_operator', 'children': ['48', '49'], 'value': 'is'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '49', 'type': 'None', 'children': []}; {'id': '50', 'type': 'comparison_operator', 'children': ['51', '52'], 'value': 'is'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'alpha'}; {'id': '52', 'type': 'None', 'children': []}, {'id': '53', 'type': 'block', 'children': ['54']}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '57', 'type': 'list', 'children': ['58', '59', '60'], 'value': '[0, 0, 0]'}; {'id': '58', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '59', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '60', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '61', 'type': 'if_statement', 'children': ['62', '65']}; {'id': '62', 'type': 'comparison_operator', 'children': ['63', '64'], 'value': 'is'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '64', 'type': 'None', 'children': []}, {'id': '65', 'type': 'block', 'children': ['66', '75', '87', '123', '130']}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'assignment', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'pt'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'ensure_nonhex_color'}, {'id': '73', 'type': 'argument_list', 'children': ['74']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '78', 'type': 'call', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'color_funcs'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'ensure_base255'}, {'id': '85', 'type': 'argument_list', 'children': ['86']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '87', 'type': 'if_statement', 'children': ['88', '91']}; {'id': '88', 'type': 'comparison_operator', 'children': ['89', '90'], 'value': 'is'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'alpha'}; {'id': '90', 'type': 'None', 'children': []}, {'id': '91', 'type': 'block', 'children': ['92']}; {'id': '92', 'type': 'if_statement', 'children': ['93', '99', '110']}, {'id': '93', 'type': 'comparison_operator', 'children': ['94', '98'], 'value': '=='}; {'id': '94', 'type': 'call', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '96', 'type': 'argument_list', 'children': ['97']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '98', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '99', 'type': 'block', 'children': ['100']}; {'id': '100', 'type': 'expression_statement', 'children': ['101']}, {'id': '101', 'type': 'augmented_assignment', 'children': ['102', '103'], 'value': '+='}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'color'}, {'id': '103', 'type': 'list', 'children': ['104'], 'value': '[int(alpha * 255)]'}; {'id': '104', 'type': 'call', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '106', 'type': 'argument_list', 'children': ['107']}, {'id': '107', 'type': 'binary_operator', 'children': ['108', '109'], 'value': '*'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'alpha'}, {'id': '109', 'type': 'integer', 'children': [], 'value': '255'}; {'id': '110', 'type': 'else_clause', 'children': ['111']}, {'id': '111', 'type': 'block', 'children': ['112']}; {'id': '112', 'type': 'expression_statement', 'children': ['113']}, {'id': '113', 'type': 'assignment', 'children': ['114', '117']}; {'id': '114', 'type': 'subscript', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '116', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '117', 'type': 'call', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'int'}, {'id': '119', 'type': 'argument_list', 'children': ['120']}; {'id': '120', 'type': 'binary_operator', 'children': ['121', '122'], 'value': '*'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'alpha'}; {'id': '122', 'type': 'integer', 'children': [], 'value': '255'}, {'id': '123', 'type': 'expression_statement', 'children': ['124']}; {'id': '124', 'type': 'assignment', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '126', 'type': 'call', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '128', 'type': 'argument_list', 'children': ['129']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '130', 'type': 'if_statement', 'children': ['131', '137', '154']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '136'], 'value': '=='}; {'id': '132', 'type': 'call', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '134', 'type': 'argument_list', 'children': ['135']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'color'}; {'id': '136', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '137', 'type': 'ERROR', 'children': ['138']}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'assignment', 'children': ['140', '143', '145']}; {'id': '140', 'type': 'subscript', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '142', 'type': 'string', 'children': [], 'value': "'color'"}, {'id': '143', 'type': 'ERROR', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'else'}, {'id': '145', 'type': 'assignment', 'children': ['146', '149', '150']}; {'id': '146', 'type': 'subscript', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '148', 'type': 'string', 'children': [], 'value': "'color'"}, {'id': '149', 'type': 'ERROR', 'children': []}; {'id': '150', 'type': 'as_pattern', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'Exception'}; {'id': '152', 'type': 'as_pattern_target', 'children': ['153']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'ex'}; {'id': '154', 'type': 'block', 'children': [], 'value': ''}, {'id': '155', 'type': 'import_statement', 'children': ['156']}; {'id': '156', 'type': 'aliased_import', 'children': ['157', '159']}, {'id': '157', 'type': 'dotted_name', 'children': ['158']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '160', 'type': 'expression_statement', 'children': ['161']}, {'id': '161', 'type': 'call', 'children': ['162', '165']}; {'id': '162', 'type': 'attribute', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'printex'}, {'id': '165', 'type': 'argument_list', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'ex'}, {'id': '167', 'type': 'keyword_argument', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '169', 'type': 'list', 'children': ['170', '171', '172'], 'value': "['color', 'orig_color', 'data']"}; {'id': '170', 'type': 'string', 'children': [], 'value': "'color'"}, {'id': '171', 'type': 'string', 'children': [], 'value': "'orig_color'"}; {'id': '172', 'type': 'string', 'children': [], 'value': "'data'"}, {'id': '173', 'type': 'raise_statement', 'children': []}; {'id': '174', 'type': 'block', 'children': [], 'value': ''}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'for'}; {'id': '176', 'type': 'expression_statement', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '178', 'type': 'comparison_operator', 'children': ['179', '180'], 'value': 'in'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'node_data'}; {'id': '180', 'type': 'call', 'children': ['181', '184']}, {'id': '181', 'type': 'attribute', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '184', 'type': 'argument_list', 'children': ['185']}, {'id': '185', 'type': 'keyword_argument', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '187', 'type': 'True', 'children': []}; {'id': '188', 'type': 'ERROR', 'children': ['189']}, {'id': '189', 'type': 'block', 'children': ['190', '194']}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'assignment', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'node_data'}; {'id': '194', 'type': 'expression_statement', 'children': ['195']}, {'id': '195', 'type': 'call', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': '_fix_agraph_color'}, {'id': '197', 'type': 'argument_list', 'children': ['198']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '199', 'type': 'block', 'children': ['200']}; {'id': '200', 'type': 'for_statement', 'children': ['201', '205', '213']}, {'id': '201', 'type': 'pattern_list', 'children': ['202', '203', '204']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'edge_data'}, {'id': '205', 'type': 'call', 'children': ['206', '209']}; {'id': '206', 'type': 'attribute', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '209', 'type': 'argument_list', 'children': ['210']}; {'id': '210', 'type': 'keyword_argument', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '212', 'type': 'True', 'children': []}, {'id': '213', 'type': 'block', 'children': ['214', '218']}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'assignment', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'edge_data'}; {'id': '218', 'type': 'expression_statement', 'children': ['219']}, {'id': '219', 'type': 'call', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': '_fix_agraph_color'}, {'id': '221', 'type': 'argument_list', 'children': ['222']} | def nx_ensure_agraph_color(graph):
from plottool import color_funcs
import plottool as pt
def _fix_agraph_color(data):
try:
orig_color = data.get('color', None)
alpha = data.get('alpha', None)
color = orig_color
if color is None and alpha is not None:
color = [0, 0, 0]
if color is not None:
color = pt.ensure_nonhex_color(color)
color = list(color_funcs.ensure_base255(color))
if alpha is not None:
if len(color) == 3:
color += [int(alpha * 255)]
else:
color[3] = int(alpha * 255)
color = tuple(color)
if len(color) == 3:
data['color'] = '
else:
data['color'] = '
except Exception as ex:
import utool as ut
ut.printex(ex, keys=['color', 'orig_color', 'data'])
raise
for node, node_data in graph.nodes(data=True):
data = node_data
_fix_agraph_color(data)
for u, v, edge_data in graph.edges(data=True):
data = edge_data
_fix_agraph_color(data) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'simplify_graph'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '5', 'type': 'block', 'children': ['6', '11', '25', '34', '68', '78', '127', '133', '139', '146', '153']}; {'id': '6', 'type': 'import_statement', 'children': ['7']}, {'id': '7', 'type': 'aliased_import', 'children': ['8', '10']}; {'id': '8', 'type': 'dotted_name', 'children': ['9']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'utool'}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '11', 'type': 'expression_statement', 'children': ['12']}; {'id': '12', 'type': 'assignment', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '14', 'type': 'call', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '16', 'type': 'argument_list', 'children': ['17']}, {'id': '17', 'type': 'call', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '19', 'type': 'argument_list', 'children': ['20']}; {'id': '20', 'type': 'call', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'graph'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '24', 'type': 'argument_list', 'children': []}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}; {'id': '28', 'type': 'call', 'children': ['29', '32']}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'make_index_lookup'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '34', 'type': 'if_statement', 'children': ['35', '40', '55']}, {'id': '35', 'type': 'call', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'is_multigraph'}, {'id': '39', 'type': 'argument_list', 'children': []}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '46', 'type': 'argument_list', 'children': ['47']}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'keyword_argument', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '54', 'type': 'True', 'children': []}, {'id': '55', 'type': 'else_clause', 'children': ['56']}; {'id': '56', 'type': 'block', 'children': ['57']}, {'id': '57', 'type': 'expression_statement', 'children': ['58']}; {'id': '58', 'type': 'assignment', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '60', 'type': 'call', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '62', 'type': 'argument_list', 'children': ['63']}, {'id': '63', 'type': 'call', 'children': ['64', '67']}; {'id': '64', 'type': 'attribute', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '67', 'type': 'argument_list', 'children': []}; {'id': '68', 'type': 'expression_statement', 'children': ['69']}, {'id': '69', 'type': 'assignment', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'new_nodes'}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'take'}, {'id': '75', 'type': 'argument_list', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '78', 'type': 'if_statement', 'children': ['79', '84', '107']}, {'id': '79', 'type': 'call', 'children': ['80', '83']}; {'id': '80', 'type': 'attribute', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'is_multigraph'}, {'id': '83', 'type': 'argument_list', 'children': []}; {'id': '84', 'type': 'block', 'children': ['85']}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'assignment', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '88', 'type': 'list_comprehension', 'children': ['89', '104']}, {'id': '89', 'type': 'tuple', 'children': ['90', '95', '100', '103']}; {'id': '90', 'type': 'subscript', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}; {'id': '92', 'type': 'subscript', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '94', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '95', 'type': 'subscript', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}, {'id': '97', 'type': 'subscript', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '99', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '100', 'type': 'subscript', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '102', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '103', 'type': 'dictionary', 'children': []}; {'id': '104', 'type': 'for_in_clause', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '107', 'type': 'else_clause', 'children': ['108']}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '112', 'type': 'list_comprehension', 'children': ['113', '124']}, {'id': '113', 'type': 'tuple', 'children': ['114', '119']}; {'id': '114', 'type': 'subscript', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}; {'id': '116', 'type': 'subscript', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '118', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '119', 'type': 'subscript', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'node_lookup'}, {'id': '121', 'type': 'subscript', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '123', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '124', 'type': 'for_in_clause', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'assignment', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'cls'}; {'id': '130', 'type': 'attribute', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'graph'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': '__class__'}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'assignment', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'new_graph'}; {'id': '136', 'type': 'call', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'cls'}; {'id': '138', 'type': 'argument_list', 'children': []}, {'id': '139', 'type': 'expression_statement', 'children': ['140']}; {'id': '140', 'type': 'call', 'children': ['141', '144']}, {'id': '141', 'type': 'attribute', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'new_graph'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'add_nodes_from'}; {'id': '144', 'type': 'argument_list', 'children': ['145']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'new_nodes'}; {'id': '146', 'type': 'expression_statement', 'children': ['147']}, {'id': '147', 'type': 'call', 'children': ['148', '151']}; {'id': '148', 'type': 'attribute', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'new_graph'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'add_edges_from'}, {'id': '151', 'type': 'argument_list', 'children': ['152']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '153', 'type': 'return_statement', 'children': ['154']} | def simplify_graph(graph):
import utool as ut
nodes = sorted(list(graph.nodes()))
node_lookup = ut.make_index_lookup(nodes)
if graph.is_multigraph():
edges = list(graph.edges(keys=True))
else:
edges = list(graph.edges())
new_nodes = ut.take(node_lookup, nodes)
if graph.is_multigraph():
new_edges = [(node_lookup[e[0]], node_lookup[e[1]], e[2], {}) for e in edges]
else:
new_edges = [(node_lookup[e[0]], node_lookup[e[1]]) for e in edges]
cls = graph.__class__
new_graph = cls()
new_graph.add_nodes_from(new_nodes)
new_graph.add_edges_from(new_edges)
return new_graph |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'subgraph_from_edges'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'edge_list'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'ref_back'}; {'id': '8', 'type': 'True', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '29', '42', '117']}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'assignment', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'sub_nodes'}, {'id': '13', 'type': 'call', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '15', 'type': 'argument_list', 'children': ['16']}; {'id': '16', 'type': 'set_comprehension', 'children': ['17', '18', '21']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '18', 'type': 'for_in_clause', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'edge_list'}, {'id': '21', 'type': 'for_in_clause', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '23', 'type': 'subscript', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '25', 'type': 'slice', 'children': ['26', '27', '28']}; {'id': '26', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '27', 'type': 'colon', 'children': []}; {'id': '28', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'multi_edge_list'}; {'id': '32', 'type': 'list_comprehension', 'children': ['33', '39']}, {'id': '33', 'type': 'subscript', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '35', 'type': 'slice', 'children': ['36', '37', '38']}; {'id': '36', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '37', 'type': 'colon', 'children': []}; {'id': '38', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '39', 'type': 'for_in_clause', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'edge_list'}; {'id': '42', 'type': 'if_statement', 'children': ['43', '44', '78']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'ref_back'}; {'id': '44', 'type': 'block', 'children': ['45', '54']}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'G_sub'}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'subgraph'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'sub_nodes'}; {'id': '54', 'type': 'for_statement', 'children': ['55', '56', '64']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '56', 'type': 'call', 'children': ['57', '60']}, {'id': '57', 'type': 'attribute', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'G_sub'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '60', 'type': 'argument_list', 'children': ['61']}, {'id': '61', 'type': 'keyword_argument', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '63', 'type': 'True', 'children': []}; {'id': '64', 'type': 'block', 'children': ['65']}, {'id': '65', 'type': 'if_statement', 'children': ['66', '69']}; {'id': '66', 'type': 'comparison_operator', 'children': ['67', '68'], 'value': 'not'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'multi_edge_list'}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'G_sub'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'remove_edge'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'list_splat', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '78', 'type': 'else_clause', 'children': ['79']}, {'id': '79', 'type': 'block', 'children': ['80', '93']}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'G_sub'}, {'id': '83', 'type': 'call', 'children': ['84', '92']}; {'id': '84', 'type': 'attribute', 'children': ['85', '91']}, {'id': '85', 'type': 'call', 'children': ['86', '89']}; {'id': '86', 'type': 'attribute', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'subgraph'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'sub_nodes'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'copy'}; {'id': '92', 'type': 'argument_list', 'children': []}, {'id': '93', 'type': 'for_statement', 'children': ['94', '95', '103']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '95', 'type': 'call', 'children': ['96', '99']}; {'id': '96', 'type': 'attribute', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'G_sub'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'edges'}, {'id': '99', 'type': 'argument_list', 'children': ['100']}; {'id': '100', 'type': 'keyword_argument', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '102', 'type': 'True', 'children': []}, {'id': '103', 'type': 'block', 'children': ['104']}; {'id': '104', 'type': 'if_statement', 'children': ['105', '108']}, {'id': '105', 'type': 'comparison_operator', 'children': ['106', '107'], 'value': 'not'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'multi_edge_list'}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'call', 'children': ['111', '114']}, {'id': '111', 'type': 'attribute', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'G_sub'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'remove_edge'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'list_splat', 'children': ['116']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '117', 'type': 'return_statement', 'children': ['118']} | def subgraph_from_edges(G, edge_list, ref_back=True):
sub_nodes = list({y for x in edge_list for y in x[0:2]})
multi_edge_list = [edge[0:3] for edge in edge_list]
if ref_back:
G_sub = G.subgraph(sub_nodes)
for edge in G_sub.edges(keys=True):
if edge not in multi_edge_list:
G_sub.remove_edge(*edge)
else:
G_sub = G.subgraph(sub_nodes).copy()
for edge in G_sub.edges(keys=True):
if edge not in multi_edge_list:
G_sub.remove_edge(*edge)
return G_sub |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '30']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'bfs_conditional'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '12', '15', '18', '21', '24', '27']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'reverse'}; {'id': '8', 'type': 'False', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '11', 'type': 'True', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '14', 'type': 'False', 'children': []}, {'id': '15', 'type': 'default_parameter', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'yield_nodes'}, {'id': '17', 'type': 'True', 'children': []}; {'id': '18', 'type': 'default_parameter', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'yield_if'}; {'id': '20', 'type': 'None', 'children': []}, {'id': '21', 'type': 'default_parameter', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'continue_if'}, {'id': '23', 'type': 'None', 'children': []}; {'id': '24', 'type': 'default_parameter', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '26', 'type': 'None', 'children': []}, {'id': '27', 'type': 'default_parameter', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'yield_source'}, {'id': '29', 'type': 'False', 'children': []}; {'id': '30', 'type': 'block', 'children': ['31', '48', '90', '99', '120', '170']}, {'id': '31', 'type': 'if_statement', 'children': ['32', '39']}; {'id': '32', 'type': 'boolean_operator', 'children': ['33', '34'], 'value': 'and'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'reverse'}; {'id': '34', 'type': 'call', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'hasattr'}; {'id': '36', 'type': 'argument_list', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '38', 'type': 'string', 'children': [], 'value': "'reverse'"}, {'id': '39', 'type': 'block', 'children': ['40']}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '43', 'type': 'call', 'children': ['44', '47']}; {'id': '44', 'type': 'attribute', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'reverse'}, {'id': '47', 'type': 'argument_list', 'children': []}; {'id': '48', 'type': 'if_statement', 'children': ['49', '56', '71']}, {'id': '49', 'type': 'call', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '51', 'type': 'argument_list', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '53', 'type': 'attribute', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'nx'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'Graph'}; {'id': '56', 'type': 'block', 'children': ['57']}, {'id': '57', 'type': 'expression_statement', 'children': ['58']}; {'id': '58', 'type': 'assignment', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'neighbors'}; {'id': '60', 'type': 'call', 'children': ['61', '64']}, {'id': '61', 'type': 'attribute', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'functools'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'partial'}; {'id': '64', 'type': 'argument_list', 'children': ['65', '68']}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '68', 'type': 'keyword_argument', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '71', 'type': 'else_clause', 'children': ['72']}; {'id': '72', 'type': 'block', 'children': ['73']}, {'id': '73', 'type': 'expression_statement', 'children': ['74']}; {'id': '74', 'type': 'assignment', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'neighbors'}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'functools'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'partial'}; {'id': '80', 'type': 'argument_list', 'children': ['81', '84', '87']}, {'id': '81', 'type': 'attribute', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '84', 'type': 'keyword_argument', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '87', 'type': 'keyword_argument', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '93', 'type': 'call', 'children': ['94', '97']}; {'id': '94', 'type': 'attribute', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'collections'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'deque'}, {'id': '97', 'type': 'argument_list', 'children': ['98']}; {'id': '98', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '99', 'type': 'if_statement', 'children': ['100', '103', '111']}; {'id': '100', 'type': 'comparison_operator', 'children': ['101', '102'], 'value': 'is'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '102', 'type': 'None', 'children': []}, {'id': '103', 'type': 'block', 'children': ['104']}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'assignment', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}, {'id': '107', 'type': 'call', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '111', 'type': 'else_clause', 'children': ['112']}; {'id': '112', 'type': 'block', 'children': ['113']}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '116', 'type': 'call', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '118', 'type': 'argument_list', 'children': ['119']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '120', 'type': 'if_statement', 'children': ['121', '124']}, {'id': '121', 'type': 'comparison_operator', 'children': ['122', '123'], 'value': 'not'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '124', 'type': 'block', 'children': ['125', '133', '140', '147', '161']}, {'id': '125', 'type': 'if_statement', 'children': ['126', '129']}; {'id': '126', 'type': 'boolean_operator', 'children': ['127', '128'], 'value': 'and'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'yield_nodes'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'yield_source'}, {'id': '129', 'type': 'block', 'children': ['130']}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'yield', 'children': ['132']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'call', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'assignment', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '143', 'type': 'call', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'neighbors'}, {'id': '145', 'type': 'argument_list', 'children': ['146']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '147', 'type': 'if_statement', 'children': ['148', '153']}; {'id': '148', 'type': 'call', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '150', 'type': 'argument_list', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '153', 'type': 'block', 'children': ['154']}; {'id': '154', 'type': 'expression_statement', 'children': ['155']}, {'id': '155', 'type': 'assignment', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '157', 'type': 'call', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'iter'}, {'id': '159', 'type': 'argument_list', 'children': ['160']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '161', 'type': 'expression_statement', 'children': ['162']}; {'id': '162', 'type': 'call', 'children': ['163', '166']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '166', 'type': 'argument_list', 'children': ['167']}, {'id': '167', 'type': 'tuple', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '170', 'type': 'while_statement', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '172', 'type': 'block', 'children': ['173', '181', '285']}, {'id': '173', 'type': 'expression_statement', 'children': ['174']}; {'id': '174', 'type': 'assignment', 'children': ['175', '178']}, {'id': '175', 'type': 'pattern_list', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '178', 'type': 'subscript', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '180', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '181', 'type': 'for_statement', 'children': ['182', '183', '184']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'edges'}; {'id': '184', 'type': 'block', 'children': ['185', '191', '231']}, {'id': '185', 'type': 'expression_statement', 'children': ['186']}; {'id': '186', 'type': 'assignment', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '188', 'type': 'subscript', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '190', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '191', 'type': 'if_statement', 'children': ['192', '193', '214']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'yield_nodes'}, {'id': '193', 'type': 'block', 'children': ['194']}; {'id': '194', 'type': 'if_statement', 'children': ['195', '198']}, {'id': '195', 'type': 'comparison_operator', 'children': ['196', '197'], 'value': 'not'}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'child'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '198', 'type': 'block', 'children': ['199']}, {'id': '199', 'type': 'if_statement', 'children': ['200', '210']}; {'id': '200', 'type': 'boolean_operator', 'children': ['201', '204'], 'value': 'or'}, {'id': '201', 'type': 'comparison_operator', 'children': ['202', '203'], 'value': 'is'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'yield_if'}, {'id': '203', 'type': 'None', 'children': []}; {'id': '204', 'type': 'call', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'yield_if'}; {'id': '206', 'type': 'argument_list', 'children': ['207', '208', '209']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'child'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '210', 'type': 'block', 'children': ['211']}, {'id': '211', 'type': 'expression_statement', 'children': ['212']}; {'id': '212', 'type': 'yield', 'children': ['213']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '214', 'type': 'else_clause', 'children': ['215']}, {'id': '215', 'type': 'block', 'children': ['216']}; {'id': '216', 'type': 'if_statement', 'children': ['217', '227']}, {'id': '217', 'type': 'boolean_operator', 'children': ['218', '221'], 'value': 'or'}; {'id': '218', 'type': 'comparison_operator', 'children': ['219', '220'], 'value': 'is'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'yield_if'}; {'id': '220', 'type': 'None', 'children': []}, {'id': '221', 'type': 'call', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'yield_if'}, {'id': '223', 'type': 'argument_list', 'children': ['224', '225', '226']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'G'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '227', 'type': 'block', 'children': ['228']}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'yield', 'children': ['230']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'edge'}, {'id': '231', 'type': 'if_statement', 'children': ['232', '235']}; {'id': '232', 'type': 'comparison_operator', 'children': ['233', '234'], 'value': 'not'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}, {'id': '235', 'type': 'block', 'children': ['236', '243']}; {'id': '236', 'type': 'expression_statement', 'children': ['237']}, {'id': '237', 'type': 'call', 'children': ['238', '241']}; {'id': '238', 'type': 'attribute', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'visited_nodes'}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '241', 'type': 'argument_list', 'children': ['242']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'child'}, {'id': '243', 'type': 'if_statement', 'children': ['244', '254']}; {'id': '244', 'type': 'boolean_operator', 'children': ['245', '248'], 'value': 'or'}, {'id': '245', 'type': 'comparison_operator', 'children': ['246', '247'], 'value': 'is'}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'continue_if'}, {'id': '247', 'type': 'None', 'children': []}; {'id': '248', 'type': 'call', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'continue_if'}; {'id': '250', 'type': 'argument_list', 'children': ['251', '252', '253']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'G'}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'child'}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'edge'}; {'id': '254', 'type': 'block', 'children': ['255', '262', '276']}, {'id': '255', 'type': 'expression_statement', 'children': ['256']}; {'id': '256', 'type': 'assignment', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '258', 'type': 'call', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'neighbors'}; {'id': '260', 'type': 'argument_list', 'children': ['261']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '262', 'type': 'if_statement', 'children': ['263', '268']}, {'id': '263', 'type': 'call', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '265', 'type': 'argument_list', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '268', 'type': 'block', 'children': ['269']}, {'id': '269', 'type': 'expression_statement', 'children': ['270']}; {'id': '270', 'type': 'assignment', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '272', 'type': 'call', 'children': ['273', '274']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'iter'}; {'id': '274', 'type': 'argument_list', 'children': ['275']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'new_edges'}; {'id': '276', 'type': 'expression_statement', 'children': ['277']}, {'id': '277', 'type': 'call', 'children': ['278', '281']}; {'id': '278', 'type': 'attribute', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '281', 'type': 'argument_list', 'children': ['282']}; {'id': '282', 'type': 'tuple', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'child'}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'new_edges'}, {'id': '285', 'type': 'expression_statement', 'children': ['286']}; {'id': '286', 'type': 'call', 'children': ['287', '290']}, {'id': '287', 'type': 'attribute', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'popleft'} | def bfs_conditional(G, source, reverse=False, keys=True, data=False,
yield_nodes=True, yield_if=None,
continue_if=None, visited_nodes=None,
yield_source=False):
if reverse and hasattr(G, 'reverse'):
G = G.reverse()
if isinstance(G, nx.Graph):
neighbors = functools.partial(G.edges, data=data)
else:
neighbors = functools.partial(G.edges, keys=keys, data=data)
queue = collections.deque([])
if visited_nodes is None:
visited_nodes = set([])
else:
visited_nodes = set(visited_nodes)
if source not in visited_nodes:
if yield_nodes and yield_source:
yield source
visited_nodes.add(source)
new_edges = neighbors(source)
if isinstance(new_edges, list):
new_edges = iter(new_edges)
queue.append((source, new_edges))
while queue:
parent, edges = queue[0]
for edge in edges:
child = edge[1]
if yield_nodes:
if child not in visited_nodes:
if yield_if is None or yield_if(G, child, edge):
yield child
else:
if yield_if is None or yield_if(G, child, edge):
yield edge
if child not in visited_nodes:
visited_nodes.add(child)
if continue_if is None or continue_if(G, child, edge):
new_edges = neighbors(child)
if isinstance(new_edges, list):
new_edges = iter(new_edges)
queue.append((child, new_edges))
queue.popleft() |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'approx_min_num_components'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'negative_edges'}; {'id': '6', 'type': 'block', 'children': ['7', '12', '16', '24', '31', '38', '86', '107', '118', '127', '157', '275', '283']}, {'id': '7', 'type': 'import_statement', 'children': ['8']}; {'id': '8', 'type': 'aliased_import', 'children': ['9', '11']}, {'id': '9', 'type': 'dotted_name', 'children': ['10']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'utool'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '15', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'g_neg'}, {'id': '19', 'type': 'call', 'children': ['20', '23']}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'nx'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'Graph'}, {'id': '23', 'type': 'argument_list', 'children': []}; {'id': '24', 'type': 'expression_statement', 'children': ['25']}, {'id': '25', 'type': 'call', 'children': ['26', '29']}; {'id': '26', 'type': 'attribute', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'add_nodes_from'}, {'id': '29', 'type': 'argument_list', 'children': ['30']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'call', 'children': ['33', '36']}, {'id': '33', 'type': 'attribute', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'g_neg'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'add_edges_from'}; {'id': '36', 'type': 'argument_list', 'children': ['37']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'negative_edges'}; {'id': '38', 'type': 'if_statement', 'children': ['39', '47', '66']}, {'id': '39', 'type': 'call', 'children': ['40', '45']}; {'id': '40', 'type': 'attribute', 'children': ['41', '44']}, {'id': '41', 'type': 'attribute', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'nx'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': '__version__'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'startswith'}, {'id': '45', 'type': 'argument_list', 'children': ['46']}; {'id': '46', 'type': 'string', 'children': [], 'value': "'2'"}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'deg0_nodes'}, {'id': '51', 'type': 'list_comprehension', 'children': ['52', '53', '62']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '53', 'type': 'for_in_clause', 'children': ['54', '57']}; {'id': '54', 'type': 'pattern_list', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '57', 'type': 'call', 'children': ['58', '61']}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'degree'}, {'id': '61', 'type': 'argument_list', 'children': []}; {'id': '62', 'type': 'if_clause', 'children': ['63']}, {'id': '63', 'type': 'comparison_operator', 'children': ['64', '65'], 'value': '=='}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '65', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '66', 'type': 'else_clause', 'children': ['67']}, {'id': '67', 'type': 'block', 'children': ['68']}; {'id': '68', 'type': 'expression_statement', 'children': ['69']}, {'id': '69', 'type': 'assignment', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'deg0_nodes'}, {'id': '71', 'type': 'list_comprehension', 'children': ['72', '73', '82']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '73', 'type': 'for_in_clause', 'children': ['74', '77']}; {'id': '74', 'type': 'pattern_list', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'n'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '77', 'type': 'call', 'children': ['78', '81']}; {'id': '78', 'type': 'attribute', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'degree_iter'}, {'id': '81', 'type': 'argument_list', 'children': []}; {'id': '82', 'type': 'if_clause', 'children': ['83']}, {'id': '83', 'type': 'comparison_operator', 'children': ['84', '85'], 'value': '=='}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '85', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '86', 'type': 'for_statement', 'children': ['87', '90', '96']}, {'id': '87', 'type': 'pattern_list', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'u'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'v'}; {'id': '90', 'type': 'call', 'children': ['91', '94']}, {'id': '91', 'type': 'attribute', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'itertwo'}; {'id': '94', 'type': 'argument_list', 'children': ['95']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'deg0_nodes'}; {'id': '96', 'type': 'block', 'children': ['97']}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'call', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'nx_contracted_nodes'}; {'id': '100', 'type': 'argument_list', 'children': ['101', '102', '103', '104']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'v'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'u'}; {'id': '104', 'type': 'keyword_argument', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'inplace'}; {'id': '106', 'type': 'True', 'children': []}, {'id': '107', 'type': 'expression_statement', 'children': ['108']}; {'id': '108', 'type': 'assignment', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'unused'}; {'id': '110', 'type': 'call', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '112', 'type': 'argument_list', 'children': ['113']}, {'id': '113', 'type': 'call', 'children': ['114', '117']}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '117', 'type': 'argument_list', 'children': []}; {'id': '118', 'type': 'expression_statement', 'children': ['119']}, {'id': '119', 'type': 'assignment', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'g_pos'}, {'id': '121', 'type': 'call', 'children': ['122', '125']}; {'id': '122', 'type': 'attribute', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'nx'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'complement'}, {'id': '125', 'type': 'argument_list', 'children': ['126']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'g_neg'}, {'id': '127', 'type': 'if_statement', 'children': ['128', '129']}; {'id': '128', 'type': 'False', 'children': []}, {'id': '129', 'type': 'block', 'children': ['130', '137', '148', '155']}; {'id': '130', 'type': 'import_from_statement', 'children': ['131', '135']}, {'id': '131', 'type': 'dotted_name', 'children': ['132', '133', '134']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'networkx'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'algorithms'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'approximation'}, {'id': '135', 'type': 'dotted_name', 'children': ['136']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'clique'}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'assignment', 'children': ['139', '142']}, {'id': '139', 'type': 'pattern_list', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'maxiset'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'cliques'}; {'id': '142', 'type': 'call', 'children': ['143', '146']}, {'id': '143', 'type': 'attribute', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'clique'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'clique_removal'}; {'id': '146', 'type': 'argument_list', 'children': ['147']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'g_pos'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '151', 'type': 'call', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '153', 'type': 'argument_list', 'children': ['154']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'cliques'}, {'id': '155', 'type': 'return_statement', 'children': ['156']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '157', 'type': 'while_statement', 'children': ['158', '164']}; {'id': '158', 'type': 'comparison_operator', 'children': ['159', '163'], 'value': '>'}, {'id': '159', 'type': 'call', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '161', 'type': 'argument_list', 'children': ['162']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'unused'}, {'id': '163', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '164', 'type': 'block', 'children': ['165', '169', '173', '179', '186', '198', '208']}, {'id': '165', 'type': 'expression_statement', 'children': ['166']}; {'id': '166', 'type': 'augmented_assignment', 'children': ['167', '168'], 'value': '+='}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'num'}; {'id': '168', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '169', 'type': 'expression_statement', 'children': ['170']}; {'id': '170', 'type': 'assignment', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'idx1'}; {'id': '172', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '173', 'type': 'expression_statement', 'children': ['174']}; {'id': '174', 'type': 'assignment', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'n1'}; {'id': '176', 'type': 'subscript', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'unused'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'idx1'}, {'id': '179', 'type': 'expression_statement', 'children': ['180']}; {'id': '180', 'type': 'call', 'children': ['181', '184']}, {'id': '181', 'type': 'attribute', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'unused'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'remove'}; {'id': '184', 'type': 'argument_list', 'children': ['185']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'n1'}; {'id': '186', 'type': 'expression_statement', 'children': ['187']}, {'id': '187', 'type': 'assignment', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'neigbs'}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'call', 'children': ['193', '196']}, {'id': '193', 'type': 'attribute', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'g_pos'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'neighbors'}; {'id': '196', 'type': 'argument_list', 'children': ['197']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'n1'}; {'id': '198', 'type': 'expression_statement', 'children': ['199']}, {'id': '199', 'type': 'assignment', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'neigbs'}, {'id': '201', 'type': 'call', 'children': ['202', '205']}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'ut'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'isect'}, {'id': '205', 'type': 'argument_list', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'neigbs'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'unused'}; {'id': '208', 'type': 'while_statement', 'children': ['209', '215']}, {'id': '209', 'type': 'comparison_operator', 'children': ['210', '214'], 'value': '>'}; {'id': '210', 'type': 'call', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '212', 'type': 'argument_list', 'children': ['213']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'neigbs'}; {'id': '214', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '215', 'type': 'block', 'children': ['216', '220', '226', '233', '244', '253', '265']}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'idx2'}, {'id': '219', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '220', 'type': 'expression_statement', 'children': ['221']}, {'id': '221', 'type': 'assignment', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'n2'}, {'id': '223', 'type': 'subscript', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'neigbs'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'idx2'}; {'id': '226', 'type': 'expression_statement', 'children': ['227']}, {'id': '227', 'type': 'call', 'children': ['228', '231']}; {'id': '228', 'type': 'attribute', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'unused'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'remove'}, {'id': '231', 'type': 'argument_list', 'children': ['232']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'n2'}, {'id': '233', 'type': 'expression_statement', 'children': ['234']}; {'id': '234', 'type': 'assignment', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '236', 'type': 'call', 'children': ['237', '240']}, {'id': '237', 'type': 'attribute', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'nx'}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'contracted_nodes'}; {'id': '240', 'type': 'argument_list', 'children': ['241', '242', '243']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'g_neg'}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'n1'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'n2'}; {'id': '244', 'type': 'expression_statement', 'children': ['245']}, {'id': '245', 'type': 'assignment', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'g_pos'}, {'id': '247', 'type': 'call', 'children': ['248', '251']}; {'id': '248', 'type': 'attribute', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'nx'}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'complement'}, {'id': '251', 'type': 'argument_list', 'children': ['252']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'g_neg'}, {'id': '253', 'type': 'expression_statement', 'children': ['254']}; {'id': '254', 'type': 'assignment', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'neigbs'}; {'id': '256', 'type': 'call', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '258', 'type': 'argument_list', 'children': ['259']}, {'id': '259', 'type': 'call', 'children': ['260', '263']}; {'id': '260', 'type': 'attribute', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'g_pos'}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'neighbors'}, {'id': '263', 'type': 'argument_list', 'children': ['264']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'n1'}, {'id': '265', 'type': 'expression_statement', 'children': ['266']}; {'id': '266', 'type': 'assignment', 'children': ['267', '268']}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'neigbs'}; {'id': '268', 'type': 'call', 'children': ['269', '272']}, {'id': '269', 'type': 'attribute', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'ut'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'isect'}; {'id': '272', 'type': 'argument_list', 'children': ['273', '274']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'neigbs'}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'unused'}, {'id': '275', 'type': 'expression_statement', 'children': ['276']}; {'id': '276', 'type': 'call', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '278', 'type': 'argument_list', 'children': ['279']}, {'id': '279', 'type': 'binary_operator', 'children': ['280', '281'], 'value': '%'}; {'id': '280', 'type': 'string', 'children': [], 'value': "'num = %r'"}, {'id': '281', 'type': 'tuple', 'children': ['282']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'num'}, {'id': '283', 'type': 'return_statement', 'children': ['284']} | def approx_min_num_components(nodes, negative_edges):
import utool as ut
num = 0
g_neg = nx.Graph()
g_neg.add_nodes_from(nodes)
g_neg.add_edges_from(negative_edges)
if nx.__version__.startswith('2'):
deg0_nodes = [n for n, d in g_neg.degree() if d == 0]
else:
deg0_nodes = [n for n, d in g_neg.degree_iter() if d == 0]
for u, v in ut.itertwo(deg0_nodes):
nx_contracted_nodes(g_neg, v, u, inplace=True)
unused = list(g_neg.nodes())
g_pos = nx.complement(g_neg)
if False:
from networkx.algorithms.approximation import clique
maxiset, cliques = clique.clique_removal(g_pos)
num = len(cliques)
return num
while len(unused) > 0:
num += 1
idx1 = 0
n1 = unused[idx1]
unused.remove(n1)
neigbs = list(g_pos.neighbors(n1))
neigbs = ut.isect(neigbs, unused)
while len(neigbs) > 0:
idx2 = 0
n2 = neigbs[idx2]
unused.remove(n2)
g_neg = nx.contracted_nodes(g_neg, n1, n2)
g_pos = nx.complement(g_neg)
neigbs = list(g_pos.neighbors(n1))
neigbs = ut.isect(neigbs, unused)
print('num = %r' % (num,))
return num |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '15']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'generate_proteins'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8', '9', '12']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'pepfn'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'proteins'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'pepheader'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'scorecol'}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'minlog'}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'higherbetter'}, {'id': '11', 'type': 'True', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'protcol'}; {'id': '14', 'type': 'False', 'children': []}, {'id': '15', 'type': 'block', 'children': ['16', '20', '27', '37', '73', '140']}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'protein_peptides'}, {'id': '19', 'type': 'dictionary', 'children': []}; {'id': '20', 'type': 'if_statement', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'minlog'}; {'id': '22', 'type': 'block', 'children': ['23']}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'higherbetter'}; {'id': '26', 'type': 'False', 'children': []}, {'id': '27', 'type': 'if_statement', 'children': ['28', '30']}; {'id': '28', 'type': 'not_operator', 'children': ['29']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'protcol'}; {'id': '30', 'type': 'block', 'children': ['31']}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'protcol'}; {'id': '34', 'type': 'attribute', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'peptabledata'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'HEADER_MASTERPROTEINS'}, {'id': '37', 'type': 'for_statement', 'children': ['38', '39', '46']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'psm'}, {'id': '39', 'type': 'call', 'children': ['40', '43']}; {'id': '40', 'type': 'attribute', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'reader'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'generate_tsv_psms'}, {'id': '43', 'type': 'argument_list', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'pepfn'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'pepheader'}; {'id': '46', 'type': 'block', 'children': ['47', '53', '59']}, {'id': '47', 'type': 'expression_statement', 'children': ['48']}; {'id': '48', 'type': 'assignment', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'p_acc'}; {'id': '50', 'type': 'subscript', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'psm'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'protcol'}, {'id': '53', 'type': 'if_statement', 'children': ['54', '57']}; {'id': '54', 'type': 'comparison_operator', 'children': ['55', '56'], 'value': 'in'}, {'id': '55', 'type': 'string', 'children': [], 'value': "';'"}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'p_acc'}, {'id': '57', 'type': 'block', 'children': ['58']}; {'id': '58', 'type': 'continue_statement', 'children': []}, {'id': '59', 'type': 'expression_statement', 'children': ['60']}; {'id': '60', 'type': 'assignment', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'protein_peptides'}; {'id': '62', 'type': 'call', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'evaluate_peptide'}; {'id': '64', 'type': 'argument_list', 'children': ['65', '66', '67', '68', '69', '70']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'protein_peptides'}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'psm'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'p_acc'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'higherbetter'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'scorecol'}; {'id': '70', 'type': 'keyword_argument', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'fncol'}; {'id': '72', 'type': 'False', 'children': []}, {'id': '73', 'type': 'if_statement', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'minlog'}, {'id': '75', 'type': 'block', 'children': ['76', '131']}; {'id': '76', 'type': 'try_statement', 'children': ['77', '101']}, {'id': '77', 'type': 'block', 'children': ['78']}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'nextbestscore'}, {'id': '81', 'type': 'call', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'min'}, {'id': '83', 'type': 'argument_list', 'children': ['84']}; {'id': '84', 'type': 'list_comprehension', 'children': ['85', '88', '95']}, {'id': '85', 'type': 'subscript', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'pep'}, {'id': '87', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '88', 'type': 'for_in_clause', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'pep'}; {'id': '90', 'type': 'call', 'children': ['91', '94']}, {'id': '91', 'type': 'attribute', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'protein_peptides'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '94', 'type': 'argument_list', 'children': []}, {'id': '95', 'type': 'if_clause', 'children': ['96']}; {'id': '96', 'type': 'comparison_operator', 'children': ['97', '100'], 'value': '>'}, {'id': '97', 'type': 'subscript', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'pep'}, {'id': '99', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '100', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '101', 'type': 'except_clause', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '103', 'type': 'block', 'children': ['104', '107', '124']}; {'id': '104', 'type': 'import_statement', 'children': ['105']}, {'id': '105', 'type': 'dotted_name', 'children': ['106']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '107', 'type': 'expression_statement', 'children': ['108']}; {'id': '108', 'type': 'call', 'children': ['109', '114']}, {'id': '109', 'type': 'attribute', 'children': ['110', '113']}; {'id': '110', 'type': 'attribute', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'write'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'call', 'children': ['116', '122']}; {'id': '116', 'type': 'attribute', 'children': ['117', '121']}, {'id': '117', 'type': 'concatenated_string', 'children': ['118', '119', '120']}; {'id': '118', 'type': 'string', 'children': [], 'value': "'Cannot find score of type {} which is above 0. '"}, {'id': '119', 'type': 'string', 'children': [], 'value': "'Only scores above zero can have a -log value. '"}; {'id': '120', 'type': 'string', 'children': [], 'value': "'Exiting.'"}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'scorecol'}; {'id': '124', 'type': 'expression_statement', 'children': ['125']}, {'id': '125', 'type': 'call', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'exit'}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '131', 'type': 'expression_statement', 'children': ['132']}; {'id': '132', 'type': 'assignment', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'nextbestscore'}; {'id': '134', 'type': 'unary_operator', 'children': ['135'], 'value': '-'}, {'id': '135', 'type': 'call', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '137', 'type': 'argument_list', 'children': ['138', '139']}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'nextbestscore'}, {'id': '139', 'type': 'integer', 'children': [], 'value': '10'}; {'id': '140', 'type': 'for_statement', 'children': ['141', '142', '143']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'protein'}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'proteins'}, {'id': '143', 'type': 'block', 'children': ['144', '182', '203', '216']}; {'id': '144', 'type': 'try_statement', 'children': ['145', '156']}, {'id': '145', 'type': 'block', 'children': ['146']}; {'id': '146', 'type': 'expression_statement', 'children': ['147']}, {'id': '147', 'type': 'assignment', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'peptide'}, {'id': '149', 'type': 'subscript', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'protein_peptides'}, {'id': '151', 'type': 'subscript', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'protein'}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'prottabledata'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'HEADER_PROTEIN'}; {'id': '156', 'type': 'except_clause', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'KeyError'}; {'id': '158', 'type': 'block', 'children': ['159', '175']}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'call', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '162', 'type': 'argument_list', 'children': ['163']}, {'id': '163', 'type': 'call', 'children': ['164', '169']}; {'id': '164', 'type': 'attribute', 'children': ['165', '168']}, {'id': '165', 'type': 'concatenated_string', 'children': ['166', '167']}; {'id': '166', 'type': 'string', 'children': [], 'value': "'WARNING - protein {} not found in peptide '"}, {'id': '167', 'type': 'string', 'children': [], 'value': "'table'"}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '169', 'type': 'argument_list', 'children': ['170']}; {'id': '170', 'type': 'subscript', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'protein'}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'prottabledata'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'HEADER_PROTEIN'}, {'id': '175', 'type': 'expression_statement', 'children': ['176']}; {'id': '176', 'type': 'assignment', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'peptide'}; {'id': '178', 'type': 'dictionary', 'children': ['179']}, {'id': '179', 'type': 'pair', 'children': ['180', '181']}; {'id': '180', 'type': 'string', 'children': [], 'value': "'score'"}, {'id': '181', 'type': 'string', 'children': [], 'value': "'NA'"}; {'id': '182', 'type': 'if_statement', 'children': ['183', '190']}, {'id': '183', 'type': 'boolean_operator', 'children': ['184', '185'], 'value': 'and'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'minlog'}, {'id': '185', 'type': 'comparison_operator', 'children': ['186', '189'], 'value': '!='}; {'id': '186', 'type': 'subscript', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'peptide'}; {'id': '188', 'type': 'string', 'children': [], 'value': "'score'"}, {'id': '189', 'type': 'string', 'children': [], 'value': "'NA'"}; {'id': '190', 'type': 'block', 'children': ['191']}, {'id': '191', 'type': 'expression_statement', 'children': ['192']}; {'id': '192', 'type': 'assignment', 'children': ['193', '196']}, {'id': '193', 'type': 'subscript', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'peptide'}, {'id': '195', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '196', 'type': 'call', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'log_score'}; {'id': '198', 'type': 'argument_list', 'children': ['199', '202']}, {'id': '199', 'type': 'subscript', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'peptide'}, {'id': '201', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'nextbestscore'}, {'id': '203', 'type': 'expression_statement', 'children': ['204']}; {'id': '204', 'type': 'assignment', 'children': ['205', '210']}, {'id': '205', 'type': 'subscript', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'protein'}, {'id': '207', 'type': 'attribute', 'children': ['208', '209']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'prottabledata'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'HEADER_QSCORE'}; {'id': '210', 'type': 'call', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '212', 'type': 'argument_list', 'children': ['213']}, {'id': '213', 'type': 'subscript', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'peptide'}, {'id': '215', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'yield', 'children': ['218']} | def generate_proteins(pepfn, proteins, pepheader, scorecol, minlog,
higherbetter=True, protcol=False):
protein_peptides = {}
if minlog:
higherbetter = False
if not protcol:
protcol = peptabledata.HEADER_MASTERPROTEINS
for psm in reader.generate_tsv_psms(pepfn, pepheader):
p_acc = psm[protcol]
if ';' in p_acc:
continue
protein_peptides = evaluate_peptide(protein_peptides, psm, p_acc,
higherbetter, scorecol,
fncol=False)
if minlog:
try:
nextbestscore = min([pep['score'] for pep in
protein_peptides.values()
if pep['score'] > 0])
except ValueError:
import sys
sys.stderr.write('Cannot find score of type {} which is above 0. '
'Only scores above zero can have a -log value. '
'Exiting.'.format(scorecol))
sys.exit(1)
nextbestscore = -log(nextbestscore, 10)
for protein in proteins:
try:
peptide = protein_peptides[protein[prottabledata.HEADER_PROTEIN]]
except KeyError:
print('WARNING - protein {} not found in peptide '
'table'.format(protein[prottabledata.HEADER_PROTEIN]))
peptide = {'score': 'NA'}
if minlog and peptide['score'] != 'NA':
peptide['score'] = log_score(peptide['score'], nextbestscore)
protein[prottabledata.HEADER_QSCORE] = str(
peptide['score'])
yield protein |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '29']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'grab_file_url'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '14', '17', '20', '23', '26']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'file_url'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'appname'}, {'id': '7', 'type': 'string', 'children': [], 'value': "'utool'"}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'download_dir'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'delay'}, {'id': '13', 'type': 'None', 'children': []}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'spoof'}; {'id': '16', 'type': 'False', 'children': []}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'fname'}, {'id': '19', 'type': 'None', 'children': []}; {'id': '20', 'type': 'default_parameter', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '22', 'type': 'True', 'children': []}, {'id': '23', 'type': 'default_parameter', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'redownload'}, {'id': '25', 'type': 'False', 'children': []}; {'id': '26', 'type': 'default_parameter', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'check_hash'}; {'id': '28', 'type': 'False', 'children': []}, {'id': '29', 'type': 'block', 'children': ['30', '32', '39', '51', '65', '73', '197', '204', '264', '271', '335']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'file_url'}, {'id': '35', 'type': 'call', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'clean_dropbox_link'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'file_url'}, {'id': '39', 'type': 'if_statement', 'children': ['40', '43']}; {'id': '40', 'type': 'comparison_operator', 'children': ['41', '42'], 'value': 'is'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'fname'}; {'id': '42', 'type': 'None', 'children': []}, {'id': '43', 'type': 'block', 'children': ['44']}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'fname'}, {'id': '47', 'type': 'call', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'basename'}, {'id': '49', 'type': 'argument_list', 'children': ['50']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'file_url'}, {'id': '51', 'type': 'if_statement', 'children': ['52', '55']}; {'id': '52', 'type': 'comparison_operator', 'children': ['53', '54'], 'value': 'is'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'download_dir'}; {'id': '54', 'type': 'None', 'children': []}, {'id': '55', 'type': 'block', 'children': ['56']}; {'id': '56', 'type': 'expression_statement', 'children': ['57']}, {'id': '57', 'type': 'assignment', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'download_dir'}, {'id': '59', 'type': 'call', 'children': ['60', '63']}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'util_cplat'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'get_app_cache_dir'}, {'id': '63', 'type': 'argument_list', 'children': ['64']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'appname'}, {'id': '65', 'type': 'expression_statement', 'children': ['66']}; {'id': '66', 'type': 'assignment', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'fpath'}; {'id': '68', 'type': 'call', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '70', 'type': 'argument_list', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'download_dir'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'fname'}, {'id': '73', 'type': 'if_statement', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'check_hash'}, {'id': '75', 'type': 'block', 'children': ['76', '96', '109', '114', '127', '146']}; {'id': '76', 'type': 'if_statement', 'children': ['77', '84', '89']}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '79', 'type': 'argument_list', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'check_hash'}, {'id': '81', 'type': 'tuple', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '84', 'type': 'block', 'children': ['85']}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'assignment', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'hash_list'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'check_hash'}, {'id': '89', 'type': 'else_clause', 'children': ['90']}; {'id': '90', 'type': 'block', 'children': ['91']}, {'id': '91', 'type': 'expression_statement', 'children': ['92']}; {'id': '92', 'type': 'assignment', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'hash_list'}; {'id': '94', 'type': 'list', 'children': ['95'], 'value': "['md5']"}, {'id': '95', 'type': 'string', 'children': [], 'value': "'md5'"}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '101']}; {'id': '98', 'type': 'pattern_list', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'hash_tag_remote'}, {'id': '101', 'type': 'call', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'grab_file_remote_hash'}, {'id': '103', 'type': 'argument_list', 'children': ['104', '105', '106']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'file_url'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'hash_list'}; {'id': '106', 'type': 'keyword_argument', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'hash_list'}; {'id': '112', 'type': 'list', 'children': ['113'], 'value': '[hash_tag_remote]'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'hash_tag_remote'}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'assignment', 'children': ['116', '119']}; {'id': '116', 'type': 'pattern_list', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'hash_local'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'hash_tag_local'}, {'id': '119', 'type': 'call', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'get_file_local_hash'}, {'id': '121', 'type': 'argument_list', 'children': ['122', '123', '124']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'fpath'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'hash_list'}; {'id': '124', 'type': 'keyword_argument', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '127', 'type': 'if_statement', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '129', 'type': 'block', 'children': ['130', '138']}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'call', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '133', 'type': 'argument_list', 'children': ['134']}; {'id': '134', 'type': 'binary_operator', 'children': ['135', '136'], 'value': '%'}, {'id': '135', 'type': 'string', 'children': [], 'value': "'[utool] Pre Local Hash: %r'"}; {'id': '136', 'type': 'tuple', 'children': ['137']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'hash_local'}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'call', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '141', 'type': 'argument_list', 'children': ['142']}; {'id': '142', 'type': 'binary_operator', 'children': ['143', '144'], 'value': '%'}, {'id': '143', 'type': 'string', 'children': [], 'value': "'[utool] Pre Remote Hash: %r'"}; {'id': '144', 'type': 'tuple', 'children': ['145']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}; {'id': '146', 'type': 'if_statement', 'children': ['147', '150', '155', '172', '183']}, {'id': '147', 'type': 'comparison_operator', 'children': ['148', '149'], 'value': 'is'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}, {'id': '149', 'type': 'None', 'children': []}; {'id': '150', 'type': 'block', 'children': ['151']}, {'id': '151', 'type': 'expression_statement', 'children': ['152']}; {'id': '152', 'type': 'assignment', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'check_hash'}; {'id': '154', 'type': 'False', 'children': []}, {'id': '155', 'type': 'elif_clause', 'children': ['156', '159']}; {'id': '156', 'type': 'comparison_operator', 'children': ['157', '158'], 'value': 'is'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'hash_local'}; {'id': '158', 'type': 'None', 'children': []}, {'id': '159', 'type': 'block', 'children': ['160', '168']}; {'id': '160', 'type': 'if_statement', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '162', 'type': 'block', 'children': ['163']}, {'id': '163', 'type': 'expression_statement', 'children': ['164']}; {'id': '164', 'type': 'call', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '166', 'type': 'argument_list', 'children': ['167']}, {'id': '167', 'type': 'string', 'children': [], 'value': "'[utool] Remote hash provided but local hash missing, redownloading.'"}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'redownload'}, {'id': '171', 'type': 'True', 'children': []}; {'id': '172', 'type': 'elif_clause', 'children': ['173', '176']}, {'id': '173', 'type': 'comparison_operator', 'children': ['174', '175'], 'value': '=='}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'hash_local'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}; {'id': '176', 'type': 'block', 'children': ['177']}, {'id': '177', 'type': 'assert_statement', 'children': ['178', '181']}; {'id': '178', 'type': 'comparison_operator', 'children': ['179', '180'], 'value': '=='}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'hash_tag_local'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'hash_tag_remote'}, {'id': '181', 'type': 'parenthesized_expression', 'children': ['182']}; {'id': '182', 'type': 'string', 'children': [], 'value': "'hash tag disagreement'"}, {'id': '183', 'type': 'else_clause', 'children': ['184']}; {'id': '184', 'type': 'block', 'children': ['185', '193']}, {'id': '185', 'type': 'if_statement', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '187', 'type': 'block', 'children': ['188']}; {'id': '188', 'type': 'expression_statement', 'children': ['189']}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'string', 'children': [], 'value': "'[utool] Both hashes provided, but they disagree, redownloading.'"}, {'id': '193', 'type': 'expression_statement', 'children': ['194']}; {'id': '194', 'type': 'assignment', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'redownload'}; {'id': '196', 'type': 'True', 'children': []}, {'id': '197', 'type': 'expression_statement', 'children': ['198']}; {'id': '198', 'type': 'call', 'children': ['199', '202']}, {'id': '199', 'type': 'attribute', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'util_path'}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'ensurepath'}; {'id': '202', 'type': 'argument_list', 'children': ['203']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'download_dir'}; {'id': '204', 'type': 'if_statement', 'children': ['205', '212', '252']}, {'id': '205', 'type': 'boolean_operator', 'children': ['206', '207'], 'value': 'or'}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'redownload'}, {'id': '207', 'type': 'not_operator', 'children': ['208']}; {'id': '208', 'type': 'call', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'exists'}; {'id': '210', 'type': 'argument_list', 'children': ['211']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'fpath'}; {'id': '212', 'type': 'block', 'children': ['213', '223', '243']}, {'id': '213', 'type': 'if_statement', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '215', 'type': 'block', 'children': ['216']}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'call', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '219', 'type': 'argument_list', 'children': ['220']}; {'id': '220', 'type': 'binary_operator', 'children': ['221', '222'], 'value': '%'}, {'id': '221', 'type': 'string', 'children': [], 'value': "'[utool] Downloading file %s'"}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'fpath'}, {'id': '223', 'type': 'if_statement', 'children': ['224', '227']}; {'id': '224', 'type': 'comparison_operator', 'children': ['225', '226'], 'value': 'is'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'delay'}; {'id': '226', 'type': 'None', 'children': []}, {'id': '227', 'type': 'block', 'children': ['228', '236']}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'call', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '231', 'type': 'argument_list', 'children': ['232']}; {'id': '232', 'type': 'binary_operator', 'children': ['233', '234'], 'value': '%'}, {'id': '233', 'type': 'string', 'children': [], 'value': "'[utool] delay download by %r seconds'"}; {'id': '234', 'type': 'tuple', 'children': ['235']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'delay'}; {'id': '236', 'type': 'expression_statement', 'children': ['237']}, {'id': '237', 'type': 'call', 'children': ['238', '241']}; {'id': '238', 'type': 'attribute', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'time'}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'sleep'}, {'id': '241', 'type': 'argument_list', 'children': ['242']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'delay'}, {'id': '243', 'type': 'expression_statement', 'children': ['244']}; {'id': '244', 'type': 'call', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'download_url'}; {'id': '246', 'type': 'argument_list', 'children': ['247', '248', '249']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'file_url'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'fpath'}, {'id': '249', 'type': 'keyword_argument', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'spoof'}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'spoof'}; {'id': '252', 'type': 'else_clause', 'children': ['253']}, {'id': '253', 'type': 'block', 'children': ['254']}; {'id': '254', 'type': 'if_statement', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '256', 'type': 'block', 'children': ['257']}, {'id': '257', 'type': 'expression_statement', 'children': ['258']}; {'id': '258', 'type': 'call', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '260', 'type': 'argument_list', 'children': ['261']}, {'id': '261', 'type': 'binary_operator', 'children': ['262', '263'], 'value': '%'}; {'id': '262', 'type': 'string', 'children': [], 'value': "'[utool] Already have file %s'"}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'fpath'}; {'id': '264', 'type': 'expression_statement', 'children': ['265']}, {'id': '265', 'type': 'call', 'children': ['266', '269']}; {'id': '266', 'type': 'attribute', 'children': ['267', '268']}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'util_path'}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'assert_exists'}, {'id': '269', 'type': 'argument_list', 'children': ['270']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'fpath'}, {'id': '271', 'type': 'if_statement', 'children': ['272', '273']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'check_hash'}, {'id': '273', 'type': 'block', 'children': ['274', '282', '301', '314', '325', '330']}; {'id': '274', 'type': 'expression_statement', 'children': ['275']}, {'id': '275', 'type': 'assignment', 'children': ['276', '277']}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'hash_fpath'}, {'id': '277', 'type': 'binary_operator', 'children': ['278', '279'], 'value': '%'}; {'id': '278', 'type': 'string', 'children': [], 'value': "'%s.%s'"}, {'id': '279', 'type': 'tuple', 'children': ['280', '281']}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'fpath'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'hash_tag_remote'}; {'id': '282', 'type': 'with_statement', 'children': ['283', '293']}, {'id': '283', 'type': 'with_clause', 'children': ['284']}; {'id': '284', 'type': 'with_item', 'children': ['285']}, {'id': '285', 'type': 'as_pattern', 'children': ['286', '291']}; {'id': '286', 'type': 'call', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'open'}; {'id': '288', 'type': 'argument_list', 'children': ['289', '290']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'hash_fpath'}; {'id': '290', 'type': 'string', 'children': [], 'value': "'w'"}, {'id': '291', 'type': 'as_pattern_target', 'children': ['292']}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'hash_file'}, {'id': '293', 'type': 'block', 'children': ['294']}; {'id': '294', 'type': 'expression_statement', 'children': ['295']}, {'id': '295', 'type': 'call', 'children': ['296', '299']}; {'id': '296', 'type': 'attribute', 'children': ['297', '298']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'hash_file'}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'write'}, {'id': '299', 'type': 'argument_list', 'children': ['300']}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}, {'id': '301', 'type': 'expression_statement', 'children': ['302']}; {'id': '302', 'type': 'assignment', 'children': ['303', '306']}, {'id': '303', 'type': 'pattern_list', 'children': ['304', '305']}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'hash_local'}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'hash_tag_local'}; {'id': '306', 'type': 'call', 'children': ['307', '308']}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'get_file_local_hash'}; {'id': '308', 'type': 'argument_list', 'children': ['309', '310', '311']}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'fpath'}; {'id': '310', 'type': 'identifier', 'children': [], 'value': 'hash_list'}, {'id': '311', 'type': 'keyword_argument', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '314', 'type': 'if_statement', 'children': ['315', '316']}, {'id': '315', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '316', 'type': 'block', 'children': ['317']}, {'id': '317', 'type': 'expression_statement', 'children': ['318']}; {'id': '318', 'type': 'call', 'children': ['319', '320']}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '320', 'type': 'argument_list', 'children': ['321']}, {'id': '321', 'type': 'binary_operator', 'children': ['322', '323'], 'value': '%'}; {'id': '322', 'type': 'string', 'children': [], 'value': "'[utool] Post Local Hash: %r'"}, {'id': '323', 'type': 'tuple', 'children': ['324']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'hash_local'}, {'id': '325', 'type': 'assert_statement', 'children': ['326', '329']}; {'id': '326', 'type': 'comparison_operator', 'children': ['327', '328'], 'value': '=='}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'hash_local'}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'hash_remote'}, {'id': '329', 'type': 'string', 'children': [], 'value': "'Post-download hash disagreement'"}; {'id': '330', 'type': 'assert_statement', 'children': ['331', '334']}, {'id': '331', 'type': 'comparison_operator', 'children': ['332', '333'], 'value': '=='}; {'id': '332', 'type': 'identifier', 'children': [], 'value': 'hash_tag_local'}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'hash_tag_remote'}; {'id': '334', 'type': 'string', 'children': [], 'value': "'Post-download hash tag disagreement'"}, {'id': '335', 'type': 'return_statement', 'children': ['336']} | def grab_file_url(file_url, appname='utool', download_dir=None, delay=None,
spoof=False, fname=None, verbose=True, redownload=False,
check_hash=False):
r
file_url = clean_dropbox_link(file_url)
if fname is None:
fname = basename(file_url)
if download_dir is None:
download_dir = util_cplat.get_app_cache_dir(appname)
fpath = join(download_dir, fname)
if check_hash:
if isinstance(check_hash, (list, tuple)):
hash_list = check_hash
else:
hash_list = ['md5']
hash_remote, hash_tag_remote = grab_file_remote_hash(file_url, hash_list, verbose=verbose)
hash_list = [hash_tag_remote]
hash_local, hash_tag_local = get_file_local_hash(fpath, hash_list, verbose=verbose)
if verbose:
print('[utool] Pre Local Hash: %r' % (hash_local, ))
print('[utool] Pre Remote Hash: %r' % (hash_remote, ))
if hash_remote is None:
check_hash = False
elif hash_local is None:
if verbose:
print('[utool] Remote hash provided but local hash missing, redownloading.')
redownload = True
elif hash_local == hash_remote:
assert hash_tag_local == hash_tag_remote, ('hash tag disagreement')
else:
if verbose:
print('[utool] Both hashes provided, but they disagree, redownloading.')
redownload = True
util_path.ensurepath(download_dir)
if redownload or not exists(fpath):
if verbose:
print('[utool] Downloading file %s' % fpath)
if delay is not None:
print('[utool] delay download by %r seconds' % (delay,))
time.sleep(delay)
download_url(file_url, fpath, spoof=spoof)
else:
if verbose:
print('[utool] Already have file %s' % fpath)
util_path.assert_exists(fpath)
if check_hash:
hash_fpath = '%s.%s' % (fpath, hash_tag_remote, )
with open(hash_fpath, 'w') as hash_file:
hash_file.write(hash_remote)
hash_local, hash_tag_local = get_file_local_hash(fpath, hash_list, verbose=verbose)
if verbose:
print('[utool] Post Local Hash: %r' % (hash_local, ))
assert hash_local == hash_remote, 'Post-download hash disagreement'
assert hash_tag_local == hash_tag_remote, 'Post-download hash tag disagreement'
return fpath |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_uniprot_evidence_level'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'header'}, {'id': '5', 'type': 'block', 'children': ['6', '14', '49']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'header'}, {'id': '9', 'type': 'call', 'children': ['10', '13']}; {'id': '10', 'type': 'attribute', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'header'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '13', 'type': 'argument_list', 'children': []}; {'id': '14', 'type': 'for_statement', 'children': ['15', '16', '17']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'header'}, {'id': '17', 'type': 'block', 'children': ['18', '27']}; {'id': '18', 'type': 'expression_statement', 'children': ['19']}, {'id': '19', 'type': 'assignment', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '21', 'type': 'call', 'children': ['22', '25']}; {'id': '22', 'type': 'attribute', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'string', 'children': [], 'value': "'='"}, {'id': '27', 'type': 'try_statement', 'children': ['28', '45']}; {'id': '28', 'type': 'block', 'children': ['29']}, {'id': '29', 'type': 'if_statement', 'children': ['30', '35']}; {'id': '30', 'type': 'comparison_operator', 'children': ['31', '34'], 'value': '=='}, {'id': '31', 'type': 'subscript', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'item'}, {'id': '33', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '34', 'type': 'string', 'children': [], 'value': "'PE'"}, {'id': '35', 'type': 'block', 'children': ['36']}; {'id': '36', 'type': 'return_statement', 'children': ['37']}, {'id': '37', 'type': 'binary_operator', 'children': ['38', '39'], 'value': '-'}; {'id': '38', 'type': 'integer', 'children': [], 'value': '5'}, {'id': '39', 'type': 'call', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'int'}, {'id': '41', 'type': 'argument_list', 'children': ['42']}; {'id': '42', 'type': 'subscript', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '44', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '45', 'type': 'except_clause', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'IndexError'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'continue_statement', 'children': []}, {'id': '49', 'type': 'return_statement', 'children': ['50']}; {'id': '50', 'type': 'unary_operator', 'children': ['51'], 'value': '-'}, {'id': '51', 'type': 'integer', 'children': [], 'value': '1'} | def get_uniprot_evidence_level(header):
header = header.split()
for item in header:
item = item.split('=')
try:
if item[0] == 'PE':
return 5 - int(item[1])
except IndexError:
continue
return -1 |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_drain'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'cycles'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '16', '27', '57', '408']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'info'}; {'id': '14', 'type': 'argument_list', 'children': ['15']}, {'id': '15', 'type': 'string', 'children': [], 'value': '"Now draining..."'}; {'id': '16', 'type': 'if_statement', 'children': ['17', '19']}, {'id': '17', 'type': 'not_operator', 'children': ['18']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'cycles'}, {'id': '19', 'type': 'block', 'children': ['20']}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'call', 'children': ['22', '25']}; {'id': '22', 'type': 'attribute', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'info'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'string', 'children': [], 'value': '"No cycle count, the pipeline may be drained forever."'}, {'id': '27', 'type': 'if_statement', 'children': ['28', '31']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'calibration'}, {'id': '31', 'type': 'block', 'children': ['32', '39']}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'call', 'children': ['34', '37']}; {'id': '34', 'type': 'attribute', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'info'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'string', 'children': [], 'value': '"Setting up the detector calibration."'}, {'id': '39', 'type': 'for_statement', 'children': ['40', '41', '44']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '41', 'type': 'attribute', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'modules'}; {'id': '44', 'type': 'block', 'children': ['45']}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '50']}, {'id': '47', 'type': 'attribute', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'detector'}; {'id': '50', 'type': 'call', 'children': ['51', '56']}, {'id': '51', 'type': 'attribute', 'children': ['52', '55']}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'calibration'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'get_detector'}; {'id': '56', 'type': 'argument_list', 'children': []}, {'id': '57', 'type': 'try_statement', 'children': ['58', '397', '398']}; {'id': '58', 'type': 'ERROR', 'children': ['59']}, {'id': '59', 'type': 'block', 'children': ['60', '350', '365', '380', '386']}; {'id': '60', 'type': 'while_statement', 'children': ['61', '65', '95']}, {'id': '61', 'type': 'not_operator', 'children': ['62']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': '_stop'}, {'id': '65', 'type': 'ERROR', 'children': ['66', '72', '78', '81', '87', '90']}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'assignment', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'cycle_start'}, {'id': '69', 'type': 'call', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'timer'}, {'id': '71', 'type': 'argument_list', 'children': []}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'cycle_start_cpu'}, {'id': '75', 'type': 'call', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'process_time'}, {'id': '77', 'type': 'argument_list', 'children': []}; {'id': '78', 'type': 'attribute', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '81', 'type': 'ERROR', 'children': ['82', '83', '84']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'Pumping'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'blob'}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'blob'}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'Blob'}, {'id': '89', 'type': 'argument_list', 'children': []}; {'id': '90', 'type': 'for_in_clause', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'modules'}, {'id': '95', 'type': 'block', 'children': ['96', '118', '162', '194', '234', '248', '254', '260', '267', '312']}; {'id': '96', 'type': 'if_statement', 'children': ['97', '102']}, {'id': '97', 'type': 'comparison_operator', 'children': ['98', '101'], 'value': 'is'}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'blob'}, {'id': '101', 'type': 'None', 'children': []}; {'id': '102', 'type': 'block', 'children': ['103', '117']}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'call', 'children': ['105', '108']}, {'id': '105', 'type': 'attribute', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '108', 'type': 'argument_list', 'children': ['109']}, {'id': '109', 'type': 'call', 'children': ['110', '113']}; {'id': '110', 'type': 'attribute', 'children': ['111', '112']}, {'id': '111', 'type': 'string', 'children': [], 'value': '"Skipping {0}, due to empty blob."'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '113', 'type': 'argument_list', 'children': ['114']}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '117', 'type': 'continue_statement', 'children': []}; {'id': '118', 'type': 'if_statement', 'children': ['119', '141']}, {'id': '119', 'type': 'boolean_operator', 'children': ['120', '123'], 'value': 'and'}; {'id': '120', 'type': 'attribute', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'only_if'}, {'id': '123', 'type': 'not_operator', 'children': ['124']}; {'id': '124', 'type': 'call', 'children': ['125', '130']}, {'id': '125', 'type': 'attribute', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'only_if'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'issubset'}; {'id': '130', 'type': 'argument_list', 'children': ['131']}, {'id': '131', 'type': 'call', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '133', 'type': 'argument_list', 'children': ['134']}; {'id': '134', 'type': 'call', 'children': ['135', '140']}, {'id': '135', 'type': 'attribute', 'children': ['136', '139']}; {'id': '136', 'type': 'attribute', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'blob'}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '140', 'type': 'argument_list', 'children': []}, {'id': '141', 'type': 'block', 'children': ['142', '161']}; {'id': '142', 'type': 'expression_statement', 'children': ['143']}, {'id': '143', 'type': 'call', 'children': ['144', '147']}; {'id': '144', 'type': 'attribute', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '147', 'type': 'argument_list', 'children': ['148']}; {'id': '148', 'type': 'call', 'children': ['149', '154']}, {'id': '149', 'type': 'attribute', 'children': ['150', '153']}; {'id': '150', 'type': 'concatenated_string', 'children': ['151', '152']}, {'id': '151', 'type': 'string', 'children': [], 'value': '"Skipping {0}, due to missing required key"'}; {'id': '152', 'type': 'string', 'children': [], 'value': '"\'{1}\'."'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '154', 'type': 'argument_list', 'children': ['155', '158']}, {'id': '155', 'type': 'attribute', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '158', 'type': 'attribute', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'only_if'}, {'id': '161', 'type': 'continue_statement', 'children': []}; {'id': '162', 'type': 'if_statement', 'children': ['163', '175']}, {'id': '163', 'type': 'comparison_operator', 'children': ['164', '174'], 'value': '!='}; {'id': '164', 'type': 'binary_operator', 'children': ['165', '171'], 'value': '%'}, {'id': '165', 'type': 'parenthesized_expression', 'children': ['166']}; {'id': '166', 'type': 'binary_operator', 'children': ['167', '170'], 'value': '+'}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': '_cycle_count'}; {'id': '170', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '171', 'type': 'attribute', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'every'}; {'id': '174', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '175', 'type': 'block', 'children': ['176', '193']}; {'id': '176', 'type': 'expression_statement', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '181']}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '181', 'type': 'argument_list', 'children': ['182']}; {'id': '182', 'type': 'call', 'children': ['183', '186']}, {'id': '183', 'type': 'attribute', 'children': ['184', '185']}; {'id': '184', 'type': 'string', 'children': [], 'value': '"Skipping {0} (every {1} iterations)."'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '186', 'type': 'argument_list', 'children': ['187', '190']}, {'id': '187', 'type': 'attribute', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '190', 'type': 'attribute', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'every'}, {'id': '193', 'type': 'continue_statement', 'children': []}; {'id': '194', 'type': 'if_statement', 'children': ['195', '200', '226']}, {'id': '195', 'type': 'comparison_operator', 'children': ['196', '199'], 'value': 'is'}; {'id': '196', 'type': 'attribute', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'blob_keys'}, {'id': '199', 'type': 'None', 'children': []}; {'id': '200', 'type': 'block', 'children': ['201']}, {'id': '201', 'type': 'expression_statement', 'children': ['202']}; {'id': '202', 'type': 'assignment', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'blob_to_send'}; {'id': '204', 'type': 'call', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'Blob'}; {'id': '206', 'type': 'argument_list', 'children': ['207']}, {'id': '207', 'type': 'dictionary_comprehension', 'children': ['208', '215', '220']}; {'id': '208', 'type': 'pair', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '210', 'type': 'subscript', 'children': ['211', '214']}, {'id': '211', 'type': 'attribute', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'blob'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '215', 'type': 'for_in_clause', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '217', 'type': 'attribute', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'blob_keys'}; {'id': '220', 'type': 'if_clause', 'children': ['221']}, {'id': '221', 'type': 'comparison_operator', 'children': ['222', '223'], 'value': 'in'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '223', 'type': 'attribute', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'blob'}; {'id': '226', 'type': 'else_clause', 'children': ['227']}, {'id': '227', 'type': 'block', 'children': ['228']}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'assignment', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'blob_to_send'}, {'id': '231', 'type': 'attribute', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'blob'}; {'id': '234', 'type': 'expression_statement', 'children': ['235']}, {'id': '235', 'type': 'call', 'children': ['236', '239']}; {'id': '236', 'type': 'attribute', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '239', 'type': 'argument_list', 'children': ['240']}; {'id': '240', 'type': 'call', 'children': ['241', '244']}, {'id': '241', 'type': 'attribute', 'children': ['242', '243']}; {'id': '242', 'type': 'string', 'children': [], 'value': '"Processing {0} "'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '244', 'type': 'argument_list', 'children': ['245']}, {'id': '245', 'type': 'attribute', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '248', 'type': 'expression_statement', 'children': ['249']}, {'id': '249', 'type': 'assignment', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'start'}, {'id': '251', 'type': 'call', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'timer'}, {'id': '253', 'type': 'argument_list', 'children': []}; {'id': '254', 'type': 'expression_statement', 'children': ['255']}, {'id': '255', 'type': 'assignment', 'children': ['256', '257']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'start_cpu'}, {'id': '257', 'type': 'call', 'children': ['258', '259']}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'process_time'}, {'id': '259', 'type': 'argument_list', 'children': []}; {'id': '260', 'type': 'expression_statement', 'children': ['261']}, {'id': '261', 'type': 'assignment', 'children': ['262', '263']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'new_blob'}, {'id': '263', 'type': 'call', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '265', 'type': 'argument_list', 'children': ['266']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'blob_to_send'}, {'id': '267', 'type': 'if_statement', 'children': ['268', '275']}; {'id': '268', 'type': 'boolean_operator', 'children': ['269', '272'], 'value': 'or'}, {'id': '269', 'type': 'attribute', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'timeit'}; {'id': '272', 'type': 'attribute', 'children': ['273', '274']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'timeit'}, {'id': '275', 'type': 'block', 'children': ['276', '294']}; {'id': '276', 'type': 'expression_statement', 'children': ['277']}, {'id': '277', 'type': 'call', 'children': ['278', '288']}; {'id': '278', 'type': 'attribute', 'children': ['279', '286', '287']}, {'id': '279', 'type': 'subscript', 'children': ['280', '285']}; {'id': '280', 'type': 'subscript', 'children': ['281', '284']}, {'id': '281', 'type': 'attribute', 'children': ['282', '283']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '283', 'type': 'identifier', 'children': [], 'value': '_timeit'}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '285', 'type': 'string', 'children': [], 'value': "'process'"}; {'id': '286', 'type': 'line_continuation', 'children': [], 'value': '\\'}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '288', 'type': 'argument_list', 'children': ['289']}, {'id': '289', 'type': 'binary_operator', 'children': ['290', '293'], 'value': '-'}; {'id': '290', 'type': 'call', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'timer'}; {'id': '292', 'type': 'argument_list', 'children': []}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'start'}; {'id': '294', 'type': 'expression_statement', 'children': ['295']}, {'id': '295', 'type': 'call', 'children': ['296', '306']}; {'id': '296', 'type': 'attribute', 'children': ['297', '304', '305']}, {'id': '297', 'type': 'subscript', 'children': ['298', '303']}; {'id': '298', 'type': 'subscript', 'children': ['299', '302']}, {'id': '299', 'type': 'attribute', 'children': ['300', '301']}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '301', 'type': 'identifier', 'children': [], 'value': '_timeit'}; {'id': '302', 'type': 'identifier', 'children': [], 'value': 'module'}, {'id': '303', 'type': 'string', 'children': [], 'value': "'process_cpu'"}; {'id': '304', 'type': 'line_continuation', 'children': [], 'value': '\\'}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '306', 'type': 'argument_list', 'children': ['307']}, {'id': '307', 'type': 'binary_operator', 'children': ['308', '311'], 'value': '-'}; {'id': '308', 'type': 'call', 'children': ['309', '310']}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'process_time'}; {'id': '310', 'type': 'argument_list', 'children': []}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'start_cpu'}; {'id': '312', 'type': 'if_statement', 'children': ['313', '318', '342']}, {'id': '313', 'type': 'comparison_operator', 'children': ['314', '317'], 'value': 'is'}; {'id': '314', 'type': 'attribute', 'children': ['315', '316']}, {'id': '315', 'type': 'identifier', 'children': [], 'value': 'module'}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'blob_keys'}, {'id': '317', 'type': 'None', 'children': []}; {'id': '318', 'type': 'block', 'children': ['319']}, {'id': '319', 'type': 'if_statement', 'children': ['320', '323']}; {'id': '320', 'type': 'comparison_operator', 'children': ['321', '322'], 'value': 'is'}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'new_blob'}; {'id': '322', 'type': 'None', 'children': []}, {'id': '323', 'type': 'block', 'children': ['324']}; {'id': '324', 'type': 'for_statement', 'children': ['325', '326', '331']}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '326', 'type': 'call', 'children': ['327', '330']}, {'id': '327', 'type': 'attribute', 'children': ['328', '329']}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'new_blob'}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '330', 'type': 'argument_list', 'children': []}, {'id': '331', 'type': 'block', 'children': ['332']}; {'id': '332', 'type': 'expression_statement', 'children': ['333']}, {'id': '333', 'type': 'assignment', 'children': ['334', '339']}; {'id': '334', 'type': 'subscript', 'children': ['335', '338']}, {'id': '335', 'type': 'attribute', 'children': ['336', '337']}; {'id': '336', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '337', 'type': 'identifier', 'children': [], 'value': 'blob'}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '339', 'type': 'subscript', 'children': ['340', '341']}; {'id': '340', 'type': 'identifier', 'children': [], 'value': 'new_blob'}, {'id': '341', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '342', 'type': 'else_clause', 'children': ['343']}, {'id': '343', 'type': 'block', 'children': ['344']}; {'id': '344', 'type': 'expression_statement', 'children': ['345']}, {'id': '345', 'type': 'assignment', 'children': ['346', '349']}; {'id': '346', 'type': 'attribute', 'children': ['347', '348']}, {'id': '347', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '348', 'type': 'identifier', 'children': [], 'value': 'blob'}, {'id': '349', 'type': 'identifier', 'children': [], 'value': 'new_blob'}; {'id': '350', 'type': 'expression_statement', 'children': ['351']}, {'id': '351', 'type': 'call', 'children': ['352', '359']}; {'id': '352', 'type': 'attribute', 'children': ['353', '358']}, {'id': '353', 'type': 'subscript', 'children': ['354', '357']}; {'id': '354', 'type': 'attribute', 'children': ['355', '356']}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '356', 'type': 'identifier', 'children': [], 'value': '_timeit'}, {'id': '357', 'type': 'string', 'children': [], 'value': "'cycles'"}; {'id': '358', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '359', 'type': 'argument_list', 'children': ['360']}; {'id': '360', 'type': 'binary_operator', 'children': ['361', '364'], 'value': '-'}, {'id': '361', 'type': 'call', 'children': ['362', '363']}; {'id': '362', 'type': 'identifier', 'children': [], 'value': 'timer'}, {'id': '363', 'type': 'argument_list', 'children': []}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'cycle_start'}, {'id': '365', 'type': 'expression_statement', 'children': ['366']}; {'id': '366', 'type': 'call', 'children': ['367', '374']}, {'id': '367', 'type': 'attribute', 'children': ['368', '373']}; {'id': '368', 'type': 'subscript', 'children': ['369', '372']}, {'id': '369', 'type': 'attribute', 'children': ['370', '371']}; {'id': '370', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '371', 'type': 'identifier', 'children': [], 'value': '_timeit'}; {'id': '372', 'type': 'string', 'children': [], 'value': "'cycles_cpu'"}, {'id': '373', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '374', 'type': 'argument_list', 'children': ['375']}, {'id': '375', 'type': 'binary_operator', 'children': ['376', '379'], 'value': '-'}; {'id': '376', 'type': 'call', 'children': ['377', '378']}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'process_time'}; {'id': '378', 'type': 'argument_list', 'children': []}, {'id': '379', 'type': 'identifier', 'children': [], 'value': 'cycle_start_cpu'}; {'id': '380', 'type': 'expression_statement', 'children': ['381']}, {'id': '381', 'type': 'augmented_assignment', 'children': ['382', '385'], 'value': '+='}; {'id': '382', 'type': 'attribute', 'children': ['383', '384']}, {'id': '383', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '384', 'type': 'identifier', 'children': [], 'value': '_cycle_count'}, {'id': '385', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '386', 'type': 'if_statement', 'children': ['387', '394']}, {'id': '387', 'type': 'boolean_operator', 'children': ['388', '389'], 'value': 'and'}; {'id': '388', 'type': 'identifier', 'children': [], 'value': 'cycles'}, {'id': '389', 'type': 'comparison_operator', 'children': ['390', '393'], 'value': '>='}; {'id': '390', 'type': 'attribute', 'children': ['391', '392']}, {'id': '391', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '392', 'type': 'identifier', 'children': [], 'value': '_cycle_count'}, {'id': '393', 'type': 'identifier', 'children': [], 'value': 'cycles'}; {'id': '394', 'type': 'block', 'children': ['395']}, {'id': '395', 'type': 'raise_statement', 'children': ['396']}; {'id': '396', 'type': 'identifier', 'children': [], 'value': 'StopIteration'}, {'id': '397', 'type': 'block', 'children': [], 'value': ''}; {'id': '398', 'type': 'except_clause', 'children': ['399', '400']}, {'id': '399', 'type': 'identifier', 'children': [], 'value': 'StopIteration'}; {'id': '400', 'type': 'block', 'children': ['401']}, {'id': '401', 'type': 'expression_statement', 'children': ['402']}; {'id': '402', 'type': 'call', 'children': ['403', '406']}, {'id': '403', 'type': 'attribute', 'children': ['404', '405']}; {'id': '404', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '405', 'type': 'identifier', 'children': [], 'value': 'info'}; {'id': '406', 'type': 'argument_list', 'children': ['407']}, {'id': '407', 'type': 'string', 'children': [], 'value': '"Nothing left to pump through."'}; {'id': '408', 'type': 'return_statement', 'children': ['409']}, {'id': '409', 'type': 'call', 'children': ['410', '413']}; {'id': '410', 'type': 'attribute', 'children': ['411', '412']}, {'id': '411', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '412', 'type': 'identifier', 'children': [], 'value': 'finish'}, {'id': '413', 'type': 'argument_list', 'children': []} | def _drain(self, cycles=None):
log.info("Now draining...")
if not cycles:
log.info("No cycle count, the pipeline may be drained forever.")
if self.calibration:
log.info("Setting up the detector calibration.")
for module in self.modules:
module.detector = self.calibration.get_detector()
try:
while not self._stop:
cycle_start = timer()
cycle_start_cpu = process_time()
log.debug("Pumping blob
self.blob = Blob()
for module in self.modules:
if self.blob is None:
log.debug(
"Skipping {0}, due to empty blob.".format(
module.name
)
)
continue
if module.only_if and not module.only_if.issubset(set(
self.blob.keys())):
log.debug(
"Skipping {0}, due to missing required key"
"'{1}'.".format(module.name, module.only_if)
)
continue
if (self._cycle_count + 1) % module.every != 0:
log.debug(
"Skipping {0} (every {1} iterations).".format(
module.name, module.every
)
)
continue
if module.blob_keys is not None:
blob_to_send = Blob({
k: self.blob[k]
for k in module.blob_keys
if k in self.blob
})
else:
blob_to_send = self.blob
log.debug("Processing {0} ".format(module.name))
start = timer()
start_cpu = process_time()
new_blob = module(blob_to_send)
if self.timeit or module.timeit:
self._timeit[module]['process'] \
.append(timer() - start)
self._timeit[module]['process_cpu'] \
.append(process_time() - start_cpu)
if module.blob_keys is not None:
if new_blob is not None:
for key in new_blob.keys():
self.blob[key] = new_blob[key]
else:
self.blob = new_blob
self._timeit['cycles'].append(timer() - cycle_start)
self._timeit['cycles_cpu'].append(
process_time() - cycle_start_cpu
)
self._cycle_count += 1
if cycles and self._cycle_count >= cycles:
raise StopIteration
except StopIteration:
log.info("Nothing left to pump through.")
return self.finish() |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '7']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'start'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'default_parameter', 'children': ['5', '6']}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '6', 'type': 'None', 'children': []}, {'id': '7', 'type': 'block', 'children': ['8', '20', '38', '50', '56', '122', '130', '138', '146', '154']}; {'id': '8', 'type': 'if_statement', 'children': ['9', '14']}, {'id': '9', 'type': 'attribute', 'children': ['10', '13']}; {'id': '10', 'type': 'attribute', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'cum'}; {'id': '14', 'type': 'block', 'children': ['15']}, {'id': '15', 'type': 'raise_statement', 'children': ['16']}; {'id': '16', 'type': 'call', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'StartError'}; {'id': '18', 'type': 'argument_list', 'children': ['19']}, {'id': '19', 'type': 'string', 'children': [], 'value': '"Already have stamps, can\'t start again (must reset)."'}; {'id': '20', 'type': 'if_statement', 'children': ['21', '32']}, {'id': '21', 'type': 'boolean_operator', 'children': ['22', '27'], 'value': 'or'}; {'id': '22', 'type': 'attribute', 'children': ['23', '26']}, {'id': '23', 'type': 'attribute', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'subdvsn_awaiting'}, {'id': '27', 'type': 'attribute', 'children': ['28', '31']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'par_subdvsn_awaiting'}; {'id': '32', 'type': 'block', 'children': ['33']}, {'id': '33', 'type': 'raise_statement', 'children': ['34']}; {'id': '34', 'type': 'call', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'StartError'}; {'id': '36', 'type': 'argument_list', 'children': ['37']}, {'id': '37', 'type': 'string', 'children': [], 'value': '"Already have subdivisions, can\'t start again (must reset)."'}; {'id': '38', 'type': 'if_statement', 'children': ['39', '44']}, {'id': '39', 'type': 'attribute', 'children': ['40', '43']}; {'id': '40', 'type': 'attribute', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'stopped'}; {'id': '44', 'type': 'block', 'children': ['45']}, {'id': '45', 'type': 'raise_statement', 'children': ['46']}; {'id': '46', 'type': 'call', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'StoppedError'}; {'id': '48', 'type': 'argument_list', 'children': ['49']}, {'id': '49', 'type': 'string', 'children': [], 'value': '"Timer already stopped (must open new or reset)."'}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '53', 'type': 'call', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'timer'}, {'id': '55', 'type': 'argument_list', 'children': []}; {'id': '56', 'type': 'if_statement', 'children': ['57', '60', '65']}, {'id': '57', 'type': 'comparison_operator', 'children': ['58', '59'], 'value': 'is'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '59', 'type': 'None', 'children': []}; {'id': '60', 'type': 'block', 'children': ['61']}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 't_start'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '65', 'type': 'else_clause', 'children': ['66']}; {'id': '66', 'type': 'block', 'children': ['67', '81', '94', '104', '118']}, {'id': '67', 'type': 'if_statement', 'children': ['68', '75']}; {'id': '68', 'type': 'comparison_operator', 'children': ['69', '72'], 'value': 'is'}, {'id': '69', 'type': 'attribute', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'root'}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'raise_statement', 'children': ['77']}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}, {'id': '79', 'type': 'argument_list', 'children': ['80']}; {'id': '80', 'type': 'string', 'children': [], 'value': '"Cannot backdate start of root timer."'}, {'id': '81', 'type': 'if_statement', 'children': ['82', '88']}; {'id': '82', 'type': 'not_operator', 'children': ['83']}, {'id': '83', 'type': 'call', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '85', 'type': 'argument_list', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'float'}; {'id': '88', 'type': 'block', 'children': ['89']}, {'id': '89', 'type': 'raise_statement', 'children': ['90']}; {'id': '90', 'type': 'call', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'TypeError'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'string', 'children': [], 'value': '"Backdate must be type float."'}; {'id': '94', 'type': 'if_statement', 'children': ['95', '98']}, {'id': '95', 'type': 'comparison_operator', 'children': ['96', '97'], 'value': '>'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '98', 'type': 'block', 'children': ['99']}, {'id': '99', 'type': 'raise_statement', 'children': ['100']}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'string', 'children': [], 'value': '"Cannot backdate to future time."'}; {'id': '104', 'type': 'if_statement', 'children': ['105', '112']}, {'id': '105', 'type': 'comparison_operator', 'children': ['106', '107'], 'value': '<'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '107', 'type': 'attribute', 'children': ['108', '111']}; {'id': '108', 'type': 'attribute', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'tm1'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'last_t'}; {'id': '112', 'type': 'block', 'children': ['113']}, {'id': '113', 'type': 'raise_statement', 'children': ['114']}; {'id': '114', 'type': 'call', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}; {'id': '116', 'type': 'argument_list', 'children': ['117']}, {'id': '117', 'type': 'string', 'children': [], 'value': '"Cannot backdate start to time previous to latest stamp in parent timer."'}; {'id': '118', 'type': 'expression_statement', 'children': ['119']}, {'id': '119', 'type': 'assignment', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 't_start'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '122', 'type': 'expression_statement', 'children': ['123']}, {'id': '123', 'type': 'assignment', 'children': ['124', '129']}; {'id': '124', 'type': 'attribute', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'paused'}, {'id': '129', 'type': 'False', 'children': []}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'assignment', 'children': ['132', '137']}; {'id': '132', 'type': 'attribute', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'tmp_total'}, {'id': '137', 'type': 'float', 'children': [], 'value': '0.'}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'assignment', 'children': ['140', '145']}; {'id': '140', 'type': 'attribute', 'children': ['141', '144']}, {'id': '141', 'type': 'attribute', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'start_t'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 't_start'}; {'id': '146', 'type': 'expression_statement', 'children': ['147']}, {'id': '147', 'type': 'assignment', 'children': ['148', '153']}; {'id': '148', 'type': 'attribute', 'children': ['149', '152']}, {'id': '149', 'type': 'attribute', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'last_t'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 't_start'}; {'id': '154', 'type': 'return_statement', 'children': ['155']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 't'} | def start(backdate=None):
if f.s.cum:
raise StartError("Already have stamps, can't start again (must reset).")
if f.t.subdvsn_awaiting or f.t.par_subdvsn_awaiting:
raise StartError("Already have subdivisions, can't start again (must reset).")
if f.t.stopped:
raise StoppedError("Timer already stopped (must open new or reset).")
t = timer()
if backdate is None:
t_start = t
else:
if f.t is f.root:
raise BackdateError("Cannot backdate start of root timer.")
if not isinstance(backdate, float):
raise TypeError("Backdate must be type float.")
if backdate > t:
raise BackdateError("Cannot backdate to future time.")
if backdate < f.tm1.last_t:
raise BackdateError("Cannot backdate start to time previous to latest stamp in parent timer.")
t_start = backdate
f.t.paused = False
f.t.tmp_total = 0.
f.t.start_t = t_start
f.t.last_t = t_start
return t |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '26']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'stamp'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '14', '17', '20', '23']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '13', 'type': 'None', 'children': []}; {'id': '14', 'type': 'default_parameter', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '16', 'type': 'None', 'children': []}, {'id': '17', 'type': 'default_parameter', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'un'}, {'id': '19', 'type': 'None', 'children': []}; {'id': '20', 'type': 'default_parameter', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'ks'}; {'id': '22', 'type': 'None', 'children': []}, {'id': '23', 'type': 'default_parameter', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'qp'}, {'id': '25', 'type': 'None', 'children': []}; {'id': '26', 'type': 'block', 'children': ['27', '33', '45', '57', '109', '119', '140', '161', '182', '191', '199', '207', '217']}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '30', 'type': 'call', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'timer'}; {'id': '32', 'type': 'argument_list', 'children': []}, {'id': '33', 'type': 'if_statement', 'children': ['34', '39']}; {'id': '34', 'type': 'attribute', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'stopped'}, {'id': '39', 'type': 'block', 'children': ['40']}; {'id': '40', 'type': 'raise_statement', 'children': ['41']}, {'id': '41', 'type': 'call', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'StoppedError'}, {'id': '43', 'type': 'argument_list', 'children': ['44']}; {'id': '44', 'type': 'string', 'children': [], 'value': '"Cannot stamp stopped timer."'}, {'id': '45', 'type': 'if_statement', 'children': ['46', '51']}; {'id': '46', 'type': 'attribute', 'children': ['47', '50']}, {'id': '47', 'type': 'attribute', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'paused'}, {'id': '51', 'type': 'block', 'children': ['52']}; {'id': '52', 'type': 'raise_statement', 'children': ['53']}, {'id': '53', 'type': 'call', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'PausedError'}, {'id': '55', 'type': 'argument_list', 'children': ['56']}; {'id': '56', 'type': 'string', 'children': [], 'value': '"Cannot stamp paused timer."'}, {'id': '57', 'type': 'if_statement', 'children': ['58', '61', '66']}; {'id': '58', 'type': 'comparison_operator', 'children': ['59', '60'], 'value': 'is'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '60', 'type': 'None', 'children': []}, {'id': '61', 'type': 'block', 'children': ['62']}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 't_stamp'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '66', 'type': 'else_clause', 'children': ['67']}, {'id': '67', 'type': 'block', 'children': ['68', '81', '91', '105']}; {'id': '68', 'type': 'if_statement', 'children': ['69', '75']}, {'id': '69', 'type': 'not_operator', 'children': ['70']}; {'id': '70', 'type': 'call', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '72', 'type': 'argument_list', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'float'}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'raise_statement', 'children': ['77']}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '79', 'type': 'argument_list', 'children': ['80']}; {'id': '80', 'type': 'string', 'children': [], 'value': '"Backdate must be type float."'}, {'id': '81', 'type': 'if_statement', 'children': ['82', '85']}; {'id': '82', 'type': 'comparison_operator', 'children': ['83', '84'], 'value': '>'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '85', 'type': 'block', 'children': ['86']}; {'id': '86', 'type': 'raise_statement', 'children': ['87']}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'string', 'children': [], 'value': '"Cannot backdate to future time."'}, {'id': '91', 'type': 'if_statement', 'children': ['92', '99']}; {'id': '92', 'type': 'comparison_operator', 'children': ['93', '94'], 'value': '<'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '94', 'type': 'attribute', 'children': ['95', '98']}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'last_t'}, {'id': '99', 'type': 'block', 'children': ['100']}; {'id': '100', 'type': 'raise_statement', 'children': ['101']}, {'id': '101', 'type': 'call', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'string', 'children': [], 'value': '"Cannot backdate to time earlier than last stamp."'}, {'id': '105', 'type': 'expression_statement', 'children': ['106']}; {'id': '106', 'type': 'assignment', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 't_stamp'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'elapsed'}; {'id': '112', 'type': 'binary_operator', 'children': ['113', '114'], 'value': '-'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 't_stamp'}; {'id': '114', 'type': 'attribute', 'children': ['115', '118']}, {'id': '115', 'type': 'attribute', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'last_t'}, {'id': '119', 'type': 'expression_statement', 'children': ['120']}; {'id': '120', 'type': 'assignment', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '122', 'type': 'conditional_expression', 'children': ['123', '126', '134'], 'value': 'if'}, {'id': '123', 'type': 'subscript', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '125', 'type': 'string', 'children': [], 'value': "'UN'"}; {'id': '126', 'type': 'parenthesized_expression', 'children': ['127']}, {'id': '127', 'type': 'boolean_operator', 'children': ['128', '131'], 'value': 'and'}; {'id': '128', 'type': 'comparison_operator', 'children': ['129', '130'], 'value': 'is'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '130', 'type': 'None', 'children': []}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '133'], 'value': 'is'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'un'}, {'id': '133', 'type': 'None', 'children': []}; {'id': '134', 'type': 'call', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'bool'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'boolean_operator', 'children': ['138', '139'], 'value': 'or'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'unique'}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'un'}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'assignment', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '143', 'type': 'conditional_expression', 'children': ['144', '147', '155'], 'value': 'if'}; {'id': '144', 'type': 'subscript', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '146', 'type': 'string', 'children': [], 'value': "'KS'"}, {'id': '147', 'type': 'parenthesized_expression', 'children': ['148']}; {'id': '148', 'type': 'boolean_operator', 'children': ['149', '152'], 'value': 'and'}, {'id': '149', 'type': 'comparison_operator', 'children': ['150', '151'], 'value': 'is'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '151', 'type': 'None', 'children': []}; {'id': '152', 'type': 'comparison_operator', 'children': ['153', '154'], 'value': 'is'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'ks'}; {'id': '154', 'type': 'None', 'children': []}, {'id': '155', 'type': 'call', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'bool'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'boolean_operator', 'children': ['159', '160'], 'value': 'or'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'ks'}, {'id': '161', 'type': 'expression_statement', 'children': ['162']}; {'id': '162', 'type': 'assignment', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '164', 'type': 'conditional_expression', 'children': ['165', '168', '176'], 'value': 'if'}, {'id': '165', 'type': 'subscript', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '167', 'type': 'string', 'children': [], 'value': "'QP'"}; {'id': '168', 'type': 'parenthesized_expression', 'children': ['169']}, {'id': '169', 'type': 'boolean_operator', 'children': ['170', '173'], 'value': 'and'}; {'id': '170', 'type': 'comparison_operator', 'children': ['171', '172'], 'value': 'is'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '172', 'type': 'None', 'children': []}, {'id': '173', 'type': 'comparison_operator', 'children': ['174', '175'], 'value': 'is'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'qp'}, {'id': '175', 'type': 'None', 'children': []}; {'id': '176', 'type': 'call', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'bool'}; {'id': '178', 'type': 'argument_list', 'children': ['179']}, {'id': '179', 'type': 'boolean_operator', 'children': ['180', '181'], 'value': 'or'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'quick_print'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'qp'}; {'id': '182', 'type': 'expression_statement', 'children': ['183']}, {'id': '183', 'type': 'call', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': '_stamp'}, {'id': '185', 'type': 'argument_list', 'children': ['186', '187', '188', '189', '190']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'elapsed'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'unique'}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'quick_print'}, {'id': '191', 'type': 'expression_statement', 'children': ['192']}; {'id': '192', 'type': 'assignment', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'tmp_self'}; {'id': '194', 'type': 'binary_operator', 'children': ['195', '198'], 'value': '-'}, {'id': '195', 'type': 'call', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'timer'}, {'id': '197', 'type': 'argument_list', 'children': []}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '199', 'type': 'expression_statement', 'children': ['200']}; {'id': '200', 'type': 'augmented_assignment', 'children': ['201', '206'], 'value': '+='}, {'id': '201', 'type': 'attribute', 'children': ['202', '205']}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'self_cut'}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'tmp_self'}, {'id': '207', 'type': 'expression_statement', 'children': ['208']}; {'id': '208', 'type': 'assignment', 'children': ['209', '214']}, {'id': '209', 'type': 'attribute', 'children': ['210', '213']}; {'id': '210', 'type': 'attribute', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'last_t'}; {'id': '214', 'type': 'binary_operator', 'children': ['215', '216'], 'value': '+'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 't_stamp'}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'tmp_self'}, {'id': '217', 'type': 'return_statement', 'children': ['218']} | def stamp(name, backdate=None,
unique=None, keep_subdivisions=None, quick_print=None,
un=None, ks=None, qp=None):
t = timer()
if f.t.stopped:
raise StoppedError("Cannot stamp stopped timer.")
if f.t.paused:
raise PausedError("Cannot stamp paused timer.")
if backdate is None:
t_stamp = t
else:
if not isinstance(backdate, float):
raise TypeError("Backdate must be type float.")
if backdate > t:
raise BackdateError("Cannot backdate to future time.")
if backdate < f.t.last_t:
raise BackdateError("Cannot backdate to time earlier than last stamp.")
t_stamp = backdate
elapsed = t_stamp - f.t.last_t
unique = SET['UN'] if (unique is None and un is None) else bool(unique or un)
keep_subdivisions = SET['KS'] if (keep_subdivisions is None and ks is None) else bool(keep_subdivisions or ks)
quick_print = SET['QP'] if (quick_print is None and qp is None) else bool(quick_print or qp)
_stamp(name, elapsed, unique, keep_subdivisions, quick_print)
tmp_self = timer() - t
f.t.self_cut += tmp_self
f.t.last_t = t_stamp + tmp_self
return t |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '28']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'stop'}, {'id': '3', 'type': 'parameters', 'children': ['4', '7', '10', '13', '16', '19', '22', '25']}; {'id': '4', 'type': 'default_parameter', 'children': ['5', '6']}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '6', 'type': 'None', 'children': []}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '9', 'type': 'None', 'children': []}; {'id': '10', 'type': 'default_parameter', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '12', 'type': 'None', 'children': []}, {'id': '13', 'type': 'default_parameter', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '15', 'type': 'None', 'children': []}; {'id': '16', 'type': 'default_parameter', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '18', 'type': 'None', 'children': []}, {'id': '19', 'type': 'default_parameter', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'un'}, {'id': '21', 'type': 'None', 'children': []}; {'id': '22', 'type': 'default_parameter', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'ks'}; {'id': '24', 'type': 'None', 'children': []}, {'id': '25', 'type': 'default_parameter', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'qp'}, {'id': '27', 'type': 'None', 'children': []}; {'id': '28', 'type': 'block', 'children': ['29', '35', '47', '113', '134', '155', '176', '222', '260', '282', '294', '306', '312', '320', '342']}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '32', 'type': 'call', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'timer'}; {'id': '34', 'type': 'argument_list', 'children': []}, {'id': '35', 'type': 'if_statement', 'children': ['36', '41']}; {'id': '36', 'type': 'attribute', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'stopped'}, {'id': '41', 'type': 'block', 'children': ['42']}; {'id': '42', 'type': 'raise_statement', 'children': ['43']}, {'id': '43', 'type': 'call', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'StoppedError'}, {'id': '45', 'type': 'argument_list', 'children': ['46']}; {'id': '46', 'type': 'string', 'children': [], 'value': '"Timer already stopped."'}, {'id': '47', 'type': 'if_statement', 'children': ['48', '51', '56']}; {'id': '48', 'type': 'comparison_operator', 'children': ['49', '50'], 'value': 'is'}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '50', 'type': 'None', 'children': []}, {'id': '51', 'type': 'block', 'children': ['52']}; {'id': '52', 'type': 'expression_statement', 'children': ['53']}, {'id': '53', 'type': 'assignment', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 't_stop'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '56', 'type': 'else_clause', 'children': ['57']}, {'id': '57', 'type': 'block', 'children': ['58', '72', '85', '95', '109']}; {'id': '58', 'type': 'if_statement', 'children': ['59', '66']}, {'id': '59', 'type': 'comparison_operator', 'children': ['60', '63'], 'value': 'is'}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '63', 'type': 'attribute', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'root'}; {'id': '66', 'type': 'block', 'children': ['67']}, {'id': '67', 'type': 'raise_statement', 'children': ['68']}; {'id': '68', 'type': 'call', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}; {'id': '70', 'type': 'argument_list', 'children': ['71']}, {'id': '71', 'type': 'string', 'children': [], 'value': '"Cannot backdate stop of root timer."'}; {'id': '72', 'type': 'if_statement', 'children': ['73', '79']}, {'id': '73', 'type': 'not_operator', 'children': ['74']}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '76', 'type': 'argument_list', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'float'}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'raise_statement', 'children': ['81']}, {'id': '81', 'type': 'call', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '83', 'type': 'argument_list', 'children': ['84']}; {'id': '84', 'type': 'string', 'children': [], 'value': '"Backdate must be type float."'}, {'id': '85', 'type': 'if_statement', 'children': ['86', '89']}; {'id': '86', 'type': 'comparison_operator', 'children': ['87', '88'], 'value': '>'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '89', 'type': 'block', 'children': ['90']}; {'id': '90', 'type': 'raise_statement', 'children': ['91']}, {'id': '91', 'type': 'call', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}, {'id': '93', 'type': 'argument_list', 'children': ['94']}; {'id': '94', 'type': 'string', 'children': [], 'value': '"Cannot backdate to future time."'}, {'id': '95', 'type': 'if_statement', 'children': ['96', '103']}; {'id': '96', 'type': 'comparison_operator', 'children': ['97', '98'], 'value': '<'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'backdate'}; {'id': '98', 'type': 'attribute', 'children': ['99', '102']}, {'id': '99', 'type': 'attribute', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'last_t'}, {'id': '103', 'type': 'block', 'children': ['104']}; {'id': '104', 'type': 'raise_statement', 'children': ['105']}, {'id': '105', 'type': 'call', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'BackdateError'}, {'id': '107', 'type': 'argument_list', 'children': ['108']}; {'id': '108', 'type': 'string', 'children': [], 'value': '"Cannot backdate to time earlier than last stamp."'}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 't_stop'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'backdate'}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '116', 'type': 'conditional_expression', 'children': ['117', '120', '128'], 'value': 'if'}, {'id': '117', 'type': 'subscript', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '119', 'type': 'string', 'children': [], 'value': "'UN'"}; {'id': '120', 'type': 'parenthesized_expression', 'children': ['121']}, {'id': '121', 'type': 'boolean_operator', 'children': ['122', '125'], 'value': 'and'}; {'id': '122', 'type': 'comparison_operator', 'children': ['123', '124'], 'value': 'is'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '124', 'type': 'None', 'children': []}, {'id': '125', 'type': 'comparison_operator', 'children': ['126', '127'], 'value': 'is'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'un'}, {'id': '127', 'type': 'None', 'children': []}; {'id': '128', 'type': 'call', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'bool'}; {'id': '130', 'type': 'argument_list', 'children': ['131']}, {'id': '131', 'type': 'boolean_operator', 'children': ['132', '133'], 'value': 'or'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'unique'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'un'}; {'id': '134', 'type': 'expression_statement', 'children': ['135']}, {'id': '135', 'type': 'assignment', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '137', 'type': 'conditional_expression', 'children': ['138', '141', '149'], 'value': 'if'}; {'id': '138', 'type': 'subscript', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '140', 'type': 'string', 'children': [], 'value': "'KS'"}, {'id': '141', 'type': 'parenthesized_expression', 'children': ['142']}; {'id': '142', 'type': 'boolean_operator', 'children': ['143', '146'], 'value': 'and'}, {'id': '143', 'type': 'comparison_operator', 'children': ['144', '145'], 'value': 'is'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '145', 'type': 'None', 'children': []}; {'id': '146', 'type': 'comparison_operator', 'children': ['147', '148'], 'value': 'is'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'ks'}; {'id': '148', 'type': 'None', 'children': []}, {'id': '149', 'type': 'call', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'bool'}, {'id': '151', 'type': 'argument_list', 'children': ['152']}; {'id': '152', 'type': 'boolean_operator', 'children': ['153', '154'], 'value': 'or'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'ks'}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'assignment', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '158', 'type': 'conditional_expression', 'children': ['159', '162', '170'], 'value': 'if'}, {'id': '159', 'type': 'subscript', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '161', 'type': 'string', 'children': [], 'value': "'QP'"}; {'id': '162', 'type': 'parenthesized_expression', 'children': ['163']}, {'id': '163', 'type': 'boolean_operator', 'children': ['164', '167'], 'value': 'and'}; {'id': '164', 'type': 'comparison_operator', 'children': ['165', '166'], 'value': 'is'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '166', 'type': 'None', 'children': []}, {'id': '167', 'type': 'comparison_operator', 'children': ['168', '169'], 'value': 'is'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'qp'}, {'id': '169', 'type': 'None', 'children': []}; {'id': '170', 'type': 'call', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'bool'}; {'id': '172', 'type': 'argument_list', 'children': ['173']}, {'id': '173', 'type': 'boolean_operator', 'children': ['174', '175'], 'value': 'or'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'quick_print'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'qp'}; {'id': '176', 'type': 'if_statement', 'children': ['177', '180', '212']}, {'id': '177', 'type': 'comparison_operator', 'children': ['178', '179'], 'value': 'is'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '179', 'type': 'None', 'children': []}; {'id': '180', 'type': 'block', 'children': ['181', '193', '203']}, {'id': '181', 'type': 'if_statement', 'children': ['182', '187']}; {'id': '182', 'type': 'attribute', 'children': ['183', '186']}, {'id': '183', 'type': 'attribute', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'paused'}, {'id': '187', 'type': 'block', 'children': ['188']}; {'id': '188', 'type': 'raise_statement', 'children': ['189']}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'PausedError'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'string', 'children': [], 'value': '"Cannot stamp paused timer."'}, {'id': '193', 'type': 'expression_statement', 'children': ['194']}; {'id': '194', 'type': 'assignment', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'elapsed'}; {'id': '196', 'type': 'binary_operator', 'children': ['197', '198'], 'value': '-'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 't_stop'}; {'id': '198', 'type': 'attribute', 'children': ['199', '202']}, {'id': '199', 'type': 'attribute', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'last_t'}, {'id': '203', 'type': 'expression_statement', 'children': ['204']}; {'id': '204', 'type': 'call', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': '_stamp'}; {'id': '206', 'type': 'argument_list', 'children': ['207', '208', '209', '210', '211']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'elapsed'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'unique'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '212', 'type': 'else_clause', 'children': ['213']}, {'id': '213', 'type': 'block', 'children': ['214']}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'call', 'children': ['216', '219']}; {'id': '216', 'type': 'attribute', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'times_priv'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'assign_subdivisions'}, {'id': '219', 'type': 'argument_list', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'UNASGN'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'keep_subdivisions'}; {'id': '222', 'type': 'for_statement', 'children': ['223', '224', '229']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '224', 'type': 'attribute', 'children': ['225', '228']}, {'id': '225', 'type': 'attribute', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}, {'id': '229', 'type': 'block', 'children': ['230']}; {'id': '230', 'type': 'if_statement', 'children': ['231', '238']}, {'id': '231', 'type': 'comparison_operator', 'children': ['232', '233'], 'value': 'not'}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '233', 'type': 'attribute', 'children': ['234', '237']}; {'id': '234', 'type': 'attribute', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'cum'}; {'id': '238', 'type': 'block', 'children': ['239', '249']}, {'id': '239', 'type': 'expression_statement', 'children': ['240']}; {'id': '240', 'type': 'assignment', 'children': ['241', '248']}, {'id': '241', 'type': 'subscript', 'children': ['242', '247']}; {'id': '242', 'type': 'attribute', 'children': ['243', '246']}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'cum'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '248', 'type': 'float', 'children': [], 'value': '0.'}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'call', 'children': ['251', '258']}, {'id': '251', 'type': 'attribute', 'children': ['252', '257']}; {'id': '252', 'type': 'attribute', 'children': ['253', '256']}, {'id': '253', 'type': 'attribute', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'order'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '258', 'type': 'argument_list', 'children': ['259']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '260', 'type': 'if_statement', 'children': ['261', '267']}, {'id': '261', 'type': 'not_operator', 'children': ['262']}; {'id': '262', 'type': 'attribute', 'children': ['263', '266']}, {'id': '263', 'type': 'attribute', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'paused'}, {'id': '267', 'type': 'block', 'children': ['268']}; {'id': '268', 'type': 'expression_statement', 'children': ['269']}, {'id': '269', 'type': 'augmented_assignment', 'children': ['270', '275'], 'value': '+='}; {'id': '270', 'type': 'attribute', 'children': ['271', '274']}, {'id': '271', 'type': 'attribute', 'children': ['272', '273']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'tmp_total'}, {'id': '275', 'type': 'binary_operator', 'children': ['276', '277'], 'value': '-'}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 't_stop'}, {'id': '277', 'type': 'attribute', 'children': ['278', '281']}; {'id': '278', 'type': 'attribute', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'start_t'}; {'id': '282', 'type': 'expression_statement', 'children': ['283']}, {'id': '283', 'type': 'augmented_assignment', 'children': ['284', '289'], 'value': '-='}; {'id': '284', 'type': 'attribute', 'children': ['285', '288']}, {'id': '285', 'type': 'attribute', 'children': ['286', '287']}; {'id': '286', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'tmp_total'}, {'id': '289', 'type': 'attribute', 'children': ['290', '293']}; {'id': '290', 'type': 'attribute', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'self_cut'}; {'id': '294', 'type': 'expression_statement', 'children': ['295']}, {'id': '295', 'type': 'augmented_assignment', 'children': ['296', '301'], 'value': '+='}; {'id': '296', 'type': 'attribute', 'children': ['297', '300']}, {'id': '297', 'type': 'attribute', 'children': ['298', '299']}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'self_cut'}, {'id': '301', 'type': 'binary_operator', 'children': ['302', '305'], 'value': '-'}; {'id': '302', 'type': 'call', 'children': ['303', '304']}, {'id': '303', 'type': 'identifier', 'children': [], 'value': 'timer'}; {'id': '304', 'type': 'argument_list', 'children': []}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '306', 'type': 'expression_statement', 'children': ['307']}, {'id': '307', 'type': 'call', 'children': ['308', '311']}; {'id': '308', 'type': 'attribute', 'children': ['309', '310']}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'times_priv'}; {'id': '310', 'type': 'identifier', 'children': [], 'value': 'dump_times'}, {'id': '311', 'type': 'argument_list', 'children': []}; {'id': '312', 'type': 'expression_statement', 'children': ['313']}, {'id': '313', 'type': 'assignment', 'children': ['314', '319']}; {'id': '314', 'type': 'attribute', 'children': ['315', '318']}, {'id': '315', 'type': 'attribute', 'children': ['316', '317']}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '317', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'stopped'}, {'id': '319', 'type': 'True', 'children': []}; {'id': '320', 'type': 'if_statement', 'children': ['321', '322']}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '322', 'type': 'block', 'children': ['323']}, {'id': '323', 'type': 'expression_statement', 'children': ['324']}; {'id': '324', 'type': 'call', 'children': ['325', '326']}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '326', 'type': 'argument_list', 'children': ['327']}, {'id': '327', 'type': 'call', 'children': ['328', '331']}; {'id': '328', 'type': 'attribute', 'children': ['329', '330']}, {'id': '329', 'type': 'string', 'children': [], 'value': '"({}) Total: {:.4f}"'}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '331', 'type': 'argument_list', 'children': ['332', '337']}; {'id': '332', 'type': 'attribute', 'children': ['333', '336']}, {'id': '333', 'type': 'attribute', 'children': ['334', '335']}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'f'}, {'id': '335', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '336', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '337', 'type': 'attribute', 'children': ['338', '341']}; {'id': '338', 'type': 'attribute', 'children': ['339', '340']}, {'id': '339', 'type': 'identifier', 'children': [], 'value': 'f'}; {'id': '340', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '341', 'type': 'identifier', 'children': [], 'value': 'total'}; {'id': '342', 'type': 'return_statement', 'children': ['343']}, {'id': '343', 'type': 'identifier', 'children': [], 'value': 't'} | def stop(name=None, backdate=None,
unique=None, keep_subdivisions=None, quick_print=None,
un=None, ks=None, qp=None):
t = timer()
if f.t.stopped:
raise StoppedError("Timer already stopped.")
if backdate is None:
t_stop = t
else:
if f.t is f.root:
raise BackdateError("Cannot backdate stop of root timer.")
if not isinstance(backdate, float):
raise TypeError("Backdate must be type float.")
if backdate > t:
raise BackdateError("Cannot backdate to future time.")
if backdate < f.t.last_t:
raise BackdateError("Cannot backdate to time earlier than last stamp.")
t_stop = backdate
unique = SET['UN'] if (unique is None and un is None) else bool(unique or un)
keep_subdivisions = SET['KS'] if (keep_subdivisions is None and ks is None) else bool(keep_subdivisions or ks)
quick_print = SET['QP'] if (quick_print is None and qp is None) else bool(quick_print or qp)
if name is not None:
if f.t.paused:
raise PausedError("Cannot stamp paused timer.")
elapsed = t_stop - f.t.last_t
_stamp(name, elapsed, unique, keep_subdivisions, quick_print)
else:
times_priv.assign_subdivisions(UNASGN, keep_subdivisions)
for s in f.t.rgstr_stamps:
if s not in f.s.cum:
f.s.cum[s] = 0.
f.s.order.append(s)
if not f.t.paused:
f.t.tmp_total += t_stop - f.t.start_t
f.t.tmp_total -= f.t.self_cut
f.t.self_cut += timer() - t
times_priv.dump_times()
f.t.stopped = True
if quick_print:
print("({}) Total: {:.4f}".format(f.t.name, f.r.total))
return t |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '38']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'timed_loop'}, {'id': '3', 'type': 'parameters', 'children': ['4', '7', '10', '15', '18', '23', '28', '33']}; {'id': '4', 'type': 'default_parameter', 'children': ['5', '6']}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '6', 'type': 'None', 'children': []}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}, {'id': '9', 'type': 'None', 'children': []}; {'id': '10', 'type': 'default_parameter', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}; {'id': '12', 'type': 'subscript', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '14', 'type': 'string', 'children': [], 'value': "'SI'"}, {'id': '15', 'type': 'default_parameter', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}, {'id': '17', 'type': 'None', 'children': []}; {'id': '18', 'type': 'default_parameter', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}; {'id': '20', 'type': 'subscript', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '22', 'type': 'string', 'children': [], 'value': "'UN'"}, {'id': '23', 'type': 'default_parameter', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}, {'id': '25', 'type': 'subscript', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '27', 'type': 'string', 'children': [], 'value': "'KS'"}; {'id': '28', 'type': 'default_parameter', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'}; {'id': '30', 'type': 'subscript', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '32', 'type': 'string', 'children': [], 'value': "'KS'"}, {'id': '33', 'type': 'default_parameter', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'quick_print'}, {'id': '35', 'type': 'subscript', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '37', 'type': 'string', 'children': [], 'value': "'QP'"}; {'id': '38', 'type': 'block', 'children': ['39']}, {'id': '39', 'type': 'return_statement', 'children': ['40']}; {'id': '40', 'type': 'call', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'TimedLoop'}; {'id': '42', 'type': 'argument_list', 'children': ['43', '46', '49', '52', '55', '58', '61']}, {'id': '43', 'type': 'keyword_argument', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '46', 'type': 'keyword_argument', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}, {'id': '49', 'type': 'keyword_argument', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}; {'id': '52', 'type': 'keyword_argument', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}, {'id': '55', 'type': 'keyword_argument', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}; {'id': '58', 'type': 'keyword_argument', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}, {'id': '61', 'type': 'keyword_argument', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'} | def timed_loop(name=None,
rgstr_stamps=None,
save_itrs=SET['SI'],
loop_end_stamp=None,
end_stamp_unique=SET['UN'],
keep_prev_subdivisions=SET['KS'],
keep_end_subdivisions=SET['KS'],
quick_print=SET['QP']):
return TimedLoop(name=name,
rgstr_stamps=rgstr_stamps,
save_itrs=save_itrs,
loop_end_stamp=loop_end_stamp,
end_stamp_unique=end_stamp_unique,
keep_prev_subdivisions=keep_prev_subdivisions,
keep_end_subdivisions=keep_end_subdivisions) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '39']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'timed_for'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '16', '19', '24', '29', '34']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'iterable'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}, {'id': '13', 'type': 'subscript', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '15', 'type': 'string', 'children': [], 'value': "'SI'"}; {'id': '16', 'type': 'default_parameter', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}; {'id': '18', 'type': 'None', 'children': []}, {'id': '19', 'type': 'default_parameter', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}, {'id': '21', 'type': 'subscript', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '23', 'type': 'string', 'children': [], 'value': "'UN'"}; {'id': '24', 'type': 'default_parameter', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}; {'id': '26', 'type': 'subscript', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '28', 'type': 'string', 'children': [], 'value': "'KS'"}, {'id': '29', 'type': 'default_parameter', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'}, {'id': '31', 'type': 'subscript', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'SET'}, {'id': '33', 'type': 'string', 'children': [], 'value': "'KS'"}; {'id': '34', 'type': 'default_parameter', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'quick_print'}; {'id': '36', 'type': 'subscript', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'SET'}; {'id': '38', 'type': 'string', 'children': [], 'value': "'QP'"}, {'id': '39', 'type': 'block', 'children': ['40']}; {'id': '40', 'type': 'return_statement', 'children': ['41']}, {'id': '41', 'type': 'call', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'TimedFor'}, {'id': '43', 'type': 'argument_list', 'children': ['44', '45', '48', '51', '54', '57', '60', '63']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'iterable'}, {'id': '45', 'type': 'keyword_argument', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '48', 'type': 'keyword_argument', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'rgstr_stamps'}, {'id': '51', 'type': 'keyword_argument', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'save_itrs'}; {'id': '54', 'type': 'keyword_argument', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'loop_end_stamp'}, {'id': '57', 'type': 'keyword_argument', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'end_stamp_unique'}; {'id': '60', 'type': 'keyword_argument', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'keep_prev_subdivisions'}, {'id': '63', 'type': 'keyword_argument', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'keep_end_subdivisions'} | def timed_for(iterable,
name=None,
rgstr_stamps=None,
save_itrs=SET['SI'],
loop_end_stamp=None,
end_stamp_unique=SET['UN'],
keep_prev_subdivisions=SET['KS'],
keep_end_subdivisions=SET['KS'],
quick_print=SET['QP']):
return TimedFor(iterable,
name=name,
rgstr_stamps=rgstr_stamps,
save_itrs=save_itrs,
loop_end_stamp=loop_end_stamp,
end_stamp_unique=end_stamp_unique,
keep_prev_subdivisions=keep_prev_subdivisions,
keep_end_subdivisions=keep_end_subdivisions) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'format_gmeta'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'acl'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'identifier'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12']}; {'id': '12', 'type': 'if_statement', 'children': ['13', '18', '122', '152']}, {'id': '13', 'type': 'call', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '15', 'type': 'argument_list', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '18', 'type': 'block', 'children': ['19', '33', '45', '49', '105']}, {'id': '19', 'type': 'if_statement', 'children': ['20', '27']}; {'id': '20', 'type': 'boolean_operator', 'children': ['21', '24'], 'value': 'or'}, {'id': '21', 'type': 'comparison_operator', 'children': ['22', '23'], 'value': 'is'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'acl'}, {'id': '23', 'type': 'None', 'children': []}; {'id': '24', 'type': 'comparison_operator', 'children': ['25', '26'], 'value': 'is'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'identifier'}; {'id': '26', 'type': 'None', 'children': []}, {'id': '27', 'type': 'block', 'children': ['28']}; {'id': '28', 'type': 'raise_statement', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'string', 'children': [], 'value': '"acl and identifier are required when formatting a GMetaEntry."'}, {'id': '33', 'type': 'if_statement', 'children': ['34', '39']}; {'id': '34', 'type': 'call', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '36', 'type': 'argument_list', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'acl'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '39', 'type': 'block', 'children': ['40']}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'acl'}, {'id': '43', 'type': 'list', 'children': ['44'], 'value': '[acl]'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'acl'}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'prefixed_acl'}; {'id': '48', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '49', 'type': 'for_statement', 'children': ['50', '51', '52']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'uuid'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'acl'}; {'id': '52', 'type': 'block', 'children': ['53']}, {'id': '53', 'type': 'if_statement', 'children': ['54', '69', '96']}; {'id': '54', 'type': 'boolean_operator', 'children': ['55', '58'], 'value': 'and'}, {'id': '55', 'type': 'comparison_operator', 'children': ['56', '57'], 'value': '!='}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'uuid'}, {'id': '57', 'type': 'string', 'children': [], 'value': '"public"'}; {'id': '58', 'type': 'not_operator', 'children': ['59']}, {'id': '59', 'type': 'call', 'children': ['60', '67']}; {'id': '60', 'type': 'attribute', 'children': ['61', '66']}, {'id': '61', 'type': 'call', 'children': ['62', '65']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'uuid'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '65', 'type': 'argument_list', 'children': []}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'startswith'}, {'id': '67', 'type': 'argument_list', 'children': ['68']}; {'id': '68', 'type': 'string', 'children': [], 'value': '"urn:"'}, {'id': '69', 'type': 'block', 'children': ['70', '83']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'prefixed_acl'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'binary_operator', 'children': ['77', '78'], 'value': '+'}, {'id': '77', 'type': 'string', 'children': [], 'value': '"urn:globus:auth:identity:"'}; {'id': '78', 'type': 'call', 'children': ['79', '82']}, {'id': '79', 'type': 'attribute', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'uuid'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '82', 'type': 'argument_list', 'children': []}, {'id': '83', 'type': 'expression_statement', 'children': ['84']}; {'id': '84', 'type': 'call', 'children': ['85', '88']}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'prefixed_acl'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '88', 'type': 'argument_list', 'children': ['89']}, {'id': '89', 'type': 'binary_operator', 'children': ['90', '91'], 'value': '+'}; {'id': '90', 'type': 'string', 'children': [], 'value': '"urn:globus:groups:id:"'}, {'id': '91', 'type': 'call', 'children': ['92', '95']}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'uuid'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '95', 'type': 'argument_list', 'children': []}; {'id': '96', 'type': 'else_clause', 'children': ['97']}, {'id': '97', 'type': 'block', 'children': ['98']}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'call', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'prefixed_acl'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'uuid'}, {'id': '105', 'type': 'return_statement', 'children': ['106']}; {'id': '106', 'type': 'dictionary', 'children': ['107', '110', '113', '116', '119']}, {'id': '107', 'type': 'pair', 'children': ['108', '109']}; {'id': '108', 'type': 'string', 'children': [], 'value': '"@datatype"'}, {'id': '109', 'type': 'string', 'children': [], 'value': '"GMetaEntry"'}; {'id': '110', 'type': 'pair', 'children': ['111', '112']}, {'id': '111', 'type': 'string', 'children': [], 'value': '"@version"'}; {'id': '112', 'type': 'string', 'children': [], 'value': '"2016-11-09"'}, {'id': '113', 'type': 'pair', 'children': ['114', '115']}; {'id': '114', 'type': 'string', 'children': [], 'value': '"subject"'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'identifier'}; {'id': '116', 'type': 'pair', 'children': ['117', '118']}, {'id': '117', 'type': 'string', 'children': [], 'value': '"visible_to"'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'prefixed_acl'}, {'id': '119', 'type': 'pair', 'children': ['120', '121']}; {'id': '120', 'type': 'string', 'children': [], 'value': '"content"'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '122', 'type': 'elif_clause', 'children': ['123', '128']}, {'id': '123', 'type': 'call', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '125', 'type': 'argument_list', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '128', 'type': 'block', 'children': ['129']}, {'id': '129', 'type': 'return_statement', 'children': ['130']}; {'id': '130', 'type': 'dictionary', 'children': ['131', '134', '137', '140']}, {'id': '131', 'type': 'pair', 'children': ['132', '133']}; {'id': '132', 'type': 'string', 'children': [], 'value': '"@datatype"'}, {'id': '133', 'type': 'string', 'children': [], 'value': '"GIngest"'}; {'id': '134', 'type': 'pair', 'children': ['135', '136']}, {'id': '135', 'type': 'string', 'children': [], 'value': '"@version"'}; {'id': '136', 'type': 'string', 'children': [], 'value': '"2016-11-09"'}, {'id': '137', 'type': 'pair', 'children': ['138', '139']}; {'id': '138', 'type': 'string', 'children': [], 'value': '"ingest_type"'}, {'id': '139', 'type': 'string', 'children': [], 'value': '"GMetaList"'}; {'id': '140', 'type': 'pair', 'children': ['141', '142']}, {'id': '141', 'type': 'string', 'children': [], 'value': '"ingest_data"'}; {'id': '142', 'type': 'dictionary', 'children': ['143', '146', '149']}, {'id': '143', 'type': 'pair', 'children': ['144', '145']}; {'id': '144', 'type': 'string', 'children': [], 'value': '"@datatype"'}, {'id': '145', 'type': 'string', 'children': [], 'value': '"GMetaList"'}; {'id': '146', 'type': 'pair', 'children': ['147', '148']}, {'id': '147', 'type': 'string', 'children': [], 'value': '"@version"'}; {'id': '148', 'type': 'string', 'children': [], 'value': '"2016-11-09"'}, {'id': '149', 'type': 'pair', 'children': ['150', '151']}; {'id': '150', 'type': 'string', 'children': [], 'value': '"gmeta"'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '152', 'type': 'else_clause', 'children': ['153']}, {'id': '153', 'type': 'block', 'children': ['154']}; {'id': '154', 'type': 'raise_statement', 'children': ['155']}, {'id': '155', 'type': 'call', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'binary_operator', 'children': ['159', '168'], 'value': '+'}, {'id': '159', 'type': 'binary_operator', 'children': ['160', '161'], 'value': '+'}; {'id': '160', 'type': 'string', 'children': [], 'value': '"Cannot format \'"'}, {'id': '161', 'type': 'call', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '163', 'type': 'argument_list', 'children': ['164']}; {'id': '164', 'type': 'call', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'type'}; {'id': '166', 'type': 'argument_list', 'children': ['167']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'data'} | def format_gmeta(data, acl=None, identifier=None):
if isinstance(data, dict):
if acl is None or identifier is None:
raise ValueError("acl and identifier are required when formatting a GMetaEntry.")
if isinstance(acl, str):
acl = [acl]
prefixed_acl = []
for uuid in acl:
if uuid != "public" and not uuid.lower().startswith("urn:"):
prefixed_acl.append("urn:globus:auth:identity:"+uuid.lower())
prefixed_acl.append("urn:globus:groups:id:"+uuid.lower())
else:
prefixed_acl.append(uuid)
return {
"@datatype": "GMetaEntry",
"@version": "2016-11-09",
"subject": identifier,
"visible_to": prefixed_acl,
"content": data
}
elif isinstance(data, list):
return {
"@datatype": "GIngest",
"@version": "2016-11-09",
"ingest_type": "GMetaList",
"ingest_data": {
"@datatype": "GMetaList",
"@version": "2016-11-09",
"gmeta": data
}
}
else:
raise TypeError("Cannot format '" + str(type(data)) + "' into GMeta.") |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '12']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'insensitive_comparison'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}; {'id': '8', 'type': 'False', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '11', 'type': 'False', 'children': []}; {'id': '12', 'type': 'block', 'children': ['13', '29']}, {'id': '13', 'type': 'if_statement', 'children': ['14', '26']}; {'id': '14', 'type': 'boolean_operator', 'children': ['15', '17'], 'value': 'and'}, {'id': '15', 'type': 'not_operator', 'children': ['16']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '17', 'type': 'comparison_operator', 'children': ['18', '22'], 'value': '!='}; {'id': '18', 'type': 'call', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'type'}; {'id': '20', 'type': 'argument_list', 'children': ['21']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '22', 'type': 'call', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'type'}; {'id': '24', 'type': 'argument_list', 'children': ['25']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '26', 'type': 'block', 'children': ['27']}, {'id': '27', 'type': 'return_statement', 'children': ['28']}; {'id': '28', 'type': 'False', 'children': []}, {'id': '29', 'type': 'if_statement', 'children': ['30', '35', '119', '232', '393']}; {'id': '30', 'type': 'call', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '32', 'type': 'argument_list', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'Mapping'}, {'id': '35', 'type': 'block', 'children': ['36', '46', '60', '87', '117']}; {'id': '36', 'type': 'if_statement', 'children': ['37', '43']}, {'id': '37', 'type': 'not_operator', 'children': ['38']}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '40', 'type': 'argument_list', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'Mapping'}, {'id': '43', 'type': 'block', 'children': ['44']}; {'id': '44', 'type': 'return_statement', 'children': ['45']}, {'id': '45', 'type': 'False', 'children': []}; {'id': '46', 'type': 'if_statement', 'children': ['47', '57']}, {'id': '47', 'type': 'not_operator', 'children': ['48']}; {'id': '48', 'type': 'comparison_operator', 'children': ['49', '53'], 'value': '=='}, {'id': '49', 'type': 'call', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '53', 'type': 'call', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '55', 'type': 'argument_list', 'children': ['56']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '57', 'type': 'block', 'children': ['58']}; {'id': '58', 'type': 'return_statement', 'children': ['59']}, {'id': '59', 'type': 'False', 'children': []}; {'id': '60', 'type': 'if_statement', 'children': ['61', '84']}, {'id': '61', 'type': 'not_operator', 'children': ['62']}; {'id': '62', 'type': 'call', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'insensitive_comparison'}; {'id': '64', 'type': 'argument_list', 'children': ['65', '73', '81']}, {'id': '65', 'type': 'call', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '67', 'type': 'argument_list', 'children': ['68']}; {'id': '68', 'type': 'call', 'children': ['69', '72']}, {'id': '69', 'type': 'attribute', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '72', 'type': 'argument_list', 'children': []}, {'id': '73', 'type': 'call', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '80', 'type': 'argument_list', 'children': []}, {'id': '81', 'type': 'keyword_argument', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '83', 'type': 'True', 'children': []}; {'id': '84', 'type': 'block', 'children': ['85']}, {'id': '85', 'type': 'return_statement', 'children': ['86']}; {'id': '86', 'type': 'False', 'children': []}, {'id': '87', 'type': 'for_statement', 'children': ['88', '91', '96']}; {'id': '88', 'type': 'pattern_list', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '91', 'type': 'call', 'children': ['92', '95']}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '95', 'type': 'argument_list', 'children': []}; {'id': '96', 'type': 'block', 'children': ['97']}, {'id': '97', 'type': 'if_statement', 'children': ['98', '114']}; {'id': '98', 'type': 'not_operator', 'children': ['99']}, {'id': '99', 'type': 'call', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'insensitive_comparison'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '105', '108', '111']}; {'id': '102', 'type': 'subscript', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '105', 'type': 'subscript', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '108', 'type': 'keyword_argument', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '111', 'type': 'keyword_argument', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}; {'id': '114', 'type': 'block', 'children': ['115']}, {'id': '115', 'type': 'return_statement', 'children': ['116']}; {'id': '116', 'type': 'False', 'children': []}, {'id': '117', 'type': 'return_statement', 'children': ['118']}; {'id': '118', 'type': 'True', 'children': []}, {'id': '119', 'type': 'elif_clause', 'children': ['120', '125']}; {'id': '120', 'type': 'call', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '122', 'type': 'argument_list', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '125', 'type': 'block', 'children': ['126', '136', '153']}; {'id': '126', 'type': 'if_statement', 'children': ['127', '133']}, {'id': '127', 'type': 'not_operator', 'children': ['128']}; {'id': '128', 'type': 'call', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '130', 'type': 'argument_list', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '133', 'type': 'block', 'children': ['134']}; {'id': '134', 'type': 'return_statement', 'children': ['135']}, {'id': '135', 'type': 'False', 'children': []}; {'id': '136', 'type': 'if_statement', 'children': ['137', '150']}, {'id': '137', 'type': 'boolean_operator', 'children': ['138', '148'], 'value': 'and'}; {'id': '138', 'type': 'not_operator', 'children': ['139']}, {'id': '139', 'type': 'comparison_operator', 'children': ['140', '144'], 'value': '=='}; {'id': '140', 'type': 'call', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '142', 'type': 'argument_list', 'children': ['143']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '144', 'type': 'call', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '146', 'type': 'argument_list', 'children': ['147']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '148', 'type': 'not_operator', 'children': ['149']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}; {'id': '150', 'type': 'block', 'children': ['151']}, {'id': '151', 'type': 'return_statement', 'children': ['152']}; {'id': '152', 'type': 'False', 'children': []}, {'id': '153', 'type': 'if_statement', 'children': ['154', '155', '226']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '155', 'type': 'block', 'children': ['156', '176', '195', '214']}; {'id': '156', 'type': 'if_statement', 'children': ['157', '163']}, {'id': '157', 'type': 'comparison_operator', 'children': ['158', '162'], 'value': '<='}; {'id': '158', 'type': 'call', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '162', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '163', 'type': 'block', 'children': ['164']}; {'id': '164', 'type': 'return_statement', 'children': ['165']}, {'id': '165', 'type': 'comparison_operator', 'children': ['166', '171'], 'value': '=='}; {'id': '166', 'type': 'call', 'children': ['167', '170']}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '170', 'type': 'argument_list', 'children': []}, {'id': '171', 'type': 'call', 'children': ['172', '175']}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '175', 'type': 'argument_list', 'children': []}; {'id': '176', 'type': 'expression_statement', 'children': ['177']}, {'id': '177', 'type': 'assignment', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'item1_list'}, {'id': '179', 'type': 'list_comprehension', 'children': ['180', '181', '188']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '181', 'type': 'for_in_clause', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '183', 'type': 'call', 'children': ['184', '187']}; {'id': '184', 'type': 'attribute', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '187', 'type': 'argument_list', 'children': []}; {'id': '188', 'type': 'if_clause', 'children': ['189']}, {'id': '189', 'type': 'not_operator', 'children': ['190']}; {'id': '190', 'type': 'call', 'children': ['191', '194']}, {'id': '191', 'type': 'attribute', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'isspace'}; {'id': '194', 'type': 'argument_list', 'children': []}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'assignment', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'item2_list'}; {'id': '198', 'type': 'list_comprehension', 'children': ['199', '200', '207']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '200', 'type': 'for_in_clause', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '202', 'type': 'call', 'children': ['203', '206']}, {'id': '203', 'type': 'attribute', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '206', 'type': 'argument_list', 'children': []}, {'id': '207', 'type': 'if_clause', 'children': ['208']}; {'id': '208', 'type': 'not_operator', 'children': ['209']}, {'id': '209', 'type': 'call', 'children': ['210', '213']}; {'id': '210', 'type': 'attribute', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'isspace'}, {'id': '213', 'type': 'argument_list', 'children': []}; {'id': '214', 'type': 'return_statement', 'children': ['215']}, {'id': '215', 'type': 'call', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'insensitive_comparison'}, {'id': '217', 'type': 'argument_list', 'children': ['218', '219', '220', '223']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'item1_list'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'item2_list'}; {'id': '220', 'type': 'keyword_argument', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '223', 'type': 'keyword_argument', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}; {'id': '226', 'type': 'else_clause', 'children': ['227']}, {'id': '227', 'type': 'block', 'children': ['228']}; {'id': '228', 'type': 'return_statement', 'children': ['229']}, {'id': '229', 'type': 'comparison_operator', 'children': ['230', '231'], 'value': '=='}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '232', 'type': 'elif_clause', 'children': ['233', '244']}, {'id': '233', 'type': 'boolean_operator', 'children': ['234', '239'], 'value': 'and'}; {'id': '234', 'type': 'call', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '236', 'type': 'argument_list', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'Container'}, {'id': '239', 'type': 'call', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '241', 'type': 'argument_list', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'Iterable'}; {'id': '244', 'type': 'block', 'children': ['245', '262', '276', '286', '290', '342']}, {'id': '245', 'type': 'if_statement', 'children': ['246', '259']}; {'id': '246', 'type': 'boolean_operator', 'children': ['247', '253'], 'value': 'or'}, {'id': '247', 'type': 'not_operator', 'children': ['248']}; {'id': '248', 'type': 'call', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '250', 'type': 'argument_list', 'children': ['251', '252']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'Container'}, {'id': '253', 'type': 'not_operator', 'children': ['254']}; {'id': '254', 'type': 'call', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '256', 'type': 'argument_list', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'Iterable'}, {'id': '259', 'type': 'block', 'children': ['260']}; {'id': '260', 'type': 'return_statement', 'children': ['261']}, {'id': '261', 'type': 'False', 'children': []}; {'id': '262', 'type': 'if_statement', 'children': ['263', '273']}, {'id': '263', 'type': 'not_operator', 'children': ['264']}; {'id': '264', 'type': 'comparison_operator', 'children': ['265', '269'], 'value': '=='}, {'id': '265', 'type': 'call', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '267', 'type': 'argument_list', 'children': ['268']}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '269', 'type': 'call', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '271', 'type': 'argument_list', 'children': ['272']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '273', 'type': 'block', 'children': ['274']}; {'id': '274', 'type': 'return_statement', 'children': ['275']}, {'id': '275', 'type': 'False', 'children': []}; {'id': '276', 'type': 'expression_statement', 'children': ['277']}, {'id': '277', 'type': 'assignment', 'children': ['278', '279']}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'item2_copy'}, {'id': '279', 'type': 'call', 'children': ['280', '281']}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '281', 'type': 'argument_list', 'children': ['282']}; {'id': '282', 'type': 'call', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'deepcopy'}; {'id': '284', 'type': 'argument_list', 'children': ['285']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '286', 'type': 'expression_statement', 'children': ['287']}, {'id': '287', 'type': 'assignment', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'remove_failed'}, {'id': '289', 'type': 'False', 'children': []}; {'id': '290', 'type': 'for_statement', 'children': ['291', '292', '293']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'elem'}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'item1'}, {'id': '293', 'type': 'block', 'children': ['294', '298', '336']}; {'id': '294', 'type': 'expression_statement', 'children': ['295']}, {'id': '295', 'type': 'assignment', 'children': ['296', '297']}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'matched'}, {'id': '297', 'type': 'False', 'children': []}; {'id': '298', 'type': 'for_statement', 'children': ['299', '300', '301']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'candidate'}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'item2'}, {'id': '301', 'type': 'block', 'children': ['302']}; {'id': '302', 'type': 'if_statement', 'children': ['303', '314']}, {'id': '303', 'type': 'call', 'children': ['304', '305']}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'insensitive_comparison'}, {'id': '305', 'type': 'argument_list', 'children': ['306', '307', '308', '311']}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'elem'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'candidate'}; {'id': '308', 'type': 'keyword_argument', 'children': ['309', '310']}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}; {'id': '310', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '311', 'type': 'keyword_argument', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}; {'id': '314', 'type': 'block', 'children': ['315', '319', '335']}, {'id': '315', 'type': 'expression_statement', 'children': ['316']}; {'id': '316', 'type': 'assignment', 'children': ['317', '318']}, {'id': '317', 'type': 'identifier', 'children': [], 'value': 'matched'}; {'id': '318', 'type': 'True', 'children': []}, {'id': '319', 'type': 'try_statement', 'children': ['320', '328']}; {'id': '320', 'type': 'block', 'children': ['321']}, {'id': '321', 'type': 'expression_statement', 'children': ['322']}; {'id': '322', 'type': 'call', 'children': ['323', '326']}, {'id': '323', 'type': 'attribute', 'children': ['324', '325']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'item2_copy'}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'remove'}; {'id': '326', 'type': 'argument_list', 'children': ['327']}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'candidate'}; {'id': '328', 'type': 'except_clause', 'children': ['329', '330']}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '330', 'type': 'block', 'children': ['331']}, {'id': '331', 'type': 'expression_statement', 'children': ['332']}; {'id': '332', 'type': 'assignment', 'children': ['333', '334']}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'remove_failed'}; {'id': '334', 'type': 'True', 'children': []}, {'id': '335', 'type': 'break_statement', 'children': []}; {'id': '336', 'type': 'if_statement', 'children': ['337', '339']}, {'id': '337', 'type': 'not_operator', 'children': ['338']}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'matched'}, {'id': '339', 'type': 'block', 'children': ['340']}; {'id': '340', 'type': 'return_statement', 'children': ['341']}, {'id': '341', 'type': 'False', 'children': []}; {'id': '342', 'type': 'if_statement', 'children': ['343', '345', '353']}, {'id': '343', 'type': 'not_operator', 'children': ['344']}; {'id': '344', 'type': 'identifier', 'children': [], 'value': 'remove_failed'}, {'id': '345', 'type': 'block', 'children': ['346']}; {'id': '346', 'type': 'return_statement', 'children': ['347']}, {'id': '347', 'type': 'comparison_operator', 'children': ['348', '352'], 'value': '=='}; {'id': '348', 'type': 'call', 'children': ['349', '350']}, {'id': '349', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '350', 'type': 'argument_list', 'children': ['351']}, {'id': '351', 'type': 'identifier', 'children': [], 'value': 'item2_copy'}; {'id': '352', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '353', 'type': 'else_clause', 'children': ['354']}; {'id': '354', 'type': 'block', 'children': ['355', '391']}, {'id': '355', 'type': 'for_statement', 'children': ['356', '357', '358']}; {'id': '356', 'type': 'identifier', 'children': [], 'value': 'elem'}, {'id': '357', 'type': 'identifier', 'children': [], 'value': 'item2'}; {'id': '358', 'type': 'block', 'children': ['359', '363', '385']}, {'id': '359', 'type': 'expression_statement', 'children': ['360']}; {'id': '360', 'type': 'assignment', 'children': ['361', '362']}, {'id': '361', 'type': 'identifier', 'children': [], 'value': 'matched'}; {'id': '362', 'type': 'False', 'children': []}, {'id': '363', 'type': 'for_statement', 'children': ['364', '365', '366']}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'candidate'}, {'id': '365', 'type': 'identifier', 'children': [], 'value': 'item1'}; {'id': '366', 'type': 'block', 'children': ['367']}, {'id': '367', 'type': 'if_statement', 'children': ['368', '379']}; {'id': '368', 'type': 'call', 'children': ['369', '370']}, {'id': '369', 'type': 'identifier', 'children': [], 'value': 'insensitive_comparison'}; {'id': '370', 'type': 'argument_list', 'children': ['371', '372', '373', '376']}, {'id': '371', 'type': 'identifier', 'children': [], 'value': 'elem'}; {'id': '372', 'type': 'identifier', 'children': [], 'value': 'candidate'}, {'id': '373', 'type': 'keyword_argument', 'children': ['374', '375']}; {'id': '374', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}, {'id': '375', 'type': 'identifier', 'children': [], 'value': 'type_insensitive'}; {'id': '376', 'type': 'keyword_argument', 'children': ['377', '378']}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}; {'id': '378', 'type': 'identifier', 'children': [], 'value': 'string_insensitive'}, {'id': '379', 'type': 'block', 'children': ['380', '384']}; {'id': '380', 'type': 'expression_statement', 'children': ['381']}, {'id': '381', 'type': 'assignment', 'children': ['382', '383']}; {'id': '382', 'type': 'identifier', 'children': [], 'value': 'matched'}, {'id': '383', 'type': 'True', 'children': []}; {'id': '384', 'type': 'break_statement', 'children': []}, {'id': '385', 'type': 'if_statement', 'children': ['386', '388']}; {'id': '386', 'type': 'not_operator', 'children': ['387']}, {'id': '387', 'type': 'identifier', 'children': [], 'value': 'matched'}; {'id': '388', 'type': 'block', 'children': ['389']}, {'id': '389', 'type': 'return_statement', 'children': ['390']}; {'id': '390', 'type': 'False', 'children': []}, {'id': '391', 'type': 'return_statement', 'children': ['392']}; {'id': '392', 'type': 'True', 'children': []}, {'id': '393', 'type': 'else_clause', 'children': ['394']}; {'id': '394', 'type': 'block', 'children': ['395']}, {'id': '395', 'type': 'return_statement', 'children': ['396']}; {'id': '396', 'type': 'comparison_operator', 'children': ['397', '398'], 'value': '=='}, {'id': '397', 'type': 'identifier', 'children': [], 'value': 'item1'} | def insensitive_comparison(item1, item2, type_insensitive=False, string_insensitive=False):
if not type_insensitive and type(item1) != type(item2):
return False
if isinstance(item1, Mapping):
if not isinstance(item2, Mapping):
return False
if not len(item1) == len(item2):
return False
if not insensitive_comparison(list(item1.keys()), list(item2.keys()),
type_insensitive=True):
return False
for key, val in item1.items():
if not insensitive_comparison(item1[key], item2[key],
type_insensitive=type_insensitive,
string_insensitive=string_insensitive):
return False
return True
elif isinstance(item1, str):
if not isinstance(item2, str):
return False
if not len(item1) == len(item2) and not string_insensitive:
return False
if string_insensitive:
if len(item1) <= 1:
return item1.lower() == item2.lower()
item1_list = [c for c in item1.lower() if not c.isspace()]
item2_list = [c for c in item2.lower() if not c.isspace()]
return insensitive_comparison(item1_list, item2_list,
type_insensitive=type_insensitive,
string_insensitive=string_insensitive)
else:
return item1 == item2
elif isinstance(item1, Container) and isinstance(item1, Iterable):
if not isinstance(item2, Container) or not isinstance(item2, Iterable):
return False
if not len(item1) == len(item2):
return False
item2_copy = list(deepcopy(item2))
remove_failed = False
for elem in item1:
matched = False
for candidate in item2:
if insensitive_comparison(elem, candidate,
type_insensitive=type_insensitive,
string_insensitive=string_insensitive):
matched = True
try:
item2_copy.remove(candidate)
except ValueError:
remove_failed = True
break
if not matched:
return False
if not remove_failed:
return len(item2_copy) == 0
else:
for elem in item2:
matched = False
for candidate in item1:
if insensitive_comparison(elem, candidate,
type_insensitive=type_insensitive,
string_insensitive=string_insensitive):
matched = True
break
if not matched:
return False
return True
else:
return item1 == item2 |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'template_to_filepath'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}; {'id': '8', 'type': 'None', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '17', '26', '33', '289']}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'assignment', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '13', 'type': 'call', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '15', 'type': 'argument_list', 'children': ['16']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '17', 'type': 'if_statement', 'children': ['18', '21']}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': 'is'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}; {'id': '20', 'type': 'None', 'children': []}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'TEMPLATE_PATTERNS'}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'assignment', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'suggest_filename'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'metadata'}, {'id': '33', 'type': 'if_statement', 'children': ['34', '49', '57', '249', '267', '283']}; {'id': '34', 'type': 'parenthesized_expression', 'children': ['35']}, {'id': '35', 'type': 'boolean_operator', 'children': ['36', '43'], 'value': 'or'}; {'id': '36', 'type': 'comparison_operator', 'children': ['37', '38'], 'value': '=='}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '38', 'type': 'call', 'children': ['39', '42']}, {'id': '39', 'type': 'attribute', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'cwd'}; {'id': '42', 'type': 'argument_list', 'children': []}, {'id': '43', 'type': 'comparison_operator', 'children': ['44', '45'], 'value': '=='}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '47', 'type': 'argument_list', 'children': ['48']}; {'id': '48', 'type': 'string', 'children': [], 'value': "'%suggested%'"}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'filepath'}, {'id': '53', 'type': 'call', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '55', 'type': 'argument_list', 'children': ['56']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}, {'id': '57', 'type': 'elif_clause', 'children': ['58', '69']}; {'id': '58', 'type': 'call', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'any'}; {'id': '60', 'type': 'generator_expression', 'children': ['61', '66']}, {'id': '61', 'type': 'comparison_operator', 'children': ['62', '63'], 'value': 'in'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'template_pattern'}, {'id': '63', 'type': 'attribute', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'parts'}; {'id': '66', 'type': 'for_in_clause', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'template_pattern'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}, {'id': '69', 'type': 'block', 'children': ['70', '84', '97', '101', '241']}; {'id': '70', 'type': 'if_statement', 'children': ['71', '79']}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'template'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'endswith'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'tuple', 'children': ['77', '78']}, {'id': '77', 'type': 'string', 'children': [], 'value': "'/'"}; {'id': '78', 'type': 'string', 'children': [], 'value': "'\\\\'"}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'augmented_assignment', 'children': ['82', '83'], 'value': '+='}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'call', 'children': ['91', '94']}, {'id': '91', 'type': 'attribute', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'replace'}; {'id': '94', 'type': 'argument_list', 'children': ['95', '96']}, {'id': '95', 'type': 'string', 'children': [], 'value': "'%suggested%'"}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'parts'}; {'id': '100', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '101', 'type': 'for_statement', 'children': ['102', '103', '106']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '103', 'type': 'attribute', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'parts'}; {'id': '106', 'type': 'block', 'children': ['107']}, {'id': '107', 'type': 'if_statement', 'children': ['108', '113', '121']}; {'id': '108', 'type': 'comparison_operator', 'children': ['109', '110'], 'value': '=='}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'part'}; {'id': '110', 'type': 'attribute', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'anchor'}, {'id': '113', 'type': 'block', 'children': ['114']}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'call', 'children': ['116', '119']}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'parts'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '119', 'type': 'argument_list', 'children': ['120']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '121', 'type': 'else_clause', 'children': ['122']}; {'id': '122', 'type': 'block', 'children': ['123', '231']}, {'id': '123', 'type': 'for_statement', 'children': ['124', '125', '126']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}; {'id': '126', 'type': 'block', 'children': ['127']}, {'id': '127', 'type': 'if_statement', 'children': ['128', '144']}; {'id': '128', 'type': 'parenthesized_expression', 'children': ['129']}, {'id': '129', 'type': 'boolean_operator', 'children': ['130', '133'], 'value': 'and'}; {'id': '130', 'type': 'comparison_operator', 'children': ['131', '132'], 'value': 'in'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '133', 'type': 'call', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'any'}, {'id': '135', 'type': 'generator_expression', 'children': ['136', '139']}; {'id': '136', 'type': 'comparison_operator', 'children': ['137', '138'], 'value': 'in'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'metadata'}, {'id': '139', 'type': 'for_in_clause', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '141', 'type': 'subscript', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '144', 'type': 'block', 'children': ['145', '164', '216']}, {'id': '145', 'type': 'expression_statement', 'children': ['146']}; {'id': '146', 'type': 'assignment', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '148', 'type': 'call', 'children': ['149', '152']}, {'id': '149', 'type': 'attribute', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'more_itertools'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'first_true'}; {'id': '152', 'type': 'argument_list', 'children': ['153', '156']}, {'id': '153', 'type': 'subscript', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'template_patterns'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '156', 'type': 'keyword_argument', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'pred'}; {'id': '158', 'type': 'lambda', 'children': ['159', '161']}, {'id': '159', 'type': 'lambda_parameters', 'children': ['160']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '161', 'type': 'comparison_operator', 'children': ['162', '163'], 'value': 'in'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '164', 'type': 'if_statement', 'children': ['165', '173']}, {'id': '165', 'type': 'call', 'children': ['166', '169']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'startswith'}, {'id': '169', 'type': 'argument_list', 'children': ['170']}; {'id': '170', 'type': 'tuple', 'children': ['171', '172']}, {'id': '171', 'type': 'string', 'children': [], 'value': "'%disc'"}; {'id': '172', 'type': 'string', 'children': [], 'value': "'%track'"}, {'id': '173', 'type': 'block', 'children': ['174', '189']}; {'id': '174', 'type': 'expression_statement', 'children': ['175']}, {'id': '175', 'type': 'assignment', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'number'}, {'id': '177', 'type': 'call', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': '_split_number_field'}, {'id': '179', 'type': 'argument_list', 'children': ['180']}; {'id': '180', 'type': 'call', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '182', 'type': 'argument_list', 'children': ['183']}, {'id': '183', 'type': 'call', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'list_to_single_value'}, {'id': '185', 'type': 'argument_list', 'children': ['186']}; {'id': '186', 'type': 'subscript', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '189', 'type': 'if_statement', 'children': ['190', '196', '208']}; {'id': '190', 'type': 'call', 'children': ['191', '194']}, {'id': '191', 'type': 'attribute', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'endswith'}; {'id': '194', 'type': 'argument_list', 'children': ['195']}, {'id': '195', 'type': 'string', 'children': [], 'value': "'2%'"}; {'id': '196', 'type': 'block', 'children': ['197']}, {'id': '197', 'type': 'expression_statement', 'children': ['198']}; {'id': '198', 'type': 'assignment', 'children': ['199', '202']}, {'id': '199', 'type': 'subscript', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'metadata'}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '202', 'type': 'call', 'children': ['203', '206']}, {'id': '203', 'type': 'attribute', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'number'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'zfill'}; {'id': '206', 'type': 'argument_list', 'children': ['207']}, {'id': '207', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '208', 'type': 'else_clause', 'children': ['209']}, {'id': '209', 'type': 'block', 'children': ['210']}; {'id': '210', 'type': 'expression_statement', 'children': ['211']}, {'id': '211', 'type': 'assignment', 'children': ['212', '215']}; {'id': '212', 'type': 'subscript', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'number'}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '219', 'type': 'call', 'children': ['220', '223']}; {'id': '220', 'type': 'attribute', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'part'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'replace'}, {'id': '223', 'type': 'argument_list', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '225', 'type': 'call', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'list_to_single_value'}, {'id': '227', 'type': 'argument_list', 'children': ['228']}; {'id': '228', 'type': 'subscript', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '231', 'type': 'expression_statement', 'children': ['232']}; {'id': '232', 'type': 'call', 'children': ['233', '236']}, {'id': '233', 'type': 'attribute', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'parts'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '236', 'type': 'argument_list', 'children': ['237']}, {'id': '237', 'type': 'call', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': '_replace_invalid_characters'}, {'id': '239', 'type': 'argument_list', 'children': ['240']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'part'}, {'id': '241', 'type': 'expression_statement', 'children': ['242']}; {'id': '242', 'type': 'assignment', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'filepath'}; {'id': '244', 'type': 'call', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'Path'}; {'id': '246', 'type': 'argument_list', 'children': ['247']}, {'id': '247', 'type': 'list_splat', 'children': ['248']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'parts'}, {'id': '249', 'type': 'elif_clause', 'children': ['250', '253']}; {'id': '250', 'type': 'comparison_operator', 'children': ['251', '252'], 'value': 'in'}, {'id': '251', 'type': 'string', 'children': [], 'value': "'%suggested%'"}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '253', 'type': 'block', 'children': ['254']}; {'id': '254', 'type': 'expression_statement', 'children': ['255']}, {'id': '255', 'type': 'assignment', 'children': ['256', '257']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'filepath'}, {'id': '257', 'type': 'call', 'children': ['258', '259']}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'Path'}, {'id': '259', 'type': 'argument_list', 'children': ['260']}; {'id': '260', 'type': 'call', 'children': ['261', '264']}, {'id': '261', 'type': 'attribute', 'children': ['262', '263']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'replace'}; {'id': '264', 'type': 'argument_list', 'children': ['265', '266']}, {'id': '265', 'type': 'string', 'children': [], 'value': "'%suggested%'"}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}, {'id': '267', 'type': 'elif_clause', 'children': ['268', '276']}; {'id': '268', 'type': 'call', 'children': ['269', '272']}, {'id': '269', 'type': 'attribute', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'template'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'endswith'}; {'id': '272', 'type': 'argument_list', 'children': ['273']}, {'id': '273', 'type': 'tuple', 'children': ['274', '275']}; {'id': '274', 'type': 'string', 'children': [], 'value': "'/'"}, {'id': '275', 'type': 'string', 'children': [], 'value': "'\\\\'"}; {'id': '276', 'type': 'block', 'children': ['277']}, {'id': '277', 'type': 'expression_statement', 'children': ['278']}; {'id': '278', 'type': 'assignment', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'filepath'}; {'id': '280', 'type': 'binary_operator', 'children': ['281', '282'], 'value': '/'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'suggested_filename'}, {'id': '283', 'type': 'else_clause', 'children': ['284']}; {'id': '284', 'type': 'block', 'children': ['285']}, {'id': '285', 'type': 'expression_statement', 'children': ['286']}; {'id': '286', 'type': 'assignment', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'filepath'}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '289', 'type': 'return_statement', 'children': ['290']} | def template_to_filepath(template, metadata, template_patterns=None):
path = Path(template)
if template_patterns is None:
template_patterns = TEMPLATE_PATTERNS
suggested_filename = suggest_filename(metadata)
if (
path == Path.cwd()
or path == Path('%suggested%')
):
filepath = Path(suggested_filename)
elif any(template_pattern in path.parts for template_pattern in template_patterns):
if template.endswith(('/', '\\')):
template += suggested_filename
path = Path(template.replace('%suggested%', suggested_filename))
parts = []
for part in path.parts:
if part == path.anchor:
parts.append(part)
else:
for key in template_patterns:
if (
key in part
and any(field in metadata for field in template_patterns[key])
):
field = more_itertools.first_true(
template_patterns[key],
pred=lambda k: k in metadata
)
if key.startswith(('%disc', '%track')):
number = _split_number_field(
str(
list_to_single_value(
metadata[field]
)
)
)
if key.endswith('2%'):
metadata[field] = number.zfill(2)
else:
metadata[field] = number
part = part.replace(
key,
list_to_single_value(
metadata[field]
)
)
parts.append(_replace_invalid_characters(part))
filepath = Path(*parts)
elif '%suggested%' in template:
filepath = Path(template.replace('%suggested%', suggested_filename))
elif template.endswith(('/', '\\')):
filepath = path / suggested_filename
else:
filepath = path
return filepath |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_get_station_codes'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'force'}, {'id': '7', 'type': 'False', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '23', '31', '35', '96', '102', '150', '246']}, {'id': '9', 'type': 'if_statement', 'children': ['10', '18']}; {'id': '10', 'type': 'boolean_operator', 'children': ['11', '13'], 'value': 'and'}, {'id': '11', 'type': 'not_operator', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'force'}, {'id': '13', 'type': 'comparison_operator', 'children': ['14', '17'], 'value': 'is'}; {'id': '14', 'type': 'attribute', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'station_codes'}, {'id': '17', 'type': 'None', 'children': []}; {'id': '18', 'type': 'block', 'children': ['19']}, {'id': '19', 'type': 'return_statement', 'children': ['20']}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'station_codes'}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'state_urls'}; {'id': '26', 'type': 'call', 'children': ['27', '30']}, {'id': '27', 'type': 'attribute', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': '_get_state_urls'}; {'id': '30', 'type': 'argument_list', 'children': []}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'state_matches'}; {'id': '34', 'type': 'None', 'children': []}, {'id': '35', 'type': 'if_statement', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'bbox'}, {'id': '39', 'type': 'block', 'children': ['40']}; {'id': '40', 'type': 'with_statement', 'children': ['41', '59']}, {'id': '41', 'type': 'with_clause', 'children': ['42']}; {'id': '42', 'type': 'with_item', 'children': ['43']}, {'id': '43', 'type': 'as_pattern', 'children': ['44', '57']}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'collection'}; {'id': '46', 'type': 'argument_list', 'children': ['47', '56']}, {'id': '47', 'type': 'call', 'children': ['48', '53']}; {'id': '48', 'type': 'attribute', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'os'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '53', 'type': 'argument_list', 'children': ['54', '55']}; {'id': '54', 'type': 'string', 'children': [], 'value': '"resources"'}, {'id': '55', 'type': 'string', 'children': [], 'value': '"ne_50m_admin_1_states_provinces_lakes_shp.shp"'}; {'id': '56', 'type': 'string', 'children': [], 'value': '"r"'}, {'id': '57', 'type': 'as_pattern_target', 'children': ['58']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '59', 'type': 'block', 'children': ['60', '79']}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'geom_matches'}, {'id': '63', 'type': 'list_comprehension', 'children': ['64', '67']}; {'id': '64', 'type': 'subscript', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '66', 'type': 'string', 'children': [], 'value': '"properties"'}, {'id': '67', 'type': 'for_in_clause', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'filter'}, {'id': '73', 'type': 'argument_list', 'children': ['74']}; {'id': '74', 'type': 'keyword_argument', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'bbox'}; {'id': '76', 'type': 'attribute', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'bbox'}, {'id': '79', 'type': 'expression_statement', 'children': ['80']}; {'id': '80', 'type': 'assignment', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'state_matches'}; {'id': '82', 'type': 'list_comprehension', 'children': ['83', '93']}, {'id': '83', 'type': 'conditional_expression', 'children': ['84', '87', '92'], 'value': 'if'}; {'id': '84', 'type': 'subscript', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '86', 'type': 'string', 'children': [], 'value': '"postal"'}, {'id': '87', 'type': 'comparison_operator', 'children': ['88', '91'], 'value': '!='}; {'id': '88', 'type': 'subscript', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '90', 'type': 'string', 'children': [], 'value': '"admin"'}, {'id': '91', 'type': 'string', 'children': [], 'value': '"Canada"'}; {'id': '92', 'type': 'string', 'children': [], 'value': '"CN"'}, {'id': '93', 'type': 'for_in_clause', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'geom_matches'}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'station_codes'}, {'id': '101', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '102', 'type': 'for_statement', 'children': ['103', '104', '105']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'state_url'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'state_urls'}, {'id': '105', 'type': 'block', 'children': ['106', '136']}; {'id': '106', 'type': 'if_statement', 'children': ['107', '110']}, {'id': '107', 'type': 'comparison_operator', 'children': ['108', '109'], 'value': 'is'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'state_matches'}, {'id': '109', 'type': 'None', 'children': []}; {'id': '110', 'type': 'block', 'children': ['111', '130']}, {'id': '111', 'type': 'expression_statement', 'children': ['112']}; {'id': '112', 'type': 'assignment', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'state_abbr'}; {'id': '114', 'type': 'subscript', 'children': ['115', '129']}, {'id': '115', 'type': 'call', 'children': ['116', '127']}; {'id': '116', 'type': 'attribute', 'children': ['117', '126']}, {'id': '117', 'type': 'subscript', 'children': ['118', '124']}; {'id': '118', 'type': 'call', 'children': ['119', '122']}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'state_url'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'string', 'children': [], 'value': '"/"'}; {'id': '124', 'type': 'unary_operator', 'children': ['125'], 'value': '-'}, {'id': '125', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '127', 'type': 'argument_list', 'children': ['128']}; {'id': '128', 'type': 'string', 'children': [], 'value': '"."'}, {'id': '129', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '130', 'type': 'if_statement', 'children': ['131', '134']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '133'], 'value': 'not'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'state_abbr'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'state_matches'}; {'id': '134', 'type': 'block', 'children': ['135']}, {'id': '135', 'type': 'continue_statement', 'children': []}; {'id': '136', 'type': 'expression_statement', 'children': ['137']}, {'id': '137', 'type': 'call', 'children': ['138', '143']}; {'id': '138', 'type': 'attribute', 'children': ['139', '142']}, {'id': '139', 'type': 'attribute', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'station_codes'}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'extend'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'call', 'children': ['145', '148']}, {'id': '145', 'type': 'attribute', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': '_get_stations_for_state'}; {'id': '148', 'type': 'argument_list', 'children': ['149']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'state_url'}; {'id': '150', 'type': 'if_statement', 'children': ['151', '154']}, {'id': '151', 'type': 'attribute', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'bbox'}; {'id': '154', 'type': 'block', 'children': ['155', '166', '177', '231']}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'assignment', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'metadata'}; {'id': '158', 'type': 'call', 'children': ['159', '162']}, {'id': '159', 'type': 'attribute', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': '_get_metadata'}; {'id': '162', 'type': 'argument_list', 'children': ['163']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'station_codes'}; {'id': '166', 'type': 'expression_statement', 'children': ['167']}, {'id': '167', 'type': 'assignment', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'parsed_metadata'}, {'id': '169', 'type': 'call', 'children': ['170', '175']}; {'id': '170', 'type': 'attribute', 'children': ['171', '174']}, {'id': '171', 'type': 'attribute', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'parser'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': '_parse_metadata'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'metadata'}, {'id': '177', 'type': 'function_definition', 'children': ['178', '179', '181']}; {'id': '178', 'type': 'function_name', 'children': [], 'value': 'in_bbox'}, {'id': '179', 'type': 'parameters', 'children': ['180']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'code'}, {'id': '181', 'type': 'block', 'children': ['182', '190', '198']}; {'id': '182', 'type': 'expression_statement', 'children': ['183']}, {'id': '183', 'type': 'assignment', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'lat'}, {'id': '185', 'type': 'subscript', 'children': ['186', '189']}; {'id': '186', 'type': 'subscript', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'parsed_metadata'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'code'}, {'id': '189', 'type': 'string', 'children': [], 'value': '"latitude"'}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'assignment', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'lon'}, {'id': '193', 'type': 'subscript', 'children': ['194', '197']}; {'id': '194', 'type': 'subscript', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'parsed_metadata'}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'code'}, {'id': '197', 'type': 'string', 'children': [], 'value': '"longitude"'}; {'id': '198', 'type': 'return_statement', 'children': ['199']}, {'id': '199', 'type': 'parenthesized_expression', 'children': ['200']}; {'id': '200', 'type': 'boolean_operator', 'children': ['201', '224'], 'value': 'and'}, {'id': '201', 'type': 'boolean_operator', 'children': ['202', '217'], 'value': 'and'}; {'id': '202', 'type': 'boolean_operator', 'children': ['203', '210'], 'value': 'and'}, {'id': '203', 'type': 'comparison_operator', 'children': ['204', '205'], 'value': '>='}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'lon'}, {'id': '205', 'type': 'subscript', 'children': ['206', '209']}; {'id': '206', 'type': 'attribute', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'bbox'}, {'id': '209', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '210', 'type': 'comparison_operator', 'children': ['211', '212'], 'value': '<='}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'lon'}; {'id': '212', 'type': 'subscript', 'children': ['213', '216']}, {'id': '213', 'type': 'attribute', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'bbox'}; {'id': '216', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '217', 'type': 'comparison_operator', 'children': ['218', '219'], 'value': '>='}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'lat'}, {'id': '219', 'type': 'subscript', 'children': ['220', '223']}; {'id': '220', 'type': 'attribute', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'bbox'}, {'id': '223', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '224', 'type': 'comparison_operator', 'children': ['225', '226'], 'value': '<='}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'lat'}; {'id': '226', 'type': 'subscript', 'children': ['227', '230']}, {'id': '227', 'type': 'attribute', 'children': ['228', '229']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'bbox'}; {'id': '230', 'type': 'integer', 'children': [], 'value': '3'}, {'id': '231', 'type': 'expression_statement', 'children': ['232']}; {'id': '232', 'type': 'assignment', 'children': ['233', '236']}, {'id': '233', 'type': 'attribute', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'station_codes'}; {'id': '236', 'type': 'call', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '238', 'type': 'argument_list', 'children': ['239']}, {'id': '239', 'type': 'call', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'filter'}, {'id': '241', 'type': 'argument_list', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'in_bbox'}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'station_codes'}; {'id': '246', 'type': 'return_statement', 'children': ['247']}, {'id': '247', 'type': 'attribute', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'station_codes'} | def _get_station_codes(self, force=False):
if not force and self.station_codes is not None:
return self.station_codes
state_urls = self._get_state_urls()
state_matches = None
if self.bbox:
with collection(
os.path.join(
"resources",
"ne_50m_admin_1_states_provinces_lakes_shp.shp",
),
"r",
) as c:
geom_matches = [
x["properties"] for x in c.filter(bbox=self.bbox)
]
state_matches = [
x["postal"] if x["admin"] != "Canada" else "CN"
for x in geom_matches
]
self.station_codes = []
for state_url in state_urls:
if state_matches is not None:
state_abbr = state_url.split("/")[-1].split(".")[0]
if state_abbr not in state_matches:
continue
self.station_codes.extend(self._get_stations_for_state(state_url))
if self.bbox:
metadata = self._get_metadata(self.station_codes)
parsed_metadata = self.parser._parse_metadata(metadata)
def in_bbox(code):
lat = parsed_metadata[code]["latitude"]
lon = parsed_metadata[code]["longitude"]
return (
lon >= self.bbox[0]
and lon <= self.bbox[2]
and lat >= self.bbox[1]
and lat <= self.bbox[3]
)
self.station_codes = list(filter(in_bbox, self.station_codes))
return self.station_codes |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_validate_query'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '5', 'type': 'block', 'children': ['6', '13', '27', '38', '85', '116', '136', '147']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '9', 'type': 'call', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'deepcopy'}, {'id': '11', 'type': 'argument_list', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '13', 'type': 'if_statement', 'children': ['14', '21']}; {'id': '14', 'type': 'comparison_operator', 'children': ['15', '18'], 'value': '=='}, {'id': '15', 'type': 'subscript', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '17', 'type': 'string', 'children': [], 'value': '"q"'}; {'id': '18', 'type': 'subscript', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'BLANK_QUERY'}; {'id': '20', 'type': 'string', 'children': [], 'value': '"q"'}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'raise_statement', 'children': ['23']}, {'id': '23', 'type': 'call', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'string', 'children': [], 'value': '"No query specified."'}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '32']}, {'id': '29', 'type': 'subscript', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '31', 'type': 'string', 'children': [], 'value': '"q"'}; {'id': '32', 'type': 'call', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': '_clean_query_string'}; {'id': '34', 'type': 'argument_list', 'children': ['35']}, {'id': '35', 'type': 'subscript', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '37', 'type': 'string', 'children': [], 'value': '"q"'}; {'id': '38', 'type': 'if_statement', 'children': ['39', '44', '56']}, {'id': '39', 'type': 'comparison_operator', 'children': ['40', '43'], 'value': 'is'}; {'id': '40', 'type': 'subscript', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '42', 'type': 'string', 'children': [], 'value': '"limit"'}, {'id': '43', 'type': 'None', 'children': []}; {'id': '44', 'type': 'block', 'children': ['45']}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '50']}, {'id': '47', 'type': 'subscript', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '49', 'type': 'string', 'children': [], 'value': '"limit"'}; {'id': '50', 'type': 'conditional_expression', 'children': ['51', '52', '55'], 'value': 'if'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'SEARCH_LIMIT'}; {'id': '52', 'type': 'subscript', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '54', 'type': 'string', 'children': [], 'value': '"advanced"'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'NONADVANCED_LIMIT'}; {'id': '56', 'type': 'elif_clause', 'children': ['57', '62']}, {'id': '57', 'type': 'comparison_operator', 'children': ['58', '61'], 'value': '>'}; {'id': '58', 'type': 'subscript', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '60', 'type': 'string', 'children': [], 'value': '"limit"'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'SEARCH_LIMIT'}; {'id': '62', 'type': 'block', 'children': ['63', '79']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'call', 'children': ['65', '68']}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'warnings'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'warn'}; {'id': '68', 'type': 'argument_list', 'children': ['69', '78']}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'string', 'children': [], 'value': "'Reduced result limit from {} to the Search maximum: {}'"}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '73', 'type': 'argument_list', 'children': ['74', '77']}; {'id': '74', 'type': 'subscript', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '76', 'type': 'string', 'children': [], 'value': '"limit"'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'SEARCH_LIMIT'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'RuntimeWarning'}, {'id': '79', 'type': 'expression_statement', 'children': ['80']}; {'id': '80', 'type': 'assignment', 'children': ['81', '84']}, {'id': '81', 'type': 'subscript', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '83', 'type': 'string', 'children': [], 'value': '"limit"'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'SEARCH_LIMIT'}, {'id': '85', 'type': 'for_statement', 'children': ['86', '89', '94']}; {'id': '86', 'type': 'pattern_list', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '89', 'type': 'call', 'children': ['90', '93']}; {'id': '90', 'type': 'attribute', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'BLANK_QUERY'}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '93', 'type': 'argument_list', 'children': []}; {'id': '94', 'type': 'block', 'children': ['95']}, {'id': '95', 'type': 'if_statement', 'children': ['96', '108']}; {'id': '96', 'type': 'comparison_operator', 'children': ['97', '107'], 'value': '=='}, {'id': '97', 'type': 'call', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '103', 'type': 'call', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'float'}, {'id': '105', 'type': 'argument_list', 'children': ['106']}; {'id': '106', 'type': 'string', 'children': [], 'value': "'nan'"}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'call', 'children': ['111', '114']}, {'id': '111', 'type': 'attribute', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '116', 'type': 'expression_statement', 'children': ['117']}, {'id': '117', 'type': 'assignment', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'to_remove'}, {'id': '119', 'type': 'list_comprehension', 'children': ['120', '121', '128']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '121', 'type': 'for_in_clause', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '123', 'type': 'call', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'query'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '127', 'type': 'argument_list', 'children': []}; {'id': '128', 'type': 'if_clause', 'children': ['129']}, {'id': '129', 'type': 'comparison_operator', 'children': ['130', '131'], 'value': 'not'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '131', 'type': 'call', 'children': ['132', '135']}; {'id': '132', 'type': 'attribute', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'BLANK_QUERY'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '135', 'type': 'argument_list', 'children': []}; {'id': '136', 'type': 'expression_statement', 'children': ['137']}, {'id': '137', 'type': 'list_comprehension', 'children': ['138', '144']}; {'id': '138', 'type': 'call', 'children': ['139', '142']}, {'id': '139', 'type': 'attribute', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'query'}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '142', 'type': 'argument_list', 'children': ['143']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '144', 'type': 'for_in_clause', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'to_remove'}, {'id': '147', 'type': 'return_statement', 'children': ['148']} | def _validate_query(query):
query = deepcopy(query)
if query["q"] == BLANK_QUERY["q"]:
raise ValueError("No query specified.")
query["q"] = _clean_query_string(query["q"])
if query["limit"] is None:
query["limit"] = SEARCH_LIMIT if query["advanced"] else NONADVANCED_LIMIT
elif query["limit"] > SEARCH_LIMIT:
warnings.warn('Reduced result limit from {} to the Search maximum: {}'
.format(query["limit"], SEARCH_LIMIT), RuntimeWarning)
query["limit"] = SEARCH_LIMIT
for key, val in BLANK_QUERY.items():
if query.get(key, float('nan')) == val:
query.pop(key)
to_remove = [field for field in query.keys() if field not in BLANK_QUERY.keys()]
[query.pop(field) for field in to_remove]
return query |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'show_fields'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'block'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '17', '101']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'mapping'}; {'id': '12', 'type': 'call', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': '_mapping'}; {'id': '16', 'type': 'argument_list', 'children': []}, {'id': '17', 'type': 'if_statement', 'children': ['18', '21', '24', '71']}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': 'is'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'block'}; {'id': '20', 'type': 'None', 'children': []}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'return_statement', 'children': ['23']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'mapping'}; {'id': '24', 'type': 'elif_clause', 'children': ['25', '28']}, {'id': '25', 'type': 'comparison_operator', 'children': ['26', '27'], 'value': '=='}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'block'}, {'id': '27', 'type': 'string', 'children': [], 'value': '"top"'}; {'id': '28', 'type': 'block', 'children': ['29', '35', '57', '61']}, {'id': '29', 'type': 'expression_statement', 'children': ['30']}; {'id': '30', 'type': 'assignment', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'blocks'}; {'id': '32', 'type': 'call', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '34', 'type': 'argument_list', 'children': []}, {'id': '35', 'type': 'for_statement', 'children': ['36', '37', '42']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '37', 'type': 'call', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'mapping'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '41', 'type': 'argument_list', 'children': []}; {'id': '42', 'type': 'block', 'children': ['43']}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'call', 'children': ['45', '48']}, {'id': '45', 'type': 'attribute', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'blocks'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '48', 'type': 'argument_list', 'children': ['49']}, {'id': '49', 'type': 'subscript', 'children': ['50', '56']}; {'id': '50', 'type': 'call', 'children': ['51', '54']}, {'id': '51', 'type': 'attribute', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '54', 'type': 'argument_list', 'children': ['55']}, {'id': '55', 'type': 'string', 'children': [], 'value': '"."'}; {'id': '56', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '57', 'type': 'expression_statement', 'children': ['58']}; {'id': '58', 'type': 'assignment', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'block_map'}; {'id': '60', 'type': 'dictionary', 'children': []}, {'id': '61', 'type': 'for_statement', 'children': ['62', '63', '64']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'blocks'}; {'id': '64', 'type': 'block', 'children': ['65']}, {'id': '65', 'type': 'expression_statement', 'children': ['66']}; {'id': '66', 'type': 'assignment', 'children': ['67', '70']}, {'id': '67', 'type': 'subscript', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'block_map'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'b'}; {'id': '70', 'type': 'string', 'children': [], 'value': '"object"'}, {'id': '71', 'type': 'else_clause', 'children': ['72']}; {'id': '72', 'type': 'block', 'children': ['73', '77']}, {'id': '73', 'type': 'expression_statement', 'children': ['74']}; {'id': '74', 'type': 'assignment', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'block_map'}; {'id': '76', 'type': 'dictionary', 'children': []}, {'id': '77', 'type': 'for_statement', 'children': ['78', '81', '86']}; {'id': '78', 'type': 'pattern_list', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'mapping'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '85', 'type': 'argument_list', 'children': []}; {'id': '86', 'type': 'block', 'children': ['87']}, {'id': '87', 'type': 'if_statement', 'children': ['88', '94']}; {'id': '88', 'type': 'call', 'children': ['89', '92']}, {'id': '89', 'type': 'attribute', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'block'}; {'id': '94', 'type': 'block', 'children': ['95']}, {'id': '95', 'type': 'expression_statement', 'children': ['96']}; {'id': '96', 'type': 'assignment', 'children': ['97', '100']}, {'id': '97', 'type': 'subscript', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'block_map'}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '101', 'type': 'return_statement', 'children': ['102']} | def show_fields(self, block=None):
mapping = self._mapping()
if block is None:
return mapping
elif block == "top":
blocks = set()
for key in mapping.keys():
blocks.add(key.split(".")[0])
block_map = {}
for b in blocks:
block_map[b] = "object"
else:
block_map = {}
for key, value in mapping.items():
if key.startswith(block):
block_map[key] = value
return block_map |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'sorted'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'by'}; {'id': '6', 'type': 'dictionary_splat_pattern', 'children': ['7']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '8', 'type': 'block', 'children': ['9', '22']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'sort_idc'}; {'id': '12', 'type': 'call', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'argsort'}; {'id': '16', 'type': 'argument_list', 'children': ['17', '20']}, {'id': '17', 'type': 'subscript', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'by'}; {'id': '20', 'type': 'dictionary_splat', 'children': ['21']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '22', 'type': 'return_statement', 'children': ['23']}, {'id': '23', 'type': 'call', 'children': ['24', '27']}; {'id': '24', 'type': 'attribute', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': '__class__'}, {'id': '27', 'type': 'argument_list', 'children': ['28', '31', '36', '41']}; {'id': '28', 'type': 'subscript', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'sort_idc'}, {'id': '31', 'type': 'keyword_argument', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'h5loc'}, {'id': '33', 'type': 'attribute', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'h5loc'}; {'id': '36', 'type': 'keyword_argument', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'split_h5'}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'split_h5'}, {'id': '41', 'type': 'keyword_argument', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'name'} | def sorted(self, by, **kwargs):
sort_idc = np.argsort(self[by], **kwargs)
return self.__class__(
self[sort_idc],
h5loc=self.h5loc,
split_h5=self.split_h5,
name=self.name
) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'create_multi_output_factor'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '9', 'type': 'block', 'children': ['10', '33', '54', '75', '84', '90', '100', '113', '331', '338']}; {'id': '10', 'type': 'if_statement', 'children': ['11', '19']}, {'id': '11', 'type': 'boolean_operator', 'children': ['12', '13'], 'value': 'and'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '13', 'type': 'not_operator', 'children': ['14']}; {'id': '14', 'type': 'call', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '16', 'type': 'argument_list', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'Node'}, {'id': '19', 'type': 'block', 'children': ['20']}; {'id': '20', 'type': 'raise_statement', 'children': ['21']}, {'id': '21', 'type': 'call', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'call', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'string', 'children': [], 'value': '"Expected Node, got {}"'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '28', 'type': 'argument_list', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'type'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '33', 'type': 'if_statement', 'children': ['34', '40']}; {'id': '34', 'type': 'not_operator', 'children': ['35']}, {'id': '35', 'type': 'call', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '37', 'type': 'argument_list', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'Node'}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'raise_statement', 'children': ['42']}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'string', 'children': [], 'value': '"Expected Node, got {}"'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '49', 'type': 'argument_list', 'children': ['50']}; {'id': '50', 'type': 'call', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'type'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '54', 'type': 'if_statement', 'children': ['55', '61']}, {'id': '55', 'type': 'not_operator', 'children': ['56']}; {'id': '56', 'type': 'call', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '58', 'type': 'argument_list', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'MultiOutputTool'}, {'id': '61', 'type': 'block', 'children': ['62']}; {'id': '62', 'type': 'raise_statement', 'children': ['63']}, {'id': '63', 'type': 'call', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '65', 'type': 'argument_list', 'children': ['66']}; {'id': '66', 'type': 'call', 'children': ['67', '70']}, {'id': '67', 'type': 'attribute', 'children': ['68', '69']}; {'id': '68', 'type': 'string', 'children': [], 'value': '"Expected MultiOutputTool, got {}"'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '70', 'type': 'argument_list', 'children': ['71']}, {'id': '71', 'type': 'call', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'type'}, {'id': '73', 'type': 'argument_list', 'children': ['74']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'input_plates'}; {'id': '78', 'type': 'conditional_expression', 'children': ['79', '82', '83'], 'value': 'if'}, {'id': '79', 'type': 'attribute', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'plates'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '83', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'plates'}; {'id': '90', 'type': 'if_statement', 'children': ['91', '97']}, {'id': '91', 'type': 'comparison_operator', 'children': ['92', '96'], 'value': '>'}; {'id': '92', 'type': 'call', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '94', 'type': 'argument_list', 'children': ['95']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'input_plates'}; {'id': '96', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '97', 'type': 'block', 'children': ['98']}; {'id': '98', 'type': 'raise_statement', 'children': ['99']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'NotImplementedError'}; {'id': '100', 'type': 'if_statement', 'children': ['101', '107']}, {'id': '101', 'type': 'comparison_operator', 'children': ['102', '106'], 'value': '=='}; {'id': '102', 'type': 'call', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '104', 'type': 'argument_list', 'children': ['105']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '106', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '107', 'type': 'block', 'children': ['108']}; {'id': '108', 'type': 'raise_statement', 'children': ['109']}, {'id': '109', 'type': 'call', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '111', 'type': 'argument_list', 'children': ['112']}; {'id': '112', 'type': 'string', 'children': [], 'value': '"No output plate found"'}, {'id': '113', 'type': 'if_statement', 'children': ['114', '120', '169']}; {'id': '114', 'type': 'comparison_operator', 'children': ['115', '119'], 'value': '=='}, {'id': '115', 'type': 'call', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '117', 'type': 'argument_list', 'children': ['118']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '119', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '120', 'type': 'block', 'children': ['121', '138']}, {'id': '121', 'type': 'if_statement', 'children': ['122', '132']}; {'id': '122', 'type': 'not_operator', 'children': ['123']}, {'id': '123', 'type': 'call', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'check_multi_output_plate_compatibility'}, {'id': '127', 'type': 'argument_list', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '129', 'type': 'subscript', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '131', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '132', 'type': 'block', 'children': ['133']}, {'id': '133', 'type': 'raise_statement', 'children': ['134']}; {'id': '134', 'type': 'call', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'IncompatiblePlatesError'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'string', 'children': [], 'value': '"Parent plate does not match input plate"'}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'assignment', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'factor'}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'MultiOutputFactor'}, {'id': '143', 'type': 'argument_list', 'children': ['144', '147', '150', '153', '156', '164']}; {'id': '144', 'type': 'keyword_argument', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '147', 'type': 'keyword_argument', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'source_node'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '150', 'type': 'keyword_argument', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '153', 'type': 'keyword_argument', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'sink_node'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '156', 'type': 'keyword_argument', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'input_plate'}; {'id': '158', 'type': 'conditional_expression', 'children': ['159', '162', '163'], 'value': 'if'}, {'id': '159', 'type': 'subscript', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '161', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '163', 'type': 'None', 'children': []}; {'id': '164', 'type': 'keyword_argument', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '166', 'type': 'subscript', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '168', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '169', 'type': 'else_clause', 'children': ['170']}; {'id': '170', 'type': 'block', 'children': ['171', '181', '194', '251', '305']}, {'id': '171', 'type': 'if_statement', 'children': ['172', '178']}; {'id': '172', 'type': 'comparison_operator', 'children': ['173', '177'], 'value': '>'}, {'id': '173', 'type': 'call', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '177', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '178', 'type': 'block', 'children': ['179']}, {'id': '179', 'type': 'raise_statement', 'children': ['180']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'NotImplementedError'}, {'id': '181', 'type': 'if_statement', 'children': ['182', '188']}; {'id': '182', 'type': 'comparison_operator', 'children': ['183', '187'], 'value': '!='}, {'id': '183', 'type': 'call', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '185', 'type': 'argument_list', 'children': ['186']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '187', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '188', 'type': 'block', 'children': ['189']}, {'id': '189', 'type': 'raise_statement', 'children': ['190']}; {'id': '190', 'type': 'call', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'IncompatiblePlatesError'}; {'id': '192', 'type': 'argument_list', 'children': ['193']}, {'id': '193', 'type': 'string', 'children': [], 'value': '"Require an input plate to match all but one of the output plates"'}; {'id': '194', 'type': 'if_statement', 'children': ['195', '202', '209']}, {'id': '195', 'type': 'comparison_operator', 'children': ['196', '199'], 'value': '=='}; {'id': '196', 'type': 'subscript', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '198', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '199', 'type': 'subscript', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '201', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '202', 'type': 'block', 'children': ['203']}, {'id': '203', 'type': 'expression_statement', 'children': ['204']}; {'id': '204', 'type': 'assignment', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'output_plate'}; {'id': '206', 'type': 'subscript', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '208', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '209', 'type': 'else_clause', 'children': ['210']}; {'id': '210', 'type': 'block', 'children': ['211', '229', '235']}, {'id': '211', 'type': 'if_statement', 'children': ['212', '223']}; {'id': '212', 'type': 'comparison_operator', 'children': ['213', '218'], 'value': '!='}, {'id': '213', 'type': 'attribute', 'children': ['214', '217']}; {'id': '214', 'type': 'subscript', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '216', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'plate_id'}; {'id': '218', 'type': 'attribute', 'children': ['219', '222']}, {'id': '219', 'type': 'subscript', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '221', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'plate_id'}, {'id': '223', 'type': 'block', 'children': ['224']}; {'id': '224', 'type': 'raise_statement', 'children': ['225']}, {'id': '225', 'type': 'call', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'IncompatiblePlatesError'}, {'id': '227', 'type': 'argument_list', 'children': ['228']}; {'id': '228', 'type': 'string', 'children': [], 'value': '"Require an input plate to match all but one of the output plates"'}, {'id': '229', 'type': 'expression_statement', 'children': ['230']}; {'id': '230', 'type': 'assignment', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'output_plate'}; {'id': '232', 'type': 'subscript', 'children': ['233', '234']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '234', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '235', 'type': 'expression_statement', 'children': ['236']}; {'id': '236', 'type': 'assignment', 'children': ['237', '244']}, {'id': '237', 'type': 'pattern_list', 'children': ['238', '241']}; {'id': '238', 'type': 'subscript', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '240', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '241', 'type': 'subscript', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '243', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '244', 'type': 'expression_list', 'children': ['245', '248']}, {'id': '245', 'type': 'subscript', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '247', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '248', 'type': 'subscript', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '250', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '251', 'type': 'if_statement', 'children': ['252', '256']}; {'id': '252', 'type': 'not_operator', 'children': ['253']}, {'id': '253', 'type': 'attribute', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'output_plate'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'is_root'}; {'id': '256', 'type': 'block', 'children': ['257', '261', '269', '296']}, {'id': '257', 'type': 'expression_statement', 'children': ['258']}; {'id': '258', 'type': 'assignment', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'match'}; {'id': '260', 'type': 'False', 'children': []}, {'id': '261', 'type': 'expression_statement', 'children': ['262']}; {'id': '262', 'type': 'assignment', 'children': ['263', '264']}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '264', 'type': 'attribute', 'children': ['265', '268']}, {'id': '265', 'type': 'subscript', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '267', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '269', 'type': 'while_statement', 'children': ['270', '273']}; {'id': '270', 'type': 'comparison_operator', 'children': ['271', '272'], 'value': 'is'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '272', 'type': 'None', 'children': []}, {'id': '273', 'type': 'block', 'children': ['274', '290']}; {'id': '274', 'type': 'if_statement', 'children': ['275', '284']}, {'id': '275', 'type': 'comparison_operator', 'children': ['276', '279'], 'value': '=='}; {'id': '276', 'type': 'attribute', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'plate_id'}, {'id': '279', 'type': 'attribute', 'children': ['280', '283']}; {'id': '280', 'type': 'attribute', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'output_plate'}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'plate_id'}; {'id': '284', 'type': 'block', 'children': ['285', '289']}, {'id': '285', 'type': 'expression_statement', 'children': ['286']}; {'id': '286', 'type': 'assignment', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'match'}; {'id': '288', 'type': 'True', 'children': []}, {'id': '289', 'type': 'break_statement', 'children': []}; {'id': '290', 'type': 'expression_statement', 'children': ['291']}, {'id': '291', 'type': 'assignment', 'children': ['292', '293']}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '293', 'type': 'attribute', 'children': ['294', '295']}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '296', 'type': 'if_statement', 'children': ['297', '299']}, {'id': '297', 'type': 'not_operator', 'children': ['298']}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'match'}, {'id': '299', 'type': 'block', 'children': ['300']}; {'id': '300', 'type': 'raise_statement', 'children': ['301']}, {'id': '301', 'type': 'call', 'children': ['302', '303']}; {'id': '302', 'type': 'identifier', 'children': [], 'value': 'IncompatiblePlatesError'}, {'id': '303', 'type': 'argument_list', 'children': ['304']}; {'id': '304', 'type': 'string', 'children': [], 'value': '"Require an input plate to match all but one of the output plates"'}, {'id': '305', 'type': 'expression_statement', 'children': ['306']}; {'id': '306', 'type': 'assignment', 'children': ['307', '308']}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '308', 'type': 'call', 'children': ['309', '310']}, {'id': '309', 'type': 'identifier', 'children': [], 'value': 'MultiOutputFactor'}; {'id': '310', 'type': 'argument_list', 'children': ['311', '314', '317', '320', '323', '328']}, {'id': '311', 'type': 'keyword_argument', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '314', 'type': 'keyword_argument', 'children': ['315', '316']}, {'id': '315', 'type': 'identifier', 'children': [], 'value': 'source_node'}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '317', 'type': 'keyword_argument', 'children': ['318', '319']}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'sink_node'}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '320', 'type': 'keyword_argument', 'children': ['321', '322']}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '322', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '323', 'type': 'keyword_argument', 'children': ['324', '325']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '325', 'type': 'subscript', 'children': ['326', '327']}; {'id': '326', 'type': 'identifier', 'children': [], 'value': 'input_plates'}, {'id': '327', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '328', 'type': 'keyword_argument', 'children': ['329', '330']}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'output_plates'}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'output_plates'}, {'id': '331', 'type': 'expression_statement', 'children': ['332']}; {'id': '332', 'type': 'call', 'children': ['333', '336']}, {'id': '333', 'type': 'attribute', 'children': ['334', '335']}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '335', 'type': 'identifier', 'children': [], 'value': '_add_factor'}; {'id': '336', 'type': 'argument_list', 'children': ['337']}, {'id': '337', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '338', 'type': 'return_statement', 'children': ['339']}, {'id': '339', 'type': 'identifier', 'children': [], 'value': 'factor'} | def create_multi_output_factor(self, tool, source, splitting_node, sink):
if source and not isinstance(source, Node):
raise ValueError("Expected Node, got {}".format(type(source)))
if not isinstance(sink, Node):
raise ValueError("Expected Node, got {}".format(type(sink)))
if not isinstance(tool, MultiOutputTool):
raise ValueError("Expected MultiOutputTool, got {}".format(type(tool)))
input_plates = source.plates if source else []
output_plates = sink.plates
if len(input_plates) > 1:
raise NotImplementedError
if len(output_plates) == 0:
raise ValueError("No output plate found")
if len(output_plates) == 1:
if not self.check_multi_output_plate_compatibility(input_plates, output_plates[0]):
raise IncompatiblePlatesError("Parent plate does not match input plate")
factor = MultiOutputFactor(tool=tool, source_node=source, splitting_node=splitting_node, sink_node=sink,
input_plate=input_plates[0] if input_plates else None,
output_plates=output_plates[0])
else:
if len(output_plates) > 2:
raise NotImplementedError
if len(input_plates) != 1:
raise IncompatiblePlatesError("Require an input plate to match all but one of the output plates")
if output_plates[0] == input_plates[0]:
output_plate = output_plates[1]
else:
if output_plates[1].plate_id != input_plates[0].plate_id:
raise IncompatiblePlatesError("Require an input plate to match all but one of the output plates")
output_plate = output_plates[0]
output_plates[1], output_plates[0] = output_plates[0], output_plates[1]
if not output_plate.is_root:
match = False
parent = input_plates[0].parent
while parent is not None:
if parent.plate_id == output_plate.parent.plate_id:
match = True
break
parent = parent.parent
if not match:
raise IncompatiblePlatesError("Require an input plate to match all but one of the output plates")
factor = MultiOutputFactor(
tool=tool, source_node=source, sink_node=sink,
splitting_node=splitting_node, input_plate=input_plates[0], output_plates=output_plates)
self._add_factor(factor)
return factor |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'to_dict'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'tool_long_names'}, {'id': '7', 'type': 'True', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '27', '82', '218', '229']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '12', 'type': 'call', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '14', 'type': 'argument_list', 'children': ['15', '18', '21']}, {'id': '15', 'type': 'keyword_argument', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '17', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '18', 'type': 'keyword_argument', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'factors'}; {'id': '20', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '21', 'type': 'keyword_argument', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'plates'}, {'id': '23', 'type': 'call', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '27', 'type': 'for_statement', 'children': ['28', '29', '32']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '32', 'type': 'block', 'children': ['33', '43', '55']}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'node_id'}; {'id': '36', 'type': 'attribute', 'children': ['37', '42']}, {'id': '37', 'type': 'subscript', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'nodes'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '43', 'type': 'expression_statement', 'children': ['44']}; {'id': '44', 'type': 'call', 'children': ['45', '50']}, {'id': '45', 'type': 'attribute', 'children': ['46', '49']}; {'id': '46', 'type': 'subscript', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '48', 'type': 'string', 'children': [], 'value': "'nodes'"}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '50', 'type': 'argument_list', 'children': ['51']}, {'id': '51', 'type': 'dictionary', 'children': ['52']}; {'id': '52', 'type': 'pair', 'children': ['53', '54']}, {'id': '53', 'type': 'string', 'children': [], 'value': "'id'"}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '55', 'type': 'for_statement', 'children': ['56', '57', '64']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'plate_id'}, {'id': '57', 'type': 'attribute', 'children': ['58', '63']}; {'id': '58', 'type': 'subscript', 'children': ['59', '62']}, {'id': '59', 'type': 'attribute', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'nodes'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'plate_ids'}; {'id': '64', 'type': 'block', 'children': ['65']}, {'id': '65', 'type': 'expression_statement', 'children': ['66']}; {'id': '66', 'type': 'call', 'children': ['67', '74']}, {'id': '67', 'type': 'attribute', 'children': ['68', '73']}; {'id': '68', 'type': 'subscript', 'children': ['69', '72']}, {'id': '69', 'type': 'subscript', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '71', 'type': 'string', 'children': [], 'value': "'plates'"}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'plate_id'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '74', 'type': 'argument_list', 'children': ['75']}, {'id': '75', 'type': 'dictionary', 'children': ['76', '79']}; {'id': '76', 'type': 'pair', 'children': ['77', '78']}, {'id': '77', 'type': 'string', 'children': [], 'value': "'id'"}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '79', 'type': 'pair', 'children': ['80', '81']}; {'id': '80', 'type': 'string', 'children': [], 'value': "'type'"}, {'id': '81', 'type': 'string', 'children': [], 'value': "'node'"}; {'id': '82', 'type': 'for_statement', 'children': ['83', '84', '87']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'factors'}, {'id': '87', 'type': 'block', 'children': ['88', '104', '141', '163']}; {'id': '88', 'type': 'expression_statement', 'children': ['89']}, {'id': '89', 'type': 'assignment', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '91', 'type': 'conditional_expression', 'children': ['92', '98', '99'], 'value': 'if'}; {'id': '92', 'type': 'call', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '94', 'type': 'argument_list', 'children': ['95']}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'factor'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'tool_long_names'}, {'id': '99', 'type': 'attribute', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '104', 'type': 'try_statement', 'children': ['105', '118']}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '109', 'type': 'list_comprehension', 'children': ['110', '113']}; {'id': '110', 'type': 'attribute', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '113', 'type': 'for_in_clause', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '115', 'type': 'attribute', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'factor'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '118', 'type': 'except_clause', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'AttributeError'}; {'id': '120', 'type': 'block', 'children': ['121']}, {'id': '121', 'type': 'if_statement', 'children': ['122', '125', '135']}; {'id': '122', 'type': 'attribute', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '125', 'type': 'block', 'children': ['126']}; {'id': '126', 'type': 'expression_statement', 'children': ['127']}, {'id': '127', 'type': 'assignment', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '129', 'type': 'list', 'children': ['130'], 'value': '[factor.source.node_id]'}; {'id': '130', 'type': 'attribute', 'children': ['131', '134']}, {'id': '131', 'type': 'attribute', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'factor'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '135', 'type': 'else_clause', 'children': ['136']}; {'id': '136', 'type': 'block', 'children': ['137']}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'assignment', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '140', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '141', 'type': 'expression_statement', 'children': ['142']}; {'id': '142', 'type': 'call', 'children': ['143', '148']}, {'id': '143', 'type': 'attribute', 'children': ['144', '147']}; {'id': '144', 'type': 'subscript', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '146', 'type': 'string', 'children': [], 'value': "'factors'"}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '148', 'type': 'argument_list', 'children': ['149']}, {'id': '149', 'type': 'dictionary', 'children': ['150', '153', '156']}; {'id': '150', 'type': 'pair', 'children': ['151', '152']}, {'id': '151', 'type': 'string', 'children': [], 'value': "'id'"}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '153', 'type': 'pair', 'children': ['154', '155']}; {'id': '154', 'type': 'string', 'children': [], 'value': "'sources'"}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '156', 'type': 'pair', 'children': ['157', '158']}, {'id': '157', 'type': 'string', 'children': [], 'value': "'sink'"}; {'id': '158', 'type': 'attribute', 'children': ['159', '162']}, {'id': '159', 'type': 'attribute', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'factor'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'node_id'}, {'id': '163', 'type': 'try_statement', 'children': ['164', '214']}; {'id': '164', 'type': 'block', 'children': ['165']}, {'id': '165', 'type': 'if_statement', 'children': ['166', '169', '195']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'plates'}, {'id': '169', 'type': 'block', 'children': ['170']}; {'id': '170', 'type': 'for_statement', 'children': ['171', '172', '175']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'plate'}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'factor'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'plates'}, {'id': '175', 'type': 'block', 'children': ['176']}; {'id': '176', 'type': 'expression_statement', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '187']}; {'id': '178', 'type': 'attribute', 'children': ['179', '186']}, {'id': '179', 'type': 'subscript', 'children': ['180', '183']}; {'id': '180', 'type': 'subscript', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '182', 'type': 'string', 'children': [], 'value': "'plates'"}, {'id': '183', 'type': 'attribute', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'plate'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'plate_id'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '187', 'type': 'argument_list', 'children': ['188']}; {'id': '188', 'type': 'dictionary', 'children': ['189', '192']}, {'id': '189', 'type': 'pair', 'children': ['190', '191']}; {'id': '190', 'type': 'string', 'children': [], 'value': "'id'"}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '192', 'type': 'pair', 'children': ['193', '194']}, {'id': '193', 'type': 'string', 'children': [], 'value': "'type'"}; {'id': '194', 'type': 'string', 'children': [], 'value': "'factor'"}, {'id': '195', 'type': 'else_clause', 'children': ['196']}; {'id': '196', 'type': 'block', 'children': ['197']}, {'id': '197', 'type': 'expression_statement', 'children': ['198']}; {'id': '198', 'type': 'call', 'children': ['199', '206']}, {'id': '199', 'type': 'attribute', 'children': ['200', '205']}; {'id': '200', 'type': 'subscript', 'children': ['201', '204']}, {'id': '201', 'type': 'subscript', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '203', 'type': 'string', 'children': [], 'value': "'plates'"}; {'id': '204', 'type': 'string', 'children': [], 'value': "'root'"}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '206', 'type': 'argument_list', 'children': ['207']}, {'id': '207', 'type': 'dictionary', 'children': ['208', '211']}; {'id': '208', 'type': 'pair', 'children': ['209', '210']}, {'id': '209', 'type': 'string', 'children': [], 'value': "'id'"}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '211', 'type': 'pair', 'children': ['212', '213']}; {'id': '212', 'type': 'string', 'children': [], 'value': "'type'"}, {'id': '213', 'type': 'string', 'children': [], 'value': "'factor'"}; {'id': '214', 'type': 'except_clause', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'AttributeError'}; {'id': '216', 'type': 'block', 'children': ['217']}, {'id': '217', 'type': 'pass_statement', 'children': []}; {'id': '218', 'type': 'expression_statement', 'children': ['219']}, {'id': '219', 'type': 'assignment', 'children': ['220', '223']}; {'id': '220', 'type': 'subscript', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '222', 'type': 'string', 'children': [], 'value': "'plates'"}, {'id': '223', 'type': 'call', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '225', 'type': 'argument_list', 'children': ['226']}; {'id': '226', 'type': 'subscript', 'children': ['227', '228']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '228', 'type': 'string', 'children': [], 'value': "'plates'"}, {'id': '229', 'type': 'return_statement', 'children': ['230']} | def to_dict(self, tool_long_names=True):
d = dict(nodes=[], factors=[], plates=defaultdict(list))
for node in self.nodes:
node_id = self.nodes[node].node_id
d['nodes'].append({'id': node_id})
for plate_id in self.nodes[node].plate_ids:
d['plates'][plate_id].append({'id': node_id, 'type': 'node'})
for factor in self.factors:
tool = str(factor.tool) if tool_long_names else factor.tool.name
try:
sources = [s.node_id for s in factor.sources]
except AttributeError:
if factor.source:
sources = [factor.source.node_id]
else:
sources = []
d['factors'].append({
'id': tool,
'sources': sources,
'sink': factor.sink.node_id})
try:
if factor.plates:
for plate in factor.plates:
d['plates'][plate.plate_id].append({'id': tool, 'type': 'factor'})
else:
d['plates']['root'].append({'id': tool, 'type': 'factor'})
except AttributeError:
pass
d['plates'] = dict(d['plates'])
return d |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '13']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'GenericPump'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'use_jppy'}, {'id': '7', 'type': 'False', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '10', 'type': 'string', 'children': [], 'value': '"GenericPump"'}, {'id': '11', 'type': 'dictionary_splat_pattern', 'children': ['12']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '13', 'type': 'block', 'children': ['14', '26', '48', '67', '87', '96', '118', '140', '158', '207', '219']}; {'id': '14', 'type': 'if_statement', 'children': ['15', '20']}, {'id': '15', 'type': 'call', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '17', 'type': 'argument_list', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '20', 'type': 'block', 'children': ['21']}, {'id': '21', 'type': 'expression_statement', 'children': ['22']}; {'id': '22', 'type': 'assignment', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'filenames'}; {'id': '24', 'type': 'list', 'children': ['25'], 'value': '[filenames]'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'filenames'}; {'id': '26', 'type': 'try_statement', 'children': ['27', '33']}, {'id': '27', 'type': 'block', 'children': ['28']}; {'id': '28', 'type': 'expression_statement', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'iter'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '33', 'type': 'except_clause', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '35', 'type': 'block', 'children': ['36', '43']}; {'id': '36', 'type': 'expression_statement', 'children': ['37']}, {'id': '37', 'type': 'call', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'critical'}, {'id': '41', 'type': 'argument_list', 'children': ['42']}; {'id': '42', 'type': 'string', 'children': [], 'value': '"Don\'t know how to iterate through filenames."'}, {'id': '43', 'type': 'raise_statement', 'children': ['44']}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'TypeError'}; {'id': '46', 'type': 'argument_list', 'children': ['47']}, {'id': '47', 'type': 'string', 'children': [], 'value': '"Invalid filenames."'}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'extensions'}, {'id': '51', 'type': 'call', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '53', 'type': 'generator_expression', 'children': ['54', '64']}; {'id': '54', 'type': 'subscript', 'children': ['55', '63']}, {'id': '55', 'type': 'call', 'children': ['56', '61']}; {'id': '56', 'type': 'attribute', 'children': ['57', '60']}, {'id': '57', 'type': 'attribute', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'os'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'splitext'}, {'id': '61', 'type': 'argument_list', 'children': ['62']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'fn'}, {'id': '63', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '64', 'type': 'for_in_clause', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'fn'}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '67', 'type': 'if_statement', 'children': ['68', '74']}; {'id': '68', 'type': 'comparison_operator', 'children': ['69', '73'], 'value': '>'}, {'id': '69', 'type': 'call', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '71', 'type': 'argument_list', 'children': ['72']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'extensions'}, {'id': '73', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '74', 'type': 'block', 'children': ['75', '82']}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'critical'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'string', 'children': [], 'value': '"Mixed filetypes, please use only files of the same type"'}; {'id': '82', 'type': 'raise_statement', 'children': ['83']}, {'id': '83', 'type': 'call', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'IOError'}, {'id': '85', 'type': 'argument_list', 'children': ['86']}; {'id': '86', 'type': 'string', 'children': [], 'value': '"Mixed filetypes."'}, {'id': '87', 'type': 'expression_statement', 'children': ['88']}; {'id': '88', 'type': 'assignment', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'extension'}; {'id': '90', 'type': 'subscript', 'children': ['91', '95']}, {'id': '91', 'type': 'call', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '93', 'type': 'argument_list', 'children': ['94']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'extensions'}, {'id': '95', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'io'}, {'id': '99', 'type': 'dictionary', 'children': ['100', '103', '106', '112', '115']}; {'id': '100', 'type': 'pair', 'children': ['101', '102']}, {'id': '101', 'type': 'string', 'children': [], 'value': "'.evt'"}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'EvtPump'}, {'id': '103', 'type': 'pair', 'children': ['104', '105']}; {'id': '104', 'type': 'string', 'children': [], 'value': "'.h5'"}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'HDF5Pump'}; {'id': '106', 'type': 'pair', 'children': ['107', '108']}, {'id': '107', 'type': 'string', 'children': [], 'value': "'.root'"}; {'id': '108', 'type': 'conditional_expression', 'children': ['109', '110', '111'], 'value': 'if'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'EventPump'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'use_jppy'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'AanetPump'}; {'id': '112', 'type': 'pair', 'children': ['113', '114']}, {'id': '113', 'type': 'string', 'children': [], 'value': "'.dat'"}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'DAQPump'}, {'id': '115', 'type': 'pair', 'children': ['116', '117']}; {'id': '116', 'type': 'string', 'children': [], 'value': "'.dqd'"}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'CLBPump'}; {'id': '118', 'type': 'if_statement', 'children': ['119', '122']}, {'id': '119', 'type': 'comparison_operator', 'children': ['120', '121'], 'value': 'not'}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'extension'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'io'}; {'id': '122', 'type': 'block', 'children': ['123', '135']}, {'id': '123', 'type': 'expression_statement', 'children': ['124']}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'critical'}; {'id': '128', 'type': 'argument_list', 'children': ['129']}, {'id': '129', 'type': 'call', 'children': ['130', '133']}; {'id': '130', 'type': 'attribute', 'children': ['131', '132']}, {'id': '131', 'type': 'string', 'children': [], 'value': '"No pump found for file extension \'{0}\'"'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '133', 'type': 'argument_list', 'children': ['134']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'extension'}, {'id': '135', 'type': 'raise_statement', 'children': ['136']}; {'id': '136', 'type': 'call', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'string', 'children': [], 'value': '"Unknown filetype"'}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'assignment', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'missing_files'}, {'id': '143', 'type': 'list_comprehension', 'children': ['144', '145', '148']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'fn'}, {'id': '145', 'type': 'for_in_clause', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'fn'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'filenames'}; {'id': '148', 'type': 'if_clause', 'children': ['149']}, {'id': '149', 'type': 'not_operator', 'children': ['150']}; {'id': '150', 'type': 'call', 'children': ['151', '156']}, {'id': '151', 'type': 'attribute', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'os'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'exists'}; {'id': '156', 'type': 'argument_list', 'children': ['157']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'fn'}; {'id': '158', 'type': 'if_statement', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'missing_files'}; {'id': '160', 'type': 'block', 'children': ['161']}, {'id': '161', 'type': 'if_statement', 'children': ['162', '171', '188']}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '167'], 'value': '=='}, {'id': '163', 'type': 'call', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '165', 'type': 'argument_list', 'children': ['166']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'missing_files'}, {'id': '167', 'type': 'call', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '169', 'type': 'argument_list', 'children': ['170']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '171', 'type': 'block', 'children': ['172', '176', '183']}; {'id': '172', 'type': 'expression_statement', 'children': ['173']}, {'id': '173', 'type': 'assignment', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'message'}, {'id': '175', 'type': 'string', 'children': [], 'value': '"None of the given files could be found."'}; {'id': '176', 'type': 'expression_statement', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '181']}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'critical'}, {'id': '181', 'type': 'argument_list', 'children': ['182']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'message'}, {'id': '183', 'type': 'raise_statement', 'children': ['184']}; {'id': '184', 'type': 'call', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'SystemExit'}; {'id': '186', 'type': 'argument_list', 'children': ['187']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'message'}; {'id': '188', 'type': 'else_clause', 'children': ['189']}, {'id': '189', 'type': 'block', 'children': ['190']}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'call', 'children': ['192', '195']}; {'id': '192', 'type': 'attribute', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'warning'}, {'id': '195', 'type': 'argument_list', 'children': ['196']}; {'id': '196', 'type': 'call', 'children': ['197', '200']}, {'id': '197', 'type': 'attribute', 'children': ['198', '199']}; {'id': '198', 'type': 'string', 'children': [], 'value': '"The following files are missing and ignored: {}"'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '200', 'type': 'argument_list', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '205']}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'string', 'children': [], 'value': "', '"}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '205', 'type': 'argument_list', 'children': ['206']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'missing_files'}, {'id': '207', 'type': 'expression_statement', 'children': ['208']}; {'id': '208', 'type': 'assignment', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'input_files'}; {'id': '210', 'type': 'binary_operator', 'children': ['211', '215'], 'value': '-'}, {'id': '211', 'type': 'call', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '213', 'type': 'argument_list', 'children': ['214']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '215', 'type': 'call', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '217', 'type': 'argument_list', 'children': ['218']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'missing_files'}, {'id': '219', 'type': 'if_statement', 'children': ['220', '226', '243']}; {'id': '220', 'type': 'comparison_operator', 'children': ['221', '225'], 'value': '=='}, {'id': '221', 'type': 'call', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '223', 'type': 'argument_list', 'children': ['224']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'input_files'}, {'id': '225', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '226', 'type': 'block', 'children': ['227']}, {'id': '227', 'type': 'return_statement', 'children': ['228']}; {'id': '228', 'type': 'call', 'children': ['229', '232']}, {'id': '229', 'type': 'subscript', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'io'}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'extension'}; {'id': '232', 'type': 'argument_list', 'children': ['233', '238', '241']}, {'id': '233', 'type': 'keyword_argument', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'filename'}, {'id': '235', 'type': 'subscript', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '237', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '238', 'type': 'keyword_argument', 'children': ['239', '240']}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '241', 'type': 'dictionary_splat', 'children': ['242']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '243', 'type': 'else_clause', 'children': ['244']}; {'id': '244', 'type': 'block', 'children': ['245']}, {'id': '245', 'type': 'return_statement', 'children': ['246']}; {'id': '246', 'type': 'call', 'children': ['247', '250']}, {'id': '247', 'type': 'subscript', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'io'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'extension'}; {'id': '250', 'type': 'argument_list', 'children': ['251', '254', '257']}, {'id': '251', 'type': 'keyword_argument', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'filenames'}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'filenames'}; {'id': '254', 'type': 'keyword_argument', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '257', 'type': 'dictionary_splat', 'children': ['258']} | def GenericPump(filenames, use_jppy=False, name="GenericPump", **kwargs):
if isinstance(filenames, str):
filenames = [filenames]
try:
iter(filenames)
except TypeError:
log.critical("Don't know how to iterate through filenames.")
raise TypeError("Invalid filenames.")
extensions = set(os.path.splitext(fn)[1] for fn in filenames)
if len(extensions) > 1:
log.critical("Mixed filetypes, please use only files of the same type")
raise IOError("Mixed filetypes.")
extension = list(extensions)[0]
io = {
'.evt': EvtPump,
'.h5': HDF5Pump,
'.root': EventPump if use_jppy else AanetPump,
'.dat': DAQPump,
'.dqd': CLBPump,
}
if extension not in io:
log.critical(
"No pump found for file extension '{0}'".format(extension)
)
raise ValueError("Unknown filetype")
missing_files = [fn for fn in filenames if not os.path.exists(fn)]
if missing_files:
if len(missing_files) == len(filenames):
message = "None of the given files could be found."
log.critical(message)
raise SystemExit(message)
else:
log.warning(
"The following files are missing and ignored: {}".format(
', '.join(missing_files)
)
)
input_files = set(filenames) - set(missing_files)
if len(input_files) == 1:
return io[extension](filename=filenames[0], name=name, **kwargs)
else:
return io[extension](filenames=filenames, name=name, **kwargs) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '10']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_sources'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'plate'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'plate_value'}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '9', 'type': 'None', 'children': []}; {'id': '10', 'type': 'block', 'children': ['11', '20', '84', '121']}, {'id': '11', 'type': 'if_statement', 'children': ['12', '15']}; {'id': '12', 'type': 'comparison_operator', 'children': ['13', '14'], 'value': 'is'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '14', 'type': 'None', 'children': []}, {'id': '15', 'type': 'block', 'children': ['16']}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '19', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '20', 'type': 'if_statement', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '24', 'type': 'block', 'children': ['25']}, {'id': '25', 'type': 'for_statement', 'children': ['26', '29', '35']}; {'id': '26', 'type': 'pattern_list', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'si'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'enumerate'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'attribute', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '35', 'type': 'block', 'children': ['36']}; {'id': '36', 'type': 'if_statement', 'children': ['37', '51', '63', '81']}, {'id': '37', 'type': 'boolean_operator', 'children': ['38', '46'], 'value': 'and'}; {'id': '38', 'type': 'comparison_operator', 'children': ['39', '45'], 'value': '=='}, {'id': '39', 'type': 'call', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '41', 'type': 'argument_list', 'children': ['42']}; {'id': '42', 'type': 'attribute', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '45', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '46', 'type': 'comparison_operator', 'children': ['47', '48'], 'value': 'in'}, {'id': '47', 'type': 'None', 'children': []}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '51', 'type': 'block', 'children': ['52']}; {'id': '52', 'type': 'expression_statement', 'children': ['53']}, {'id': '53', 'type': 'call', 'children': ['54', '57']}; {'id': '54', 'type': 'attribute', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '57', 'type': 'argument_list', 'children': ['58']}; {'id': '58', 'type': 'subscript', 'children': ['59', '62']}, {'id': '59', 'type': 'attribute', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '62', 'type': 'None', 'children': []}, {'id': '63', 'type': 'elif_clause', 'children': ['64', '69']}; {'id': '64', 'type': 'comparison_operator', 'children': ['65', '66'], 'value': 'in'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'plate_value'}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'source'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'subscript', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'plate_value'}, {'id': '81', 'type': 'else_clause', 'children': ['82']}; {'id': '82', 'type': 'block', 'children': ['83']}, {'id': '83', 'type': 'pass_statement', 'children': []}; {'id': '84', 'type': 'if_statement', 'children': ['85', '89']}, {'id': '85', 'type': 'not_operator', 'children': ['86']}; {'id': '86', 'type': 'attribute', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'plate'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'is_root'}, {'id': '89', 'type': 'block', 'children': ['90', '108']}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'parent_plate_value'}, {'id': '93', 'type': 'call', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '95', 'type': 'generator_expression', 'children': ['96', '97', '100']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'pv'}, {'id': '97', 'type': 'for_in_clause', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'pv'}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'plate_value'}; {'id': '100', 'type': 'if_clause', 'children': ['101']}, {'id': '101', 'type': 'comparison_operator', 'children': ['102', '105'], 'value': '!='}; {'id': '102', 'type': 'subscript', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'pv'}; {'id': '104', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '105', 'type': 'attribute', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'plate'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'meta_data_id'}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '111', 'type': 'call', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'get_sources'}, {'id': '115', 'type': 'argument_list', 'children': ['116', '119', '120']}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'plate'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'parent_plate_value'}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '121', 'type': 'return_statement', 'children': ['122']} | def get_sources(self, plate, plate_value, sources=None):
if sources is None:
sources = []
if self.sources:
for si, source in enumerate(self.sources):
if len(source.streams) == 1 and None in source.streams:
sources.append(source.streams[None])
elif plate_value in source.streams:
sources.append(source.streams[plate_value])
else:
pass
if not plate.is_root:
parent_plate_value = tuple(pv for pv in plate_value if pv[0] != plate.meta_data_id)
sources = self.get_sources(plate.parent, parent_plate_value, sources)
return sources |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_splitting_stream'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'input_plate_value'}; {'id': '6', 'type': 'block', 'children': ['7', '15', '35', '59', '81', '91', '252']}, {'id': '7', 'type': 'if_statement', 'children': ['8', '12']}; {'id': '8', 'type': 'not_operator', 'children': ['9']}, {'id': '9', 'type': 'attribute', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '12', 'type': 'block', 'children': ['13']}, {'id': '13', 'type': 'return_statement', 'children': ['14']}; {'id': '14', 'type': 'None', 'children': []}, {'id': '15', 'type': 'if_statement', 'children': ['16', '26']}; {'id': '16', 'type': 'comparison_operator', 'children': ['17', '25'], 'value': '=='}, {'id': '17', 'type': 'call', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '19', 'type': 'argument_list', 'children': ['20']}; {'id': '20', 'type': 'attribute', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'plates'}, {'id': '25', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '26', 'type': 'block', 'children': ['27']}, {'id': '27', 'type': 'return_statement', 'children': ['28']}; {'id': '28', 'type': 'subscript', 'children': ['29', '34']}, {'id': '29', 'type': 'attribute', 'children': ['30', '33']}; {'id': '30', 'type': 'attribute', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '34', 'type': 'None', 'children': []}, {'id': '35', 'type': 'if_statement', 'children': ['36', '46']}; {'id': '36', 'type': 'comparison_operator', 'children': ['37', '45'], 'value': '>'}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '39', 'type': 'argument_list', 'children': ['40']}; {'id': '40', 'type': 'attribute', 'children': ['41', '44']}, {'id': '41', 'type': 'attribute', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'plates'}, {'id': '45', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '46', 'type': 'block', 'children': ['47']}, {'id': '47', 'type': 'raise_statement', 'children': ['48']}; {'id': '48', 'type': 'call', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '50', 'type': 'argument_list', 'children': ['51']}, {'id': '51', 'type': 'call', 'children': ['52', '55']}; {'id': '52', 'type': 'attribute', 'children': ['53', '54']}, {'id': '53', 'type': 'string', 'children': [], 'value': '"Splitting node cannot live on multiple plates for factor {}"'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '55', 'type': 'argument_list', 'children': ['56']}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'factor_id'}, {'id': '59', 'type': 'if_statement', 'children': ['60', '75']}; {'id': '60', 'type': 'boolean_operator', 'children': ['61', '65'], 'value': 'and'}, {'id': '61', 'type': 'not_operator', 'children': ['62']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '65', 'type': 'comparison_operator', 'children': ['66', '74'], 'value': '>'}; {'id': '66', 'type': 'call', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '68', 'type': 'argument_list', 'children': ['69']}, {'id': '69', 'type': 'attribute', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'plates'}; {'id': '74', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'raise_statement', 'children': ['77']}, {'id': '77', 'type': 'call', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '79', 'type': 'argument_list', 'children': ['80']}; {'id': '80', 'type': 'string', 'children': [], 'value': '"Splitting node cannot live on a plate if there is no input plate"'}, {'id': '81', 'type': 'expression_statement', 'children': ['82']}; {'id': '82', 'type': 'assignment', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'splitting_plate'}; {'id': '84', 'type': 'subscript', 'children': ['85', '90']}, {'id': '85', 'type': 'attribute', 'children': ['86', '89']}; {'id': '86', 'type': 'attribute', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'plates'}; {'id': '90', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '91', 'type': 'if_statement', 'children': ['92', '97', '108']}; {'id': '92', 'type': 'comparison_operator', 'children': ['93', '96'], 'value': '=='}, {'id': '93', 'type': 'attribute', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'input_plate'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'splitting_plate'}, {'id': '97', 'type': 'block', 'children': ['98']}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'assignment', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'splitting_stream'}, {'id': '101', 'type': 'subscript', 'children': ['102', '107']}; {'id': '102', 'type': 'attribute', 'children': ['103', '106']}, {'id': '103', 'type': 'attribute', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'input_plate_value'}; {'id': '108', 'type': 'else_clause', 'children': ['109']}, {'id': '109', 'type': 'block', 'children': ['110']}; {'id': '110', 'type': 'if_statement', 'children': ['111', '119', '170', '228']}, {'id': '111', 'type': 'call', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'splitting_plate'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'is_child'}, {'id': '115', 'type': 'argument_list', 'children': ['116']}; {'id': '116', 'type': 'attribute', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '119', 'type': 'block', 'children': ['120', '145', '158']}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'assignment', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'ppv'}, {'id': '123', 'type': 'call', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'filter'}, {'id': '125', 'type': 'argument_list', 'children': ['126', '138']}; {'id': '126', 'type': 'lambda', 'children': ['127', '129']}, {'id': '127', 'type': 'lambda_parameters', 'children': ['128']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '129', 'type': 'call', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'all'}, {'id': '131', 'type': 'generator_expression', 'children': ['132', '135']}; {'id': '132', 'type': 'comparison_operator', 'children': ['133', '134'], 'value': 'in'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'p'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'input_plate_value'}, {'id': '135', 'type': 'for_in_clause', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '138', 'type': 'attribute', 'children': ['139', '144']}, {'id': '139', 'type': 'attribute', 'children': ['140', '143']}; {'id': '140', 'type': 'attribute', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '145', 'type': 'if_statement', 'children': ['146', '152']}; {'id': '146', 'type': 'comparison_operator', 'children': ['147', '151'], 'value': '!='}, {'id': '147', 'type': 'call', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '149', 'type': 'argument_list', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'ppv'}, {'id': '151', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '152', 'type': 'block', 'children': ['153']}, {'id': '153', 'type': 'raise_statement', 'children': ['154']}; {'id': '154', 'type': 'call', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '156', 'type': 'argument_list', 'children': ['157']}, {'id': '157', 'type': 'string', 'children': [], 'value': '"Parent plate value not found"'}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'splitting_stream'}, {'id': '161', 'type': 'subscript', 'children': ['162', '167']}; {'id': '162', 'type': 'attribute', 'children': ['163', '166']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '167', 'type': 'subscript', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'ppv'}, {'id': '169', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '170', 'type': 'elif_clause', 'children': ['171', '179']}, {'id': '171', 'type': 'call', 'children': ['172', '175']}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'splitting_plate'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'is_descendant'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'attribute', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '179', 'type': 'block', 'children': ['180', '205', '218']}; {'id': '180', 'type': 'expression_statement', 'children': ['181']}, {'id': '181', 'type': 'assignment', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'ppv'}, {'id': '183', 'type': 'call', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'filter'}, {'id': '185', 'type': 'argument_list', 'children': ['186', '198']}; {'id': '186', 'type': 'lambda', 'children': ['187', '189']}, {'id': '187', 'type': 'lambda_parameters', 'children': ['188']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'all'}, {'id': '191', 'type': 'generator_expression', 'children': ['192', '195']}; {'id': '192', 'type': 'comparison_operator', 'children': ['193', '194'], 'value': 'in'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'p'}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'input_plate_value'}, {'id': '195', 'type': 'for_in_clause', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '198', 'type': 'attribute', 'children': ['199', '204']}, {'id': '199', 'type': 'attribute', 'children': ['200', '203']}; {'id': '200', 'type': 'attribute', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'input_plate'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'parent'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '205', 'type': 'if_statement', 'children': ['206', '212']}; {'id': '206', 'type': 'comparison_operator', 'children': ['207', '211'], 'value': '!='}, {'id': '207', 'type': 'call', 'children': ['208', '209']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '209', 'type': 'argument_list', 'children': ['210']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'ppv'}, {'id': '211', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '212', 'type': 'block', 'children': ['213']}, {'id': '213', 'type': 'raise_statement', 'children': ['214']}; {'id': '214', 'type': 'call', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '216', 'type': 'argument_list', 'children': ['217']}, {'id': '217', 'type': 'string', 'children': [], 'value': '"Parent plate value not found"'}; {'id': '218', 'type': 'expression_statement', 'children': ['219']}, {'id': '219', 'type': 'assignment', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'splitting_stream'}, {'id': '221', 'type': 'subscript', 'children': ['222', '227']}; {'id': '222', 'type': 'attribute', 'children': ['223', '226']}, {'id': '223', 'type': 'attribute', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'streams'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'ppv'}; {'id': '228', 'type': 'else_clause', 'children': ['229']}, {'id': '229', 'type': 'block', 'children': ['230']}; {'id': '230', 'type': 'raise_statement', 'children': ['231']}, {'id': '231', 'type': 'call', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'IncompatiblePlatesError'}, {'id': '233', 'type': 'argument_list', 'children': ['234']}; {'id': '234', 'type': 'call', 'children': ['235', '238']}, {'id': '235', 'type': 'attribute', 'children': ['236', '237']}; {'id': '236', 'type': 'string', 'children': [], 'value': '"Splitting node plate {} does not match input plate {} for factor {}"'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '238', 'type': 'argument_list', 'children': ['239', '242', '249']}, {'id': '239', 'type': 'attribute', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'input_plate'}; {'id': '242', 'type': 'subscript', 'children': ['243', '248']}, {'id': '243', 'type': 'attribute', 'children': ['244', '247']}; {'id': '244', 'type': 'attribute', 'children': ['245', '246']}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'splitting_node'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'plates'}; {'id': '248', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '249', 'type': 'attribute', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'factor_id'}; {'id': '252', 'type': 'return_statement', 'children': ['253']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'splitting_stream'} | def get_splitting_stream(self, input_plate_value):
if not self.splitting_node:
return None
if len(self.splitting_node.plates) == 0:
return self.splitting_node.streams[None]
if len(self.splitting_node.plates) > 1:
raise ValueError("Splitting node cannot live on multiple plates for factor {}"
.format(self.factor_id))
if not self.input_plate and len(self.splitting_node.plates) > 0:
raise ValueError("Splitting node cannot live on a plate if there is no input plate")
splitting_plate = self.splitting_node.plates[0]
if self.input_plate == splitting_plate:
splitting_stream = self.splitting_node.streams[input_plate_value]
else:
if splitting_plate.is_child(self.input_plate):
ppv = filter(lambda x: all(p in input_plate_value for p in x), self.input_plate.parent.values)
if len(ppv) != 1:
raise ValueError("Parent plate value not found")
splitting_stream = self.splitting_node.streams[ppv[0]]
elif splitting_plate.is_descendant(self.input_plate):
ppv = filter(lambda x: all(p in input_plate_value for p in x), self.input_plate.parent.values)
if len(ppv) != 1:
raise ValueError("Parent plate value not found")
splitting_stream = self.splitting_node.streams[ppv]
else:
raise IncompatiblePlatesError(
"Splitting node plate {} does not match input plate {} for factor {}"
.format(self.input_plate, self.splitting_node.plates[0], self.factor_id))
return splitting_stream |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '16']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'iter_cognates'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11', '14']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'column'}, {'id': '7', 'type': 'string', 'children': [], 'value': "'Segments'"}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '10', 'type': 'string', 'children': [], 'value': "'turchin'"}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'threshold'}, {'id': '13', 'type': 'float', 'children': [], 'value': '0.5'}; {'id': '14', 'type': 'dictionary_splat_pattern', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'kw'}; {'id': '16', 'type': 'block', 'children': ['17', '121']}, {'id': '17', 'type': 'if_statement', 'children': ['18', '21']}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': '=='}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '20', 'type': 'string', 'children': [], 'value': "'turchin'"}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'for_statement', 'children': ['23', '24', '29']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '24', 'type': 'subscript', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'objects'}; {'id': '28', 'type': 'string', 'children': [], 'value': "'FormTable'"}, {'id': '29', 'type': 'block', 'children': ['30', '47', '61', '82', '95']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'assignment', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '33', 'type': 'call', 'children': ['34', '37']}; {'id': '34', 'type': 'attribute', 'children': ['35', '36']}, {'id': '35', 'type': 'string', 'children': [], 'value': "''"}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'call', 'children': ['39', '42']}, {'id': '39', 'type': 'attribute', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'lingpy'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'tokens2class'}; {'id': '42', 'type': 'argument_list', 'children': ['43', '46']}, {'id': '43', 'type': 'subscript', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'column'}; {'id': '46', 'type': 'string', 'children': [], 'value': "'dolgo'"}, {'id': '47', 'type': 'if_statement', 'children': ['48', '54']}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'string', 'children': [], 'value': "'V'"}; {'id': '54', 'type': 'block', 'children': ['55']}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'assignment', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'sounds'}; {'id': '58', 'type': 'binary_operator', 'children': ['59', '60'], 'value': '+'}, {'id': '59', 'type': 'string', 'children': [], 'value': "'H'"}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'sounds'}; {'id': '64', 'type': 'call', 'children': ['65', '68']}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'string', 'children': [], 'value': "'-'"}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '68', 'type': 'argument_list', 'children': ['69']}, {'id': '69', 'type': 'subscript', 'children': ['70', '79']}; {'id': '70', 'type': 'list_comprehension', 'children': ['71', '72', '75']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '72', 'type': 'for_in_clause', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '75', 'type': 'if_clause', 'children': ['76']}; {'id': '76', 'type': 'comparison_operator', 'children': ['77', '78'], 'value': '!='}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '78', 'type': 'string', 'children': [], 'value': "'V'"}, {'id': '79', 'type': 'slice', 'children': ['80', '81']}; {'id': '80', 'type': 'colon', 'children': []}, {'id': '81', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '82', 'type': 'expression_statement', 'children': ['83']}, {'id': '83', 'type': 'assignment', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'cogid'}, {'id': '85', 'type': 'binary_operator', 'children': ['86', '94'], 'value': '+'}; {'id': '86', 'type': 'binary_operator', 'children': ['87', '93'], 'value': '+'}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'slug'}, {'id': '89', 'type': 'argument_list', 'children': ['90']}; {'id': '90', 'type': 'subscript', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '92', 'type': 'string', 'children': [], 'value': "'Parameter_ID'"}, {'id': '93', 'type': 'string', 'children': [], 'value': "'-'"}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '95', 'type': 'if_statement', 'children': ['96', '99']}; {'id': '96', 'type': 'comparison_operator', 'children': ['97', '98'], 'value': 'not'}, {'id': '97', 'type': 'string', 'children': [], 'value': "'0'"}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'sounds'}, {'id': '99', 'type': 'block', 'children': ['100']}; {'id': '100', 'type': 'expression_statement', 'children': ['101']}, {'id': '101', 'type': 'yield', 'children': ['102']}; {'id': '102', 'type': 'call', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '104', 'type': 'argument_list', 'children': ['105', '110', '115', '118']}, {'id': '105', 'type': 'keyword_argument', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'Form_ID'}, {'id': '107', 'type': 'subscript', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '109', 'type': 'string', 'children': [], 'value': "'ID'"}; {'id': '110', 'type': 'keyword_argument', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'Form'}; {'id': '112', 'type': 'subscript', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '114', 'type': 'string', 'children': [], 'value': "'Value'"}, {'id': '115', 'type': 'keyword_argument', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'Cognateset_ID'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'cogid'}; {'id': '118', 'type': 'keyword_argument', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'Cognate_Detection_Method'}; {'id': '120', 'type': 'string', 'children': [], 'value': "'CMM'"}, {'id': '121', 'type': 'if_statement', 'children': ['122', '127']}; {'id': '122', 'type': 'comparison_operator', 'children': ['123', '124'], 'value': 'in'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '124', 'type': 'list', 'children': ['125', '126'], 'value': "['sca', 'lexstat']"}, {'id': '125', 'type': 'string', 'children': [], 'value': "'sca'"}; {'id': '126', 'type': 'string', 'children': [], 'value': "'lexstat'"}, {'id': '127', 'type': 'block', 'children': ['128', '135', '148', '163']}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'assignment', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'lex'}, {'id': '131', 'type': 'call', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': '_cldf2lexstat'}, {'id': '133', 'type': 'argument_list', 'children': ['134']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '135', 'type': 'if_statement', 'children': ['136', '139']}; {'id': '136', 'type': 'comparison_operator', 'children': ['137', '138'], 'value': '=='}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '138', 'type': 'string', 'children': [], 'value': "'lexstat'"}, {'id': '139', 'type': 'block', 'children': ['140']}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'call', 'children': ['142', '145']}; {'id': '142', 'type': 'attribute', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'get_scorer'}, {'id': '145', 'type': 'argument_list', 'children': ['146']}; {'id': '146', 'type': 'dictionary_splat', 'children': ['147']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'kw'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'call', 'children': ['150', '153']}; {'id': '150', 'type': 'attribute', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'cluster'}, {'id': '153', 'type': 'argument_list', 'children': ['154', '157', '160']}; {'id': '154', 'type': 'keyword_argument', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'method'}, {'id': '157', 'type': 'keyword_argument', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'threshold'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'threshold'}; {'id': '160', 'type': 'keyword_argument', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'ref'}; {'id': '162', 'type': 'string', 'children': [], 'value': "'cogid'"}, {'id': '163', 'type': 'for_statement', 'children': ['164', '165', '166']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '166', 'type': 'block', 'children': ['167']}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'yield', 'children': ['169']}, {'id': '169', 'type': 'call', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'Cognate'}, {'id': '171', 'type': 'argument_list', 'children': ['172', '178', '184', '190']}; {'id': '172', 'type': 'keyword_argument', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'Form_ID'}; {'id': '174', 'type': 'subscript', 'children': ['175', '176', '177']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '177', 'type': 'string', 'children': [], 'value': "'lid'"}; {'id': '178', 'type': 'keyword_argument', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'Form'}; {'id': '180', 'type': 'subscript', 'children': ['181', '182', '183']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '183', 'type': 'string', 'children': [], 'value': "'value'"}; {'id': '184', 'type': 'keyword_argument', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'Cognateset_ID'}; {'id': '186', 'type': 'subscript', 'children': ['187', '188', '189']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'lex'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '189', 'type': 'string', 'children': [], 'value': "'cogid'"}; {'id': '190', 'type': 'keyword_argument', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'Cognate_Detection_Method'}; {'id': '192', 'type': 'binary_operator', 'children': ['193', '194'], 'value': '+'}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'method'}; {'id': '194', 'type': 'call', 'children': ['195', '198']}, {'id': '195', 'type': 'attribute', 'children': ['196', '197']}; {'id': '196', 'type': 'string', 'children': [], 'value': "'-t{0:.2f}'"}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '198', 'type': 'argument_list', 'children': ['199']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'threshold'} | def iter_cognates(dataset, column='Segments', method='turchin', threshold=0.5, **kw):
if method == 'turchin':
for row in dataset.objects['FormTable']:
sounds = ''.join(lingpy.tokens2class(row[column], 'dolgo'))
if sounds.startswith('V'):
sounds = 'H' + sounds
sounds = '-'.join([s for s in sounds if s != 'V'][:2])
cogid = slug(row['Parameter_ID']) + '-' + sounds
if '0' not in sounds:
yield dict(
Form_ID=row['ID'],
Form=row['Value'],
Cognateset_ID=cogid,
Cognate_Detection_Method='CMM')
if method in ['sca', 'lexstat']:
lex = _cldf2lexstat(dataset)
if method == 'lexstat':
lex.get_scorer(**kw)
lex.cluster(method=method, threshold=threshold, ref='cogid')
for k in lex:
yield Cognate(
Form_ID=lex[k, 'lid'],
Form=lex[k, 'value'],
Cognateset_ID=lex[k, 'cogid'],
Cognate_Detection_Method=method + '-t{0:.2f}'.format(threshold)) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_tool_class'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '6', 'type': 'block', 'children': ['7', '39', '43', '105', '115', '123', '133']}, {'id': '7', 'type': 'if_statement', 'children': ['8', '13', '21', '32']}; {'id': '8', 'type': 'call', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '10', 'type': 'argument_list', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'string_types'}, {'id': '13', 'type': 'block', 'children': ['14']}; {'id': '14', 'type': 'expression_statement', 'children': ['15']}, {'id': '15', 'type': 'assignment', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'tool_id'}, {'id': '17', 'type': 'call', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'StreamId'}, {'id': '19', 'type': 'argument_list', 'children': ['20']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '21', 'type': 'elif_clause', 'children': ['22', '27']}; {'id': '22', 'type': 'call', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '24', 'type': 'argument_list', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'StreamId'}, {'id': '27', 'type': 'block', 'children': ['28']}; {'id': '28', 'type': 'expression_statement', 'children': ['29']}, {'id': '29', 'type': 'assignment', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'tool_id'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '32', 'type': 'else_clause', 'children': ['33']}, {'id': '33', 'type': 'block', 'children': ['34']}; {'id': '34', 'type': 'raise_statement', 'children': ['35']}, {'id': '35', 'type': 'call', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '39', 'type': 'expression_statement', 'children': ['40']}; {'id': '40', 'type': 'assignment', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}; {'id': '42', 'type': 'None', 'children': []}, {'id': '43', 'type': 'if_statement', 'children': ['44', '49', '69']}; {'id': '44', 'type': 'comparison_operator', 'children': ['45', '46'], 'value': 'in'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'tool_id'}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'tools'}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}, {'id': '53', 'type': 'call', 'children': ['54', '61']}; {'id': '54', 'type': 'attribute', 'children': ['55', '60']}, {'id': '55', 'type': 'subscript', 'children': ['56', '59']}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'tools'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'tool_id'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'window'}, {'id': '61', 'type': 'argument_list', 'children': ['62']}; {'id': '62', 'type': 'tuple', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'MIN_DATE'}; {'id': '64', 'type': 'attribute', 'children': ['65', '68']}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'tools'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'up_to_timestamp'}, {'id': '69', 'type': 'else_clause', 'children': ['70']}; {'id': '70', 'type': 'block', 'children': ['71']}, {'id': '71', 'type': 'for_statement', 'children': ['72', '73', '76']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'tool_channel'}, {'id': '73', 'type': 'attribute', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'tool_channels'}; {'id': '76', 'type': 'block', 'children': ['77', '85']}, {'id': '77', 'type': 'if_statement', 'children': ['78', '83']}; {'id': '78', 'type': 'comparison_operator', 'children': ['79', '80'], 'value': '=='}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'tool_channel'}; {'id': '80', 'type': 'attribute', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'tools'}, {'id': '83', 'type': 'block', 'children': ['84']}; {'id': '84', 'type': 'continue_statement', 'children': []}, {'id': '85', 'type': 'if_statement', 'children': ['86', '89']}; {'id': '86', 'type': 'comparison_operator', 'children': ['87', '88'], 'value': 'in'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'tool_id'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'tool_channel'}, {'id': '89', 'type': 'block', 'children': ['90']}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}, {'id': '93', 'type': 'call', 'children': ['94', '99']}; {'id': '94', 'type': 'attribute', 'children': ['95', '98']}, {'id': '95', 'type': 'subscript', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'tool_channel'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'tool_id'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'window'}, {'id': '99', 'type': 'argument_list', 'children': ['100']}; {'id': '100', 'type': 'tuple', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'MIN_DATE'}; {'id': '102', 'type': 'attribute', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'tool_channel'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'up_to_timestamp'}, {'id': '105', 'type': 'if_statement', 'children': ['106', '109']}; {'id': '106', 'type': 'comparison_operator', 'children': ['107', '108'], 'value': 'is'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}; {'id': '108', 'type': 'None', 'children': []}, {'id': '109', 'type': 'block', 'children': ['110']}; {'id': '110', 'type': 'raise_statement', 'children': ['111']}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'ToolNotFoundError'}, {'id': '113', 'type': 'argument_list', 'children': ['114']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '115', 'type': 'expression_statement', 'children': ['116']}; {'id': '116', 'type': 'assignment', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'last'}; {'id': '118', 'type': 'call', 'children': ['119', '122']}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'last'}; {'id': '122', 'type': 'argument_list', 'children': []}, {'id': '123', 'type': 'if_statement', 'children': ['124', '127']}; {'id': '124', 'type': 'comparison_operator', 'children': ['125', '126'], 'value': 'is'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'last'}; {'id': '126', 'type': 'None', 'children': []}, {'id': '127', 'type': 'block', 'children': ['128']}; {'id': '128', 'type': 'raise_statement', 'children': ['129']}, {'id': '129', 'type': 'call', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'ToolNotFoundError'}, {'id': '131', 'type': 'argument_list', 'children': ['132']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'tool'}, {'id': '133', 'type': 'return_statement', 'children': ['134']}; {'id': '134', 'type': 'attribute', 'children': ['135', '140']}, {'id': '135', 'type': 'call', 'children': ['136', '139']}; {'id': '136', 'type': 'attribute', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'tool_stream_view'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'last'}, {'id': '139', 'type': 'argument_list', 'children': []} | def get_tool_class(self, tool):
if isinstance(tool, string_types):
tool_id = StreamId(tool)
elif isinstance(tool, StreamId):
tool_id = tool
else:
raise TypeError(tool)
tool_stream_view = None
if tool_id in self.tools:
tool_stream_view = self.tools[tool_id].window((MIN_DATE, self.tools.up_to_timestamp))
else:
for tool_channel in self.tool_channels:
if tool_channel == self.tools:
continue
if tool_id in tool_channel:
tool_stream_view = tool_channel[tool_id].window((MIN_DATE, tool_channel.up_to_timestamp))
if tool_stream_view is None:
raise ToolNotFoundError(tool)
last = tool_stream_view.last()
if last is None:
raise ToolNotFoundError(tool)
return tool_stream_view.last().value |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'pmt_angles'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'block', 'children': ['6', '45']}; {'id': '6', 'type': 'if_statement', 'children': ['7', '12']}, {'id': '7', 'type': 'comparison_operator', 'children': ['8', '11'], 'value': '=='}; {'id': '8', 'type': 'attribute', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '10', 'type': 'identifier', 'children': [], 'value': '_pmt_angles'}, {'id': '11', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '12', 'type': 'block', 'children': ['13', '33']}, {'id': '13', 'type': 'expression_statement', 'children': ['14']}; {'id': '14', 'type': 'assignment', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'mask'}; {'id': '16', 'type': 'binary_operator', 'children': ['17', '25'], 'value': '&'}, {'id': '17', 'type': 'parenthesized_expression', 'children': ['18']}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '24'], 'value': '=='}, {'id': '19', 'type': 'attribute', 'children': ['20', '23']}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'pmts'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'du'}; {'id': '24', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '25', 'type': 'parenthesized_expression', 'children': ['26']}; {'id': '26', 'type': 'comparison_operator', 'children': ['27', '32'], 'value': '=='}, {'id': '27', 'type': 'attribute', 'children': ['28', '31']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'pmts'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'floor'}; {'id': '32', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': '_pmt_angles'}; {'id': '38', 'type': 'subscript', 'children': ['39', '44']}, {'id': '39', 'type': 'attribute', 'children': ['40', '43']}; {'id': '40', 'type': 'attribute', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'pmts'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'dir'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'mask'}, {'id': '45', 'type': 'return_statement', 'children': ['46']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'self'} | def pmt_angles(self):
if self._pmt_angles == []:
mask = (self.pmts.du == 1) & (self.pmts.floor == 1)
self._pmt_angles = self.pmts.dir[mask]
return self._pmt_angles |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '7']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_get_point'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'point'}, {'id': '7', 'type': 'block', 'children': ['8', '22']}; {'id': '8', 'type': 'expression_statement', 'children': ['9']}, {'id': '9', 'type': 'assignment', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'cur_points_z'}, {'id': '11', 'type': 'list_comprehension', 'children': ['12', '17']}; {'id': '12', 'type': 'attribute', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'location'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'z'}, {'id': '17', 'type': 'for_in_clause', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '19', 'type': 'attribute', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'elements'}; {'id': '22', 'type': 'try_statement', 'children': ['23', '41']}, {'id': '23', 'type': 'block', 'children': ['24', '35']}; {'id': '24', 'type': 'expression_statement', 'children': ['25']}, {'id': '25', 'type': 'assignment', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'cur_idx'}, {'id': '27', 'type': 'call', 'children': ['28', '31']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'cur_points_z'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'attribute', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'point'}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'z'}, {'id': '35', 'type': 'return_statement', 'children': ['36']}; {'id': '36', 'type': 'subscript', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'elements'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'cur_idx'}, {'id': '41', 'type': 'except_clause', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '43', 'type': 'block', 'children': ['44', '54', '60', '69', '77', '87']}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'new_idx'}, {'id': '47', 'type': 'call', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'bisect_left'}, {'id': '49', 'type': 'argument_list', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'cur_points_z'}, {'id': '51', 'type': 'attribute', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'point'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'z'}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'new_point'}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'Point'}, {'id': '59', 'type': 'argument_list', 'children': []}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '65']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'new_point'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'location'}, {'id': '65', 'type': 'call', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'sPoint'}, {'id': '67', 'type': 'argument_list', 'children': ['68']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'point'}, {'id': '69', 'type': 'expression_statement', 'children': ['70']}; {'id': '70', 'type': 'assignment', 'children': ['71', '74']}, {'id': '71', 'type': 'attribute', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'new_point'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'time'}; {'id': '74', 'type': 'attribute', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'time'}, {'id': '77', 'type': 'expression_statement', 'children': ['78']}; {'id': '78', 'type': 'call', 'children': ['79', '84']}, {'id': '79', 'type': 'attribute', 'children': ['80', '83']}; {'id': '80', 'type': 'attribute', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'elements'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'insert'}; {'id': '84', 'type': 'argument_list', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'new_idx'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'new_point'}, {'id': '87', 'type': 'return_statement', 'children': ['88']} | def _get_point(self, profile, point):
cur_points_z = [p.location.z for p in profile.elements]
try:
cur_idx = cur_points_z.index(point.z)
return profile.elements[cur_idx]
except ValueError:
new_idx = bisect_left(cur_points_z, point.z)
new_point = Point()
new_point.location = sPoint(point)
new_point.time = profile.time
profile.elements.insert(new_idx, new_point)
return new_point |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'metadata_sorter'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '6', 'type': 'block', 'children': ['7', '14']}, {'id': '7', 'type': 'if_statement', 'children': ['8', '11']}; {'id': '8', 'type': 'comparison_operator', 'children': ['9', '10'], 'value': '=='}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '11', 'type': 'block', 'children': ['12']}; {'id': '12', 'type': 'return_statement', 'children': ['13']}, {'id': '13', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '14', 'type': 'if_statement', 'children': ['15', '22', '41', '49', '56']}, {'id': '15', 'type': 'boolean_operator', 'children': ['16', '19'], 'value': 'and'}; {'id': '16', 'type': 'comparison_operator', 'children': ['17', '18'], 'value': 'in'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}, {'id': '19', 'type': 'comparison_operator', 'children': ['20', '21'], 'value': 'in'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}; {'id': '22', 'type': 'block', 'children': ['23']}, {'id': '23', 'type': 'return_statement', 'children': ['24']}; {'id': '24', 'type': 'conditional_expression', 'children': ['25', '27', '40'], 'value': 'if'}, {'id': '25', 'type': 'unary_operator', 'children': ['26'], 'value': '-'}; {'id': '26', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '27', 'type': 'comparison_operator', 'children': ['28', '34'], 'value': '<'}; {'id': '28', 'type': 'call', 'children': ['29', '32']}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'index'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '34', 'type': 'call', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'index'}; {'id': '38', 'type': 'argument_list', 'children': ['39']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '40', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '41', 'type': 'elif_clause', 'children': ['42', '45']}; {'id': '42', 'type': 'comparison_operator', 'children': ['43', '44'], 'value': 'in'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}, {'id': '45', 'type': 'block', 'children': ['46']}; {'id': '46', 'type': 'return_statement', 'children': ['47']}, {'id': '47', 'type': 'unary_operator', 'children': ['48'], 'value': '-'}; {'id': '48', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '49', 'type': 'elif_clause', 'children': ['50', '53']}; {'id': '50', 'type': 'comparison_operator', 'children': ['51', '52'], 'value': 'in'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'METADATA_SORTER_FIRST'}, {'id': '53', 'type': 'block', 'children': ['54']}; {'id': '54', 'type': 'return_statement', 'children': ['55']}, {'id': '55', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '56', 'type': 'else_clause', 'children': ['57']}, {'id': '57', 'type': 'block', 'children': ['58']}; {'id': '58', 'type': 'if_statement', 'children': ['59', '72', '87', '97', '108']}, {'id': '59', 'type': 'boolean_operator', 'children': ['60', '66'], 'value': 'and'}; {'id': '60', 'type': 'call', 'children': ['61', '64']}, {'id': '61', 'type': 'attribute', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '64', 'type': 'argument_list', 'children': ['65']}, {'id': '65', 'type': 'string', 'children': [], 'value': "'_'"}; {'id': '66', 'type': 'call', 'children': ['67', '70']}, {'id': '67', 'type': 'attribute', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '70', 'type': 'argument_list', 'children': ['71']}, {'id': '71', 'type': 'string', 'children': [], 'value': "'_'"}; {'id': '72', 'type': 'block', 'children': ['73']}, {'id': '73', 'type': 'return_statement', 'children': ['74']}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'cmp'}; {'id': '76', 'type': 'argument_list', 'children': ['77', '82']}, {'id': '77', 'type': 'subscript', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '79', 'type': 'slice', 'children': ['80', '81']}; {'id': '80', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '81', 'type': 'colon', 'children': []}; {'id': '82', 'type': 'subscript', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '84', 'type': 'slice', 'children': ['85', '86']}, {'id': '85', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '86', 'type': 'colon', 'children': []}, {'id': '87', 'type': 'elif_clause', 'children': ['88', '94']}; {'id': '88', 'type': 'call', 'children': ['89', '92']}, {'id': '89', 'type': 'attribute', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'string', 'children': [], 'value': "'_'"}; {'id': '94', 'type': 'block', 'children': ['95']}, {'id': '95', 'type': 'return_statement', 'children': ['96']}; {'id': '96', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '97', 'type': 'elif_clause', 'children': ['98', '104']}; {'id': '98', 'type': 'call', 'children': ['99', '102']}, {'id': '99', 'type': 'attribute', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'string', 'children': [], 'value': "'_'"}; {'id': '104', 'type': 'block', 'children': ['105']}, {'id': '105', 'type': 'return_statement', 'children': ['106']}; {'id': '106', 'type': 'unary_operator', 'children': ['107'], 'value': '-'}, {'id': '107', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '108', 'type': 'else_clause', 'children': ['109']}, {'id': '109', 'type': 'block', 'children': ['110']}; {'id': '110', 'type': 'return_statement', 'children': ['111']}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'cmp'}, {'id': '113', 'type': 'argument_list', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'y'} | def metadata_sorter(x, y):
if x == y:
return 0
if x in METADATA_SORTER_FIRST and y in METADATA_SORTER_FIRST:
return -1 if METADATA_SORTER_FIRST.index(x) < METADATA_SORTER_FIRST.index(y) else 1
elif x in METADATA_SORTER_FIRST:
return -1
elif y in METADATA_SORTER_FIRST:
return 1
else:
if x.startswith('_') and y.startswith('_'):
return cmp(x[1:], y[1:])
elif x.startswith('_'):
return 1
elif y.startswith('_'):
return -1
else:
return cmp(x, y) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '7']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'computeStrongestPaths'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '7', 'type': 'block', 'children': ['8', '18', '25', '31', '43', '105', '181']}; {'id': '8', 'type': 'expression_statement', 'children': ['9']}, {'id': '9', 'type': 'assignment', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'cands'}, {'id': '11', 'type': 'call', 'children': ['12', '17']}; {'id': '12', 'type': 'attribute', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'candMap'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '17', 'type': 'argument_list', 'children': []}; {'id': '18', 'type': 'expression_statement', 'children': ['19']}, {'id': '19', 'type': 'assignment', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '21', 'type': 'call', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'cands'}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '30', 'type': 'argument_list', 'children': []}, {'id': '31', 'type': 'for_statement', 'children': ['32', '33', '34']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '34', 'type': 'block', 'children': ['35']}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '40']}, {'id': '37', 'type': 'subscript', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'cand'}; {'id': '40', 'type': 'call', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '42', 'type': 'argument_list', 'children': []}, {'id': '43', 'type': 'for_statement', 'children': ['44', '45', '52']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '47', 'type': 'argument_list', 'children': ['48', '49']}; {'id': '48', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '49', 'type': 'binary_operator', 'children': ['50', '51'], 'value': '+'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '51', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '52', 'type': 'block', 'children': ['53']}, {'id': '53', 'type': 'for_statement', 'children': ['54', '55', '62']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '55', 'type': 'call', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '57', 'type': 'argument_list', 'children': ['58', '59']}; {'id': '58', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '59', 'type': 'binary_operator', 'children': ['60', '61'], 'value': '+'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '61', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '62', 'type': 'block', 'children': ['63', '70']}, {'id': '63', 'type': 'if_statement', 'children': ['64', '68']}; {'id': '64', 'type': 'parenthesized_expression', 'children': ['65']}, {'id': '65', 'type': 'comparison_operator', 'children': ['66', '67'], 'value': '=='}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '68', 'type': 'block', 'children': ['69']}, {'id': '69', 'type': 'continue_statement', 'children': []}; {'id': '70', 'type': 'if_statement', 'children': ['71', '82', '95']}, {'id': '71', 'type': 'comparison_operator', 'children': ['72', '77'], 'value': '>'}; {'id': '72', 'type': 'subscript', 'children': ['73', '76']}, {'id': '73', 'type': 'subscript', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '77', 'type': 'subscript', 'children': ['78', '81']}; {'id': '78', 'type': 'subscript', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '82', 'type': 'block', 'children': ['83']}, {'id': '83', 'type': 'expression_statement', 'children': ['84']}; {'id': '84', 'type': 'assignment', 'children': ['85', '90']}, {'id': '85', 'type': 'subscript', 'children': ['86', '89']}; {'id': '86', 'type': 'subscript', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '90', 'type': 'subscript', 'children': ['91', '94']}, {'id': '91', 'type': 'subscript', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '95', 'type': 'else_clause', 'children': ['96']}; {'id': '96', 'type': 'block', 'children': ['97']}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '104']}, {'id': '99', 'type': 'subscript', 'children': ['100', '103']}; {'id': '100', 'type': 'subscript', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '104', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '105', 'type': 'for_statement', 'children': ['106', '107', '114']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '107', 'type': 'call', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '109', 'type': 'argument_list', 'children': ['110', '111']}; {'id': '110', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '111', 'type': 'binary_operator', 'children': ['112', '113'], 'value': '+'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '113', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '114', 'type': 'block', 'children': ['115']}, {'id': '115', 'type': 'for_statement', 'children': ['116', '117', '124']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '117', 'type': 'call', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '119', 'type': 'argument_list', 'children': ['120', '121']}; {'id': '120', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '121', 'type': 'binary_operator', 'children': ['122', '123'], 'value': '+'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '123', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '124', 'type': 'block', 'children': ['125', '132']}, {'id': '125', 'type': 'if_statement', 'children': ['126', '130']}; {'id': '126', 'type': 'parenthesized_expression', 'children': ['127']}, {'id': '127', 'type': 'comparison_operator', 'children': ['128', '129'], 'value': '=='}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '130', 'type': 'block', 'children': ['131']}, {'id': '131', 'type': 'continue_statement', 'children': []}; {'id': '132', 'type': 'for_statement', 'children': ['133', '134', '141']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '134', 'type': 'call', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '136', 'type': 'argument_list', 'children': ['137', '138']}, {'id': '137', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '138', 'type': 'binary_operator', 'children': ['139', '140'], 'value': '+'}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'numCands'}; {'id': '140', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '141', 'type': 'block', 'children': ['142', '153']}; {'id': '142', 'type': 'if_statement', 'children': ['143', '151']}, {'id': '143', 'type': 'parenthesized_expression', 'children': ['144']}; {'id': '144', 'type': 'boolean_operator', 'children': ['145', '148'], 'value': 'or'}, {'id': '145', 'type': 'comparison_operator', 'children': ['146', '147'], 'value': '=='}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '148', 'type': 'comparison_operator', 'children': ['149', '150'], 'value': '=='}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '151', 'type': 'block', 'children': ['152']}; {'id': '152', 'type': 'continue_statement', 'children': []}, {'id': '153', 'type': 'expression_statement', 'children': ['154']}; {'id': '154', 'type': 'assignment', 'children': ['155', '160']}, {'id': '155', 'type': 'subscript', 'children': ['156', '159']}; {'id': '156', 'type': 'subscript', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '160', 'type': 'call', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'max'}; {'id': '162', 'type': 'argument_list', 'children': ['163', '168']}, {'id': '163', 'type': 'subscript', 'children': ['164', '167']}; {'id': '164', 'type': 'subscript', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '168', 'type': 'call', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'min'}; {'id': '170', 'type': 'argument_list', 'children': ['171', '176']}, {'id': '171', 'type': 'subscript', 'children': ['172', '175']}; {'id': '172', 'type': 'subscript', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '176', 'type': 'subscript', 'children': ['177', '180']}, {'id': '177', 'type': 'subscript', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'strongestPaths'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '181', 'type': 'return_statement', 'children': ['182']} | def computeStrongestPaths(self, profile, pairwisePreferences):
cands = profile.candMap.keys()
numCands = len(cands)
strongestPaths = dict()
for cand in cands:
strongestPaths[cand] = dict()
for i in range(1, numCands + 1):
for j in range(1, numCands + 1):
if (i == j):
continue
if pairwisePreferences[i][j] > pairwisePreferences[j][i]:
strongestPaths[i][j] = pairwisePreferences[i][j]
else:
strongestPaths[i][j] = 0
for i in range(1, numCands + 1):
for j in range(1, numCands + 1):
if (i == j):
continue
for k in range(1, numCands + 1):
if (i == k or j == k):
continue
strongestPaths[j][k] = max(strongestPaths[j][k], min(strongestPaths[j][i], strongestPaths[i][k]))
return strongestPaths |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'computePairwisePreferences'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'block', 'children': ['7', '17', '23', '35', '56', '183']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '10', 'type': 'call', 'children': ['11', '16']}, {'id': '11', 'type': 'attribute', 'children': ['12', '15']}; {'id': '12', 'type': 'attribute', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'candMap'}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '16', 'type': 'argument_list', 'children': []}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}; {'id': '20', 'type': 'call', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '22', 'type': 'argument_list', 'children': []}, {'id': '23', 'type': 'for_statement', 'children': ['24', '25', '26']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '26', 'type': 'block', 'children': ['27']}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '32']}, {'id': '29', 'type': 'subscript', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'cand'}; {'id': '32', 'type': 'call', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '34', 'type': 'argument_list', 'children': []}, {'id': '35', 'type': 'for_statement', 'children': ['36', '37', '38']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '38', 'type': 'block', 'children': ['39']}, {'id': '39', 'type': 'for_statement', 'children': ['40', '41', '42']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '42', 'type': 'block', 'children': ['43']}, {'id': '43', 'type': 'if_statement', 'children': ['44', '47']}; {'id': '44', 'type': 'comparison_operator', 'children': ['45', '46'], 'value': '!='}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '55']}; {'id': '50', 'type': 'subscript', 'children': ['51', '54']}, {'id': '51', 'type': 'subscript', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '55', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '56', 'type': 'for_statement', 'children': ['57', '58', '61']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'preference'}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'preferences'}, {'id': '61', 'type': 'block', 'children': ['62', '68']}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '65', 'type': 'attribute', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'preference'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '68', 'type': 'for_statement', 'children': ['69', '72', '79']}, {'id': '69', 'type': 'pattern_list', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '72', 'type': 'call', 'children': ['73', '76']}, {'id': '73', 'type': 'attribute', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'itertools'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'combinations'}; {'id': '76', 'type': 'argument_list', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'cands'}; {'id': '78', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'if_statement', 'children': ['81', '88', '110', '140', '161']}, {'id': '81', 'type': 'comparison_operator', 'children': ['82', '83'], 'value': 'not'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '83', 'type': 'call', 'children': ['84', '87']}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '87', 'type': 'argument_list', 'children': []}; {'id': '88', 'type': 'block', 'children': ['89']}, {'id': '89', 'type': 'if_statement', 'children': ['90', '97']}; {'id': '90', 'type': 'comparison_operator', 'children': ['91', '92'], 'value': 'in'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '92', 'type': 'call', 'children': ['93', '96']}, {'id': '93', 'type': 'attribute', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '96', 'type': 'argument_list', 'children': []}, {'id': '97', 'type': 'block', 'children': ['98']}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'augmented_assignment', 'children': ['100', '105'], 'value': '+='}; {'id': '100', 'type': 'subscript', 'children': ['101', '104']}, {'id': '101', 'type': 'subscript', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '105', 'type': 'binary_operator', 'children': ['106', '107'], 'value': '*'}; {'id': '106', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '107', 'type': 'attribute', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'preference'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'count'}; {'id': '110', 'type': 'elif_clause', 'children': ['111', '118']}, {'id': '111', 'type': 'comparison_operator', 'children': ['112', '113'], 'value': 'not'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '113', 'type': 'call', 'children': ['114', '117']}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '117', 'type': 'argument_list', 'children': []}; {'id': '118', 'type': 'block', 'children': ['119']}, {'id': '119', 'type': 'if_statement', 'children': ['120', '127']}; {'id': '120', 'type': 'comparison_operator', 'children': ['121', '122'], 'value': 'in'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '122', 'type': 'call', 'children': ['123', '126']}, {'id': '123', 'type': 'attribute', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '126', 'type': 'argument_list', 'children': []}, {'id': '127', 'type': 'block', 'children': ['128']}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'augmented_assignment', 'children': ['130', '135'], 'value': '+='}; {'id': '130', 'type': 'subscript', 'children': ['131', '134']}, {'id': '131', 'type': 'subscript', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '135', 'type': 'binary_operator', 'children': ['136', '137'], 'value': '*'}; {'id': '136', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '137', 'type': 'attribute', 'children': ['138', '139']}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'preference'}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'count'}; {'id': '140', 'type': 'elif_clause', 'children': ['141', '148']}, {'id': '141', 'type': 'comparison_operator', 'children': ['142', '147'], 'value': '=='}; {'id': '142', 'type': 'subscript', 'children': ['143', '146']}, {'id': '143', 'type': 'subscript', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '147', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '148', 'type': 'block', 'children': ['149']}, {'id': '149', 'type': 'expression_statement', 'children': ['150']}; {'id': '150', 'type': 'augmented_assignment', 'children': ['151', '156'], 'value': '+='}, {'id': '151', 'type': 'subscript', 'children': ['152', '155']}; {'id': '152', 'type': 'subscript', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '156', 'type': 'binary_operator', 'children': ['157', '158'], 'value': '*'}, {'id': '157', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '158', 'type': 'attribute', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'preference'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'count'}, {'id': '161', 'type': 'elif_clause', 'children': ['162', '170']}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '168'], 'value': '=='}, {'id': '163', 'type': 'subscript', 'children': ['164', '167']}; {'id': '164', 'type': 'subscript', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '168', 'type': 'unary_operator', 'children': ['169'], 'value': '-'}, {'id': '169', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '170', 'type': 'block', 'children': ['171']}, {'id': '171', 'type': 'expression_statement', 'children': ['172']}; {'id': '172', 'type': 'augmented_assignment', 'children': ['173', '178'], 'value': '+='}, {'id': '173', 'type': 'subscript', 'children': ['174', '177']}; {'id': '174', 'type': 'subscript', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'pairwisePreferences'}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '178', 'type': 'binary_operator', 'children': ['179', '180'], 'value': '*'}, {'id': '179', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '180', 'type': 'attribute', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'preference'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'count'}, {'id': '183', 'type': 'return_statement', 'children': ['184']} | def computePairwisePreferences(self, profile):
cands = profile.candMap.keys()
pairwisePreferences = dict()
for cand in cands:
pairwisePreferences[cand] = dict()
for cand1 in cands:
for cand2 in cands:
if cand1 != cand2:
pairwisePreferences[cand1][cand2] = 0
for preference in profile.preferences:
wmgMap = preference.wmgMap
for cand1, cand2 in itertools.combinations(cands, 2):
if cand1 not in wmgMap.keys():
if cand2 in wmgMap.keys():
pairwisePreferences[cand2][cand1] += 1 * preference.count
elif cand2 not in wmgMap.keys():
if cand1 in wmgMap.keys():
pairwisePreferences[cand1][cand2] += 1 * preference.count
elif wmgMap[cand1][cand2] == 1:
pairwisePreferences[cand1][cand2] += 1 * preference.count
elif wmgMap[cand1][cand2] == -1:
pairwisePreferences[cand2][cand1] += 1 * preference.count
return pairwisePreferences |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'STVsocwinners'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'block', 'children': ['7', '15', '23', '29', '64', '78', '85', '91', '97', '106', '110', '117', '264']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'getOrderVectors'}; {'id': '14', 'type': 'argument_list', 'children': []}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '18', 'type': 'call', 'children': ['19', '22']}, {'id': '19', 'type': 'attribute', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'getPreferenceCounts'}; {'id': '22', 'type': 'argument_list', 'children': []}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '26', 'type': 'attribute', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '29', 'type': 'if_statement', 'children': ['30', '38', '49']}; {'id': '30', 'type': 'comparison_operator', 'children': ['31', '37'], 'value': '=='}, {'id': '31', 'type': 'call', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'min'}, {'id': '33', 'type': 'argument_list', 'children': ['34']}; {'id': '34', 'type': 'subscript', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '36', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '37', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '38', 'type': 'block', 'children': ['39']}, {'id': '39', 'type': 'expression_statement', 'children': ['40']}; {'id': '40', 'type': 'assignment', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '47', 'type': 'argument_list', 'children': ['48']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '49', 'type': 'else_clause', 'children': ['50']}; {'id': '50', 'type': 'block', 'children': ['51']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '54', 'type': 'call', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '56', 'type': 'argument_list', 'children': ['57']}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '59', 'type': 'argument_list', 'children': ['60', '61']}; {'id': '60', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '61', 'type': 'binary_operator', 'children': ['62', '63'], 'value': '+'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '63', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '69']}; {'id': '66', 'type': 'pattern_list', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'startstate'}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'preprocessing'}, {'id': '73', 'type': 'argument_list', 'children': ['74', '75', '76', '77']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'ordering'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'm_star'}, {'id': '81', 'type': 'call', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '83', 'type': 'argument_list', 'children': ['84']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'startstate'}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'assignment', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '88', 'type': 'call', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '90', 'type': 'argument_list', 'children': []}, {'id': '91', 'type': 'expression_statement', 'children': ['92']}; {'id': '92', 'type': 'assignment', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}; {'id': '94', 'type': 'call', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '96', 'type': 'argument_list', 'children': []}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'root'}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'Node'}; {'id': '102', 'type': 'argument_list', 'children': ['103']}, {'id': '103', 'type': 'keyword_argument', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'stackNode'}, {'id': '109', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'call', 'children': ['112', '115']}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'stackNode'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '115', 'type': 'argument_list', 'children': ['116']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'root'}, {'id': '117', 'type': 'while_statement', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'stackNode'}, {'id': '119', 'type': 'block', 'children': ['120', '128', '138', '168', '174', '186', '197']}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'assignment', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '123', 'type': 'call', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'stackNode'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'pop'}, {'id': '127', 'type': 'argument_list', 'children': []}; {'id': '128', 'type': 'expression_statement', 'children': ['129']}, {'id': '129', 'type': 'assignment', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '131', 'type': 'call', 'children': ['132', '137']}; {'id': '132', 'type': 'attribute', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'node'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'copy'}, {'id': '137', 'type': 'argument_list', 'children': []}; {'id': '138', 'type': 'if_statement', 'children': ['139', '154']}, {'id': '139', 'type': 'boolean_operator', 'children': ['140', '146'], 'value': 'and'}; {'id': '140', 'type': 'comparison_operator', 'children': ['141', '145'], 'value': '=='}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '145', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '146', 'type': 'comparison_operator', 'children': ['147', '153'], 'value': 'not'}, {'id': '147', 'type': 'subscript', 'children': ['148', '152']}; {'id': '148', 'type': 'call', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '150', 'type': 'argument_list', 'children': ['151']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '152', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '154', 'type': 'block', 'children': ['155', '167']}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'call', 'children': ['157', '160']}, {'id': '157', 'type': 'attribute', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'known_winners'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'subscript', 'children': ['162', '166']}; {'id': '162', 'type': 'call', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '164', 'type': 'argument_list', 'children': ['165']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '166', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '167', 'type': 'continue_statement', 'children': []}; {'id': '168', 'type': 'if_statement', 'children': ['169', '172']}, {'id': '169', 'type': 'comparison_operator', 'children': ['170', '171'], 'value': '<='}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '172', 'type': 'block', 'children': ['173']}, {'id': '173', 'type': 'continue_statement', 'children': []}; {'id': '174', 'type': 'expression_statement', 'children': ['175']}, {'id': '175', 'type': 'assignment', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'plural_score'}, {'id': '177', 'type': 'call', 'children': ['178', '181']}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'get_plurality_scores3'}, {'id': '181', 'type': 'argument_list', 'children': ['182', '183', '184', '185']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'm_star'}; {'id': '186', 'type': 'expression_statement', 'children': ['187']}, {'id': '187', 'type': 'assignment', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'minscore'}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'min'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'call', 'children': ['193', '196']}, {'id': '193', 'type': 'attribute', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'plural_score'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '196', 'type': 'argument_list', 'children': []}, {'id': '197', 'type': 'for_statement', 'children': ['198', '199', '200']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '200', 'type': 'block', 'children': ['201']}, {'id': '201', 'type': 'if_statement', 'children': ['202', '207']}; {'id': '202', 'type': 'comparison_operator', 'children': ['203', '206'], 'value': '=='}, {'id': '203', 'type': 'subscript', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'plural_score'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'minscore'}, {'id': '207', 'type': 'block', 'children': ['208', '216', '223', '233']}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'assignment', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'child_state'}, {'id': '211', 'type': 'call', 'children': ['212', '215']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'copy'}, {'id': '215', 'type': 'argument_list', 'children': []}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'call', 'children': ['218', '221']}; {'id': '218', 'type': 'attribute', 'children': ['219', '220']}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'child_state'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'remove'}, {'id': '221', 'type': 'argument_list', 'children': ['222']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}, {'id': '223', 'type': 'expression_statement', 'children': ['224']}; {'id': '224', 'type': 'assignment', 'children': ['225', '226']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'tpc'}; {'id': '226', 'type': 'call', 'children': ['227', '228']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '228', 'type': 'argument_list', 'children': ['229']}, {'id': '229', 'type': 'call', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '231', 'type': 'argument_list', 'children': ['232']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'child_state'}, {'id': '233', 'type': 'if_statement', 'children': ['234', '237', '239']}; {'id': '234', 'type': 'comparison_operator', 'children': ['235', '236'], 'value': 'in'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'tpc'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}, {'id': '237', 'type': 'block', 'children': ['238']}; {'id': '238', 'type': 'continue_statement', 'children': []}, {'id': '239', 'type': 'else_clause', 'children': ['240']}; {'id': '240', 'type': 'block', 'children': ['241', '248', '257']}, {'id': '241', 'type': 'expression_statement', 'children': ['242']}; {'id': '242', 'type': 'call', 'children': ['243', '246']}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '246', 'type': 'argument_list', 'children': ['247']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'tpc'}; {'id': '248', 'type': 'expression_statement', 'children': ['249']}, {'id': '249', 'type': 'assignment', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '251', 'type': 'call', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'Node'}, {'id': '253', 'type': 'argument_list', 'children': ['254']}; {'id': '254', 'type': 'keyword_argument', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'child_state'}, {'id': '257', 'type': 'expression_statement', 'children': ['258']}; {'id': '258', 'type': 'call', 'children': ['259', '262']}, {'id': '259', 'type': 'attribute', 'children': ['260', '261']}; {'id': '260', 'type': 'identifier', 'children': [], 'value': 'stackNode'}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '262', 'type': 'argument_list', 'children': ['263']}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'child_node'}; {'id': '264', 'type': 'return_statement', 'children': ['265']}, {'id': '265', 'type': 'call', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '267', 'type': 'argument_list', 'children': ['268']} | def STVsocwinners(self, profile):
ordering = profile.getOrderVectors()
prefcounts = profile.getPreferenceCounts()
m = profile.numCands
if min(ordering[0]) == 0:
startstate = set(range(m))
else:
startstate = set(range(1, m + 1))
ordering, startstate = self.preprocessing(ordering, prefcounts, m, startstate)
m_star = len(startstate)
known_winners = set()
hashtable2 = set()
root = Node(value=startstate)
stackNode = []
stackNode.append(root)
while stackNode:
node = stackNode.pop()
state = node.value.copy()
if len(state) == 1 and list(state)[0] not in known_winners:
known_winners.add(list(state)[0])
continue
if state <= known_winners:
continue
plural_score = self.get_plurality_scores3(prefcounts, ordering, state, m_star)
minscore = min(plural_score.values())
for to_be_deleted in state:
if plural_score[to_be_deleted] == minscore:
child_state = state.copy()
child_state.remove(to_be_deleted)
tpc = tuple(sorted(child_state))
if tpc in hashtable2:
continue
else:
hashtable2.add(tpc)
child_node = Node(value=child_state)
stackNode.append(child_node)
return sorted(known_winners) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'baldwinsoc_winners'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'block', 'children': ['7', '15', '21', '29', '64', '78', '84', '90', '99', '103', '110', '283']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'getOrderVectors'}; {'id': '14', 'type': 'argument_list', 'children': []}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '18', 'type': 'attribute', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'numCands'}, {'id': '21', 'type': 'expression_statement', 'children': ['22']}; {'id': '22', 'type': 'assignment', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '24', 'type': 'call', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'getPreferenceCounts'}; {'id': '28', 'type': 'argument_list', 'children': []}, {'id': '29', 'type': 'if_statement', 'children': ['30', '38', '49']}; {'id': '30', 'type': 'comparison_operator', 'children': ['31', '37'], 'value': '=='}, {'id': '31', 'type': 'call', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'min'}, {'id': '33', 'type': 'argument_list', 'children': ['34']}; {'id': '34', 'type': 'subscript', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '36', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '37', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '38', 'type': 'block', 'children': ['39']}, {'id': '39', 'type': 'expression_statement', 'children': ['40']}; {'id': '40', 'type': 'assignment', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '47', 'type': 'argument_list', 'children': ['48']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '49', 'type': 'else_clause', 'children': ['50']}; {'id': '50', 'type': 'block', 'children': ['51']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'startstate'}; {'id': '54', 'type': 'call', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '56', 'type': 'argument_list', 'children': ['57']}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '59', 'type': 'argument_list', 'children': ['60', '61']}; {'id': '60', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '61', 'type': 'binary_operator', 'children': ['62', '63'], 'value': '+'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '63', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'wmg'}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'getWmg2'}, {'id': '71', 'type': 'argument_list', 'children': ['72', '73', '74', '75']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'startstate'}, {'id': '75', 'type': 'keyword_argument', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'normalize'}, {'id': '77', 'type': 'False', 'children': []}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'known_winners'}, {'id': '81', 'type': 'call', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '83', 'type': 'argument_list', 'children': []}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}, {'id': '87', 'type': 'call', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '89', 'type': 'argument_list', 'children': []}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'root'}, {'id': '93', 'type': 'call', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'Node'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'keyword_argument', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'startstate'}, {'id': '99', 'type': 'expression_statement', 'children': ['100']}; {'id': '100', 'type': 'assignment', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'stackNode'}; {'id': '102', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'call', 'children': ['105', '108']}, {'id': '105', 'type': 'attribute', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'stackNode'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '108', 'type': 'argument_list', 'children': ['109']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'root'}; {'id': '110', 'type': 'while_statement', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'stackNode'}; {'id': '112', 'type': 'block', 'children': ['113', '121', '131', '161', '167', '173', '183', '205', '216']}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '116', 'type': 'call', 'children': ['117', '120']}, {'id': '117', 'type': 'attribute', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'stackNode'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '120', 'type': 'argument_list', 'children': []}, {'id': '121', 'type': 'expression_statement', 'children': ['122']}; {'id': '122', 'type': 'assignment', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '124', 'type': 'call', 'children': ['125', '130']}, {'id': '125', 'type': 'attribute', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'node'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'copy'}; {'id': '130', 'type': 'argument_list', 'children': []}, {'id': '131', 'type': 'if_statement', 'children': ['132', '147']}; {'id': '132', 'type': 'boolean_operator', 'children': ['133', '139'], 'value': 'and'}, {'id': '133', 'type': 'comparison_operator', 'children': ['134', '138'], 'value': '=='}; {'id': '134', 'type': 'call', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '138', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '139', 'type': 'comparison_operator', 'children': ['140', '146'], 'value': 'not'}; {'id': '140', 'type': 'subscript', 'children': ['141', '145']}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '145', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'known_winners'}, {'id': '147', 'type': 'block', 'children': ['148', '160']}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'call', 'children': ['150', '153']}; {'id': '150', 'type': 'attribute', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '153', 'type': 'argument_list', 'children': ['154']}; {'id': '154', 'type': 'subscript', 'children': ['155', '159']}, {'id': '155', 'type': 'call', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '159', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '160', 'type': 'continue_statement', 'children': []}, {'id': '161', 'type': 'if_statement', 'children': ['162', '165']}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '164'], 'value': '<='}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'known_winners'}, {'id': '165', 'type': 'block', 'children': ['166']}; {'id': '166', 'type': 'continue_statement', 'children': []}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'assignment', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'plural_score'}; {'id': '170', 'type': 'call', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '172', 'type': 'argument_list', 'children': []}, {'id': '173', 'type': 'for_statement', 'children': ['174', '175', '176']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '176', 'type': 'block', 'children': ['177']}, {'id': '177', 'type': 'expression_statement', 'children': ['178']}; {'id': '178', 'type': 'assignment', 'children': ['179', '182']}, {'id': '179', 'type': 'subscript', 'children': ['180', '181']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'plural_score'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'cand'}; {'id': '182', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '183', 'type': 'for_statement', 'children': ['184', '187', '194']}; {'id': '184', 'type': 'pattern_list', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '187', 'type': 'call', 'children': ['188', '191']}; {'id': '188', 'type': 'attribute', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'itertools'}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'permutations'}, {'id': '191', 'type': 'argument_list', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '193', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '194', 'type': 'block', 'children': ['195']}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'augmented_assignment', 'children': ['197', '200'], 'value': '+='}, {'id': '197', 'type': 'subscript', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'plural_score'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '200', 'type': 'subscript', 'children': ['201', '204']}, {'id': '201', 'type': 'subscript', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'wmg'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '205', 'type': 'expression_statement', 'children': ['206']}; {'id': '206', 'type': 'assignment', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'minscore'}; {'id': '208', 'type': 'call', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'min'}; {'id': '210', 'type': 'argument_list', 'children': ['211']}, {'id': '211', 'type': 'call', 'children': ['212', '215']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'plural_score'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '215', 'type': 'argument_list', 'children': []}; {'id': '216', 'type': 'for_statement', 'children': ['217', '218', '219']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '219', 'type': 'block', 'children': ['220']}; {'id': '220', 'type': 'if_statement', 'children': ['221', '226']}, {'id': '221', 'type': 'comparison_operator', 'children': ['222', '225'], 'value': '=='}; {'id': '222', 'type': 'subscript', 'children': ['223', '224']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'plural_score'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'minscore'}; {'id': '226', 'type': 'block', 'children': ['227', '235', '242', '252']}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'child_state'}; {'id': '230', 'type': 'call', 'children': ['231', '234']}, {'id': '231', 'type': 'attribute', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'copy'}; {'id': '234', 'type': 'argument_list', 'children': []}, {'id': '235', 'type': 'expression_statement', 'children': ['236']}; {'id': '236', 'type': 'call', 'children': ['237', '240']}, {'id': '237', 'type': 'attribute', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'child_state'}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'remove'}; {'id': '240', 'type': 'argument_list', 'children': ['241']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'to_be_deleted'}; {'id': '242', 'type': 'expression_statement', 'children': ['243']}, {'id': '243', 'type': 'assignment', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'tpc'}, {'id': '245', 'type': 'call', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '247', 'type': 'argument_list', 'children': ['248']}; {'id': '248', 'type': 'call', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '250', 'type': 'argument_list', 'children': ['251']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'child_state'}; {'id': '252', 'type': 'if_statement', 'children': ['253', '256', '258']}, {'id': '253', 'type': 'comparison_operator', 'children': ['254', '255'], 'value': 'in'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'tpc'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}; {'id': '256', 'type': 'block', 'children': ['257']}, {'id': '257', 'type': 'continue_statement', 'children': []}; {'id': '258', 'type': 'else_clause', 'children': ['259']}, {'id': '259', 'type': 'block', 'children': ['260', '267', '276']}; {'id': '260', 'type': 'expression_statement', 'children': ['261']}, {'id': '261', 'type': 'call', 'children': ['262', '265']}; {'id': '262', 'type': 'attribute', 'children': ['263', '264']}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'hashtable2'}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '265', 'type': 'argument_list', 'children': ['266']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'tpc'}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'assignment', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'child_node'}; {'id': '270', 'type': 'call', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'Node'}; {'id': '272', 'type': 'argument_list', 'children': ['273']}, {'id': '273', 'type': 'keyword_argument', 'children': ['274', '275']}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'child_state'}; {'id': '276', 'type': 'expression_statement', 'children': ['277']}, {'id': '277', 'type': 'call', 'children': ['278', '281']}; {'id': '278', 'type': 'attribute', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'stackNode'}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '281', 'type': 'argument_list', 'children': ['282']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'child_node'}, {'id': '283', 'type': 'return_statement', 'children': ['284']}; {'id': '284', 'type': 'call', 'children': ['285', '286']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '286', 'type': 'argument_list', 'children': ['287']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'known_winners'} | def baldwinsoc_winners(self, profile):
ordering = profile.getOrderVectors()
m = profile.numCands
prefcounts = profile.getPreferenceCounts()
if min(ordering[0]) == 0:
startstate = set(range(m))
else:
startstate = set(range(1, m + 1))
wmg = self.getWmg2(prefcounts, ordering, startstate, normalize=False)
known_winners = set()
hashtable2 = set()
root = Node(value=startstate)
stackNode = []
stackNode.append(root)
while stackNode:
node = stackNode.pop()
state = node.value.copy()
if len(state) == 1 and list(state)[0] not in known_winners:
known_winners.add(list(state)[0])
continue
if state <= known_winners:
continue
plural_score = dict()
for cand in state:
plural_score[cand] = 0
for cand1, cand2 in itertools.permutations(state, 2):
plural_score[cand1] += wmg[cand1][cand2]
minscore = min(plural_score.values())
for to_be_deleted in state:
if plural_score[to_be_deleted] == minscore:
child_state = state.copy()
child_state.remove(to_be_deleted)
tpc = tuple(sorted(child_state))
if tpc in hashtable2:
continue
else:
hashtable2.add(tpc)
child_node = Node(value=child_state)
stackNode.append(child_node)
return sorted(known_winners) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'getWmg2'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'ordering'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'normalize'}; {'id': '10', 'type': 'False', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '18', '30', '58', '93', '165']}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '15', 'type': 'call', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '17', 'type': 'argument_list', 'children': []}; {'id': '18', 'type': 'for_statement', 'children': ['19', '20', '21']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'cand'}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'state'}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '27']}; {'id': '24', 'type': 'subscript', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '27', 'type': 'call', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '29', 'type': 'argument_list', 'children': []}; {'id': '30', 'type': 'for_statement', 'children': ['31', '34', '41']}, {'id': '31', 'type': 'pattern_list', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '34', 'type': 'call', 'children': ['35', '38']}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'itertools'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'combinations'}; {'id': '38', 'type': 'argument_list', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'state'}; {'id': '40', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '41', 'type': 'block', 'children': ['42', '50']}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '49']}; {'id': '44', 'type': 'subscript', 'children': ['45', '48']}, {'id': '45', 'type': 'subscript', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '49', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '57']}; {'id': '52', 'type': 'subscript', 'children': ['53', '56']}, {'id': '53', 'type': 'subscript', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '57', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '58', 'type': 'for_statement', 'children': ['59', '60', '68']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '60', 'type': 'call', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '62', 'type': 'argument_list', 'children': ['63', '64']}, {'id': '63', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '64', 'type': 'call', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '66', 'type': 'argument_list', 'children': ['67']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '68', 'type': 'block', 'children': ['69']}, {'id': '69', 'type': 'for_statement', 'children': ['70', '73', '82']}; {'id': '70', 'type': 'pattern_list', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '73', 'type': 'call', 'children': ['74', '77']}; {'id': '74', 'type': 'attribute', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'itertools'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'combinations'}, {'id': '77', 'type': 'argument_list', 'children': ['78', '81']}; {'id': '78', 'type': 'subscript', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'ordering'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '81', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '82', 'type': 'block', 'children': ['83']}, {'id': '83', 'type': 'expression_statement', 'children': ['84']}; {'id': '84', 'type': 'augmented_assignment', 'children': ['85', '90'], 'value': '+='}, {'id': '85', 'type': 'subscript', 'children': ['86', '89']}; {'id': '86', 'type': 'subscript', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '90', 'type': 'subscript', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '93', 'type': 'if_statement', 'children': ['94', '97']}; {'id': '94', 'type': 'comparison_operator', 'children': ['95', '96'], 'value': '=='}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'normalize'}; {'id': '96', 'type': 'True', 'children': []}, {'id': '97', 'type': 'block', 'children': ['98', '105', '130']}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'assignment', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'maxEdge'}, {'id': '101', 'type': 'call', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'float'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'string', 'children': [], 'value': "'-inf'"}, {'id': '105', 'type': 'for_statement', 'children': ['106', '107', '112']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '107', 'type': 'call', 'children': ['108', '111']}; {'id': '108', 'type': 'attribute', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '111', 'type': 'argument_list', 'children': []}; {'id': '112', 'type': 'block', 'children': ['113']}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'maxEdge'}; {'id': '116', 'type': 'call', 'children': ['117', '118']}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'max'}; {'id': '118', 'type': 'argument_list', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'maxEdge'}; {'id': '120', 'type': 'call', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'max'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'call', 'children': ['124', '129']}; {'id': '124', 'type': 'attribute', 'children': ['125', '128']}, {'id': '125', 'type': 'subscript', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'cand'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '129', 'type': 'argument_list', 'children': []}; {'id': '130', 'type': 'for_statement', 'children': ['131', '132', '137']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '136', 'type': 'argument_list', 'children': []}, {'id': '137', 'type': 'block', 'children': ['138']}; {'id': '138', 'type': 'for_statement', 'children': ['139', '140', '147']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '140', 'type': 'call', 'children': ['141', '146']}, {'id': '141', 'type': 'attribute', 'children': ['142', '145']}; {'id': '142', 'type': 'subscript', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '146', 'type': 'argument_list', 'children': []}, {'id': '147', 'type': 'block', 'children': ['148']}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '155']}; {'id': '150', 'type': 'subscript', 'children': ['151', '154']}, {'id': '151', 'type': 'subscript', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '155', 'type': 'binary_operator', 'children': ['156', '164'], 'value': '/'}; {'id': '156', 'type': 'call', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'float'}; {'id': '158', 'type': 'argument_list', 'children': ['159']}, {'id': '159', 'type': 'subscript', 'children': ['160', '163']}; {'id': '160', 'type': 'subscript', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'wmgMap'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'maxEdge'}, {'id': '165', 'type': 'return_statement', 'children': ['166']} | def getWmg2(self, prefcounts, ordering, state, normalize=False):
wmgMap = dict()
for cand in state:
wmgMap[cand] = dict()
for cand1, cand2 in itertools.combinations(state, 2):
wmgMap[cand1][cand2] = 0
wmgMap[cand2][cand1] = 0
for i in range(0, len(prefcounts)):
for cand1, cand2 in itertools.combinations(ordering[i], 2):
wmgMap[cand1][cand2] += prefcounts[i]
if normalize == True:
maxEdge = float('-inf')
for cand in wmgMap.keys():
maxEdge = max(maxEdge, max(wmgMap[cand].values()))
for cand1 in wmgMap.keys():
for cand2 in wmgMap[cand1].keys():
wmgMap[cand1][cand2] = float(wmgMap[cand1][cand2]) / maxEdge
return wmgMap |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'PluRunOff_cowinners'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '6', 'type': 'block', 'children': ['7', '15', '37', '45', '52', '60', '71', '77', '81', '180', '306']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'elecType'}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'getElecType'}; {'id': '14', 'type': 'argument_list', 'children': []}, {'id': '15', 'type': 'if_statement', 'children': ['16', '27']}; {'id': '16', 'type': 'boolean_operator', 'children': ['17', '24'], 'value': 'and'}, {'id': '17', 'type': 'boolean_operator', 'children': ['18', '21'], 'value': 'and'}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': '!='}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'elecType'}; {'id': '20', 'type': 'string', 'children': [], 'value': '"soc"'}, {'id': '21', 'type': 'comparison_operator', 'children': ['22', '23'], 'value': '!='}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'elecType'}, {'id': '23', 'type': 'string', 'children': [], 'value': '"toc"'}; {'id': '24', 'type': 'comparison_operator', 'children': ['25', '26'], 'value': '!='}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'elecType'}; {'id': '26', 'type': 'string', 'children': [], 'value': '"csv"'}, {'id': '27', 'type': 'block', 'children': ['28', '33']}; {'id': '28', 'type': 'expression_statement', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'string', 'children': [], 'value': '"ERROR: unsupported election type"'}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'call', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'exit'}; {'id': '36', 'type': 'argument_list', 'children': []}, {'id': '37', 'type': 'expression_statement', 'children': ['38']}; {'id': '38', 'type': 'assignment', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '40', 'type': 'call', 'children': ['41', '44']}, {'id': '41', 'type': 'attribute', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'getPreferenceCounts'}; {'id': '44', 'type': 'argument_list', 'children': []}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'len_prefcounts'}; {'id': '48', 'type': 'call', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '50', 'type': 'argument_list', 'children': ['51']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}; {'id': '52', 'type': 'expression_statement', 'children': ['53']}, {'id': '53', 'type': 'assignment', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'rankmaps'}, {'id': '55', 'type': 'call', 'children': ['56', '59']}; {'id': '56', 'type': 'attribute', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'profile'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'getRankMaps'}, {'id': '59', 'type': 'argument_list', 'children': []}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'ranking'}, {'id': '63', 'type': 'call', 'children': ['64', '69']}; {'id': '64', 'type': 'attribute', 'children': ['65', '68']}, {'id': '65', 'type': 'call', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'MechanismPlurality'}, {'id': '67', 'type': 'argument_list', 'children': []}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'getRanking'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'profile'}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '76', 'type': 'argument_list', 'children': []}, {'id': '77', 'type': 'expression_statement', 'children': ['78']}; {'id': '78', 'type': 'assignment', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'top_2_combinations'}; {'id': '80', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '81', 'type': 'if_statement', 'children': ['82', '92', '118']}; {'id': '82', 'type': 'comparison_operator', 'children': ['83', '91'], 'value': '>'}, {'id': '83', 'type': 'call', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '85', 'type': 'argument_list', 'children': ['86']}; {'id': '86', 'type': 'subscript', 'children': ['87', '90']}, {'id': '87', 'type': 'subscript', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'ranking'}, {'id': '89', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '90', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '91', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '92', 'type': 'block', 'children': ['93']}, {'id': '93', 'type': 'for_statement', 'children': ['94', '97', '108']}; {'id': '94', 'type': 'pattern_list', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'cand1'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'cand2'}, {'id': '97', 'type': 'call', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'itertools'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'combinations'}, {'id': '101', 'type': 'argument_list', 'children': ['102', '107']}; {'id': '102', 'type': 'subscript', 'children': ['103', '106']}, {'id': '103', 'type': 'subscript', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'ranking'}, {'id': '105', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '106', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '107', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'call', 'children': ['111', '114']}, {'id': '111', 'type': 'attribute', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'top_2_combinations'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '114', 'type': 'argument_list', 'children': ['115']}, {'id': '115', 'type': 'list', 'children': ['116', '117'], 'value': '[cand1, cand2]'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'cand1'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'cand2'}; {'id': '118', 'type': 'else_clause', 'children': ['119']}, {'id': '119', 'type': 'block', 'children': ['120', '130']}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'assignment', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'max_cand'}, {'id': '123', 'type': 'subscript', 'children': ['124', '129']}; {'id': '124', 'type': 'subscript', 'children': ['125', '128']}, {'id': '125', 'type': 'subscript', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'ranking'}, {'id': '127', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '128', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '129', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '130', 'type': 'if_statement', 'children': ['131', '141', '159']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '140'], 'value': '>'}; {'id': '132', 'type': 'call', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '134', 'type': 'argument_list', 'children': ['135']}, {'id': '135', 'type': 'subscript', 'children': ['136', '139']}; {'id': '136', 'type': 'subscript', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'ranking'}; {'id': '138', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '139', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '140', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '141', 'type': 'block', 'children': ['142']}; {'id': '142', 'type': 'for_statement', 'children': ['143', '144', '149']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'second_max_cand'}; {'id': '144', 'type': 'subscript', 'children': ['145', '148']}, {'id': '145', 'type': 'subscript', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'ranking'}, {'id': '147', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '148', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '149', 'type': 'block', 'children': ['150']}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'call', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'top_2_combinations'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '155', 'type': 'argument_list', 'children': ['156']}; {'id': '156', 'type': 'list', 'children': ['157', '158'], 'value': '[max_cand, second_max_cand]'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'max_cand'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'second_max_cand'}, {'id': '159', 'type': 'else_clause', 'children': ['160']}; {'id': '160', 'type': 'block', 'children': ['161', '171']}, {'id': '161', 'type': 'expression_statement', 'children': ['162']}; {'id': '162', 'type': 'assignment', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'second_max_cand'}; {'id': '164', 'type': 'subscript', 'children': ['165', '170']}, {'id': '165', 'type': 'subscript', 'children': ['166', '169']}; {'id': '166', 'type': 'subscript', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'ranking'}; {'id': '168', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '169', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '170', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '171', 'type': 'expression_statement', 'children': ['172']}; {'id': '172', 'type': 'call', 'children': ['173', '176']}, {'id': '173', 'type': 'attribute', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'top_2_combinations'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '176', 'type': 'argument_list', 'children': ['177']}, {'id': '177', 'type': 'list', 'children': ['178', '179'], 'value': '[max_cand, second_max_cand]'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'max_cand'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'second_max_cand'}; {'id': '180', 'type': 'for_statement', 'children': ['181', '182', '183']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'top_2'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'top_2_combinations'}, {'id': '183', 'type': 'block', 'children': ['184', '198', '268', '279', '297']}; {'id': '184', 'type': 'expression_statement', 'children': ['185']}, {'id': '185', 'type': 'assignment', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'dict_top2'}, {'id': '187', 'type': 'dictionary', 'children': ['188', '193']}; {'id': '188', 'type': 'pair', 'children': ['189', '192']}, {'id': '189', 'type': 'subscript', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'top_2'}, {'id': '191', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '192', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '193', 'type': 'pair', 'children': ['194', '197']}; {'id': '194', 'type': 'subscript', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'top_2'}; {'id': '196', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '197', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '198', 'type': 'for_statement', 'children': ['199', '200', '204']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '200', 'type': 'call', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '202', 'type': 'argument_list', 'children': ['203']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'len_prefcounts'}; {'id': '204', 'type': 'block', 'children': ['205', '227', '238', '256']}, {'id': '205', 'type': 'expression_statement', 'children': ['206']}; {'id': '206', 'type': 'assignment', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'vote_top2'}; {'id': '208', 'type': 'dictionary_comprehension', 'children': ['209', '212', '223']}, {'id': '209', 'type': 'pair', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '212', 'type': 'for_in_clause', 'children': ['213', '216']}, {'id': '213', 'type': 'pattern_list', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '216', 'type': 'call', 'children': ['217', '222']}, {'id': '217', 'type': 'attribute', 'children': ['218', '221']}; {'id': '218', 'type': 'subscript', 'children': ['219', '220']}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'rankmaps'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '222', 'type': 'argument_list', 'children': []}, {'id': '223', 'type': 'if_clause', 'children': ['224']}; {'id': '224', 'type': 'comparison_operator', 'children': ['225', '226'], 'value': 'in'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'top_2'}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'top_position'}; {'id': '230', 'type': 'call', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'min'}; {'id': '232', 'type': 'argument_list', 'children': ['233']}, {'id': '233', 'type': 'call', 'children': ['234', '237']}; {'id': '234', 'type': 'attribute', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'vote_top2'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'values'}, {'id': '237', 'type': 'argument_list', 'children': []}; {'id': '238', 'type': 'expression_statement', 'children': ['239']}, {'id': '239', 'type': 'assignment', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '241', 'type': 'list_comprehension', 'children': ['242', '243', '250']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '243', 'type': 'for_in_clause', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '245', 'type': 'call', 'children': ['246', '249']}; {'id': '246', 'type': 'attribute', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'vote_top2'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '249', 'type': 'argument_list', 'children': []}; {'id': '250', 'type': 'if_clause', 'children': ['251']}, {'id': '251', 'type': 'comparison_operator', 'children': ['252', '255'], 'value': '=='}; {'id': '252', 'type': 'subscript', 'children': ['253', '254']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'vote_top2'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'top_position'}; {'id': '256', 'type': 'for_statement', 'children': ['257', '258', '259']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '259', 'type': 'block', 'children': ['260']}; {'id': '260', 'type': 'expression_statement', 'children': ['261']}, {'id': '261', 'type': 'augmented_assignment', 'children': ['262', '265'], 'value': '+='}; {'id': '262', 'type': 'subscript', 'children': ['263', '264']}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'dict_top2'}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '265', 'type': 'subscript', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'prefcounts'}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '268', 'type': 'expression_statement', 'children': ['269']}, {'id': '269', 'type': 'assignment', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'max_value'}, {'id': '271', 'type': 'call', 'children': ['272', '273']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'max'}, {'id': '273', 'type': 'argument_list', 'children': ['274']}; {'id': '274', 'type': 'call', 'children': ['275', '278']}, {'id': '275', 'type': 'attribute', 'children': ['276', '277']}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'dict_top2'}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'values'}; {'id': '278', 'type': 'argument_list', 'children': []}, {'id': '279', 'type': 'expression_statement', 'children': ['280']}; {'id': '280', 'type': 'assignment', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'winners'}; {'id': '282', 'type': 'list_comprehension', 'children': ['283', '284', '291']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '284', 'type': 'for_in_clause', 'children': ['285', '286']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '286', 'type': 'call', 'children': ['287', '290']}, {'id': '287', 'type': 'attribute', 'children': ['288', '289']}; {'id': '288', 'type': 'identifier', 'children': [], 'value': 'dict_top2'}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '290', 'type': 'argument_list', 'children': []}, {'id': '291', 'type': 'if_clause', 'children': ['292']}; {'id': '292', 'type': 'comparison_operator', 'children': ['293', '296'], 'value': '=='}, {'id': '293', 'type': 'subscript', 'children': ['294', '295']}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'dict_top2'}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'max_value'}, {'id': '297', 'type': 'expression_statement', 'children': ['298']}; {'id': '298', 'type': 'assignment', 'children': ['299', '300']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '300', 'type': 'binary_operator', 'children': ['301', '302'], 'value': '|'}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'known_winners'}; {'id': '302', 'type': 'call', 'children': ['303', '304']}, {'id': '303', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '304', 'type': 'argument_list', 'children': ['305']}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'winners'}; {'id': '306', 'type': 'return_statement', 'children': ['307']}, {'id': '307', 'type': 'call', 'children': ['308', '309']}; {'id': '308', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '309', 'type': 'argument_list', 'children': ['310']} | def PluRunOff_cowinners(self, profile):
elecType = profile.getElecType()
if elecType != "soc" and elecType != "toc" and elecType != "csv":
print("ERROR: unsupported election type")
exit()
prefcounts = profile.getPreferenceCounts()
len_prefcounts = len(prefcounts)
rankmaps = profile.getRankMaps()
ranking = MechanismPlurality().getRanking(profile)
known_winners = set()
top_2_combinations = []
if len(ranking[0][0]) > 1:
for cand1, cand2 in itertools.combinations(ranking[0][0], 2):
top_2_combinations.append([cand1, cand2])
else:
max_cand = ranking[0][0][0]
if len(ranking[0][1]) > 1:
for second_max_cand in ranking[0][1]:
top_2_combinations.append([max_cand, second_max_cand])
else:
second_max_cand = ranking[0][1][0]
top_2_combinations.append([max_cand, second_max_cand])
for top_2 in top_2_combinations:
dict_top2 = {top_2[0]: 0, top_2[1]: 0}
for i in range(len_prefcounts):
vote_top2 = {key: value for key, value in rankmaps[i].items() if key in top_2}
top_position = min(vote_top2.values())
keys = [x for x in vote_top2.keys() if vote_top2[x] == top_position]
for key in keys:
dict_top2[key] += prefcounts[i]
max_value = max(dict_top2.values())
winners = [y for y in dict_top2.keys() if dict_top2[y] == max_value]
known_winners = known_winners | set(winners)
return sorted(known_winners) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_cache_offsets'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'up_to_index'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '10', 'type': 'True', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '74', '153', '161', '171']}; {'id': '12', 'type': 'if_statement', 'children': ['13', '15', '57']}, {'id': '13', 'type': 'not_operator', 'children': ['14']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'up_to_index'}, {'id': '15', 'type': 'block', 'children': ['16', '26', '36', '42']}; {'id': '16', 'type': 'if_statement', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'verbose'}; {'id': '18', 'type': 'block', 'children': ['19']}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'call', 'children': ['21', '24']}, {'id': '21', 'type': 'attribute', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '24', 'type': 'argument_list', 'children': ['25']}, {'id': '25', 'type': 'string', 'children': [], 'value': '"Caching event file offsets, this may take a bit."'}; {'id': '26', 'type': 'expression_statement', 'children': ['27']}, {'id': '27', 'type': 'call', 'children': ['28', '33']}; {'id': '28', 'type': 'attribute', 'children': ['29', '32']}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'blob_file'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'seek'}, {'id': '33', 'type': 'argument_list', 'children': ['34', '35']}; {'id': '34', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '35', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '36', 'type': 'expression_statement', 'children': ['37']}, {'id': '37', 'type': 'assignment', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}, {'id': '41', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '42', 'type': 'if_statement', 'children': ['43', '47']}, {'id': '43', 'type': 'not_operator', 'children': ['44']}; {'id': '44', 'type': 'attribute', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'raw_header'}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'call', 'children': ['50', '55']}; {'id': '50', 'type': 'attribute', 'children': ['51', '54']}, {'id': '51', 'type': 'attribute', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '55', 'type': 'argument_list', 'children': ['56']}; {'id': '56', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '57', 'type': 'else_clause', 'children': ['58']}; {'id': '58', 'type': 'block', 'children': ['59']}, {'id': '59', 'type': 'expression_statement', 'children': ['60']}; {'id': '60', 'type': 'call', 'children': ['61', '66']}, {'id': '61', 'type': 'attribute', 'children': ['62', '65']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'blob_file'}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'seek'}; {'id': '66', 'type': 'argument_list', 'children': ['67', '73']}, {'id': '67', 'type': 'subscript', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}, {'id': '71', 'type': 'unary_operator', 'children': ['72'], 'value': '-'}; {'id': '72', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '73', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '74', 'type': 'for_statement', 'children': ['75', '76', '85']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'line'}; {'id': '76', 'type': 'call', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'iter'}; {'id': '78', 'type': 'argument_list', 'children': ['79', '84']}, {'id': '79', 'type': 'attribute', 'children': ['80', '83']}; {'id': '80', 'type': 'attribute', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'blob_file'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'readline'}; {'id': '84', 'type': 'string', 'children': [], 'value': "''"}, {'id': '85', 'type': 'block', 'children': ['86', '93', '138']}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '89', 'type': 'call', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'try_decode_string'}, {'id': '91', 'type': 'argument_list', 'children': ['92']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '93', 'type': 'if_statement', 'children': ['94', '100']}; {'id': '94', 'type': 'call', 'children': ['95', '98']}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'line'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'startswith'}; {'id': '98', 'type': 'argument_list', 'children': ['99']}, {'id': '99', 'type': 'string', 'children': [], 'value': "'end_event:'"}; {'id': '100', 'type': 'block', 'children': ['101', '107']}, {'id': '101', 'type': 'expression_statement', 'children': ['102']}; {'id': '102', 'type': 'call', 'children': ['103', '106']}, {'id': '103', 'type': 'attribute', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': '_record_offset'}; {'id': '106', 'type': 'argument_list', 'children': []}, {'id': '107', 'type': 'if_statement', 'children': ['108', '118']}; {'id': '108', 'type': 'comparison_operator', 'children': ['109', '117'], 'value': '=='}, {'id': '109', 'type': 'binary_operator', 'children': ['110', '116'], 'value': '%'}; {'id': '110', 'type': 'call', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '112', 'type': 'argument_list', 'children': ['113']}, {'id': '113', 'type': 'attribute', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}; {'id': '116', 'type': 'integer', 'children': [], 'value': '100'}, {'id': '117', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '118', 'type': 'block', 'children': ['119', '130']}, {'id': '119', 'type': 'if_statement', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'verbose'}, {'id': '121', 'type': 'block', 'children': ['122']}; {'id': '122', 'type': 'expression_statement', 'children': ['123']}, {'id': '123', 'type': 'call', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '125', 'type': 'argument_list', 'children': ['126', '127']}; {'id': '126', 'type': 'string', 'children': [], 'value': "'.'"}, {'id': '127', 'type': 'keyword_argument', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'end'}, {'id': '129', 'type': 'string', 'children': [], 'value': "''"}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'call', 'children': ['132', '137']}; {'id': '132', 'type': 'attribute', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'stdout'}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'flush'}, {'id': '137', 'type': 'argument_list', 'children': []}; {'id': '138', 'type': 'if_statement', 'children': ['139', '151']}, {'id': '139', 'type': 'boolean_operator', 'children': ['140', '141'], 'value': 'and'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'up_to_index'}, {'id': '141', 'type': 'comparison_operator', 'children': ['142', '148'], 'value': '>='}; {'id': '142', 'type': 'call', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '144', 'type': 'argument_list', 'children': ['145']}, {'id': '145', 'type': 'attribute', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}; {'id': '148', 'type': 'binary_operator', 'children': ['149', '150'], 'value': '+'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'up_to_index'}; {'id': '150', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '151', 'type': 'block', 'children': ['152']}; {'id': '152', 'type': 'return_statement', 'children': []}, {'id': '153', 'type': 'expression_statement', 'children': ['154']}; {'id': '154', 'type': 'call', 'children': ['155', '160']}, {'id': '155', 'type': 'attribute', 'children': ['156', '159']}; {'id': '156', 'type': 'attribute', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'event_offsets'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'pop'}; {'id': '160', 'type': 'argument_list', 'children': []}, {'id': '161', 'type': 'if_statement', 'children': ['162', '164']}; {'id': '162', 'type': 'not_operator', 'children': ['163']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'up_to_index'}; {'id': '164', 'type': 'block', 'children': ['165']}, {'id': '165', 'type': 'expression_statement', 'children': ['166']}; {'id': '166', 'type': 'assignment', 'children': ['167', '170']}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'whole_file_cached'}; {'id': '170', 'type': 'True', 'children': []}, {'id': '171', 'type': 'expression_statement', 'children': ['172']}; {'id': '172', 'type': 'call', 'children': ['173', '176']}, {'id': '173', 'type': 'attribute', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '176', 'type': 'argument_list', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '181']}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'string', 'children': [], 'value': '"\\n{0} events indexed."'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '181', 'type': 'argument_list', 'children': ['182']}; {'id': '182', 'type': 'call', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '184', 'type': 'argument_list', 'children': ['185']}, {'id': '185', 'type': 'attribute', 'children': ['186', '187']}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'event_offsets'} | def _cache_offsets(self, up_to_index=None, verbose=True):
if not up_to_index:
if verbose:
self.print("Caching event file offsets, this may take a bit.")
self.blob_file.seek(0, 0)
self.event_offsets = []
if not self.raw_header:
self.event_offsets.append(0)
else:
self.blob_file.seek(self.event_offsets[-1], 0)
for line in iter(self.blob_file.readline, ''):
line = try_decode_string(line)
if line.startswith('end_event:'):
self._record_offset()
if len(self.event_offsets) % 100 == 0:
if verbose:
print('.', end='')
sys.stdout.flush()
if up_to_index and len(self.event_offsets) >= up_to_index + 1:
return
self.event_offsets.pop()
if not up_to_index:
self.whole_file_cached = True
self.print("\n{0} events indexed.".format(len(self.event_offsets))) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '7']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'getUtilities'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'decision'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'binaryRelations'}, {'id': '7', 'type': 'block', 'children': ['8', '15', '19', '134']}; {'id': '8', 'type': 'expression_statement', 'children': ['9']}, {'id': '9', 'type': 'assignment', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '11', 'type': 'call', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '13', 'type': 'argument_list', 'children': ['14']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'binaryRelations'}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'utilities'}; {'id': '18', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '19', 'type': 'for_statement', 'children': ['20', '21', '22']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'decision'}; {'id': '22', 'type': 'block', 'children': ['23', '30', '34', '86']}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'tops'}; {'id': '26', 'type': 'list', 'children': ['27'], 'value': '[cand-1]'}, {'id': '27', 'type': 'binary_operator', 'children': ['28', '29'], 'value': '-'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'cand'}, {'id': '29', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'assignment', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '33', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '34', 'type': 'while_statement', 'children': ['35', '41']}, {'id': '35', 'type': 'comparison_operator', 'children': ['36', '37'], 'value': '<'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '39', 'type': 'argument_list', 'children': ['40']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'tops'}, {'id': '41', 'type': 'block', 'children': ['42', '48', '82']}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '45', 'type': 'subscript', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'tops'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'index'}; {'id': '48', 'type': 'for_statement', 'children': ['49', '50', '54']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '50', 'type': 'call', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '54', 'type': 'block', 'children': ['55', '61']}, {'id': '55', 'type': 'if_statement', 'children': ['56', '59']}; {'id': '56', 'type': 'comparison_operator', 'children': ['57', '58'], 'value': '=='}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '59', 'type': 'block', 'children': ['60']}; {'id': '60', 'type': 'continue_statement', 'children': []}, {'id': '61', 'type': 'if_statement', 'children': ['62', '69']}; {'id': '62', 'type': 'comparison_operator', 'children': ['63', '68'], 'value': '>'}, {'id': '63', 'type': 'subscript', 'children': ['64', '67']}; {'id': '64', 'type': 'subscript', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'binaryRelations'}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '68', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'if_statement', 'children': ['71', '74']}, {'id': '71', 'type': 'comparison_operator', 'children': ['72', '73'], 'value': 'not'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'tops'}; {'id': '74', 'type': 'block', 'children': ['75']}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'tops'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '82', 'type': 'expression_statement', 'children': ['83']}, {'id': '83', 'type': 'augmented_assignment', 'children': ['84', '85'], 'value': '+='}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'index'}, {'id': '85', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '86', 'type': 'if_statement', 'children': ['87', '95', '125']}, {'id': '87', 'type': 'comparison_operator', 'children': ['88', '92'], 'value': '<='}; {'id': '88', 'type': 'call', 'children': ['89', '90']}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '90', 'type': 'argument_list', 'children': ['91']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'tops'}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '95', 'type': 'block', 'children': ['96']}; {'id': '96', 'type': 'if_statement', 'children': ['97', '102', '110']}, {'id': '97', 'type': 'comparison_operator', 'children': ['98', '101'], 'value': '=='}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'isLoss'}, {'id': '101', 'type': 'False', 'children': []}; {'id': '102', 'type': 'block', 'children': ['103']}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'call', 'children': ['105', '108']}, {'id': '105', 'type': 'attribute', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'utilities'}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '108', 'type': 'argument_list', 'children': ['109']}, {'id': '109', 'type': 'float', 'children': [], 'value': '1.0'}; {'id': '110', 'type': 'elif_clause', 'children': ['111', '116']}, {'id': '111', 'type': 'comparison_operator', 'children': ['112', '115'], 'value': '=='}; {'id': '112', 'type': 'attribute', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'isLoss'}, {'id': '115', 'type': 'True', 'children': []}; {'id': '116', 'type': 'block', 'children': ['117']}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'call', 'children': ['119', '122']}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'utilities'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'unary_operator', 'children': ['124'], 'value': '-'}; {'id': '124', 'type': 'float', 'children': [], 'value': '1.0'}, {'id': '125', 'type': 'else_clause', 'children': ['126']}; {'id': '126', 'type': 'block', 'children': ['127']}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'call', 'children': ['129', '132']}, {'id': '129', 'type': 'attribute', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'utilities'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '132', 'type': 'argument_list', 'children': ['133']}, {'id': '133', 'type': 'float', 'children': [], 'value': '0.0'}; {'id': '134', 'type': 'return_statement', 'children': ['135']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'utilities'} | def getUtilities(self, decision, binaryRelations):
m = len(binaryRelations)
utilities = []
for cand in decision:
tops = [cand-1]
index = 0
while index < len(tops):
s = tops[index]
for j in range(m):
if j == s:
continue
if binaryRelations[j][s] > 0:
if j not in tops:
tops.append(j)
index += 1
if len(tops) <= self.k:
if self.isLoss == False:
utilities.append(1.0)
elif self.isLoss == True:
utilities.append(-1.0)
else:
utilities.append(0.0)
return utilities |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'execute'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'alignment_stream'}; {'id': '10', 'type': 'None', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '33', '53', '65']}; {'id': '12', 'type': 'if_statement', 'children': ['13', '19']}, {'id': '13', 'type': 'not_operator', 'children': ['14']}; {'id': '14', 'type': 'call', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '16', 'type': 'argument_list', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'TimeInterval'}, {'id': '19', 'type': 'block', 'children': ['20']}; {'id': '20', 'type': 'raise_statement', 'children': ['21']}, {'id': '21', 'type': 'call', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'TypeError'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'call', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'string', 'children': [], 'value': "'Expected TimeInterval, got {}'"}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '28', 'type': 'argument_list', 'children': ['29']}, {'id': '29', 'type': 'call', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'type'}, {'id': '31', 'type': 'argument_list', 'children': ['32']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '33', 'type': 'if_statement', 'children': ['34', '43']}; {'id': '34', 'type': 'comparison_operator', 'children': ['35', '38'], 'value': '>'}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'end'}; {'id': '38', 'type': 'attribute', 'children': ['39', '42']}, {'id': '39', 'type': 'attribute', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'up_to_timestamp'}, {'id': '43', 'type': 'block', 'children': ['44']}; {'id': '44', 'type': 'raise_statement', 'children': ['45']}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'StreamNotAvailableError'}, {'id': '47', 'type': 'argument_list', 'children': ['48']}; {'id': '48', 'type': 'attribute', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'up_to_timestamp'}, {'id': '53', 'type': 'expression_statement', 'children': ['54']}; {'id': '54', 'type': 'assignment', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'required_intervals'}; {'id': '56', 'type': 'binary_operator', 'children': ['57', '62'], 'value': '-'}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'TimeIntervals'}, {'id': '59', 'type': 'argument_list', 'children': ['60']}; {'id': '60', 'type': 'list', 'children': ['61'], 'value': '[interval]'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'calculated_intervals'}, {'id': '65', 'type': 'if_statement', 'children': ['66', '70']}; {'id': '66', 'type': 'not_operator', 'children': ['67']}, {'id': '67', 'type': 'attribute', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'required_intervals'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'is_empty'}; {'id': '70', 'type': 'block', 'children': ['71', '75', '113', '125', '147', '167']}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'document_count'}; {'id': '74', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '75', 'type': 'for_statement', 'children': ['76', '77', '78']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'required_intervals'}; {'id': '78', 'type': 'block', 'children': ['79', '107']}, {'id': '79', 'type': 'for_statement', 'children': ['80', '81', '95']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'stream_instance'}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': '_execute'}, {'id': '85', 'type': 'argument_list', 'children': ['86', '89', '92']}; {'id': '86', 'type': 'keyword_argument', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'sources'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'sources'}, {'id': '89', 'type': 'keyword_argument', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'alignment_stream'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'alignment_stream'}; {'id': '92', 'type': 'keyword_argument', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '95', 'type': 'block', 'children': ['96', '103']}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'call', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'writer'}, {'id': '101', 'type': 'argument_list', 'children': ['102']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'stream_instance'}, {'id': '103', 'type': 'expression_statement', 'children': ['104']}; {'id': '104', 'type': 'augmented_assignment', 'children': ['105', '106'], 'value': '+='}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'document_count'}; {'id': '106', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '107', 'type': 'expression_statement', 'children': ['108']}; {'id': '108', 'type': 'augmented_assignment', 'children': ['109', '112'], 'value': '+='}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'calculated_intervals'}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'required_intervals'}; {'id': '116', 'type': 'binary_operator', 'children': ['117', '122'], 'value': '-'}, {'id': '117', 'type': 'call', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'TimeIntervals'}, {'id': '119', 'type': 'argument_list', 'children': ['120']}; {'id': '120', 'type': 'list', 'children': ['121'], 'value': '[interval]'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '122', 'type': 'attribute', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'sink'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'calculated_intervals'}, {'id': '125', 'type': 'if_statement', 'children': ['126', '130']}; {'id': '126', 'type': 'not_operator', 'children': ['127']}, {'id': '127', 'type': 'attribute', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'required_intervals'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'is_empty'}; {'id': '130', 'type': 'block', 'children': ['131']}, {'id': '131', 'type': 'expression_statement', 'children': ['132']}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'logging'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'call', 'children': ['138', '141']}; {'id': '138', 'type': 'attribute', 'children': ['139', '140']}, {'id': '139', 'type': 'string', 'children': [], 'value': '"{} execution error for time interval {} on stream {}"'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '141', 'type': 'argument_list', 'children': ['142', '145', '146']}; {'id': '142', 'type': 'attribute', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '147', 'type': 'if_statement', 'children': ['148', '150']}; {'id': '148', 'type': 'not_operator', 'children': ['149']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'document_count'}; {'id': '150', 'type': 'block', 'children': ['151']}, {'id': '151', 'type': 'expression_statement', 'children': ['152']}; {'id': '152', 'type': 'call', 'children': ['153', '156']}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'logging'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '156', 'type': 'argument_list', 'children': ['157']}, {'id': '157', 'type': 'call', 'children': ['158', '161']}; {'id': '158', 'type': 'attribute', 'children': ['159', '160']}, {'id': '159', 'type': 'string', 'children': [], 'value': '"{} did not produce any data for time interval {} on stream {}"'}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '161', 'type': 'argument_list', 'children': ['162', '165', '166']}; {'id': '162', 'type': 'attribute', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'sink'}, {'id': '167', 'type': 'expression_statement', 'children': ['168']}; {'id': '168', 'type': 'call', 'children': ['169', '172']}, {'id': '169', 'type': 'attribute', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'write_to_history'}; {'id': '172', 'type': 'argument_list', 'children': ['173', '176', '181']}, {'id': '173', 'type': 'keyword_argument', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'interval'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'interval'}; {'id': '176', 'type': 'keyword_argument', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'tool'}; {'id': '178', 'type': 'attribute', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '181', 'type': 'keyword_argument', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'document_count'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'document_count'} | def execute(self, sources, sink, interval, alignment_stream=None):
if not isinstance(interval, TimeInterval):
raise TypeError('Expected TimeInterval, got {}'.format(type(interval)))
if interval.end > sink.channel.up_to_timestamp:
raise StreamNotAvailableError(sink.channel.up_to_timestamp)
required_intervals = TimeIntervals([interval]) - sink.calculated_intervals
if not required_intervals.is_empty:
document_count = 0
for interval in required_intervals:
for stream_instance in self._execute(
sources=sources, alignment_stream=alignment_stream, interval=interval):
sink.writer(stream_instance)
document_count += 1
sink.calculated_intervals += interval
required_intervals = TimeIntervals([interval]) - sink.calculated_intervals
if not required_intervals.is_empty:
logging.error("{} execution error for time interval {} on stream {}".format(
self.name, interval, sink))
if not document_count:
logging.debug("{} did not produce any data for time interval {} on stream {}".format(
self.name, interval, sink))
self.write_to_history(
interval=interval,
tool=self.name,
document_count=document_count
) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'upload_runsummary'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'csv_filename'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'dryrun'}, {'id': '7', 'type': 'False', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '19', '43', '75', '84', '119', '125', '141', '157', '178', '183', '203', '213', '230', '235', '245', '286', '298', '309', '314', '335']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'call', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '12', 'type': 'argument_list', 'children': ['13']}, {'id': '13', 'type': 'call', 'children': ['14', '17']}; {'id': '14', 'type': 'attribute', 'children': ['15', '16']}, {'id': '15', 'type': 'string', 'children': [], 'value': '"Checking \'{}\' for consistency."'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '17', 'type': 'argument_list', 'children': ['18']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'csv_filename'}, {'id': '19', 'type': 'if_statement', 'children': ['20', '29']}; {'id': '20', 'type': 'not_operator', 'children': ['21']}, {'id': '21', 'type': 'call', 'children': ['22', '27']}; {'id': '22', 'type': 'attribute', 'children': ['23', '26']}, {'id': '23', 'type': 'attribute', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'os'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'path'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'exists'}, {'id': '27', 'type': 'argument_list', 'children': ['28']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'csv_filename'}, {'id': '29', 'type': 'block', 'children': ['30', '42']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'call', 'children': ['32', '35']}; {'id': '32', 'type': 'attribute', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'critical'}, {'id': '35', 'type': 'argument_list', 'children': ['36']}; {'id': '36', 'type': 'call', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'string', 'children': [], 'value': '"{} -> file not found."'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'csv_filename'}; {'id': '42', 'type': 'return_statement', 'children': []}, {'id': '43', 'type': 'try_statement', 'children': ['44', '57']}; {'id': '44', 'type': 'block', 'children': ['45']}, {'id': '45', 'type': 'expression_statement', 'children': ['46']}; {'id': '46', 'type': 'assignment', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'df'}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'pd'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'read_csv'}; {'id': '52', 'type': 'argument_list', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'csv_filename'}; {'id': '54', 'type': 'keyword_argument', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'sep'}; {'id': '56', 'type': 'string', 'children': [], 'value': "'\\t'"}, {'id': '57', 'type': 'except_clause', 'children': ['58', '66']}; {'id': '58', 'type': 'as_pattern', 'children': ['59', '64']}, {'id': '59', 'type': 'attribute', 'children': ['60', '63']}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'pd'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'errors'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'EmptyDataError'}; {'id': '64', 'type': 'as_pattern_target', 'children': ['65']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '66', 'type': 'block', 'children': ['67', '74']}, {'id': '67', 'type': 'expression_statement', 'children': ['68']}; {'id': '68', 'type': 'call', 'children': ['69', '72']}, {'id': '69', 'type': 'attribute', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '72', 'type': 'argument_list', 'children': ['73']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '74', 'type': 'return_statement', 'children': []}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'cols'}; {'id': '78', 'type': 'call', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'attribute', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'df'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'columns'}; {'id': '84', 'type': 'if_statement', 'children': ['85', '92']}, {'id': '85', 'type': 'not_operator', 'children': ['86']}; {'id': '86', 'type': 'call', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'REQUIRED_COLUMNS'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'issubset'}; {'id': '90', 'type': 'argument_list', 'children': ['91']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'cols'}; {'id': '92', 'type': 'block', 'children': ['93', '118']}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'call', 'children': ['95', '98']}, {'id': '95', 'type': 'attribute', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '98', 'type': 'argument_list', 'children': ['99']}, {'id': '99', 'type': 'call', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'string', 'children': [], 'value': '"Missing columns: {}."'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'call', 'children': ['105', '108']}, {'id': '105', 'type': 'attribute', 'children': ['106', '107']}; {'id': '106', 'type': 'string', 'children': [], 'value': "', '"}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '108', 'type': 'generator_expression', 'children': ['109', '113']}, {'id': '109', 'type': 'call', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '111', 'type': 'argument_list', 'children': ['112']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '113', 'type': 'for_in_clause', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '115', 'type': 'binary_operator', 'children': ['116', '117'], 'value': '-'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'REQUIRED_COLUMNS'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'cols'}; {'id': '118', 'type': 'return_statement', 'children': []}, {'id': '119', 'type': 'expression_statement', 'children': ['120']}; {'id': '120', 'type': 'assignment', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'parameters'}; {'id': '122', 'type': 'binary_operator', 'children': ['123', '124'], 'value': '-'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'cols'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'REQUIRED_COLUMNS'}, {'id': '125', 'type': 'if_statement', 'children': ['126', '132']}; {'id': '126', 'type': 'comparison_operator', 'children': ['127', '131'], 'value': '<'}, {'id': '127', 'type': 'call', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '129', 'type': 'argument_list', 'children': ['130']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'parameters'}, {'id': '131', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '132', 'type': 'block', 'children': ['133', '140']}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'call', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'string', 'children': [], 'value': '"No parameter columns found."'}; {'id': '140', 'type': 'return_statement', 'children': []}, {'id': '141', 'type': 'if_statement', 'children': ['142', '148']}; {'id': '142', 'type': 'comparison_operator', 'children': ['143', '147'], 'value': '=='}, {'id': '143', 'type': 'call', 'children': ['144', '145']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '145', 'type': 'argument_list', 'children': ['146']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'df'}, {'id': '147', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '148', 'type': 'block', 'children': ['149', '156']}, {'id': '149', 'type': 'expression_statement', 'children': ['150']}; {'id': '150', 'type': 'call', 'children': ['151', '154']}, {'id': '151', 'type': 'attribute', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'critical'}; {'id': '154', 'type': 'argument_list', 'children': ['155']}, {'id': '155', 'type': 'string', 'children': [], 'value': '"Empty dataset."'}; {'id': '156', 'type': 'return_statement', 'children': []}, {'id': '157', 'type': 'expression_statement', 'children': ['158']}; {'id': '158', 'type': 'call', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'call', 'children': ['162', '165']}; {'id': '162', 'type': 'attribute', 'children': ['163', '164']}, {'id': '163', 'type': 'string', 'children': [], 'value': '"Found data for parameters: {}."'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '165', 'type': 'argument_list', 'children': ['166']}; {'id': '166', 'type': 'call', 'children': ['167', '170']}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'string', 'children': [], 'value': "', '"}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '170', 'type': 'generator_expression', 'children': ['171', '175']}, {'id': '171', 'type': 'call', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '173', 'type': 'argument_list', 'children': ['174']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '175', 'type': 'for_in_clause', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'parameters'}; {'id': '178', 'type': 'expression_statement', 'children': ['179']}, {'id': '179', 'type': 'call', 'children': ['180', '181']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '181', 'type': 'argument_list', 'children': ['182']}; {'id': '182', 'type': 'string', 'children': [], 'value': '"Converting CSV data into JSON"'}, {'id': '183', 'type': 'if_statement', 'children': ['184', '185', '197']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'dryrun'}, {'id': '185', 'type': 'block', 'children': ['186', '193']}; {'id': '186', 'type': 'expression_statement', 'children': ['187']}, {'id': '187', 'type': 'call', 'children': ['188', '191']}; {'id': '188', 'type': 'attribute', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'warn'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'string', 'children': [], 'value': '"Dryrun: adding \'TEST_\' prefix to parameter names"'}, {'id': '193', 'type': 'expression_statement', 'children': ['194']}; {'id': '194', 'type': 'assignment', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '196', 'type': 'string', 'children': [], 'value': '"TEST_"'}, {'id': '197', 'type': 'else_clause', 'children': ['198']}; {'id': '198', 'type': 'block', 'children': ['199']}, {'id': '199', 'type': 'expression_statement', 'children': ['200']}; {'id': '200', 'type': 'assignment', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '202', 'type': 'string', 'children': [], 'value': '""'}, {'id': '203', 'type': 'expression_statement', 'children': ['204']}; {'id': '204', 'type': 'assignment', 'children': ['205', '206']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '206', 'type': 'call', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'convert_runsummary_to_json'}; {'id': '208', 'type': 'argument_list', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'df'}; {'id': '210', 'type': 'keyword_argument', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'prefix'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'prefix'}, {'id': '213', 'type': 'expression_statement', 'children': ['214']}; {'id': '214', 'type': 'call', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '216', 'type': 'argument_list', 'children': ['217']}, {'id': '217', 'type': 'call', 'children': ['218', '221']}; {'id': '218', 'type': 'attribute', 'children': ['219', '220']}, {'id': '219', 'type': 'string', 'children': [], 'value': '"We have {:.3f} MB to upload."'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '221', 'type': 'argument_list', 'children': ['222']}; {'id': '222', 'type': 'binary_operator', 'children': ['223', '227'], 'value': '/'}, {'id': '223', 'type': 'call', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '225', 'type': 'argument_list', 'children': ['226']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '227', 'type': 'binary_operator', 'children': ['228', '229'], 'value': '**'}; {'id': '228', 'type': 'integer', 'children': [], 'value': '1024'}, {'id': '229', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '230', 'type': 'expression_statement', 'children': ['231']}, {'id': '231', 'type': 'call', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '233', 'type': 'argument_list', 'children': ['234']}; {'id': '234', 'type': 'string', 'children': [], 'value': '"Requesting database session."'}, {'id': '235', 'type': 'expression_statement', 'children': ['236']}; {'id': '236', 'type': 'assignment', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'db'}; {'id': '238', 'type': 'call', 'children': ['239', '244']}, {'id': '239', 'type': 'attribute', 'children': ['240', '243']}; {'id': '240', 'type': 'attribute', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'kp'}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'DBManager'}; {'id': '244', 'type': 'argument_list', 'children': []}, {'id': '245', 'type': 'if_statement', 'children': ['246', '253', '258']}; {'id': '246', 'type': 'call', 'children': ['247', '252']}, {'id': '247', 'type': 'attribute', 'children': ['248', '251']}; {'id': '248', 'type': 'attribute', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'kp'}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'we_are_in_lyon'}; {'id': '252', 'type': 'argument_list', 'children': []}, {'id': '253', 'type': 'block', 'children': ['254']}; {'id': '254', 'type': 'expression_statement', 'children': ['255']}, {'id': '255', 'type': 'assignment', 'children': ['256', '257']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'session_cookie'}, {'id': '257', 'type': 'string', 'children': [], 'value': '"sid=_kmcprod_134.158_lyo7783844001343100343mcprod1223user"'}; {'id': '258', 'type': 'else_clause', 'children': ['259']}, {'id': '259', 'type': 'block', 'children': ['260', '276']}; {'id': '260', 'type': 'expression_statement', 'children': ['261']}, {'id': '261', 'type': 'assignment', 'children': ['262', '263']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'session_cookie'}, {'id': '263', 'type': 'call', 'children': ['264', '273']}; {'id': '264', 'type': 'attribute', 'children': ['265', '272']}, {'id': '265', 'type': 'call', 'children': ['266', '271']}; {'id': '266', 'type': 'attribute', 'children': ['267', '270']}, {'id': '267', 'type': 'attribute', 'children': ['268', '269']}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'kp'}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'Config'}, {'id': '271', 'type': 'argument_list', 'children': []}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '273', 'type': 'argument_list', 'children': ['274', '275']}; {'id': '274', 'type': 'string', 'children': [], 'value': "'DB'"}, {'id': '275', 'type': 'string', 'children': [], 'value': "'session_cookie'"}; {'id': '276', 'type': 'if_statement', 'children': ['277', '280']}, {'id': '277', 'type': 'comparison_operator', 'children': ['278', '279'], 'value': 'is'}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'session_cookie'}, {'id': '279', 'type': 'None', 'children': []}; {'id': '280', 'type': 'block', 'children': ['281']}, {'id': '281', 'type': 'raise_statement', 'children': ['282']}; {'id': '282', 'type': 'call', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'SystemExit'}; {'id': '284', 'type': 'argument_list', 'children': ['285']}, {'id': '285', 'type': 'string', 'children': [], 'value': '"Could not restore DB session."'}; {'id': '286', 'type': 'expression_statement', 'children': ['287']}, {'id': '287', 'type': 'call', 'children': ['288', '291']}; {'id': '288', 'type': 'attribute', 'children': ['289', '290']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '290', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '291', 'type': 'argument_list', 'children': ['292']}; {'id': '292', 'type': 'call', 'children': ['293', '296']}, {'id': '293', 'type': 'attribute', 'children': ['294', '295']}; {'id': '294', 'type': 'string', 'children': [], 'value': '"Using the session cookie: {}"'}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '296', 'type': 'argument_list', 'children': ['297']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'session_cookie'}; {'id': '298', 'type': 'expression_statement', 'children': ['299']}, {'id': '299', 'type': 'assignment', 'children': ['300', '303']}; {'id': '300', 'type': 'pattern_list', 'children': ['301', '302']}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'cookie_key'}; {'id': '302', 'type': 'identifier', 'children': [], 'value': 'sid'}, {'id': '303', 'type': 'call', 'children': ['304', '307']}; {'id': '304', 'type': 'attribute', 'children': ['305', '306']}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'session_cookie'}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '307', 'type': 'argument_list', 'children': ['308']}; {'id': '308', 'type': 'string', 'children': [], 'value': "'='"}, {'id': '309', 'type': 'expression_statement', 'children': ['310']}; {'id': '310', 'type': 'call', 'children': ['311', '312']}, {'id': '311', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '312', 'type': 'argument_list', 'children': ['313']}, {'id': '313', 'type': 'string', 'children': [], 'value': '"Uploading the data to the database."'}; {'id': '314', 'type': 'expression_statement', 'children': ['315']}, {'id': '315', 'type': 'assignment', 'children': ['316', '317']}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '317', 'type': 'call', 'children': ['318', '321']}; {'id': '318', 'type': 'attribute', 'children': ['319', '320']}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'requests'}; {'id': '320', 'type': 'identifier', 'children': [], 'value': 'post'}, {'id': '321', 'type': 'argument_list', 'children': ['322', '323', '329']}; {'id': '322', 'type': 'identifier', 'children': [], 'value': 'RUNSUMMARY_URL'}, {'id': '323', 'type': 'keyword_argument', 'children': ['324', '325']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'cookies'}, {'id': '325', 'type': 'dictionary', 'children': ['326']}; {'id': '326', 'type': 'pair', 'children': ['327', '328']}, {'id': '327', 'type': 'identifier', 'children': [], 'value': 'cookie_key'}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'sid'}, {'id': '329', 'type': 'keyword_argument', 'children': ['330', '331']}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'files'}, {'id': '331', 'type': 'dictionary', 'children': ['332']}; {'id': '332', 'type': 'pair', 'children': ['333', '334']}, {'id': '333', 'type': 'string', 'children': [], 'value': "'datafile'"}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '335', 'type': 'if_statement', 'children': ['336', '341', '414']}; {'id': '336', 'type': 'comparison_operator', 'children': ['337', '340'], 'value': '=='}, {'id': '337', 'type': 'attribute', 'children': ['338', '339']}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '339', 'type': 'identifier', 'children': [], 'value': 'status_code'}; {'id': '340', 'type': 'integer', 'children': [], 'value': '200'}, {'id': '341', 'type': 'block', 'children': ['342', '356', '361', '372', '393']}; {'id': '342', 'type': 'expression_statement', 'children': ['343']}, {'id': '343', 'type': 'call', 'children': ['344', '347']}; {'id': '344', 'type': 'attribute', 'children': ['345', '346']}, {'id': '345', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '346', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '347', 'type': 'argument_list', 'children': ['348']}; {'id': '348', 'type': 'call', 'children': ['349', '352']}, {'id': '349', 'type': 'attribute', 'children': ['350', '351']}; {'id': '350', 'type': 'string', 'children': [], 'value': '"POST request status code: {}"'}, {'id': '351', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '352', 'type': 'argument_list', 'children': ['353']}, {'id': '353', 'type': 'attribute', 'children': ['354', '355']}; {'id': '354', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'status_code'}; {'id': '356', 'type': 'expression_statement', 'children': ['357']}, {'id': '357', 'type': 'call', 'children': ['358', '359']}; {'id': '358', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '359', 'type': 'argument_list', 'children': ['360']}; {'id': '360', 'type': 'string', 'children': [], 'value': '"Database response:"'}, {'id': '361', 'type': 'expression_statement', 'children': ['362']}; {'id': '362', 'type': 'assignment', 'children': ['363', '364']}, {'id': '363', 'type': 'identifier', 'children': [], 'value': 'db_answer'}; {'id': '364', 'type': 'call', 'children': ['365', '368']}, {'id': '365', 'type': 'attribute', 'children': ['366', '367']}; {'id': '366', 'type': 'identifier', 'children': [], 'value': 'json'}, {'id': '367', 'type': 'identifier', 'children': [], 'value': 'loads'}; {'id': '368', 'type': 'argument_list', 'children': ['369']}, {'id': '369', 'type': 'attribute', 'children': ['370', '371']}; {'id': '370', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '371', 'type': 'identifier', 'children': [], 'value': 'text'}; {'id': '372', 'type': 'for_statement', 'children': ['373', '376', '381']}, {'id': '373', 'type': 'pattern_list', 'children': ['374', '375']}; {'id': '374', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '375', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '376', 'type': 'call', 'children': ['377', '380']}, {'id': '377', 'type': 'attribute', 'children': ['378', '379']}; {'id': '378', 'type': 'identifier', 'children': [], 'value': 'db_answer'}, {'id': '379', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '380', 'type': 'argument_list', 'children': []}, {'id': '381', 'type': 'block', 'children': ['382']}; {'id': '382', 'type': 'expression_statement', 'children': ['383']}, {'id': '383', 'type': 'call', 'children': ['384', '385']}; {'id': '384', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '385', 'type': 'argument_list', 'children': ['386']}; {'id': '386', 'type': 'call', 'children': ['387', '390']}, {'id': '387', 'type': 'attribute', 'children': ['388', '389']}; {'id': '388', 'type': 'string', 'children': [], 'value': '" -> {}: {}"'}, {'id': '389', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '390', 'type': 'argument_list', 'children': ['391', '392']}, {'id': '391', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '392', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '393', 'type': 'if_statement', 'children': ['394', '399', '405']}; {'id': '394', 'type': 'comparison_operator', 'children': ['395', '398'], 'value': '=='}, {'id': '395', 'type': 'subscript', 'children': ['396', '397']}; {'id': '396', 'type': 'identifier', 'children': [], 'value': 'db_answer'}, {'id': '397', 'type': 'string', 'children': [], 'value': "'Result'"}; {'id': '398', 'type': 'string', 'children': [], 'value': "'OK'"}, {'id': '399', 'type': 'block', 'children': ['400']}; {'id': '400', 'type': 'expression_statement', 'children': ['401']}, {'id': '401', 'type': 'call', 'children': ['402', '403']}; {'id': '402', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '403', 'type': 'argument_list', 'children': ['404']}; {'id': '404', 'type': 'string', 'children': [], 'value': '"Upload successful."'}, {'id': '405', 'type': 'else_clause', 'children': ['406']}; {'id': '406', 'type': 'block', 'children': ['407']}, {'id': '407', 'type': 'expression_statement', 'children': ['408']}; {'id': '408', 'type': 'call', 'children': ['409', '412']}, {'id': '409', 'type': 'attribute', 'children': ['410', '411']}; {'id': '410', 'type': 'identifier', 'children': [], 'value': 'log'}, {'id': '411', 'type': 'identifier', 'children': [], 'value': 'critical'}; {'id': '412', 'type': 'argument_list', 'children': ['413']}, {'id': '413', 'type': 'string', 'children': [], 'value': '"Something went wrong."'}; {'id': '414', 'type': 'else_clause', 'children': ['415']}, {'id': '415', 'type': 'block', 'children': ['416', '430', '437']}; {'id': '416', 'type': 'expression_statement', 'children': ['417']}, {'id': '417', 'type': 'call', 'children': ['418', '421']}; {'id': '418', 'type': 'attribute', 'children': ['419', '420']}, {'id': '419', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '420', 'type': 'identifier', 'children': [], 'value': 'error'}, {'id': '421', 'type': 'argument_list', 'children': ['422']}; {'id': '422', 'type': 'call', 'children': ['423', '426']}, {'id': '423', 'type': 'attribute', 'children': ['424', '425']}; {'id': '424', 'type': 'string', 'children': [], 'value': '"POST request status code: {}"'}, {'id': '425', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '426', 'type': 'argument_list', 'children': ['427']}, {'id': '427', 'type': 'attribute', 'children': ['428', '429']}; {'id': '428', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '429', 'type': 'identifier', 'children': [], 'value': 'status_code'}; {'id': '430', 'type': 'expression_statement', 'children': ['431']}, {'id': '431', 'type': 'call', 'children': ['432', '435']}; {'id': '432', 'type': 'attribute', 'children': ['433', '434']}, {'id': '433', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '434', 'type': 'identifier', 'children': [], 'value': 'critical'}, {'id': '435', 'type': 'argument_list', 'children': ['436']}; {'id': '436', 'type': 'string', 'children': [], 'value': '"Something went wrong..."'}, {'id': '437', 'type': 'return_statement', 'children': []} | def upload_runsummary(csv_filename, dryrun=False):
print("Checking '{}' for consistency.".format(csv_filename))
if not os.path.exists(csv_filename):
log.critical("{} -> file not found.".format(csv_filename))
return
try:
df = pd.read_csv(csv_filename, sep='\t')
except pd.errors.EmptyDataError as e:
log.error(e)
return
cols = set(df.columns)
if not REQUIRED_COLUMNS.issubset(cols):
log.error(
"Missing columns: {}.".format(
', '.join(str(c) for c in REQUIRED_COLUMNS - cols)
)
)
return
parameters = cols - REQUIRED_COLUMNS
if len(parameters) < 1:
log.error("No parameter columns found.")
return
if len(df) == 0:
log.critical("Empty dataset.")
return
print(
"Found data for parameters: {}.".format(
', '.join(str(c) for c in parameters)
)
)
print("Converting CSV data into JSON")
if dryrun:
log.warn("Dryrun: adding 'TEST_' prefix to parameter names")
prefix = "TEST_"
else:
prefix = ""
data = convert_runsummary_to_json(df, prefix=prefix)
print("We have {:.3f} MB to upload.".format(len(data) / 1024**2))
print("Requesting database session.")
db = kp.db.DBManager()
if kp.db.we_are_in_lyon():
session_cookie = "sid=_kmcprod_134.158_lyo7783844001343100343mcprod1223user"
else:
session_cookie = kp.config.Config().get('DB', 'session_cookie')
if session_cookie is None:
raise SystemExit("Could not restore DB session.")
log.debug("Using the session cookie: {}".format(session_cookie))
cookie_key, sid = session_cookie.split('=')
print("Uploading the data to the database.")
r = requests.post(
RUNSUMMARY_URL, cookies={cookie_key: sid}, files={'datafile': data}
)
if r.status_code == 200:
log.debug("POST request status code: {}".format(r.status_code))
print("Database response:")
db_answer = json.loads(r.text)
for key, value in db_answer.items():
print(" -> {}: {}".format(key, value))
if db_answer['Result'] == 'OK':
print("Upload successful.")
else:
log.critical("Something went wrong.")
else:
log.error("POST request status code: {}".format(r.status_code))
log.critical("Something went wrong...")
return |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_movie'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}, {'id': '5', 'type': 'block', 'children': ['6', '12', '21']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'redis_key'}, {'id': '9', 'type': 'binary_operator', 'children': ['10', '11'], 'value': '%'}; {'id': '10', 'type': 'string', 'children': [], 'value': "'m_%s'"}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'cached'}, {'id': '15', 'type': 'call', 'children': ['16', '19']}; {'id': '16', 'type': 'attribute', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'redis_ro_conn'}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '19', 'type': 'argument_list', 'children': ['20']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'redis_key'}, {'id': '21', 'type': 'if_statement', 'children': ['22', '23', '29']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'cached'}, {'id': '23', 'type': 'block', 'children': ['24']}; {'id': '24', 'type': 'return_statement', 'children': ['25']}, {'id': '25', 'type': 'call', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'Response'}, {'id': '27', 'type': 'argument_list', 'children': ['28']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'cached'}, {'id': '29', 'type': 'else_clause', 'children': ['30']}; {'id': '30', 'type': 'block', 'children': ['31', '85', '192', '216', '234', '256', '268', '281']}, {'id': '31', 'type': 'try_statement', 'children': ['32', '60']}; {'id': '32', 'type': 'block', 'children': ['33', '42', '51']}, {'id': '33', 'type': 'expression_statement', 'children': ['34']}; {'id': '34', 'type': 'assignment', 'children': ['35', '36']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '36', 'type': 'call', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'get_on_tmdb'}; {'id': '38', 'type': 'argument_list', 'children': ['39']}, {'id': '39', 'type': 'binary_operator', 'children': ['40', '41'], 'value': '%'}; {'id': '40', 'type': 'string', 'children': [], 'value': "u'/movie/%d'"}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'cast'}, {'id': '45', 'type': 'call', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'get_on_tmdb'}, {'id': '47', 'type': 'argument_list', 'children': ['48']}; {'id': '48', 'type': 'binary_operator', 'children': ['49', '50'], 'value': '%'}, {'id': '49', 'type': 'string', 'children': [], 'value': "u'/movie/%d/casts'"}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'alternative'}; {'id': '54', 'type': 'call', 'children': ['55', '56']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'get_on_tmdb'}; {'id': '56', 'type': 'argument_list', 'children': ['57']}, {'id': '57', 'type': 'binary_operator', 'children': ['58', '59'], 'value': '%'}; {'id': '58', 'type': 'string', 'children': [], 'value': "u'/movie/%d/alternative_titles'"}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}; {'id': '60', 'type': 'except_clause', 'children': ['61', '67']}, {'id': '61', 'type': 'as_pattern', 'children': ['62', '65']}; {'id': '62', 'type': 'attribute', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'requests'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'HTTPError'}, {'id': '65', 'type': 'as_pattern_target', 'children': ['66']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'err'}, {'id': '67', 'type': 'block', 'children': ['68']}; {'id': '68', 'type': 'return_statement', 'children': ['69']}, {'id': '69', 'type': 'call', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'Response'}, {'id': '71', 'type': 'argument_list', 'children': ['72', '78']}; {'id': '72', 'type': 'binary_operator', 'children': ['73', '74'], 'value': '%'}, {'id': '73', 'type': 'string', 'children': [], 'value': "'TMDB API error: %s'"}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '76', 'type': 'argument_list', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'err'}; {'id': '78', 'type': 'keyword_argument', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'status'}; {'id': '80', 'type': 'attribute', 'children': ['81', '84']}, {'id': '81', 'type': 'attribute', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'err'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'response'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'status_code'}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'assignment', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'movie'}; {'id': '88', 'type': 'dictionary', 'children': ['89', '94', '99', '122', '139', '150', '161', '172', '189']}, {'id': '89', 'type': 'pair', 'children': ['90', '91']}; {'id': '90', 'type': 'string', 'children': [], 'value': "'title'"}, {'id': '91', 'type': 'subscript', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'details'}, {'id': '93', 'type': 'string', 'children': [], 'value': "'original_title'"}; {'id': '94', 'type': 'pair', 'children': ['95', '96']}, {'id': '95', 'type': 'string', 'children': [], 'value': "'score'"}; {'id': '96', 'type': 'subscript', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '98', 'type': 'string', 'children': [], 'value': "'popularity'"}, {'id': '99', 'type': 'pair', 'children': ['100', '101']}; {'id': '100', 'type': 'string', 'children': [], 'value': "'directors'"}, {'id': '101', 'type': 'list_comprehension', 'children': ['102', '105', '110']}; {'id': '102', 'type': 'subscript', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '104', 'type': 'string', 'children': [], 'value': "'name'"}, {'id': '105', 'type': 'for_in_clause', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '107', 'type': 'subscript', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'cast'}, {'id': '109', 'type': 'string', 'children': [], 'value': "'crew'"}; {'id': '110', 'type': 'if_clause', 'children': ['111']}, {'id': '111', 'type': 'boolean_operator', 'children': ['112', '117'], 'value': 'and'}; {'id': '112', 'type': 'comparison_operator', 'children': ['113', '116'], 'value': '=='}, {'id': '113', 'type': 'subscript', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '115', 'type': 'string', 'children': [], 'value': "'department'"}; {'id': '116', 'type': 'string', 'children': [], 'value': "'Directing'"}, {'id': '117', 'type': 'comparison_operator', 'children': ['118', '121'], 'value': '=='}; {'id': '118', 'type': 'subscript', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '120', 'type': 'string', 'children': [], 'value': "'job'"}, {'id': '121', 'type': 'string', 'children': [], 'value': "'Director'"}; {'id': '122', 'type': 'pair', 'children': ['123', '124']}, {'id': '123', 'type': 'string', 'children': [], 'value': "'writers'"}; {'id': '124', 'type': 'list_comprehension', 'children': ['125', '128', '133']}, {'id': '125', 'type': 'subscript', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '127', 'type': 'string', 'children': [], 'value': "'name'"}; {'id': '128', 'type': 'for_in_clause', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '130', 'type': 'subscript', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'cast'}; {'id': '132', 'type': 'string', 'children': [], 'value': "'crew'"}, {'id': '133', 'type': 'if_clause', 'children': ['134']}; {'id': '134', 'type': 'comparison_operator', 'children': ['135', '138'], 'value': '=='}, {'id': '135', 'type': 'subscript', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '137', 'type': 'string', 'children': [], 'value': "'department'"}; {'id': '138', 'type': 'string', 'children': [], 'value': "'Writing'"}, {'id': '139', 'type': 'pair', 'children': ['140', '141']}; {'id': '140', 'type': 'string', 'children': [], 'value': "'cast'"}, {'id': '141', 'type': 'list_comprehension', 'children': ['142', '145']}; {'id': '142', 'type': 'subscript', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '144', 'type': 'string', 'children': [], 'value': "'name'"}, {'id': '145', 'type': 'for_in_clause', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '147', 'type': 'subscript', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'cast'}, {'id': '149', 'type': 'string', 'children': [], 'value': "'cast'"}; {'id': '150', 'type': 'pair', 'children': ['151', '152']}, {'id': '151', 'type': 'string', 'children': [], 'value': "'genres'"}; {'id': '152', 'type': 'list_comprehension', 'children': ['153', '156']}, {'id': '153', 'type': 'subscript', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '155', 'type': 'string', 'children': [], 'value': "'name'"}; {'id': '156', 'type': 'for_in_clause', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '158', 'type': 'subscript', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '160', 'type': 'string', 'children': [], 'value': "'genres'"}, {'id': '161', 'type': 'pair', 'children': ['162', '163']}; {'id': '162', 'type': 'string', 'children': [], 'value': "'countries'"}, {'id': '163', 'type': 'list_comprehension', 'children': ['164', '167']}; {'id': '164', 'type': 'subscript', 'children': ['165', '166']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '166', 'type': 'string', 'children': [], 'value': "'name'"}, {'id': '167', 'type': 'for_in_clause', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '169', 'type': 'subscript', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'details'}, {'id': '171', 'type': 'string', 'children': [], 'value': "'production_countries'"}; {'id': '172', 'type': 'pair', 'children': ['173', '174']}, {'id': '173', 'type': 'string', 'children': [], 'value': "'tmdb_votes'"}; {'id': '174', 'type': 'call', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '176', 'type': 'argument_list', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'round'}, {'id': '179', 'type': 'argument_list', 'children': ['180']}; {'id': '180', 'type': 'binary_operator', 'children': ['181', '188'], 'value': '*'}, {'id': '181', 'type': 'call', 'children': ['182', '185']}; {'id': '182', 'type': 'attribute', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '185', 'type': 'argument_list', 'children': ['186', '187']}; {'id': '186', 'type': 'string', 'children': [], 'value': "'vote_average'"}, {'id': '187', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '188', 'type': 'float', 'children': [], 'value': '0.5'}, {'id': '189', 'type': 'pair', 'children': ['190', '191']}; {'id': '190', 'type': 'string', 'children': [], 'value': "'_tmdb_id'"}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'tmdb_id'}; {'id': '192', 'type': 'if_statement', 'children': ['193', '199']}, {'id': '193', 'type': 'call', 'children': ['194', '197']}; {'id': '194', 'type': 'attribute', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '197', 'type': 'argument_list', 'children': ['198']}; {'id': '198', 'type': 'string', 'children': [], 'value': "'release_date'"}, {'id': '199', 'type': 'block', 'children': ['200']}; {'id': '200', 'type': 'expression_statement', 'children': ['201']}, {'id': '201', 'type': 'assignment', 'children': ['202', '205']}; {'id': '202', 'type': 'subscript', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'movie'}; {'id': '204', 'type': 'string', 'children': [], 'value': "'year'"}, {'id': '205', 'type': 'attribute', 'children': ['206', '215']}; {'id': '206', 'type': 'call', 'children': ['207', '210']}, {'id': '207', 'type': 'attribute', 'children': ['208', '209']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'datetime'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'strptime'}; {'id': '210', 'type': 'argument_list', 'children': ['211', '214']}, {'id': '211', 'type': 'subscript', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'details'}, {'id': '213', 'type': 'string', 'children': [], 'value': "'release_date'"}; {'id': '214', 'type': 'string', 'children': [], 'value': "'%Y-%m-%d'"}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'year'}; {'id': '216', 'type': 'if_statement', 'children': ['217', '223']}, {'id': '217', 'type': 'call', 'children': ['218', '221']}; {'id': '218', 'type': 'attribute', 'children': ['219', '220']}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '221', 'type': 'argument_list', 'children': ['222']}; {'id': '222', 'type': 'string', 'children': [], 'value': "'belongs_to_collection'"}, {'id': '223', 'type': 'block', 'children': ['224']}; {'id': '224', 'type': 'expression_statement', 'children': ['225']}, {'id': '225', 'type': 'assignment', 'children': ['226', '229']}; {'id': '226', 'type': 'subscript', 'children': ['227', '228']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'movie'}; {'id': '228', 'type': 'string', 'children': [], 'value': "'collection'"}, {'id': '229', 'type': 'subscript', 'children': ['230', '233']}; {'id': '230', 'type': 'subscript', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'details'}; {'id': '232', 'type': 'string', 'children': [], 'value': "'belongs_to_collection'"}, {'id': '233', 'type': 'string', 'children': [], 'value': "'name'"}; {'id': '234', 'type': 'for_statement', 'children': ['235', '236', '239']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'alt'}; {'id': '236', 'type': 'subscript', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'alternative'}; {'id': '238', 'type': 'string', 'children': [], 'value': "'titles'"}, {'id': '239', 'type': 'block', 'children': ['240']}; {'id': '240', 'type': 'expression_statement', 'children': ['241']}, {'id': '241', 'type': 'assignment', 'children': ['242', '253']}; {'id': '242', 'type': 'subscript', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'movie'}; {'id': '244', 'type': 'binary_operator', 'children': ['245', '246'], 'value': '%'}, {'id': '245', 'type': 'string', 'children': [], 'value': "'title_%s'"}; {'id': '246', 'type': 'call', 'children': ['247', '252']}, {'id': '247', 'type': 'attribute', 'children': ['248', '251']}; {'id': '248', 'type': 'subscript', 'children': ['249', '250']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'alt'}; {'id': '250', 'type': 'string', 'children': [], 'value': "'iso_3166_1'"}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '252', 'type': 'argument_list', 'children': []}, {'id': '253', 'type': 'subscript', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'alt'}, {'id': '255', 'type': 'string', 'children': [], 'value': "'title'"}; {'id': '256', 'type': 'expression_statement', 'children': ['257']}, {'id': '257', 'type': 'assignment', 'children': ['258', '259']}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'json_response'}, {'id': '259', 'type': 'call', 'children': ['260', '263']}; {'id': '260', 'type': 'attribute', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'json'}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'dumps'}, {'id': '263', 'type': 'argument_list', 'children': ['264']}; {'id': '264', 'type': 'dictionary', 'children': ['265']}, {'id': '265', 'type': 'pair', 'children': ['266', '267']}; {'id': '266', 'type': 'string', 'children': [], 'value': "'movie'"}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'movie'}; {'id': '268', 'type': 'expression_statement', 'children': ['269']}, {'id': '269', 'type': 'call', 'children': ['270', '273']}; {'id': '270', 'type': 'attribute', 'children': ['271', '272']}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'redis_conn'}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'setex'}, {'id': '273', 'type': 'argument_list', 'children': ['274', '275', '280']}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'redis_key'}, {'id': '275', 'type': 'subscript', 'children': ['276', '279']}; {'id': '276', 'type': 'attribute', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'app'}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '279', 'type': 'string', 'children': [], 'value': "'CACHE_TTL'"}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'json_response'}, {'id': '281', 'type': 'return_statement', 'children': ['282']}; {'id': '282', 'type': 'call', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'Response'}; {'id': '284', 'type': 'argument_list', 'children': ['285']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'json_response'} | def get_movie(tmdb_id):
redis_key = 'm_%s' % tmdb_id
cached = redis_ro_conn.get(redis_key)
if cached:
return Response(cached)
else:
try:
details = get_on_tmdb(u'/movie/%d' % tmdb_id)
cast = get_on_tmdb(u'/movie/%d/casts' % tmdb_id)
alternative = get_on_tmdb(u'/movie/%d/alternative_titles' % tmdb_id)
except requests.HTTPError as err:
return Response('TMDB API error: %s' % str(err), status=err.response.status_code)
movie = {'title': details['original_title'],
'score': details['popularity'],
'directors': [x['name'] for x in cast['crew'] if x['department'] == 'Directing' and x['job'] == 'Director'],
'writers': [x['name'] for x in cast['crew'] if x['department'] == 'Writing'],
'cast': [x['name'] for x in cast['cast']],
'genres': [x['name'] for x in details['genres']],
'countries': [x['name'] for x in details['production_countries']],
'tmdb_votes': int(round(details.get('vote_average', 0) * 0.5)),
'_tmdb_id': tmdb_id}
if details.get('release_date'):
movie['year'] = datetime.strptime(details['release_date'], '%Y-%m-%d').year
if details.get('belongs_to_collection'):
movie['collection'] = details['belongs_to_collection']['name']
for alt in alternative['titles']:
movie['title_%s' % alt['iso_3166_1'].lower()] = alt['title']
json_response = json.dumps({'movie': movie})
redis_conn.setex(redis_key, app.config['CACHE_TTL'], json_response)
return Response(json_response) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '13']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'parse_pattern'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'format_string'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'env'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'wrapper'}; {'id': '8', 'type': 'lambda', 'children': ['9', '12']}, {'id': '9', 'type': 'lambda_parameters', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'y'}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'y'}, {'id': '13', 'type': 'block', 'children': ['14', '20', '41', '45', '136']}; {'id': '14', 'type': 'expression_statement', 'children': ['15']}, {'id': '15', 'type': 'assignment', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'formatter'}, {'id': '17', 'type': 'call', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'Formatter'}, {'id': '19', 'type': 'argument_list', 'children': []}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'assignment', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'fields'}, {'id': '23', 'type': 'list_comprehension', 'children': ['24', '27', '35']}; {'id': '24', 'type': 'subscript', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '26', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '27', 'type': 'for_in_clause', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '29', 'type': 'call', 'children': ['30', '33']}; {'id': '30', 'type': 'attribute', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'formatter'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'parse'}, {'id': '33', 'type': 'argument_list', 'children': ['34']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'format_string'}, {'id': '35', 'type': 'if_clause', 'children': ['36']}; {'id': '36', 'type': 'comparison_operator', 'children': ['37', '40'], 'value': 'is'}, {'id': '37', 'type': 'subscript', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '39', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '40', 'type': 'None', 'children': []}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'prepared_env'}; {'id': '44', 'type': 'dictionary', 'children': []}, {'id': '45', 'type': 'for_statement', 'children': ['46', '47', '48']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'fields'}; {'id': '48', 'type': 'block', 'children': ['49', '113', '126']}, {'id': '49', 'type': 'for_statement', 'children': ['50', '51', '65', '107']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'field_alt'}, {'id': '51', 'type': 'generator_expression', 'children': ['52', '57']}; {'id': '52', 'type': 'call', 'children': ['53', '56']}, {'id': '53', 'type': 'attribute', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'strip'}; {'id': '56', 'type': 'argument_list', 'children': []}, {'id': '57', 'type': 'for_in_clause', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '59', 'type': 'call', 'children': ['60', '63']}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'field'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '63', 'type': 'argument_list', 'children': ['64']}; {'id': '64', 'type': 'string', 'children': [], 'value': "'|'"}, {'id': '65', 'type': 'block', 'children': ['66', '101']}; {'id': '66', 'type': 'if_statement', 'children': ['67', '79', '90']}, {'id': '67', 'type': 'boolean_operator', 'children': ['68', '73'], 'value': 'and'}; {'id': '68', 'type': 'comparison_operator', 'children': ['69', '72'], 'value': 'in'}, {'id': '69', 'type': 'subscript', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'field_alt'}, {'id': '71', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '72', 'type': 'string', 'children': [], 'value': '\'\\\'"\''}, {'id': '73', 'type': 'comparison_operator', 'children': ['74', '78'], 'value': 'in'}; {'id': '74', 'type': 'subscript', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'field_alt'}; {'id': '76', 'type': 'unary_operator', 'children': ['77'], 'value': '-'}, {'id': '77', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '78', 'type': 'string', 'children': [], 'value': '\'\\\'"\''}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'field_values'}, {'id': '83', 'type': 'subscript', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'field_alt'}, {'id': '85', 'type': 'slice', 'children': ['86', '87', '88']}; {'id': '86', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '87', 'type': 'colon', 'children': []}; {'id': '88', 'type': 'unary_operator', 'children': ['89'], 'value': '-'}, {'id': '89', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '90', 'type': 'else_clause', 'children': ['91']}, {'id': '91', 'type': 'block', 'children': ['92']}; {'id': '92', 'type': 'expression_statement', 'children': ['93']}, {'id': '93', 'type': 'assignment', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'field_values'}, {'id': '95', 'type': 'call', 'children': ['96', '99']}; {'id': '96', 'type': 'attribute', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'env'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '99', 'type': 'argument_list', 'children': ['100']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'field_alt'}, {'id': '101', 'type': 'if_statement', 'children': ['102', '105']}; {'id': '102', 'type': 'comparison_operator', 'children': ['103', '104'], 'value': 'is'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'field_values'}; {'id': '104', 'type': 'None', 'children': []}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'break_statement', 'children': []}, {'id': '107', 'type': 'else_clause', 'children': ['108']}; {'id': '108', 'type': 'block', 'children': ['109']}, {'id': '109', 'type': 'expression_statement', 'children': ['110']}; {'id': '110', 'type': 'assignment', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'field_values'}; {'id': '112', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '113', 'type': 'if_statement', 'children': ['114', '120']}; {'id': '114', 'type': 'not_operator', 'children': ['115']}, {'id': '115', 'type': 'call', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'isinstance'}, {'id': '117', 'type': 'argument_list', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'field_values'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '120', 'type': 'block', 'children': ['121']}, {'id': '121', 'type': 'expression_statement', 'children': ['122']}; {'id': '122', 'type': 'assignment', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'field_values'}; {'id': '124', 'type': 'list', 'children': ['125'], 'value': '[field_values]'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'field_values'}; {'id': '126', 'type': 'expression_statement', 'children': ['127']}, {'id': '127', 'type': 'assignment', 'children': ['128', '131']}; {'id': '128', 'type': 'subscript', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'prepared_env'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'field'}, {'id': '131', 'type': 'call', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'wrapper'}, {'id': '133', 'type': 'argument_list', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'field_alt'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'field_values'}; {'id': '136', 'type': 'return_statement', 'children': ['137']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'prepared_env'} | def parse_pattern(format_string, env, wrapper=lambda x, y: y):
formatter = Formatter()
fields = [x[1] for x in formatter.parse(format_string) if x[1] is not None]
prepared_env = {}
for field in fields:
for field_alt in (x.strip() for x in field.split('|')):
if field_alt[0] in '\'"' and field_alt[-1] in '\'"':
field_values = field_alt[1:-1]
else:
field_values = env.get(field_alt)
if field_values is not None:
break
else:
field_values = []
if not isinstance(field_values, list):
field_values = [field_values]
prepared_env[field] = wrapper(field_alt, field_values)
return prepared_env |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '26']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'calibrate_dom'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7', '10', '13', '16', '19', '22']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'dom_id'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'detector'}, {'id': '7', 'type': 'default_parameter', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'livetime'}, {'id': '9', 'type': 'None', 'children': []}; {'id': '10', 'type': 'default_parameter', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'fit_ang_dist'}; {'id': '12', 'type': 'False', 'children': []}, {'id': '13', 'type': 'default_parameter', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'scale_mc_to_data'}, {'id': '15', 'type': 'True', 'children': []}; {'id': '16', 'type': 'default_parameter', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'ad_fit_shape'}; {'id': '18', 'type': 'string', 'children': [], 'value': "'pexp'"}, {'id': '19', 'type': 'default_parameter', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'fit_background'}, {'id': '21', 'type': 'True', 'children': []}; {'id': '22', 'type': 'default_parameter', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'ctmin'}; {'id': '24', 'type': 'unary_operator', 'children': ['25'], 'value': '-'}, {'id': '25', 'type': 'float', 'children': [], 'value': '1.'}; {'id': '26', 'type': 'block', 'children': ['27', '89', '108', '116', '125', '133', '141', '149', '175', '195', '199', '290', '308', '317', '326', '336', '347', '358', '368', '379', '388', '455']}, {'id': '27', 'type': 'if_statement', 'children': ['28', '33']}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '30', 'type': 'argument_list', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '33', 'type': 'block', 'children': ['34', '38', '48']}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'filename'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '38', 'type': 'expression_statement', 'children': ['39']}, {'id': '39', 'type': 'assignment', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'loaders'}, {'id': '41', 'type': 'dictionary', 'children': ['42', '45']}; {'id': '42', 'type': 'pair', 'children': ['43', '44']}, {'id': '43', 'type': 'string', 'children': [], 'value': "'.h5'"}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'load_k40_coincidences_from_hdf5'}, {'id': '45', 'type': 'pair', 'children': ['46', '47']}; {'id': '46', 'type': 'string', 'children': [], 'value': "'.root'"}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'load_k40_coincidences_from_rootfile'}; {'id': '48', 'type': 'try_statement', 'children': ['49', '65', '77']}, {'id': '49', 'type': 'block', 'children': ['50']}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'loader'}, {'id': '53', 'type': 'subscript', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'loaders'}, {'id': '55', 'type': 'subscript', 'children': ['56', '64']}; {'id': '56', 'type': 'call', 'children': ['57', '62']}, {'id': '57', 'type': 'attribute', 'children': ['58', '61']}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'os'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'path'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'splitext'}; {'id': '62', 'type': 'argument_list', 'children': ['63']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'filename'}; {'id': '64', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '65', 'type': 'except_clause', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '67', 'type': 'block', 'children': ['68', '75']}; {'id': '68', 'type': 'expression_statement', 'children': ['69']}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'log'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'critical'}, {'id': '73', 'type': 'argument_list', 'children': ['74']}; {'id': '74', 'type': 'string', 'children': [], 'value': "'File format not supported.'"}, {'id': '75', 'type': 'raise_statement', 'children': ['76']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'IOError'}, {'id': '77', 'type': 'else_clause', 'children': ['78']}; {'id': '78', 'type': 'block', 'children': ['79']}, {'id': '79', 'type': 'expression_statement', 'children': ['80']}; {'id': '80', 'type': 'assignment', 'children': ['81', '84']}, {'id': '81', 'type': 'pattern_list', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'livetime'}; {'id': '84', 'type': 'call', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'loader'}; {'id': '86', 'type': 'argument_list', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'filename'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'dom_id'}, {'id': '89', 'type': 'expression_statement', 'children': ['90']}; {'id': '90', 'type': 'assignment', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '92', 'type': 'call', 'children': ['93', '96']}, {'id': '93', 'type': 'attribute', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'array'}; {'id': '96', 'type': 'argument_list', 'children': ['97']}, {'id': '97', 'type': 'call', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '99', 'type': 'argument_list', 'children': ['100']}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'combinations'}; {'id': '102', 'type': 'argument_list', 'children': ['103', '107']}, {'id': '103', 'type': 'call', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '105', 'type': 'argument_list', 'children': ['106']}; {'id': '106', 'type': 'integer', 'children': [], 'value': '31'}, {'id': '107', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'calculate_angles'}, {'id': '113', 'type': 'argument_list', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'detector'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '116', 'type': 'expression_statement', 'children': ['117']}, {'id': '117', 'type': 'assignment', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'cos_angles'}, {'id': '119', 'type': 'call', 'children': ['120', '123']}; {'id': '120', 'type': 'attribute', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'cos'}, {'id': '123', 'type': 'argument_list', 'children': ['124']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '125', 'type': 'expression_statement', 'children': ['126']}; {'id': '126', 'type': 'assignment', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'angles'}; {'id': '128', 'type': 'subscript', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'angles'}; {'id': '130', 'type': 'comparison_operator', 'children': ['131', '132'], 'value': '>='}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'cos_angles'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'ctmin'}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'assignment', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '136', 'type': 'subscript', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '138', 'type': 'comparison_operator', 'children': ['139', '140'], 'value': '>='}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'cos_angles'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'ctmin'}, {'id': '141', 'type': 'expression_statement', 'children': ['142']}; {'id': '142', 'type': 'assignment', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '144', 'type': 'subscript', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '146', 'type': 'comparison_operator', 'children': ['147', '148'], 'value': '>='}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'cos_angles'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'ctmin'}, {'id': '149', 'type': 'try_statement', 'children': ['150', '171']}; {'id': '150', 'type': 'block', 'children': ['151', '162']}, {'id': '151', 'type': 'expression_statement', 'children': ['152']}; {'id': '152', 'type': 'assignment', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'fit_res'}; {'id': '154', 'type': 'call', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'fit_delta_ts'}; {'id': '156', 'type': 'argument_list', 'children': ['157', '158', '159']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'livetime'}, {'id': '159', 'type': 'keyword_argument', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'fit_background'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'fit_background'}; {'id': '162', 'type': 'expression_statement', 'children': ['163']}, {'id': '163', 'type': 'assignment', 'children': ['164', '170']}; {'id': '164', 'type': 'pattern_list', 'children': ['165', '166', '167', '168', '169']}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'rates'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'means'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'sigmas'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'popts'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'pcovs'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'fit_res'}, {'id': '171', 'type': 'except_clause', 'children': ['172']}; {'id': '172', 'type': 'block', 'children': ['173']}, {'id': '173', 'type': 'return_statement', 'children': ['174']}; {'id': '174', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '175', 'type': 'expression_statement', 'children': ['176']}; {'id': '176', 'type': 'assignment', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'rate_errors'}; {'id': '178', 'type': 'call', 'children': ['179', '182']}, {'id': '179', 'type': 'attribute', 'children': ['180', '181']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'array'}; {'id': '182', 'type': 'argument_list', 'children': ['183']}, {'id': '183', 'type': 'list_comprehension', 'children': ['184', '192']}; {'id': '184', 'type': 'subscript', 'children': ['185', '191']}, {'id': '185', 'type': 'call', 'children': ['186', '189']}; {'id': '186', 'type': 'attribute', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'diag'}, {'id': '189', 'type': 'argument_list', 'children': ['190']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'pc'}, {'id': '191', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '192', 'type': 'for_in_clause', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'pc'}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'pcovs'}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'assignment', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'scale_factor'}; {'id': '198', 'type': 'None', 'children': []}, {'id': '199', 'type': 'if_statement', 'children': ['200', '201', '221']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'fit_ang_dist'}, {'id': '201', 'type': 'block', 'children': ['202', '214']}; {'id': '202', 'type': 'expression_statement', 'children': ['203']}, {'id': '203', 'type': 'assignment', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'fit_res'}, {'id': '205', 'type': 'call', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'fit_angular_distribution'}, {'id': '207', 'type': 'argument_list', 'children': ['208', '209', '210', '211']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'rates'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'rate_errors'}, {'id': '211', 'type': 'keyword_argument', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'shape'}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'ad_fit_shape'}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'assignment', 'children': ['216', '220']}; {'id': '216', 'type': 'pattern_list', 'children': ['217', '218', '219']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'exp_popts'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'exp_pcov'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'fit_res'}, {'id': '221', 'type': 'else_clause', 'children': ['222']}; {'id': '222', 'type': 'block', 'children': ['223', '237', '271', '277', '281', '285']}, {'id': '223', 'type': 'expression_statement', 'children': ['224']}; {'id': '224', 'type': 'assignment', 'children': ['225', '226']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'mc_fitted_rates'}; {'id': '226', 'type': 'call', 'children': ['227', '228']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'exponential_polinomial'}; {'id': '228', 'type': 'argument_list', 'children': ['229', '235']}, {'id': '229', 'type': 'call', 'children': ['230', '233']}; {'id': '230', 'type': 'attribute', 'children': ['231', '232']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'cos'}, {'id': '233', 'type': 'argument_list', 'children': ['234']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '235', 'type': 'list_splat', 'children': ['236']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'MC_ANG_DIST'}, {'id': '237', 'type': 'if_statement', 'children': ['238', '239', '265']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'scale_mc_to_data'}, {'id': '239', 'type': 'block', 'children': ['240']}; {'id': '240', 'type': 'expression_statement', 'children': ['241']}, {'id': '241', 'type': 'assignment', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'scale_factor'}, {'id': '243', 'type': 'binary_operator', 'children': ['244', '254', '255'], 'value': '/'}; {'id': '244', 'type': 'call', 'children': ['245', '248']}, {'id': '245', 'type': 'attribute', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'mean'}; {'id': '248', 'type': 'argument_list', 'children': ['249']}, {'id': '249', 'type': 'subscript', 'children': ['250', '251']}; {'id': '250', 'type': 'identifier', 'children': [], 'value': 'rates'}, {'id': '251', 'type': 'comparison_operator', 'children': ['252', '253'], 'value': '<'}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '253', 'type': 'float', 'children': [], 'value': '1.5'}; {'id': '254', 'type': 'line_continuation', 'children': [], 'value': '\\'}, {'id': '255', 'type': 'call', 'children': ['256', '259']}; {'id': '256', 'type': 'attribute', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'np'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'mean'}, {'id': '259', 'type': 'argument_list', 'children': ['260']}; {'id': '260', 'type': 'subscript', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'mc_fitted_rates'}; {'id': '262', 'type': 'comparison_operator', 'children': ['263', '264'], 'value': '<'}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'angles'}; {'id': '264', 'type': 'float', 'children': [], 'value': '1.5'}, {'id': '265', 'type': 'else_clause', 'children': ['266']}; {'id': '266', 'type': 'block', 'children': ['267']}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'assignment', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'scale_factor'}; {'id': '270', 'type': 'float', 'children': [], 'value': '1.'}, {'id': '271', 'type': 'expression_statement', 'children': ['272']}; {'id': '272', 'type': 'assignment', 'children': ['273', '274']}, {'id': '273', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}; {'id': '274', 'type': 'binary_operator', 'children': ['275', '276'], 'value': '*'}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'mc_fitted_rates'}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'scale_factor'}, {'id': '277', 'type': 'expression_statement', 'children': ['278']}; {'id': '278', 'type': 'assignment', 'children': ['279', '280']}, {'id': '279', 'type': 'identifier', 'children': [], 'value': 'exp_popts'}; {'id': '280', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '281', 'type': 'expression_statement', 'children': ['282']}; {'id': '282', 'type': 'assignment', 'children': ['283', '284']}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'exp_pcov'}; {'id': '284', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '285', 'type': 'expression_statement', 'children': ['286']}; {'id': '286', 'type': 'call', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'print'}; {'id': '288', 'type': 'argument_list', 'children': ['289']}, {'id': '289', 'type': 'string', 'children': [], 'value': "'Using angular distribution from Monte Carlo'"}; {'id': '290', 'type': 'if_statement', 'children': ['291', '293', '302']}, {'id': '291', 'type': 'not_operator', 'children': ['292']}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'fit_background'}, {'id': '293', 'type': 'block', 'children': ['294']}; {'id': '294', 'type': 'expression_statement', 'children': ['295']}, {'id': '295', 'type': 'assignment', 'children': ['296', '297']}; {'id': '296', 'type': 'identifier', 'children': [], 'value': 'minimize_weights'}, {'id': '297', 'type': 'call', 'children': ['298', '299']}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'calculate_weights'}, {'id': '299', 'type': 'argument_list', 'children': ['300', '301']}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'data'}; {'id': '302', 'type': 'else_clause', 'children': ['303']}, {'id': '303', 'type': 'block', 'children': ['304']}; {'id': '304', 'type': 'expression_statement', 'children': ['305']}, {'id': '305', 'type': 'assignment', 'children': ['306', '307']}; {'id': '306', 'type': 'identifier', 'children': [], 'value': 'minimize_weights'}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}; {'id': '308', 'type': 'expression_statement', 'children': ['309']}, {'id': '309', 'type': 'assignment', 'children': ['310', '311']}; {'id': '310', 'type': 'identifier', 'children': [], 'value': 'opt_t0s'}, {'id': '311', 'type': 'call', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'minimize_t0s'}, {'id': '313', 'type': 'argument_list', 'children': ['314', '315', '316']}; {'id': '314', 'type': 'identifier', 'children': [], 'value': 'means'}, {'id': '315', 'type': 'identifier', 'children': [], 'value': 'minimize_weights'}; {'id': '316', 'type': 'identifier', 'children': [], 'value': 'combs'}, {'id': '317', 'type': 'expression_statement', 'children': ['318']}; {'id': '318', 'type': 'assignment', 'children': ['319', '320']}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'opt_sigmas'}; {'id': '320', 'type': 'call', 'children': ['321', '322']}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'minimize_sigmas'}; {'id': '322', 'type': 'argument_list', 'children': ['323', '324', '325']}, {'id': '323', 'type': 'identifier', 'children': [], 'value': 'sigmas'}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'minimize_weights'}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '326', 'type': 'expression_statement', 'children': ['327']}, {'id': '327', 'type': 'assignment', 'children': ['328', '329']}; {'id': '328', 'type': 'identifier', 'children': [], 'value': 'opt_qes'}, {'id': '329', 'type': 'call', 'children': ['330', '331']}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'minimize_qes'}, {'id': '331', 'type': 'argument_list', 'children': ['332', '333', '334', '335']}; {'id': '332', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}, {'id': '333', 'type': 'identifier', 'children': [], 'value': 'rates'}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'minimize_weights'}, {'id': '335', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '336', 'type': 'expression_statement', 'children': ['337']}, {'id': '337', 'type': 'assignment', 'children': ['338', '339']}; {'id': '338', 'type': 'identifier', 'children': [], 'value': 'corrected_means'}, {'id': '339', 'type': 'call', 'children': ['340', '341']}; {'id': '340', 'type': 'identifier', 'children': [], 'value': 'correct_means'}, {'id': '341', 'type': 'argument_list', 'children': ['342', '343', '346']}; {'id': '342', 'type': 'identifier', 'children': [], 'value': 'means'}, {'id': '343', 'type': 'attribute', 'children': ['344', '345']}; {'id': '344', 'type': 'identifier', 'children': [], 'value': 'opt_t0s'}, {'id': '345', 'type': 'identifier', 'children': [], 'value': 'x'}; {'id': '346', 'type': 'identifier', 'children': [], 'value': 'combs'}, {'id': '347', 'type': 'expression_statement', 'children': ['348']}; {'id': '348', 'type': 'assignment', 'children': ['349', '350']}, {'id': '349', 'type': 'identifier', 'children': [], 'value': 'corrected_rates'}; {'id': '350', 'type': 'call', 'children': ['351', '352']}, {'id': '351', 'type': 'identifier', 'children': [], 'value': 'correct_rates'}; {'id': '352', 'type': 'argument_list', 'children': ['353', '354', '357']}, {'id': '353', 'type': 'identifier', 'children': [], 'value': 'rates'}; {'id': '354', 'type': 'attribute', 'children': ['355', '356']}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'opt_qes'}; {'id': '356', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '357', 'type': 'identifier', 'children': [], 'value': 'combs'}; {'id': '358', 'type': 'expression_statement', 'children': ['359']}, {'id': '359', 'type': 'assignment', 'children': ['360', '363']}; {'id': '360', 'type': 'pattern_list', 'children': ['361', '362']}, {'id': '361', 'type': 'identifier', 'children': [], 'value': 'rms_means'}; {'id': '362', 'type': 'identifier', 'children': [], 'value': 'rms_corrected_means'}, {'id': '363', 'type': 'call', 'children': ['364', '365']}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'calculate_rms_means'}, {'id': '365', 'type': 'argument_list', 'children': ['366', '367']}; {'id': '366', 'type': 'identifier', 'children': [], 'value': 'means'}, {'id': '367', 'type': 'identifier', 'children': [], 'value': 'corrected_means'}; {'id': '368', 'type': 'expression_statement', 'children': ['369']}, {'id': '369', 'type': 'assignment', 'children': ['370', '373']}; {'id': '370', 'type': 'pattern_list', 'children': ['371', '372']}, {'id': '371', 'type': 'identifier', 'children': [], 'value': 'rms_rates'}; {'id': '372', 'type': 'identifier', 'children': [], 'value': 'rms_corrected_rates'}, {'id': '373', 'type': 'call', 'children': ['374', '375']}; {'id': '374', 'type': 'identifier', 'children': [], 'value': 'calculate_rms_rates'}, {'id': '375', 'type': 'argument_list', 'children': ['376', '377', '378']}; {'id': '376', 'type': 'identifier', 'children': [], 'value': 'rates'}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}; {'id': '378', 'type': 'identifier', 'children': [], 'value': 'corrected_rates'}, {'id': '379', 'type': 'expression_statement', 'children': ['380']}; {'id': '380', 'type': 'assignment', 'children': ['381', '382']}, {'id': '381', 'type': 'identifier', 'children': [], 'value': 'cos_angles'}; {'id': '382', 'type': 'call', 'children': ['383', '386']}, {'id': '383', 'type': 'attribute', 'children': ['384', '385']}; {'id': '384', 'type': 'identifier', 'children': [], 'value': 'np'}, {'id': '385', 'type': 'identifier', 'children': [], 'value': 'cos'}; {'id': '386', 'type': 'argument_list', 'children': ['387']}, {'id': '387', 'type': 'identifier', 'children': [], 'value': 'angles'}; {'id': '388', 'type': 'expression_statement', 'children': ['389']}, {'id': '389', 'type': 'assignment', 'children': ['390', '391']}; {'id': '390', 'type': 'identifier', 'children': [], 'value': 'return_data'}, {'id': '391', 'type': 'dictionary', 'children': ['392', '395', '398', '401', '404', '407', '410', '413', '416', '419', '422', '425', '428', '431', '434', '437', '440', '443', '446', '449', '452']}; {'id': '392', 'type': 'pair', 'children': ['393', '394']}, {'id': '393', 'type': 'string', 'children': [], 'value': "'opt_t0s'"}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'opt_t0s'}, {'id': '395', 'type': 'pair', 'children': ['396', '397']}; {'id': '396', 'type': 'string', 'children': [], 'value': "'opt_qes'"}, {'id': '397', 'type': 'identifier', 'children': [], 'value': 'opt_qes'}; {'id': '398', 'type': 'pair', 'children': ['399', '400']}, {'id': '399', 'type': 'string', 'children': [], 'value': "'data'"}; {'id': '400', 'type': 'identifier', 'children': [], 'value': 'data'}, {'id': '401', 'type': 'pair', 'children': ['402', '403']}; {'id': '402', 'type': 'string', 'children': [], 'value': "'means'"}, {'id': '403', 'type': 'identifier', 'children': [], 'value': 'means'}; {'id': '404', 'type': 'pair', 'children': ['405', '406']}, {'id': '405', 'type': 'string', 'children': [], 'value': "'rates'"}; {'id': '406', 'type': 'identifier', 'children': [], 'value': 'rates'}, {'id': '407', 'type': 'pair', 'children': ['408', '409']}; {'id': '408', 'type': 'string', 'children': [], 'value': "'fitted_rates'"}, {'id': '409', 'type': 'identifier', 'children': [], 'value': 'fitted_rates'}; {'id': '410', 'type': 'pair', 'children': ['411', '412']}, {'id': '411', 'type': 'string', 'children': [], 'value': "'angles'"}; {'id': '412', 'type': 'identifier', 'children': [], 'value': 'angles'}, {'id': '413', 'type': 'pair', 'children': ['414', '415']}; {'id': '414', 'type': 'string', 'children': [], 'value': "'corrected_means'"}, {'id': '415', 'type': 'identifier', 'children': [], 'value': 'corrected_means'}; {'id': '416', 'type': 'pair', 'children': ['417', '418']}, {'id': '417', 'type': 'string', 'children': [], 'value': "'corrected_rates'"}; {'id': '418', 'type': 'identifier', 'children': [], 'value': 'corrected_rates'}, {'id': '419', 'type': 'pair', 'children': ['420', '421']}; {'id': '420', 'type': 'string', 'children': [], 'value': "'rms_means'"}, {'id': '421', 'type': 'identifier', 'children': [], 'value': 'rms_means'}; {'id': '422', 'type': 'pair', 'children': ['423', '424']}, {'id': '423', 'type': 'string', 'children': [], 'value': "'rms_corrected_means'"}; {'id': '424', 'type': 'identifier', 'children': [], 'value': 'rms_corrected_means'}, {'id': '425', 'type': 'pair', 'children': ['426', '427']}; {'id': '426', 'type': 'string', 'children': [], 'value': "'rms_rates'"}, {'id': '427', 'type': 'identifier', 'children': [], 'value': 'rms_rates'}; {'id': '428', 'type': 'pair', 'children': ['429', '430']}, {'id': '429', 'type': 'string', 'children': [], 'value': "'rms_corrected_rates'"}; {'id': '430', 'type': 'identifier', 'children': [], 'value': 'rms_corrected_rates'}, {'id': '431', 'type': 'pair', 'children': ['432', '433']}; {'id': '432', 'type': 'string', 'children': [], 'value': "'gaussian_popts'"}, {'id': '433', 'type': 'identifier', 'children': [], 'value': 'popts'}; {'id': '434', 'type': 'pair', 'children': ['435', '436']}, {'id': '435', 'type': 'string', 'children': [], 'value': "'livetime'"}; {'id': '436', 'type': 'identifier', 'children': [], 'value': 'livetime'}, {'id': '437', 'type': 'pair', 'children': ['438', '439']}; {'id': '438', 'type': 'string', 'children': [], 'value': "'exp_popts'"}, {'id': '439', 'type': 'identifier', 'children': [], 'value': 'exp_popts'}; {'id': '440', 'type': 'pair', 'children': ['441', '442']}, {'id': '441', 'type': 'string', 'children': [], 'value': "'exp_pcov'"}; {'id': '442', 'type': 'identifier', 'children': [], 'value': 'exp_pcov'}, {'id': '443', 'type': 'pair', 'children': ['444', '445']}; {'id': '444', 'type': 'string', 'children': [], 'value': "'scale_factor'"}, {'id': '445', 'type': 'identifier', 'children': [], 'value': 'scale_factor'}; {'id': '446', 'type': 'pair', 'children': ['447', '448']}, {'id': '447', 'type': 'string', 'children': [], 'value': "'opt_sigmas'"}; {'id': '448', 'type': 'identifier', 'children': [], 'value': 'opt_sigmas'}, {'id': '449', 'type': 'pair', 'children': ['450', '451']}; {'id': '450', 'type': 'string', 'children': [], 'value': "'sigmas'"}, {'id': '451', 'type': 'identifier', 'children': [], 'value': 'sigmas'}; {'id': '452', 'type': 'pair', 'children': ['453', '454']}, {'id': '453', 'type': 'string', 'children': [], 'value': "'combs'"}; {'id': '454', 'type': 'identifier', 'children': [], 'value': 'combs'}, {'id': '455', 'type': 'return_statement', 'children': ['456']} | def calibrate_dom(
dom_id,
data,
detector,
livetime=None,
fit_ang_dist=False,
scale_mc_to_data=True,
ad_fit_shape='pexp',
fit_background=True,
ctmin=-1.
):
if isinstance(data, str):
filename = data
loaders = {
'.h5': load_k40_coincidences_from_hdf5,
'.root': load_k40_coincidences_from_rootfile
}
try:
loader = loaders[os.path.splitext(filename)[1]]
except KeyError:
log.critical('File format not supported.')
raise IOError
else:
data, livetime = loader(filename, dom_id)
combs = np.array(list(combinations(range(31), 2)))
angles = calculate_angles(detector, combs)
cos_angles = np.cos(angles)
angles = angles[cos_angles >= ctmin]
data = data[cos_angles >= ctmin]
combs = combs[cos_angles >= ctmin]
try:
fit_res = fit_delta_ts(data, livetime, fit_background=fit_background)
rates, means, sigmas, popts, pcovs = fit_res
except:
return 0
rate_errors = np.array([np.diag(pc)[2] for pc in pcovs])
scale_factor = None
if fit_ang_dist:
fit_res = fit_angular_distribution(
angles, rates, rate_errors, shape=ad_fit_shape
)
fitted_rates, exp_popts, exp_pcov = fit_res
else:
mc_fitted_rates = exponential_polinomial(np.cos(angles), *MC_ANG_DIST)
if scale_mc_to_data:
scale_factor = np.mean(rates[angles < 1.5]) / \
np.mean(mc_fitted_rates[angles < 1.5])
else:
scale_factor = 1.
fitted_rates = mc_fitted_rates * scale_factor
exp_popts = []
exp_pcov = []
print('Using angular distribution from Monte Carlo')
if not fit_background:
minimize_weights = calculate_weights(fitted_rates, data)
else:
minimize_weights = fitted_rates
opt_t0s = minimize_t0s(means, minimize_weights, combs)
opt_sigmas = minimize_sigmas(sigmas, minimize_weights, combs)
opt_qes = minimize_qes(fitted_rates, rates, minimize_weights, combs)
corrected_means = correct_means(means, opt_t0s.x, combs)
corrected_rates = correct_rates(rates, opt_qes.x, combs)
rms_means, rms_corrected_means = calculate_rms_means(
means, corrected_means
)
rms_rates, rms_corrected_rates = calculate_rms_rates(
rates, fitted_rates, corrected_rates
)
cos_angles = np.cos(angles)
return_data = {
'opt_t0s': opt_t0s,
'opt_qes': opt_qes,
'data': data,
'means': means,
'rates': rates,
'fitted_rates': fitted_rates,
'angles': angles,
'corrected_means': corrected_means,
'corrected_rates': corrected_rates,
'rms_means': rms_means,
'rms_corrected_means': rms_corrected_means,
'rms_rates': rms_rates,
'rms_corrected_rates': rms_corrected_rates,
'gaussian_popts': popts,
'livetime': livetime,
'exp_popts': exp_popts,
'exp_pcov': exp_pcov,
'scale_factor': scale_factor,
'opt_sigmas': opt_sigmas,
'sigmas': sigmas,
'combs': combs
}
return return_data |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '17']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'analyze'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'analysis'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'lookup'}; {'id': '8', 'type': 'call', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'dict'}; {'id': '10', 'type': 'argument_list', 'children': ['11', '14']}, {'id': '11', 'type': 'keyword_argument', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'bipa'}, {'id': '13', 'type': 'dictionary', 'children': []}; {'id': '14', 'type': 'keyword_argument', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'dolgo'}; {'id': '16', 'type': 'dictionary', 'children': []}, {'id': '17', 'type': 'block', 'children': ['18', '27', '46', '146', '177', '252']}; {'id': '18', 'type': 'if_statement', 'children': ['19', '21']}, {'id': '19', 'type': 'not_operator', 'children': ['20']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '21', 'type': 'block', 'children': ['22']}; {'id': '22', 'type': 'raise_statement', 'children': ['23']}, {'id': '23', 'type': 'call', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'ValueError'}, {'id': '25', 'type': 'argument_list', 'children': ['26']}; {'id': '26', 'type': 'string', 'children': [], 'value': "'Empty sequence.'"}, {'id': '27', 'type': 'if_statement', 'children': ['28', '40']}; {'id': '28', 'type': 'not_operator', 'children': ['29']}, {'id': '29', 'type': 'list_comprehension', 'children': ['30', '31', '34']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'segment'}, {'id': '31', 'type': 'for_in_clause', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'segment'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'segments'}; {'id': '34', 'type': 'if_clause', 'children': ['35']}, {'id': '35', 'type': 'call', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'segment'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'strip'}, {'id': '39', 'type': 'argument_list', 'children': []}; {'id': '40', 'type': 'block', 'children': ['41']}, {'id': '41', 'type': 'raise_statement', 'children': ['42']}; {'id': '42', 'type': 'call', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '44', 'type': 'argument_list', 'children': ['45']}, {'id': '45', 'type': 'string', 'children': [], 'value': "'No information in the sequence.'"}; {'id': '46', 'type': 'try_statement', 'children': ['47', '138']}, {'id': '47', 'type': 'block', 'children': ['48', '56']}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '53']}; {'id': '50', 'type': 'pattern_list', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'bipa_analysis'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'sc_analysis'}, {'id': '53', 'type': 'expression_list', 'children': ['54', '55']}; {'id': '54', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '55', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '56', 'type': 'for_statement', 'children': ['57', '58', '59']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '59', 'type': 'block', 'children': ['60', '71', '90', '97', '108', '131']}; {'id': '60', 'type': 'expression_statement', 'children': ['61']}, {'id': '61', 'type': 'assignment', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'a'}, {'id': '63', 'type': 'call', 'children': ['64', '69']}; {'id': '64', 'type': 'attribute', 'children': ['65', '68']}, {'id': '65', 'type': 'subscript', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'lookup'}, {'id': '67', 'type': 'string', 'children': [], 'value': "'bipa'"}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '71', 'type': 'if_statement', 'children': ['72', '75']}; {'id': '72', 'type': 'comparison_operator', 'children': ['73', '74'], 'value': 'is'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'a'}; {'id': '74', 'type': 'None', 'children': []}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'expression_statement', 'children': ['77']}, {'id': '77', 'type': 'assignment', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'a'}, {'id': '79', 'type': 'call', 'children': ['80', '85']}; {'id': '80', 'type': 'attribute', 'children': ['81', '84']}, {'id': '81', 'type': 'subscript', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'lookup'}, {'id': '83', 'type': 'string', 'children': [], 'value': "'bipa'"}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'setdefault'}, {'id': '85', 'type': 'argument_list', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '87', 'type': 'subscript', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'BIPA'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'call', 'children': ['92', '95']}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'bipa_analysis'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'a'}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'sc'}; {'id': '100', 'type': 'call', 'children': ['101', '106']}, {'id': '101', 'type': 'attribute', 'children': ['102', '105']}; {'id': '102', 'type': 'subscript', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'lookup'}; {'id': '104', 'type': 'string', 'children': [], 'value': "'dolgo'"}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '106', 'type': 'argument_list', 'children': ['107']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '108', 'type': 'if_statement', 'children': ['109', '112']}, {'id': '109', 'type': 'comparison_operator', 'children': ['110', '111'], 'value': 'is'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'sc'}, {'id': '111', 'type': 'None', 'children': []}; {'id': '112', 'type': 'block', 'children': ['113']}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'sc'}; {'id': '116', 'type': 'call', 'children': ['117', '122']}, {'id': '117', 'type': 'attribute', 'children': ['118', '121']}; {'id': '118', 'type': 'subscript', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'lookup'}; {'id': '120', 'type': 'string', 'children': [], 'value': "'dolgo'"}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'setdefault'}; {'id': '122', 'type': 'argument_list', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'BIPA'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'translate'}; {'id': '128', 'type': 'argument_list', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'DOLGO'}, {'id': '131', 'type': 'expression_statement', 'children': ['132']}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'sc_analysis'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '136', 'type': 'argument_list', 'children': ['137']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'sc'}; {'id': '138', 'type': 'except_clause', 'children': ['139']}, {'id': '139', 'type': 'block', 'children': ['140', '145']}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '145', 'type': 'raise_statement', 'children': []}; {'id': '146', 'type': 'for_statement', 'children': ['147', '150', '155']}, {'id': '147', 'type': 'pattern_list', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'sound_class'}; {'id': '150', 'type': 'call', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '152', 'type': 'argument_list', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'bipa_analysis'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'sc_analysis'}, {'id': '155', 'type': 'block', 'children': ['156']}; {'id': '156', 'type': 'if_statement', 'children': ['157', '170']}, {'id': '157', 'type': 'boolean_operator', 'children': ['158', '167'], 'value': 'or'}; {'id': '158', 'type': 'call', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '160', 'type': 'argument_list', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}; {'id': '162', 'type': 'attribute', 'children': ['163', '166']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'pyclts'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'models'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'UnknownSound'}, {'id': '167', 'type': 'comparison_operator', 'children': ['168', '169'], 'value': '=='}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'sound_class'}, {'id': '169', 'type': 'string', 'children': [], 'value': "'?'"}; {'id': '170', 'type': 'block', 'children': ['171']}, {'id': '171', 'type': 'expression_statement', 'children': ['172']}; {'id': '172', 'type': 'augmented_assignment', 'children': ['173', '176'], 'value': '+='}, {'id': '173', 'type': 'attribute', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'analysis'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'general_errors'}; {'id': '176', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '177', 'type': 'for_statement', 'children': ['178', '182', '188']}; {'id': '178', 'type': 'pattern_list', 'children': ['179', '180', '181']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'segment'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'sound_class'}; {'id': '182', 'type': 'call', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '184', 'type': 'argument_list', 'children': ['185', '186', '187']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'segments'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'bipa_analysis'}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'sc_analysis'}; {'id': '188', 'type': 'block', 'children': ['189', '199', '238']}, {'id': '189', 'type': 'expression_statement', 'children': ['190']}; {'id': '190', 'type': 'call', 'children': ['191', '196']}, {'id': '191', 'type': 'attribute', 'children': ['192', '195']}; {'id': '192', 'type': 'attribute', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'analysis'}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'update'}; {'id': '196', 'type': 'argument_list', 'children': ['197']}, {'id': '197', 'type': 'list', 'children': ['198'], 'value': '[segment]'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'segment'}, {'id': '199', 'type': 'if_statement', 'children': ['200', '209', '219']}; {'id': '200', 'type': 'call', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '202', 'type': 'argument_list', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}; {'id': '204', 'type': 'attribute', 'children': ['205', '208']}, {'id': '205', 'type': 'attribute', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'pyclts'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'models'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'UnknownSound'}, {'id': '209', 'type': 'block', 'children': ['210']}; {'id': '210', 'type': 'expression_statement', 'children': ['211']}, {'id': '211', 'type': 'call', 'children': ['212', '217']}; {'id': '212', 'type': 'attribute', 'children': ['213', '216']}, {'id': '213', 'type': 'attribute', 'children': ['214', '215']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'analysis'}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'bipa_errors'}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '217', 'type': 'argument_list', 'children': ['218']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'segment'}, {'id': '219', 'type': 'else_clause', 'children': ['220']}; {'id': '220', 'type': 'block', 'children': ['221']}, {'id': '221', 'type': 'expression_statement', 'children': ['222']}; {'id': '222', 'type': 'call', 'children': ['223', '232']}, {'id': '223', 'type': 'attribute', 'children': ['224', '231']}; {'id': '224', 'type': 'subscript', 'children': ['225', '228']}, {'id': '225', 'type': 'attribute', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'analysis'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'replacements'}; {'id': '228', 'type': 'attribute', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'source'}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '232', 'type': 'argument_list', 'children': ['233']}, {'id': '233', 'type': 'call', 'children': ['234', '237']}; {'id': '234', 'type': 'attribute', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'sound_bipa'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': '__unicode__'}, {'id': '237', 'type': 'argument_list', 'children': []}; {'id': '238', 'type': 'if_statement', 'children': ['239', '242']}, {'id': '239', 'type': 'comparison_operator', 'children': ['240', '241'], 'value': '=='}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'sound_class'}, {'id': '241', 'type': 'string', 'children': [], 'value': "'?'"}; {'id': '242', 'type': 'block', 'children': ['243']}, {'id': '243', 'type': 'expression_statement', 'children': ['244']}; {'id': '244', 'type': 'call', 'children': ['245', '250']}, {'id': '245', 'type': 'attribute', 'children': ['246', '249']}; {'id': '246', 'type': 'attribute', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'analysis'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'sclass_errors'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '250', 'type': 'argument_list', 'children': ['251']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'segment'}; {'id': '252', 'type': 'return_statement', 'children': ['253']}, {'id': '253', 'type': 'expression_list', 'children': ['254', '255', '256', '257']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'segments'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'bipa_analysis'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'sc_analysis'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'analysis'} | def analyze(segments, analysis, lookup=dict(bipa={}, dolgo={})):
if not segments:
raise ValueError('Empty sequence.')
if not [segment for segment in segments if segment.strip()]:
raise ValueError('No information in the sequence.')
try:
bipa_analysis, sc_analysis = [], []
for s in segments:
a = lookup['bipa'].get(s)
if a is None:
a = lookup['bipa'].setdefault(s, BIPA[s])
bipa_analysis.append(a)
sc = lookup['dolgo'].get(s)
if sc is None:
sc = lookup['dolgo'].setdefault(s, BIPA.translate(s, DOLGO))
sc_analysis.append(sc)
except:
print(segments)
raise
for sound_bipa, sound_class in zip(bipa_analysis, sc_analysis):
if isinstance(sound_bipa, pyclts.models.UnknownSound) or sound_class == '?':
analysis.general_errors += 1
for segment, sound_bipa, sound_class in zip(segments, bipa_analysis, sc_analysis):
analysis.segments.update([segment])
if isinstance(sound_bipa, pyclts.models.UnknownSound):
analysis.bipa_errors.add(segment)
else:
analysis.replacements[sound_bipa.source].add(sound_bipa.__unicode__())
if sound_class == '?':
analysis.sclass_errors.add(segment)
return segments, bipa_analysis, sc_analysis, analysis |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '15']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'setup_limits'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '12']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'conf_file'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'limits_file'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '8', 'type': 'True', 'children': []}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'dry_run'}, {'id': '11', 'type': 'False', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '14', 'type': 'False', 'children': []}, {'id': '15', 'type': 'block', 'children': ['16', '23', '34', '42', '54', '66', '75', '79', '145', '166', '179', '185', '189', '235', '270']}; {'id': '16', 'type': 'if_statement', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'dry_run'}; {'id': '18', 'type': 'block', 'children': ['19']}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'assignment', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '22', 'type': 'True', 'children': []}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'conf'}; {'id': '26', 'type': 'call', 'children': ['27', '30']}, {'id': '27', 'type': 'attribute', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'Config'}; {'id': '30', 'type': 'argument_list', 'children': ['31']}, {'id': '31', 'type': 'keyword_argument', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'conf_file'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'conf_file'}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '37', 'type': 'call', 'children': ['38', '41']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'conf'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'get_database'}, {'id': '41', 'type': 'argument_list', 'children': []}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'limits_key'}, {'id': '45', 'type': 'call', 'children': ['46', '51']}; {'id': '46', 'type': 'attribute', 'children': ['47', '50']}, {'id': '47', 'type': 'subscript', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'conf'}, {'id': '49', 'type': 'string', 'children': [], 'value': "'control'"}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '51', 'type': 'argument_list', 'children': ['52', '53']}; {'id': '52', 'type': 'string', 'children': [], 'value': "'limits_key'"}, {'id': '53', 'type': 'string', 'children': [], 'value': "'limits'"}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'control_channel'}, {'id': '57', 'type': 'call', 'children': ['58', '63']}; {'id': '58', 'type': 'attribute', 'children': ['59', '62']}, {'id': '59', 'type': 'subscript', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'conf'}, {'id': '61', 'type': 'string', 'children': [], 'value': "'control'"}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '63', 'type': 'argument_list', 'children': ['64', '65']}; {'id': '64', 'type': 'string', 'children': [], 'value': "'channel'"}, {'id': '65', 'type': 'string', 'children': [], 'value': "'control'"}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'assignment', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'limits_tree'}, {'id': '69', 'type': 'call', 'children': ['70', '73']}; {'id': '70', 'type': 'attribute', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'etree'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'parse'}, {'id': '73', 'type': 'argument_list', 'children': ['74']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'limits_file'}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'lims'}; {'id': '78', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '79', 'type': 'for_statement', 'children': ['80', '83', '91']}; {'id': '80', 'type': 'pattern_list', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'lim'}, {'id': '83', 'type': 'call', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'enumerate'}, {'id': '85', 'type': 'argument_list', 'children': ['86']}; {'id': '86', 'type': 'call', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'limits_tree'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'getroot'}; {'id': '90', 'type': 'argument_list', 'children': []}, {'id': '91', 'type': 'block', 'children': ['92', '113']}; {'id': '92', 'type': 'if_statement', 'children': ['93', '98']}, {'id': '93', 'type': 'comparison_operator', 'children': ['94', '97'], 'value': '!='}; {'id': '94', 'type': 'attribute', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'lim'}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'tag'}, {'id': '97', 'type': 'string', 'children': [], 'value': "'limit'"}; {'id': '98', 'type': 'block', 'children': ['99', '112']}, {'id': '99', 'type': 'expression_statement', 'children': ['100']}; {'id': '100', 'type': 'call', 'children': ['101', '104']}, {'id': '101', 'type': 'attribute', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'warnings'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'warn'}; {'id': '104', 'type': 'argument_list', 'children': ['105']}, {'id': '105', 'type': 'binary_operator', 'children': ['106', '107'], 'value': '%'}; {'id': '106', 'type': 'string', 'children': [], 'value': '"Unrecognized tag %r in limits file at index %d"'}, {'id': '107', 'type': 'tuple', 'children': ['108', '111']}; {'id': '108', 'type': 'attribute', 'children': ['109', '110']}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'lim'}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'tag'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '112', 'type': 'continue_statement', 'children': []}, {'id': '113', 'type': 'try_statement', 'children': ['114', '127']}; {'id': '114', 'type': 'block', 'children': ['115']}, {'id': '115', 'type': 'expression_statement', 'children': ['116']}; {'id': '116', 'type': 'call', 'children': ['117', '120']}, {'id': '117', 'type': 'attribute', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'lims'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '120', 'type': 'argument_list', 'children': ['121']}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'parse_limit_node'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '125', '126']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'idx'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'lim'}, {'id': '127', 'type': 'except_clause', 'children': ['128', '132']}; {'id': '128', 'type': 'as_pattern', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'Exception'}; {'id': '130', 'type': 'as_pattern_target', 'children': ['131']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'exc'}; {'id': '132', 'type': 'block', 'children': ['133', '144']}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'call', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'warnings'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'warn'}; {'id': '138', 'type': 'argument_list', 'children': ['139']}, {'id': '139', 'type': 'binary_operator', 'children': ['140', '141'], 'value': '%'}; {'id': '140', 'type': 'string', 'children': [], 'value': '"Couldn\'t understand limit at index %d: %s"'}, {'id': '141', 'type': 'tuple', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'idx'}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'exc'}; {'id': '144', 'type': 'continue_statement', 'children': []}, {'id': '145', 'type': 'if_statement', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '147', 'type': 'block', 'children': ['148', '154']}; {'id': '148', 'type': 'print_statement', 'children': ['149', '153']}, {'id': '149', 'type': 'chevron', 'children': ['150']}; {'id': '150', 'type': 'attribute', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '153', 'type': 'string', 'children': [], 'value': '"Installing the following limits:"'}; {'id': '154', 'type': 'for_statement', 'children': ['155', '156', '157']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'lim'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'lims'}, {'id': '157', 'type': 'block', 'children': ['158']}; {'id': '158', 'type': 'print_statement', 'children': ['159', '163']}, {'id': '159', 'type': 'chevron', 'children': ['160']}; {'id': '160', 'type': 'attribute', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '163', 'type': 'binary_operator', 'children': ['164', '165'], 'value': '%'}; {'id': '164', 'type': 'string', 'children': [], 'value': '" %r"'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'lim'}; {'id': '166', 'type': 'if_statement', 'children': ['167', '169']}, {'id': '167', 'type': 'not_operator', 'children': ['168']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'dry_run'}, {'id': '169', 'type': 'block', 'children': ['170']}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'call', 'children': ['172', '175']}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'database'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'limit_update'}, {'id': '175', 'type': 'argument_list', 'children': ['176', '177', '178']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'limits_key'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'lims'}, {'id': '179', 'type': 'if_statement', 'children': ['180', '183']}; {'id': '180', 'type': 'comparison_operator', 'children': ['181', '182'], 'value': 'is'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '182', 'type': 'False', 'children': []}, {'id': '183', 'type': 'block', 'children': ['184']}; {'id': '184', 'type': 'return_statement', 'children': []}, {'id': '185', 'type': 'expression_statement', 'children': ['186']}; {'id': '186', 'type': 'assignment', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'params'}; {'id': '188', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '189', 'type': 'if_statement', 'children': ['190', '193', '195', '225']}; {'id': '190', 'type': 'comparison_operator', 'children': ['191', '192'], 'value': 'is'}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '192', 'type': 'True', 'children': []}, {'id': '193', 'type': 'block', 'children': ['194']}; {'id': '194', 'type': 'pass_statement', 'children': []}, {'id': '195', 'type': 'elif_clause', 'children': ['196', '218']}; {'id': '196', 'type': 'parenthesized_expression', 'children': ['197']}, {'id': '197', 'type': 'boolean_operator', 'children': ['198', '206'], 'value': 'or'}; {'id': '198', 'type': 'call', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '200', 'type': 'argument_list', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '202', 'type': 'tuple', 'children': ['203', '204', '205']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'long'}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'float'}; {'id': '206', 'type': 'parenthesized_expression', 'children': ['207']}, {'id': '207', 'type': 'boolean_operator', 'children': ['208', '213'], 'value': 'and'}; {'id': '208', 'type': 'call', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'isinstance'}; {'id': '210', 'type': 'argument_list', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'basestring'}, {'id': '213', 'type': 'call', 'children': ['214', '217']}; {'id': '214', 'type': 'attribute', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'do_reload'}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'isdigit'}, {'id': '217', 'type': 'argument_list', 'children': []}; {'id': '218', 'type': 'block', 'children': ['219']}, {'id': '219', 'type': 'expression_statement', 'children': ['220']}; {'id': '220', 'type': 'assignment', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'params'}; {'id': '222', 'type': 'list', 'children': ['223', '224'], 'value': "['spread', do_reload]"}, {'id': '223', 'type': 'string', 'children': [], 'value': "'spread'"}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'do_reload'}, {'id': '225', 'type': 'else_clause', 'children': ['226']}; {'id': '226', 'type': 'block', 'children': ['227']}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'params'}; {'id': '230', 'type': 'list', 'children': ['231'], 'value': '[str(do_reload)]'}, {'id': '231', 'type': 'call', 'children': ['232', '233']}; {'id': '232', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '233', 'type': 'argument_list', 'children': ['234']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'do_reload'}, {'id': '235', 'type': 'if_statement', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '237', 'type': 'block', 'children': ['238', '243', '250']}; {'id': '238', 'type': 'expression_statement', 'children': ['239']}, {'id': '239', 'type': 'assignment', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '241', 'type': 'list', 'children': ['242'], 'value': "['reload']"}; {'id': '242', 'type': 'string', 'children': [], 'value': "'reload'"}, {'id': '243', 'type': 'expression_statement', 'children': ['244']}; {'id': '244', 'type': 'call', 'children': ['245', '248']}, {'id': '245', 'type': 'attribute', 'children': ['246', '247']}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'extend'}; {'id': '248', 'type': 'argument_list', 'children': ['249']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'params'}; {'id': '250', 'type': 'print_statement', 'children': ['251', '255']}, {'id': '251', 'type': 'chevron', 'children': ['252']}; {'id': '252', 'type': 'attribute', 'children': ['253', '254']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '255', 'type': 'parenthesized_expression', 'children': ['256']}; {'id': '256', 'type': 'binary_operator', 'children': ['257', '258'], 'value': '%'}, {'id': '257', 'type': 'string', 'children': [], 'value': '"Issuing command: %s"'}; {'id': '258', 'type': 'call', 'children': ['259', '262']}, {'id': '259', 'type': 'attribute', 'children': ['260', '261']}; {'id': '260', 'type': 'string', 'children': [], 'value': "' '"}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '262', 'type': 'generator_expression', 'children': ['263', '267']}, {'id': '263', 'type': 'call', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '265', 'type': 'argument_list', 'children': ['266']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '267', 'type': 'for_in_clause', 'children': ['268', '269']}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '270', 'type': 'if_statement', 'children': ['271', '273']}, {'id': '271', 'type': 'not_operator', 'children': ['272']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'dry_run'}, {'id': '273', 'type': 'block', 'children': ['274']}; {'id': '274', 'type': 'expression_statement', 'children': ['275']}, {'id': '275', 'type': 'call', 'children': ['276', '279']}; {'id': '276', 'type': 'attribute', 'children': ['277', '278']}, {'id': '277', 'type': 'identifier', 'children': [], 'value': 'database'}; {'id': '278', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '279', 'type': 'argument_list', 'children': ['280', '281', '282', '283']}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'control_channel'}; {'id': '282', 'type': 'string', 'children': [], 'value': "'reload'"}, {'id': '283', 'type': 'list_splat', 'children': ['284']} | def setup_limits(conf_file, limits_file, do_reload=True,
dry_run=False, debug=False):
if dry_run:
debug = True
conf = config.Config(conf_file=conf_file)
db = conf.get_database()
limits_key = conf['control'].get('limits_key', 'limits')
control_channel = conf['control'].get('channel', 'control')
limits_tree = etree.parse(limits_file)
lims = []
for idx, lim in enumerate(limits_tree.getroot()):
if lim.tag != 'limit':
warnings.warn("Unrecognized tag %r in limits file at index %d" %
(lim.tag, idx))
continue
try:
lims.append(parse_limit_node(db, idx, lim))
except Exception as exc:
warnings.warn("Couldn't understand limit at index %d: %s" %
(idx, exc))
continue
if debug:
print >>sys.stderr, "Installing the following limits:"
for lim in lims:
print >>sys.stderr, " %r" % lim
if not dry_run:
database.limit_update(db, limits_key, lims)
if do_reload is False:
return
params = []
if do_reload is True:
pass
elif (isinstance(do_reload, (int, long, float)) or
(isinstance(do_reload, basestring) and do_reload.isdigit())):
params = ['spread', do_reload]
else:
params = [str(do_reload)]
if debug:
cmd = ['reload']
cmd.extend(params)
print >>sys.stderr, ("Issuing command: %s" %
' '.join(str(c) for c in cmd))
if not dry_run:
database.command(db, control_channel, 'reload', *params) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'make_limit_node'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'root'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'limit'}; {'id': '6', 'type': 'block', 'children': ['7', '23']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'limit_node'}; {'id': '10', 'type': 'call', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'etree'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'SubElement'}; {'id': '14', 'type': 'argument_list', 'children': ['15', '16', '17']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'root'}; {'id': '16', 'type': 'string', 'children': [], 'value': "'limit'"}, {'id': '17', 'type': 'dictionary', 'children': ['18']}; {'id': '18', 'type': 'pair', 'children': ['19', '20']}, {'id': '19', 'type': 'string', 'children': [], 'value': "'class'"}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'limit'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': '_limit_full_name'}, {'id': '23', 'type': 'for_statement', 'children': ['24', '25', '31']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '25', 'type': 'call', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '27', 'type': 'argument_list', 'children': ['28']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'limit'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'attrs'}, {'id': '31', 'type': 'block', 'children': ['32', '40', '50', '58', '88', '101']}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'desc'}, {'id': '35', 'type': 'subscript', 'children': ['36', '39']}; {'id': '36', 'type': 'attribute', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'limit'}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'attrs'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'attr'}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '43']}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'attr_type'}, {'id': '43', 'type': 'call', 'children': ['44', '47']}; {'id': '44', 'type': 'attribute', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'desc'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '47', 'type': 'argument_list', 'children': ['48', '49']}; {'id': '48', 'type': 'string', 'children': [], 'value': "'type'"}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '53', 'type': 'call', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'getattr'}, {'id': '55', 'type': 'argument_list', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'limit'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'attr'}; {'id': '58', 'type': 'if_statement', 'children': ['59', '62']}, {'id': '59', 'type': 'comparison_operator', 'children': ['60', '61'], 'value': 'in'}; {'id': '60', 'type': 'string', 'children': [], 'value': "'default'"}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'desc'}; {'id': '62', 'type': 'block', 'children': ['63', '82']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '66', 'type': 'parenthesized_expression', 'children': ['67']}, {'id': '67', 'type': 'conditional_expression', 'children': ['68', '73', '79'], 'value': 'if'}; {'id': '68', 'type': 'call', 'children': ['69', '72']}, {'id': '69', 'type': 'subscript', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'desc'}, {'id': '71', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '72', 'type': 'argument_list', 'children': []}, {'id': '73', 'type': 'call', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'callable'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'subscript', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'desc'}; {'id': '78', 'type': 'string', 'children': [], 'value': "'default'"}, {'id': '79', 'type': 'subscript', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'desc'}, {'id': '81', 'type': 'string', 'children': [], 'value': "'default'"}; {'id': '82', 'type': 'if_statement', 'children': ['83', '86']}, {'id': '83', 'type': 'comparison_operator', 'children': ['84', '85'], 'value': '=='}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'default'}; {'id': '86', 'type': 'block', 'children': ['87']}, {'id': '87', 'type': 'continue_statement', 'children': []}; {'id': '88', 'type': 'expression_statement', 'children': ['89']}, {'id': '89', 'type': 'assignment', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'attr_node'}, {'id': '91', 'type': 'call', 'children': ['92', '95']}; {'id': '92', 'type': 'attribute', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'etree'}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'SubElement'}, {'id': '95', 'type': 'argument_list', 'children': ['96', '97', '98']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'limit_node'}, {'id': '97', 'type': 'string', 'children': [], 'value': "'attr'"}; {'id': '98', 'type': 'keyword_argument', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '101', 'type': 'if_statement', 'children': ['102', '105', '129', '177']}; {'id': '102', 'type': 'comparison_operator', 'children': ['103', '104'], 'value': '=='}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'attr_type'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'for_statement', 'children': ['107', '108', '109']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '109', 'type': 'block', 'children': ['110', '120']}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'assignment', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'val_node'}, {'id': '113', 'type': 'call', 'children': ['114', '117']}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'etree'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'SubElement'}, {'id': '117', 'type': 'argument_list', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'attr_node'}, {'id': '119', 'type': 'string', 'children': [], 'value': "'value'"}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'assignment', 'children': ['122', '125']}; {'id': '122', 'type': 'attribute', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'val_node'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'text'}, {'id': '125', 'type': 'call', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '127', 'type': 'argument_list', 'children': ['128']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '129', 'type': 'elif_clause', 'children': ['130', '133']}; {'id': '130', 'type': 'comparison_operator', 'children': ['131', '132'], 'value': '=='}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'attr_type'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '133', 'type': 'block', 'children': ['134']}; {'id': '134', 'type': 'for_statement', 'children': ['135', '138', '154']}, {'id': '135', 'type': 'pattern_list', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'val'}; {'id': '138', 'type': 'call', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '140', 'type': 'argument_list', 'children': ['141', '146']}, {'id': '141', 'type': 'call', 'children': ['142', '145']}; {'id': '142', 'type': 'attribute', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '145', 'type': 'argument_list', 'children': []}; {'id': '146', 'type': 'keyword_argument', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '148', 'type': 'lambda', 'children': ['149', '151']}, {'id': '149', 'type': 'lambda_parameters', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '151', 'type': 'subscript', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '153', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '154', 'type': 'block', 'children': ['155', '168']}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'assignment', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'val_node'}; {'id': '158', 'type': 'call', 'children': ['159', '162']}, {'id': '159', 'type': 'attribute', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'etree'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'SubElement'}; {'id': '162', 'type': 'argument_list', 'children': ['163', '164', '165']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'attr_node'}; {'id': '164', 'type': 'string', 'children': [], 'value': "'value'"}, {'id': '165', 'type': 'keyword_argument', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '173']}; {'id': '170', 'type': 'attribute', 'children': ['171', '172']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'val_node'}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'text'}, {'id': '173', 'type': 'call', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'str'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'val'}, {'id': '177', 'type': 'else_clause', 'children': ['178']}; {'id': '178', 'type': 'block', 'children': ['179']}, {'id': '179', 'type': 'expression_statement', 'children': ['180']}; {'id': '180', 'type': 'assignment', 'children': ['181', '184']}, {'id': '181', 'type': 'attribute', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'attr_node'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'text'}; {'id': '184', 'type': 'call', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '186', 'type': 'argument_list', 'children': ['187']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'value'} | def make_limit_node(root, limit):
limit_node = etree.SubElement(root, 'limit',
{'class': limit._limit_full_name})
for attr in sorted(limit.attrs):
desc = limit.attrs[attr]
attr_type = desc.get('type', str)
value = getattr(limit, attr)
if 'default' in desc:
default = (desc['default']() if callable(desc['default']) else
desc['default'])
if value == default:
continue
attr_node = etree.SubElement(limit_node, 'attr', name=attr)
if attr_type == list:
for val in value:
val_node = etree.SubElement(attr_node, 'value')
val_node.text = str(val)
elif attr_type == dict:
for key, val in sorted(value.items(), key=lambda x: x[0]):
val_node = etree.SubElement(attr_node, 'value', key=key)
val_node.text = str(val)
else:
attr_node.text = str(value) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '15']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'turnstile_command'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '9', '12']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'conf_file'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'arguments'}; {'id': '8', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '9', 'type': 'default_parameter', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'channel'}, {'id': '11', 'type': 'None', 'children': []}; {'id': '12', 'type': 'default_parameter', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '14', 'type': 'False', 'children': []}, {'id': '15', 'type': 'block', 'children': ['16', '27', '35', '47', '55', '59', '122', '146', '157', '162', '170', '177']}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'conf'}, {'id': '19', 'type': 'call', 'children': ['20', '23']}; {'id': '20', 'type': 'attribute', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'Config'}, {'id': '23', 'type': 'argument_list', 'children': ['24']}; {'id': '24', 'type': 'keyword_argument', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'conf_file'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'conf_file'}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'db'}; {'id': '30', 'type': 'call', 'children': ['31', '34']}, {'id': '31', 'type': 'attribute', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'conf'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'get_database'}; {'id': '34', 'type': 'argument_list', 'children': []}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'control_channel'}; {'id': '38', 'type': 'call', 'children': ['39', '44']}, {'id': '39', 'type': 'attribute', 'children': ['40', '43']}; {'id': '40', 'type': 'subscript', 'children': ['41', '42']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'conf'}; {'id': '42', 'type': 'string', 'children': [], 'value': "'control'"}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '44', 'type': 'argument_list', 'children': ['45', '46']}, {'id': '45', 'type': 'string', 'children': [], 'value': "'channel'"}; {'id': '46', 'type': 'string', 'children': [], 'value': "'control'"}, {'id': '47', 'type': 'expression_statement', 'children': ['48']}; {'id': '48', 'type': 'assignment', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '50', 'type': 'call', 'children': ['51', '54']}, {'id': '51', 'type': 'attribute', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '54', 'type': 'argument_list', 'children': []}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'assignment', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'ts_conv'}; {'id': '58', 'type': 'False', 'children': []}, {'id': '59', 'type': 'if_statement', 'children': ['60', '63']}; {'id': '60', 'type': 'comparison_operator', 'children': ['61', '62'], 'value': '=='}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '62', 'type': 'string', 'children': [], 'value': "'ping'"}, {'id': '63', 'type': 'block', 'children': ['64', '91', '114']}; {'id': '64', 'type': 'if_statement', 'children': ['65', '66', '73']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'arguments'}; {'id': '66', 'type': 'block', 'children': ['67']}, {'id': '67', 'type': 'expression_statement', 'children': ['68']}; {'id': '68', 'type': 'assignment', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '70', 'type': 'subscript', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'arguments'}; {'id': '72', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '73', 'type': 'else_clause', 'children': ['74']}; {'id': '74', 'type': 'block', 'children': ['75', '86']}, {'id': '75', 'type': 'expression_statement', 'children': ['76']}; {'id': '76', 'type': 'assignment', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '78', 'type': 'call', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'call', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'uuid'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'uuid4'}, {'id': '85', 'type': 'argument_list', 'children': []}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '89', 'type': 'list', 'children': ['90'], 'value': '[channel]'}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'channel'}, {'id': '91', 'type': 'if_statement', 'children': ['92', '98']}; {'id': '92', 'type': 'comparison_operator', 'children': ['93', '97'], 'value': '<'}, {'id': '93', 'type': 'call', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '97', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '98', 'type': 'block', 'children': ['99', '110']}, {'id': '99', 'type': 'expression_statement', 'children': ['100']}; {'id': '100', 'type': 'call', 'children': ['101', '104']}, {'id': '101', 'type': 'attribute', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '104', 'type': 'argument_list', 'children': ['105']}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'time'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'time'}, {'id': '109', 'type': 'argument_list', 'children': []}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'assignment', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'ts_conv'}, {'id': '113', 'type': 'True', 'children': []}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'assignment', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '117', 'type': 'subscript', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '119', 'type': 'slice', 'children': ['120', '121']}; {'id': '120', 'type': 'colon', 'children': []}, {'id': '121', 'type': 'integer', 'children': [], 'value': '2'}; {'id': '122', 'type': 'if_statement', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'debug'}; {'id': '124', 'type': 'block', 'children': ['125', '132']}, {'id': '125', 'type': 'expression_statement', 'children': ['126']}; {'id': '126', 'type': 'assignment', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '128', 'type': 'binary_operator', 'children': ['129', '131'], 'value': '+'}, {'id': '129', 'type': 'list', 'children': ['130'], 'value': '[command]'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'arguments'}; {'id': '132', 'type': 'print_statement', 'children': ['133', '137']}, {'id': '133', 'type': 'chevron', 'children': ['134']}; {'id': '134', 'type': 'attribute', 'children': ['135', '136']}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '137', 'type': 'parenthesized_expression', 'children': ['138']}; {'id': '138', 'type': 'binary_operator', 'children': ['139', '140'], 'value': '%'}, {'id': '139', 'type': 'string', 'children': [], 'value': '"Issuing command: %s"'}; {'id': '140', 'type': 'call', 'children': ['141', '144']}, {'id': '141', 'type': 'attribute', 'children': ['142', '143']}; {'id': '142', 'type': 'string', 'children': [], 'value': "' '"}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '144', 'type': 'argument_list', 'children': ['145']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '146', 'type': 'expression_statement', 'children': ['147']}, {'id': '147', 'type': 'call', 'children': ['148', '151']}; {'id': '148', 'type': 'attribute', 'children': ['149', '150']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'database'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '151', 'type': 'argument_list', 'children': ['152', '153', '154', '155']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'control_channel'}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '155', 'type': 'list_splat', 'children': ['156']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'arguments'}, {'id': '157', 'type': 'if_statement', 'children': ['158', '160']}; {'id': '158', 'type': 'not_operator', 'children': ['159']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '160', 'type': 'block', 'children': ['161']}, {'id': '161', 'type': 'return_statement', 'children': []}; {'id': '162', 'type': 'expression_statement', 'children': ['163']}, {'id': '163', 'type': 'assignment', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'pubsub'}, {'id': '165', 'type': 'call', 'children': ['166', '169']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'db'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'pubsub'}, {'id': '169', 'type': 'argument_list', 'children': []}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'call', 'children': ['172', '175']}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'pubsub'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'subscribe'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'channel'}, {'id': '177', 'type': 'try_statement', 'children': ['178', '296']}; {'id': '178', 'type': 'block', 'children': ['179', '183']}, {'id': '179', 'type': 'expression_statement', 'children': ['180']}; {'id': '180', 'type': 'assignment', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'count'}; {'id': '182', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '183', 'type': 'for_statement', 'children': ['184', '185', '190']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '185', 'type': 'call', 'children': ['186', '189']}; {'id': '186', 'type': 'attribute', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'pubsub'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'listen'}, {'id': '189', 'type': 'argument_list', 'children': []}; {'id': '190', 'type': 'block', 'children': ['191', '211', '228', '232', '243', '285']}, {'id': '191', 'type': 'if_statement', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'debug'}, {'id': '193', 'type': 'block', 'children': ['194', '203']}; {'id': '194', 'type': 'expression_statement', 'children': ['195']}, {'id': '195', 'type': 'assignment', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'formatted'}, {'id': '197', 'type': 'call', 'children': ['198', '201']}; {'id': '198', 'type': 'attribute', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'pprint'}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'pformat'}, {'id': '201', 'type': 'argument_list', 'children': ['202']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '203', 'type': 'print_statement', 'children': ['204', '208']}; {'id': '204', 'type': 'chevron', 'children': ['205']}, {'id': '205', 'type': 'attribute', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '208', 'type': 'binary_operator', 'children': ['209', '210'], 'value': '%'}, {'id': '209', 'type': 'string', 'children': [], 'value': '"Received message: %s"'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'formatted'}, {'id': '211', 'type': 'if_statement', 'children': ['212', '226']}; {'id': '212', 'type': 'parenthesized_expression', 'children': ['213']}, {'id': '213', 'type': 'boolean_operator', 'children': ['214', '221'], 'value': 'or'}; {'id': '214', 'type': 'comparison_operator', 'children': ['215', '218'], 'value': 'not'}, {'id': '215', 'type': 'subscript', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '217', 'type': 'string', 'children': [], 'value': "'type'"}; {'id': '218', 'type': 'tuple', 'children': ['219', '220']}, {'id': '219', 'type': 'string', 'children': [], 'value': "'pmessage'"}; {'id': '220', 'type': 'string', 'children': [], 'value': "'message'"}, {'id': '221', 'type': 'comparison_operator', 'children': ['222', '225'], 'value': '!='}; {'id': '222', 'type': 'subscript', 'children': ['223', '224']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '224', 'type': 'string', 'children': [], 'value': "'channel'"}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '226', 'type': 'block', 'children': ['227']}, {'id': '227', 'type': 'continue_statement', 'children': []}; {'id': '228', 'type': 'expression_statement', 'children': ['229']}, {'id': '229', 'type': 'augmented_assignment', 'children': ['230', '231'], 'value': '+='}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'count'}, {'id': '231', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '232', 'type': 'expression_statement', 'children': ['233']}, {'id': '233', 'type': 'assignment', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'response'}, {'id': '235', 'type': 'call', 'children': ['236', '241']}; {'id': '236', 'type': 'attribute', 'children': ['237', '240']}, {'id': '237', 'type': 'subscript', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '239', 'type': 'string', 'children': [], 'value': "'data'"}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '241', 'type': 'argument_list', 'children': ['242']}; {'id': '242', 'type': 'string', 'children': [], 'value': "':'"}, {'id': '243', 'type': 'if_statement', 'children': ['244', '251']}; {'id': '244', 'type': 'boolean_operator', 'children': ['245', '246'], 'value': 'and'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'ts_conv'}; {'id': '246', 'type': 'comparison_operator', 'children': ['247', '250'], 'value': '=='}, {'id': '247', 'type': 'subscript', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'response'}, {'id': '249', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '250', 'type': 'string', 'children': [], 'value': "'pong'"}, {'id': '251', 'type': 'block', 'children': ['252']}; {'id': '252', 'type': 'try_statement', 'children': ['253', '281']}, {'id': '253', 'type': 'block', 'children': ['254', '272']}; {'id': '254', 'type': 'expression_statement', 'children': ['255']}, {'id': '255', 'type': 'assignment', 'children': ['256', '257']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'rtt'}, {'id': '257', 'type': 'binary_operator', 'children': ['258', '271'], 'value': '*'}; {'id': '258', 'type': 'parenthesized_expression', 'children': ['259']}, {'id': '259', 'type': 'binary_operator', 'children': ['260', '265'], 'value': '-'}; {'id': '260', 'type': 'call', 'children': ['261', '264']}, {'id': '261', 'type': 'attribute', 'children': ['262', '263']}; {'id': '262', 'type': 'identifier', 'children': [], 'value': 'time'}, {'id': '263', 'type': 'identifier', 'children': [], 'value': 'time'}; {'id': '264', 'type': 'argument_list', 'children': []}, {'id': '265', 'type': 'call', 'children': ['266', '267']}; {'id': '266', 'type': 'identifier', 'children': [], 'value': 'float'}, {'id': '267', 'type': 'argument_list', 'children': ['268']}; {'id': '268', 'type': 'subscript', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'response'}; {'id': '270', 'type': 'integer', 'children': [], 'value': '2'}, {'id': '271', 'type': 'integer', 'children': [], 'value': '100'}; {'id': '272', 'type': 'expression_statement', 'children': ['273']}, {'id': '273', 'type': 'call', 'children': ['274', '277']}; {'id': '274', 'type': 'attribute', 'children': ['275', '276']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'response'}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '277', 'type': 'argument_list', 'children': ['278']}; {'id': '278', 'type': 'binary_operator', 'children': ['279', '280'], 'value': '%'}, {'id': '279', 'type': 'string', 'children': [], 'value': "'(RTT %.2fms)'"}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'rtt'}, {'id': '281', 'type': 'except_clause', 'children': ['282', '283']}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'Exception'}, {'id': '283', 'type': 'block', 'children': ['284']}; {'id': '284', 'type': 'pass_statement', 'children': []}, {'id': '285', 'type': 'print_statement', 'children': ['286']}; {'id': '286', 'type': 'binary_operator', 'children': ['287', '288'], 'value': '%'}, {'id': '287', 'type': 'string', 'children': [], 'value': '"Response % 5d: %s"'}; {'id': '288', 'type': 'tuple', 'children': ['289', '290']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'count'}; {'id': '290', 'type': 'call', 'children': ['291', '294']}, {'id': '291', 'type': 'attribute', 'children': ['292', '293']}; {'id': '292', 'type': 'string', 'children': [], 'value': "' '"}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '294', 'type': 'argument_list', 'children': ['295']}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'response'}; {'id': '296', 'type': 'except_clause', 'children': ['297', '298']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'KeyboardInterrupt'}; {'id': '298', 'type': 'block', 'children': ['299']}, {'id': '299', 'type': 'pass_statement', 'children': []} | def turnstile_command(conf_file, command, arguments=[], channel=None,
debug=False):
conf = config.Config(conf_file=conf_file)
db = conf.get_database()
control_channel = conf['control'].get('channel', 'control')
command = command.lower()
ts_conv = False
if command == 'ping':
if arguments:
channel = arguments[0]
else:
channel = str(uuid.uuid4())
arguments = [channel]
if len(arguments) < 2:
arguments.append(time.time())
ts_conv = True
arguments = arguments[:2]
if debug:
cmd = [command] + arguments
print >>sys.stderr, ("Issuing command: %s" %
' '.join(cmd))
database.command(db, control_channel, command, *arguments)
if not channel:
return
pubsub = db.pubsub()
pubsub.subscribe(channel)
try:
count = 0
for msg in pubsub.listen():
if debug:
formatted = pprint.pformat(msg)
print >>sys.stderr, "Received message: %s" % formatted
if (msg['type'] not in ('pmessage', 'message') or
msg['channel'] != channel):
continue
count += 1
response = msg['data'].split(':')
if ts_conv and response[0] == 'pong':
try:
rtt = (time.time() - float(response[2])) * 100
response.append('(RTT %.2fms)' % rtt)
except Exception:
pass
print "Response % 5d: %s" % (count, ' '.join(response))
except KeyboardInterrupt:
pass |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'humanize_timesince'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'start_time'}, {'id': '5', 'type': 'block', 'children': ['6', '12', '20', '31', '39', '62', '70', '93', '99', '122', '130', '153', '161', '184']}; {'id': '6', 'type': 'if_statement', 'children': ['7', '9']}, {'id': '7', 'type': 'not_operator', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'start_time'}, {'id': '9', 'type': 'block', 'children': ['10']}; {'id': '10', 'type': 'return_statement', 'children': ['11']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'start_time'}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'delta'}, {'id': '15', 'type': 'binary_operator', 'children': ['16', '19'], 'value': '-'}; {'id': '16', 'type': 'call', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'local_now'}; {'id': '18', 'type': 'argument_list', 'children': []}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'start_time'}; {'id': '20', 'type': 'if_statement', 'children': ['21', '28']}, {'id': '21', 'type': 'comparison_operator', 'children': ['22', '27'], 'value': '<'}; {'id': '22', 'type': 'call', 'children': ['23', '26']}, {'id': '23', 'type': 'attribute', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'delta'}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'total_seconds'}; {'id': '26', 'type': 'argument_list', 'children': []}, {'id': '27', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '28', 'type': 'block', 'children': ['29']}, {'id': '29', 'type': 'return_statement', 'children': ['30']}; {'id': '30', 'type': 'string', 'children': [], 'value': "'a few seconds ago'"}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'num_years'}; {'id': '34', 'type': 'binary_operator', 'children': ['35', '38'], 'value': '//'}, {'id': '35', 'type': 'attribute', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'delta'}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'days'}; {'id': '38', 'type': 'integer', 'children': [], 'value': '365'}, {'id': '39', 'type': 'if_statement', 'children': ['40', '43']}; {'id': '40', 'type': 'comparison_operator', 'children': ['41', '42'], 'value': '>'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'num_years'}; {'id': '42', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '43', 'type': 'block', 'children': ['44']}; {'id': '44', 'type': 'return_statement', 'children': ['45']}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'string', 'children': [], 'value': "'{} year{} ago'"}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '49', 'type': 'argument_list', 'children': ['50']}; {'id': '50', 'type': 'list_splat', 'children': ['51']}, {'id': '51', 'type': 'parenthesized_expression', 'children': ['52']}; {'id': '52', 'type': 'conditional_expression', 'children': ['53', '56', '59'], 'value': 'if'}, {'id': '53', 'type': 'tuple', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'num_years'}, {'id': '55', 'type': 'string', 'children': [], 'value': "'s'"}; {'id': '56', 'type': 'comparison_operator', 'children': ['57', '58'], 'value': '>'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'num_years'}; {'id': '58', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '59', 'type': 'tuple', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'num_years'}, {'id': '61', 'type': 'string', 'children': [], 'value': "''"}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'num_weeks'}, {'id': '65', 'type': 'binary_operator', 'children': ['66', '69'], 'value': '//'}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'delta'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'days'}, {'id': '69', 'type': 'integer', 'children': [], 'value': '7'}; {'id': '70', 'type': 'if_statement', 'children': ['71', '74']}, {'id': '71', 'type': 'comparison_operator', 'children': ['72', '73'], 'value': '>'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'num_weeks'}, {'id': '73', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '74', 'type': 'block', 'children': ['75']}, {'id': '75', 'type': 'return_statement', 'children': ['76']}; {'id': '76', 'type': 'call', 'children': ['77', '80']}, {'id': '77', 'type': 'attribute', 'children': ['78', '79']}; {'id': '78', 'type': 'string', 'children': [], 'value': "'{} week{} ago'"}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '80', 'type': 'argument_list', 'children': ['81']}, {'id': '81', 'type': 'list_splat', 'children': ['82']}; {'id': '82', 'type': 'parenthesized_expression', 'children': ['83']}, {'id': '83', 'type': 'conditional_expression', 'children': ['84', '87', '90'], 'value': 'if'}; {'id': '84', 'type': 'tuple', 'children': ['85', '86']}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'num_weeks'}; {'id': '86', 'type': 'string', 'children': [], 'value': "'s'"}, {'id': '87', 'type': 'comparison_operator', 'children': ['88', '89'], 'value': '>'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'num_weeks'}, {'id': '89', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '90', 'type': 'tuple', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'num_weeks'}; {'id': '92', 'type': 'string', 'children': [], 'value': "''"}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'num_days'}; {'id': '96', 'type': 'attribute', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'delta'}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'days'}, {'id': '99', 'type': 'if_statement', 'children': ['100', '103']}; {'id': '100', 'type': 'comparison_operator', 'children': ['101', '102'], 'value': '>'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'num_days'}; {'id': '102', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '103', 'type': 'block', 'children': ['104']}; {'id': '104', 'type': 'return_statement', 'children': ['105']}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'string', 'children': [], 'value': "'{} day{} ago'"}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'list_splat', 'children': ['111']}, {'id': '111', 'type': 'parenthesized_expression', 'children': ['112']}; {'id': '112', 'type': 'conditional_expression', 'children': ['113', '116', '119'], 'value': 'if'}, {'id': '113', 'type': 'tuple', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'num_days'}, {'id': '115', 'type': 'string', 'children': [], 'value': "'s'"}; {'id': '116', 'type': 'comparison_operator', 'children': ['117', '118'], 'value': '>'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'num_days'}; {'id': '118', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '119', 'type': 'tuple', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'num_days'}, {'id': '121', 'type': 'string', 'children': [], 'value': "''"}; {'id': '122', 'type': 'expression_statement', 'children': ['123']}, {'id': '123', 'type': 'assignment', 'children': ['124', '125']}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'num_hours'}, {'id': '125', 'type': 'binary_operator', 'children': ['126', '129'], 'value': '//'}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'delta'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'seconds'}, {'id': '129', 'type': 'integer', 'children': [], 'value': '3600'}; {'id': '130', 'type': 'if_statement', 'children': ['131', '134']}, {'id': '131', 'type': 'comparison_operator', 'children': ['132', '133'], 'value': '>'}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'num_hours'}, {'id': '133', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '134', 'type': 'block', 'children': ['135']}, {'id': '135', 'type': 'return_statement', 'children': ['136']}; {'id': '136', 'type': 'call', 'children': ['137', '140']}, {'id': '137', 'type': 'attribute', 'children': ['138', '139']}; {'id': '138', 'type': 'string', 'children': [], 'value': "'{} hour{} ago'"}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '140', 'type': 'argument_list', 'children': ['141']}, {'id': '141', 'type': 'list_splat', 'children': ['142']}; {'id': '142', 'type': 'parenthesized_expression', 'children': ['143']}, {'id': '143', 'type': 'conditional_expression', 'children': ['144', '147', '150'], 'value': 'if'}; {'id': '144', 'type': 'tuple', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'num_hours'}; {'id': '146', 'type': 'string', 'children': [], 'value': "'s'"}, {'id': '147', 'type': 'comparison_operator', 'children': ['148', '149'], 'value': '>'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'num_hours'}, {'id': '149', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '150', 'type': 'tuple', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'num_hours'}; {'id': '152', 'type': 'string', 'children': [], 'value': "''"}, {'id': '153', 'type': 'expression_statement', 'children': ['154']}; {'id': '154', 'type': 'assignment', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'num_minutes'}; {'id': '156', 'type': 'binary_operator', 'children': ['157', '160'], 'value': '//'}, {'id': '157', 'type': 'attribute', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'delta'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'seconds'}; {'id': '160', 'type': 'integer', 'children': [], 'value': '60'}, {'id': '161', 'type': 'if_statement', 'children': ['162', '165']}; {'id': '162', 'type': 'comparison_operator', 'children': ['163', '164'], 'value': '>'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'num_minutes'}; {'id': '164', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '165', 'type': 'block', 'children': ['166']}; {'id': '166', 'type': 'return_statement', 'children': ['167']}, {'id': '167', 'type': 'call', 'children': ['168', '171']}; {'id': '168', 'type': 'attribute', 'children': ['169', '170']}, {'id': '169', 'type': 'string', 'children': [], 'value': "'{} minute{} ago'"}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '171', 'type': 'argument_list', 'children': ['172']}; {'id': '172', 'type': 'list_splat', 'children': ['173']}, {'id': '173', 'type': 'parenthesized_expression', 'children': ['174']}; {'id': '174', 'type': 'conditional_expression', 'children': ['175', '178', '181'], 'value': 'if'}, {'id': '175', 'type': 'tuple', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'num_minutes'}, {'id': '177', 'type': 'string', 'children': [], 'value': "'s'"}; {'id': '178', 'type': 'comparison_operator', 'children': ['179', '180'], 'value': '>'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'num_minutes'}; {'id': '180', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '181', 'type': 'tuple', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'num_minutes'}, {'id': '183', 'type': 'string', 'children': [], 'value': "''"}; {'id': '184', 'type': 'return_statement', 'children': ['185']}, {'id': '185', 'type': 'string', 'children': [], 'value': "'a few seconds ago'"} | def humanize_timesince(start_time):
if not start_time:
return start_time
delta = local_now() - start_time
if delta.total_seconds() < 0:
return 'a few seconds ago'
num_years = delta.days // 365
if num_years > 0:
return '{} year{} ago'.format(
*((num_years, 's') if num_years > 1 else (num_years, '')))
num_weeks = delta.days // 7
if num_weeks > 0:
return '{} week{} ago'.format(
*((num_weeks, 's') if num_weeks > 1 else (num_weeks, '')))
num_days = delta.days
if num_days > 0:
return '{} day{} ago'.format(
*((num_days, 's') if num_days > 1 else (num_days, '')))
num_hours = delta.seconds // 3600
if num_hours > 0:
return '{} hour{} ago'.format(*((num_hours, 's') if num_hours > 1 else (num_hours, '')))
num_minutes = delta.seconds // 60
if num_minutes > 0:
return '{} minute{} ago'.format(
*((num_minutes, 's') if num_minutes > 1 else (num_minutes, '')))
return 'a few seconds ago' |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'fire'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '7']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'list_splat_pattern', 'children': ['6']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '7', 'type': 'dictionary_splat_pattern', 'children': ['8']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'kw'}, {'id': '9', 'type': 'block', 'children': ['10', '14', '27', '345']}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'assignment', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '13', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '14', 'type': 'with_statement', 'children': ['15', '20']}, {'id': '15', 'type': 'with_clause', 'children': ['16']}; {'id': '16', 'type': 'with_item', 'children': ['17']}, {'id': '17', 'type': 'attribute', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': '_hlock'}; {'id': '20', 'type': 'block', 'children': ['21']}, {'id': '21', 'type': 'expression_statement', 'children': ['22']}; {'id': '22', 'type': 'assignment', 'children': ['23', '24']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'handlers'}; {'id': '24', 'type': 'attribute', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'handlers'}, {'id': '27', 'type': 'if_statement', 'children': ['28', '33', '95']}; {'id': '28', 'type': 'comparison_operator', 'children': ['29', '32'], 'value': '=='}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'threads'}; {'id': '32', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '33', 'type': 'block', 'children': ['34']}; {'id': '34', 'type': 'for_statement', 'children': ['35', '36', '37']}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'handlers'}, {'id': '37', 'type': 'block', 'children': ['38', '47']}; {'id': '38', 'type': 'expression_statement', 'children': ['39']}, {'id': '39', 'type': 'assignment', 'children': ['40', '44']}; {'id': '40', 'type': 'pattern_list', 'children': ['41', '42', '43']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'h'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '44', 'type': 'subscript', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'handlers'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '47', 'type': 'try_statement', 'children': ['48', '74']}; {'id': '48', 'type': 'block', 'children': ['49', '64']}, {'id': '49', 'type': 'expression_statement', 'children': ['50']}; {'id': '50', 'type': 'assignment', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '52', 'type': 'call', 'children': ['53', '56']}, {'id': '53', 'type': 'attribute', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '55', 'type': 'identifier', 'children': [], 'value': '_memoize'}; {'id': '56', 'type': 'argument_list', 'children': ['57', '58', '59', '60', '62']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'h'}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '60', 'type': 'list_splat', 'children': ['61']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '62', 'type': 'dictionary_splat', 'children': ['63']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'kw'}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'call', 'children': ['66', '69']}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '69', 'type': 'argument_list', 'children': ['70']}; {'id': '70', 'type': 'call', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '72', 'type': 'argument_list', 'children': ['73']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '74', 'type': 'except_clause', 'children': ['75']}, {'id': '75', 'type': 'block', 'children': ['76']}; {'id': '76', 'type': 'expression_statement', 'children': ['77']}, {'id': '77', 'type': 'call', 'children': ['78', '81']}; {'id': '78', 'type': 'attribute', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '81', 'type': 'argument_list', 'children': ['82']}; {'id': '82', 'type': 'tuple', 'children': ['83', '84', '94']}, {'id': '83', 'type': 'False', 'children': []}; {'id': '84', 'type': 'call', 'children': ['85', '88']}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': '_error'}; {'id': '88', 'type': 'argument_list', 'children': ['89']}, {'id': '89', 'type': 'call', 'children': ['90', '93']}; {'id': '90', 'type': 'attribute', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'exc_info'}, {'id': '93', 'type': 'argument_list', 'children': []}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'h'}, {'id': '95', 'type': 'elif_clause', 'children': ['96', '101']}; {'id': '96', 'type': 'comparison_operator', 'children': ['97', '100'], 'value': '>'}, {'id': '97', 'type': 'attribute', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'threads'}; {'id': '100', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '101', 'type': 'block', 'children': ['102', '108', '114', '236']}; {'id': '102', 'type': 'expression_statement', 'children': ['103']}, {'id': '103', 'type': 'assignment', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '105', 'type': 'call', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'Queue'}, {'id': '107', 'type': 'argument_list', 'children': []}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'assignment', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'rlock'}, {'id': '111', 'type': 'call', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'RLock'}, {'id': '113', 'type': 'argument_list', 'children': []}; {'id': '114', 'type': 'function_definition', 'children': ['115', '116', '121']}, {'id': '115', 'type': 'function_name', 'children': [], 'value': '_execute'}; {'id': '116', 'type': 'parameters', 'children': ['117', '119']}, {'id': '117', 'type': 'list_splat_pattern', 'children': ['118']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '119', 'type': 'dictionary_splat_pattern', 'children': ['120']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'kw'}, {'id': '121', 'type': 'block', 'children': ['122']}; {'id': '122', 'type': 'while_statement', 'children': ['123', '124']}, {'id': '123', 'type': 'True', 'children': []}; {'id': '124', 'type': 'block', 'children': ['125']}, {'id': '125', 'type': 'try_statement', 'children': ['126', '232']}; {'id': '126', 'type': 'block', 'children': ['127', '135', '147', '156', '226']}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'assignment', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '130', 'type': 'call', 'children': ['131', '134']}, {'id': '131', 'type': 'attribute', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'get'}; {'id': '134', 'type': 'argument_list', 'children': []}, {'id': '135', 'type': 'if_statement', 'children': ['136', '139']}; {'id': '136', 'type': 'comparison_operator', 'children': ['137', '138'], 'value': 'is'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '138', 'type': 'None', 'children': []}, {'id': '139', 'type': 'block', 'children': ['140', '146']}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'call', 'children': ['142', '145']}; {'id': '142', 'type': 'attribute', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'task_done'}, {'id': '145', 'type': 'argument_list', 'children': []}; {'id': '146', 'type': 'break_statement', 'children': []}, {'id': '147', 'type': 'expression_statement', 'children': ['148']}; {'id': '148', 'type': 'assignment', 'children': ['149', '153']}, {'id': '149', 'type': 'pattern_list', 'children': ['150', '151', '152']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'h'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '153', 'type': 'subscript', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'handlers'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '156', 'type': 'try_statement', 'children': ['157', '194']}, {'id': '157', 'type': 'block', 'children': ['158', '173']}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '161', 'type': 'call', 'children': ['162', '165']}; {'id': '162', 'type': 'attribute', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': '_memoize'}, {'id': '165', 'type': 'argument_list', 'children': ['166', '167', '168', '169', '171']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'h'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 't'}, {'id': '169', 'type': 'list_splat', 'children': ['170']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '171', 'type': 'dictionary_splat', 'children': ['172']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'kw'}, {'id': '173', 'type': 'if_statement', 'children': ['174', '178']}; {'id': '174', 'type': 'not_operator', 'children': ['175']}, {'id': '175', 'type': 'attribute', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'asynch'}; {'id': '178', 'type': 'block', 'children': ['179']}, {'id': '179', 'type': 'with_statement', 'children': ['180', '183']}; {'id': '180', 'type': 'with_clause', 'children': ['181']}, {'id': '181', 'type': 'with_item', 'children': ['182']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'rlock'}, {'id': '183', 'type': 'block', 'children': ['184']}; {'id': '184', 'type': 'expression_statement', 'children': ['185']}, {'id': '185', 'type': 'call', 'children': ['186', '189']}; {'id': '186', 'type': 'attribute', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '189', 'type': 'argument_list', 'children': ['190']}; {'id': '190', 'type': 'call', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '192', 'type': 'argument_list', 'children': ['193']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '194', 'type': 'except_clause', 'children': ['195']}, {'id': '195', 'type': 'block', 'children': ['196']}; {'id': '196', 'type': 'if_statement', 'children': ['197', '201']}, {'id': '197', 'type': 'not_operator', 'children': ['198']}; {'id': '198', 'type': 'attribute', 'children': ['199', '200']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'asynch'}, {'id': '201', 'type': 'block', 'children': ['202']}; {'id': '202', 'type': 'with_statement', 'children': ['203', '206']}, {'id': '203', 'type': 'with_clause', 'children': ['204']}; {'id': '204', 'type': 'with_item', 'children': ['205']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'rlock'}; {'id': '206', 'type': 'block', 'children': ['207']}, {'id': '207', 'type': 'expression_statement', 'children': ['208']}; {'id': '208', 'type': 'call', 'children': ['209', '212']}, {'id': '209', 'type': 'attribute', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '212', 'type': 'argument_list', 'children': ['213']}, {'id': '213', 'type': 'tuple', 'children': ['214', '215', '225']}; {'id': '214', 'type': 'False', 'children': []}, {'id': '215', 'type': 'call', 'children': ['216', '219']}; {'id': '216', 'type': 'attribute', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': '_error'}, {'id': '219', 'type': 'argument_list', 'children': ['220']}; {'id': '220', 'type': 'call', 'children': ['221', '224']}, {'id': '221', 'type': 'attribute', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'sys'}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'exc_info'}; {'id': '224', 'type': 'argument_list', 'children': []}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'h'}; {'id': '226', 'type': 'expression_statement', 'children': ['227']}, {'id': '227', 'type': 'call', 'children': ['228', '231']}; {'id': '228', 'type': 'attribute', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'task_done'}, {'id': '231', 'type': 'argument_list', 'children': []}; {'id': '232', 'type': 'except_clause', 'children': ['233', '234']}, {'id': '233', 'type': 'identifier', 'children': [], 'value': 'Empty'}; {'id': '234', 'type': 'block', 'children': ['235']}, {'id': '235', 'type': 'break_statement', 'children': []}; {'id': '236', 'type': 'if_statement', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'handlers'}; {'id': '238', 'type': 'block', 'children': ['239', '250', '284', '319', '333']}, {'id': '239', 'type': 'expression_statement', 'children': ['240']}; {'id': '240', 'type': 'assignment', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'threads'}; {'id': '242', 'type': 'call', 'children': ['243', '246']}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': '_threads'}; {'id': '246', 'type': 'argument_list', 'children': ['247']}, {'id': '247', 'type': 'keyword_argument', 'children': ['248', '249']}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'handlers'}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'handlers'}; {'id': '250', 'type': 'for_statement', 'children': ['251', '252', '256']}, {'id': '251', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '252', 'type': 'call', 'children': ['253', '254']}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'range'}; {'id': '254', 'type': 'argument_list', 'children': ['255']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'threads'}; {'id': '256', 'type': 'block', 'children': ['257', '272', '278']}, {'id': '257', 'type': 'expression_statement', 'children': ['258']}; {'id': '258', 'type': 'assignment', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '260', 'type': 'call', 'children': ['261', '262']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'Thread'}; {'id': '262', 'type': 'argument_list', 'children': ['263', '266', '269']}, {'id': '263', 'type': 'keyword_argument', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'target'}, {'id': '265', 'type': 'identifier', 'children': [], 'value': '_execute'}; {'id': '266', 'type': 'keyword_argument', 'children': ['267', '268']}, {'id': '267', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '268', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '269', 'type': 'keyword_argument', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'kw'}; {'id': '272', 'type': 'expression_statement', 'children': ['273']}, {'id': '273', 'type': 'assignment', 'children': ['274', '277']}; {'id': '274', 'type': 'attribute', 'children': ['275', '276']}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'daemon'}, {'id': '277', 'type': 'True', 'children': []}; {'id': '278', 'type': 'expression_statement', 'children': ['279']}, {'id': '279', 'type': 'call', 'children': ['280', '283']}; {'id': '280', 'type': 'attribute', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 't'}; {'id': '282', 'type': 'identifier', 'children': [], 'value': 'start'}, {'id': '283', 'type': 'argument_list', 'children': []}; {'id': '284', 'type': 'for_statement', 'children': ['285', '286', '287']}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '286', 'type': 'identifier', 'children': [], 'value': 'handlers'}, {'id': '287', 'type': 'block', 'children': ['288', '295']}; {'id': '288', 'type': 'expression_statement', 'children': ['289']}, {'id': '289', 'type': 'call', 'children': ['290', '293']}; {'id': '290', 'type': 'attribute', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '292', 'type': 'identifier', 'children': [], 'value': 'put'}, {'id': '293', 'type': 'argument_list', 'children': ['294']}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '295', 'type': 'if_statement', 'children': ['296', '299']}; {'id': '296', 'type': 'attribute', 'children': ['297', '298']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'asynch'}, {'id': '299', 'type': 'block', 'children': ['300', '309']}; {'id': '300', 'type': 'expression_statement', 'children': ['301']}, {'id': '301', 'type': 'assignment', 'children': ['302', '306']}; {'id': '302', 'type': 'pattern_list', 'children': ['303', '304', '305']}, {'id': '303', 'type': 'identifier', 'children': [], 'value': 'h'}; {'id': '304', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '305', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '306', 'type': 'subscript', 'children': ['307', '308']}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'handlers'}; {'id': '308', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '309', 'type': 'expression_statement', 'children': ['310']}; {'id': '310', 'type': 'call', 'children': ['311', '314']}, {'id': '311', 'type': 'attribute', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '314', 'type': 'argument_list', 'children': ['315']}, {'id': '315', 'type': 'tuple', 'children': ['316', '317', '318']}; {'id': '316', 'type': 'None', 'children': []}, {'id': '317', 'type': 'None', 'children': []}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'h'}, {'id': '319', 'type': 'for_statement', 'children': ['320', '321', '325']}; {'id': '320', 'type': 'identifier', 'children': [], 'value': '_'}, {'id': '321', 'type': 'call', 'children': ['322', '323']}; {'id': '322', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '323', 'type': 'argument_list', 'children': ['324']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'threads'}, {'id': '325', 'type': 'block', 'children': ['326']}; {'id': '326', 'type': 'expression_statement', 'children': ['327']}, {'id': '327', 'type': 'call', 'children': ['328', '331']}; {'id': '328', 'type': 'attribute', 'children': ['329', '330']}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'queue'}; {'id': '330', 'type': 'identifier', 'children': [], 'value': 'put'}, {'id': '331', 'type': 'argument_list', 'children': ['332']}; {'id': '332', 'type': 'None', 'children': []}, {'id': '333', 'type': 'if_statement', 'children': ['334', '338']}; {'id': '334', 'type': 'not_operator', 'children': ['335']}, {'id': '335', 'type': 'attribute', 'children': ['336', '337']}; {'id': '336', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '337', 'type': 'identifier', 'children': [], 'value': 'asynch'}; {'id': '338', 'type': 'block', 'children': ['339']}, {'id': '339', 'type': 'expression_statement', 'children': ['340']}; {'id': '340', 'type': 'call', 'children': ['341', '344']}, {'id': '341', 'type': 'attribute', 'children': ['342', '343']}; {'id': '342', 'type': 'identifier', 'children': [], 'value': 'queue'}, {'id': '343', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '344', 'type': 'argument_list', 'children': []}, {'id': '345', 'type': 'return_statement', 'children': ['346']}; {'id': '346', 'type': 'boolean_operator', 'children': ['347', '351'], 'value': 'or'}, {'id': '347', 'type': 'call', 'children': ['348', '349']}; {'id': '348', 'type': 'identifier', 'children': [], 'value': 'tuple'}, {'id': '349', 'type': 'argument_list', 'children': ['350']}; {'id': '350', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '351', 'type': 'None', 'children': []} | def fire(self, *args, **kw):
result = []
with self._hlock:
handlers = self.handlers
if self.threads == 0:
for k in handlers:
h, m, t = handlers[k]
try:
r = self._memoize(h, m, t, *args, **kw)
result.append(tuple(r))
except:
result.append((False, self._error(sys.exc_info()), h))
elif self.threads > 0:
queue = Queue()
rlock = RLock()
def _execute(*args, **kw):
while True:
try:
item = queue.get()
if item is None:
queue.task_done()
break
h, m, t = handlers[item]
try:
r = self._memoize(h, m, t, *args, **kw)
if not self.asynch:
with rlock:
result.append(tuple(r))
except:
if not self.asynch:
with rlock:
result.append((False, self._error(sys.exc_info()), h))
queue.task_done()
except Empty:
break
if handlers:
threads = self._threads(handlers=handlers)
for _ in range(threads):
t = Thread(target=_execute, args=args, kwargs=kw)
t.daemon = True
t.start()
for k in handlers:
queue.put(k)
if self.asynch:
h, _, _ = handlers[k]
result.append((None, None, h))
for _ in range(threads):
queue.put(None)
if not self.asynch:
queue.join()
return tuple(result) or None |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '18']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'runGetResults'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8', '11']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '7', 'type': 'True', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '10', 'type': 'True', 'children': []}, {'id': '11', 'type': 'default_parameter', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'encoding'}, {'id': '13', 'type': 'call', 'children': ['14', '17']}; {'id': '14', 'type': 'attribute', 'children': ['15', '16']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'sys'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'getdefaultencoding'}, {'id': '17', 'type': 'argument_list', 'children': []}; {'id': '18', 'type': 'block', 'children': ['19', '21', '59', '94', '115', '187', '191', '195', '199', '233', '267', '271', '278', '369', '409', '415']}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'string', 'children': [], 'value': '\'\'\'\n runGetResults - Simple method to run a command and return the results of the execution as a dict.\n @param cmd <str/list> - String of command and arguments, or list of command and arguments\n If cmd is a string, the command will be executed as if ran exactly as written in a shell. This mode supports shell-isms like \'&&\' and \'|\'\n If cmd is a list, the first element will be the executable, and further elements are arguments that will be passed to that executable.\n @param stdout <True/False> - Default True, Whether to gather and include program\'s stdout data in results.\n If False, that data the program prints to stdout will just be output to the current tty and not recorded.\n If True, it will NOT be output to the tty, and will be recorded under the key "stdout" in the return dict.\n @param stderr <True/False or "stdout"/subprocess.STDOUT> - Default True, Whether to gather and include program\'s stderr data in results, or to combine with "stdout" data.\n If False, the data the program prints to stderr will just be output to the current tty and not recorded\n If True, it will NOT be output to the tty, and will be recorded under the key "stderr" in the return dict.\n If "stdout" or subprocess.STDOUT - stderr data will be blended with stdout data. This requires that stdout=True.\n @param encoding <None/str> - Default sys.getdefaultencoding(), the program\'s output will automatically be decoded using the provided codec (e.x. "utf-8" or "ascii").\n If None or False-ish, data will not be decoded (i.e. in python3 will be "bytes" type)\n If unsure, leave this as it\'s default value, or provide "utf-8"\n @return <dict> - Dict of results. Has following keys:\n \'returnCode\' - <int> - Always present, included the integer return-code from the command.\n \'stdout\' <unciode/str/bytes (depending on\n \'stderr\' <unicode/str/bytes (depending on\n @raises - SimpleCommandFailure if it cannot launch the given command, for reasons such as: cannot find the executable, or no permission to execute, etc\n \'\'\''}, {'id': '21', 'type': 'if_statement', 'children': ['22', '29', '36', '53']}; {'id': '22', 'type': 'comparison_operator', 'children': ['23', '24'], 'value': 'in'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '24', 'type': 'tuple', 'children': ['25', '26']}, {'id': '25', 'type': 'string', 'children': [], 'value': "'stdout'"}; {'id': '26', 'type': 'attribute', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'subprocess'}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'STDOUT'}, {'id': '29', 'type': 'block', 'children': ['30']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'assignment', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '33', 'type': 'attribute', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'subprocess'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'STDOUT'}; {'id': '36', 'type': 'elif_clause', 'children': ['37', '46']}, {'id': '37', 'type': 'boolean_operator', 'children': ['38', '41'], 'value': 'or'}; {'id': '38', 'type': 'comparison_operator', 'children': ['39', '40'], 'value': '=='}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '40', 'type': 'True', 'children': []}, {'id': '41', 'type': 'comparison_operator', 'children': ['42', '43'], 'value': '=='}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'subprocess'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'PIPE'}; {'id': '46', 'type': 'block', 'children': ['47']}, {'id': '47', 'type': 'expression_statement', 'children': ['48']}; {'id': '48', 'type': 'assignment', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '50', 'type': 'attribute', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'subprocess'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'PIPE'}, {'id': '53', 'type': 'else_clause', 'children': ['54']}; {'id': '54', 'type': 'block', 'children': ['55']}, {'id': '55', 'type': 'expression_statement', 'children': ['56']}; {'id': '56', 'type': 'assignment', 'children': ['57', '58']}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '58', 'type': 'None', 'children': []}, {'id': '59', 'type': 'if_statement', 'children': ['60', '69', '76']}; {'id': '60', 'type': 'boolean_operator', 'children': ['61', '64'], 'value': 'or'}, {'id': '61', 'type': 'comparison_operator', 'children': ['62', '63'], 'value': '=='}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '63', 'type': 'True', 'children': []}; {'id': '64', 'type': 'comparison_operator', 'children': ['65', '66'], 'value': '=='}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'stdout'}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'subprocess'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'STDOUT'}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'assignment', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '73', 'type': 'attribute', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'subprocess'}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'PIPE'}; {'id': '76', 'type': 'else_clause', 'children': ['77']}, {'id': '77', 'type': 'block', 'children': ['78', '82']}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '81', 'type': 'None', 'children': []}; {'id': '82', 'type': 'if_statement', 'children': ['83', '88']}, {'id': '83', 'type': 'comparison_operator', 'children': ['84', '85'], 'value': '=='}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'subprocess'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'PIPE'}; {'id': '88', 'type': 'block', 'children': ['89']}, {'id': '89', 'type': 'raise_statement', 'children': ['90']}; {'id': '90', 'type': 'call', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '92', 'type': 'argument_list', 'children': ['93']}, {'id': '93', 'type': 'string', 'children': [], 'value': "'Cannot redirect stderr to stdout if stdout is not captured.'"}; {'id': '94', 'type': 'if_statement', 'children': ['95', '104', '109']}, {'id': '95', 'type': 'call', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'issubclass'}, {'id': '97', 'type': 'argument_list', 'children': ['98', '101']}; {'id': '98', 'type': 'attribute', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': '__class__'}, {'id': '101', 'type': 'tuple', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'tuple'}; {'id': '104', 'type': 'block', 'children': ['105']}, {'id': '105', 'type': 'expression_statement', 'children': ['106']}; {'id': '106', 'type': 'assignment', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'shell'}; {'id': '108', 'type': 'False', 'children': []}, {'id': '109', 'type': 'else_clause', 'children': ['110']}; {'id': '110', 'type': 'block', 'children': ['111']}, {'id': '111', 'type': 'expression_statement', 'children': ['112']}; {'id': '112', 'type': 'assignment', 'children': ['113', '114']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'shell'}; {'id': '114', 'type': 'True', 'children': []}, {'id': '115', 'type': 'try_statement', 'children': ['116', '135']}; {'id': '116', 'type': 'block', 'children': ['117']}, {'id': '117', 'type': 'expression_statement', 'children': ['118']}; {'id': '118', 'type': 'assignment', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'pipe'}; {'id': '120', 'type': 'call', 'children': ['121', '124']}, {'id': '121', 'type': 'attribute', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'subprocess'}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'Popen'}; {'id': '124', 'type': 'argument_list', 'children': ['125', '126', '129', '132']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '126', 'type': 'keyword_argument', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'stdout'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '129', 'type': 'keyword_argument', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '132', 'type': 'keyword_argument', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'shell'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'shell'}, {'id': '135', 'type': 'except_clause', 'children': ['136', '140']}; {'id': '136', 'type': 'as_pattern', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'Exception'}; {'id': '138', 'type': 'as_pattern_target', 'children': ['139']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '140', 'type': 'block', 'children': ['141', '172']}, {'id': '141', 'type': 'try_statement', 'children': ['142', '163']}; {'id': '142', 'type': 'block', 'children': ['143']}, {'id': '143', 'type': 'if_statement', 'children': ['144', '147', '157']}; {'id': '144', 'type': 'comparison_operator', 'children': ['145', '146'], 'value': 'is'}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'shell'}; {'id': '146', 'type': 'True', 'children': []}, {'id': '147', 'type': 'block', 'children': ['148']}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'assignment', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'cmdStr'}, {'id': '151', 'type': 'call', 'children': ['152', '155']}; {'id': '152', 'type': 'attribute', 'children': ['153', '154']}, {'id': '153', 'type': 'string', 'children': [], 'value': "' '"}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '155', 'type': 'argument_list', 'children': ['156']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '157', 'type': 'else_clause', 'children': ['158']}; {'id': '158', 'type': 'block', 'children': ['159']}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'assignment', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'cmdStr'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'cmd'}, {'id': '163', 'type': 'except_clause', 'children': ['164']}; {'id': '164', 'type': 'block', 'children': ['165']}, {'id': '165', 'type': 'expression_statement', 'children': ['166']}; {'id': '166', 'type': 'assignment', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'cmdStr'}; {'id': '168', 'type': 'call', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'repr'}; {'id': '170', 'type': 'argument_list', 'children': ['171']}, {'id': '171', 'type': 'identifier', 'children': [], 'value': 'cmd'}; {'id': '172', 'type': 'raise_statement', 'children': ['173']}, {'id': '173', 'type': 'call', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'SimpleCommandFailure'}, {'id': '175', 'type': 'argument_list', 'children': ['176', '184']}; {'id': '176', 'type': 'binary_operator', 'children': ['177', '178'], 'value': '%'}, {'id': '177', 'type': 'string', 'children': [], 'value': '\'Failed to execute "%s": %s\''}; {'id': '178', 'type': 'tuple', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'cmdStr'}; {'id': '180', 'type': 'call', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'str'}; {'id': '182', 'type': 'argument_list', 'children': ['183']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '184', 'type': 'keyword_argument', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'returnCode'}; {'id': '186', 'type': 'integer', 'children': [], 'value': '255'}, {'id': '187', 'type': 'expression_statement', 'children': ['188']}; {'id': '188', 'type': 'assignment', 'children': ['189', '190']}, {'id': '189', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '190', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '191', 'type': 'expression_statement', 'children': ['192']}; {'id': '192', 'type': 'assignment', 'children': ['193', '194']}, {'id': '193', 'type': 'identifier', 'children': [], 'value': 'fileNoToKey'}; {'id': '194', 'type': 'dictionary', 'children': []}, {'id': '195', 'type': 'expression_statement', 'children': ['196']}; {'id': '196', 'type': 'assignment', 'children': ['197', '198']}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'ret'}; {'id': '198', 'type': 'dictionary', 'children': []}, {'id': '199', 'type': 'if_statement', 'children': ['200', '205']}; {'id': '200', 'type': 'comparison_operator', 'children': ['201', '202'], 'value': '=='}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'stdout'}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'subprocess'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'PIPE'}, {'id': '205', 'type': 'block', 'children': ['206', '215', '227']}; {'id': '206', 'type': 'expression_statement', 'children': ['207']}, {'id': '207', 'type': 'call', 'children': ['208', '211']}; {'id': '208', 'type': 'attribute', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'attribute', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'pipe'}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'stdout'}, {'id': '215', 'type': 'expression_statement', 'children': ['216']}; {'id': '216', 'type': 'assignment', 'children': ['217', '226']}, {'id': '217', 'type': 'subscript', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'fileNoToKey'}, {'id': '219', 'type': 'call', 'children': ['220', '225']}; {'id': '220', 'type': 'attribute', 'children': ['221', '224']}, {'id': '221', 'type': 'attribute', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'pipe'}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'stdout'}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'fileno'}, {'id': '225', 'type': 'argument_list', 'children': []}; {'id': '226', 'type': 'string', 'children': [], 'value': "'stdout'"}, {'id': '227', 'type': 'expression_statement', 'children': ['228']}; {'id': '228', 'type': 'assignment', 'children': ['229', '232']}, {'id': '229', 'type': 'subscript', 'children': ['230', '231']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '231', 'type': 'string', 'children': [], 'value': "'stdout'"}; {'id': '232', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '233', 'type': 'if_statement', 'children': ['234', '239']}; {'id': '234', 'type': 'comparison_operator', 'children': ['235', '236'], 'value': '=='}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '236', 'type': 'attribute', 'children': ['237', '238']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'subprocess'}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'PIPE'}, {'id': '239', 'type': 'block', 'children': ['240', '249', '261']}; {'id': '240', 'type': 'expression_statement', 'children': ['241']}, {'id': '241', 'type': 'call', 'children': ['242', '245']}; {'id': '242', 'type': 'attribute', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '245', 'type': 'argument_list', 'children': ['246']}; {'id': '246', 'type': 'attribute', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'pipe'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'stderr'}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'assignment', 'children': ['251', '260']}, {'id': '251', 'type': 'subscript', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'fileNoToKey'}, {'id': '253', 'type': 'call', 'children': ['254', '259']}; {'id': '254', 'type': 'attribute', 'children': ['255', '258']}, {'id': '255', 'type': 'attribute', 'children': ['256', '257']}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'pipe'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'stderr'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'fileno'}, {'id': '259', 'type': 'argument_list', 'children': []}; {'id': '260', 'type': 'string', 'children': [], 'value': "'stderr'"}, {'id': '261', 'type': 'expression_statement', 'children': ['262']}; {'id': '262', 'type': 'assignment', 'children': ['263', '266']}, {'id': '263', 'type': 'subscript', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '265', 'type': 'string', 'children': [], 'value': "'stderr'"}; {'id': '266', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'assignment', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'returnCode'}; {'id': '270', 'type': 'None', 'children': []}, {'id': '271', 'type': 'expression_statement', 'children': ['272']}; {'id': '272', 'type': 'call', 'children': ['273', '276']}, {'id': '273', 'type': 'attribute', 'children': ['274', '275']}; {'id': '274', 'type': 'identifier', 'children': [], 'value': 'time'}, {'id': '275', 'type': 'identifier', 'children': [], 'value': 'sleep'}; {'id': '276', 'type': 'argument_list', 'children': ['277']}, {'id': '277', 'type': 'float', 'children': [], 'value': '.02'}; {'id': '278', 'type': 'while_statement', 'children': ['279', '284']}, {'id': '279', 'type': 'boolean_operator', 'children': ['280', '283'], 'value': 'or'}; {'id': '280', 'type': 'comparison_operator', 'children': ['281', '282'], 'value': 'is'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'returnCode'}; {'id': '282', 'type': 'None', 'children': []}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '284', 'type': 'block', 'children': ['285', '293']}, {'id': '285', 'type': 'expression_statement', 'children': ['286']}; {'id': '286', 'type': 'assignment', 'children': ['287', '288']}, {'id': '287', 'type': 'identifier', 'children': [], 'value': 'returnCode'}; {'id': '288', 'type': 'call', 'children': ['289', '292']}, {'id': '289', 'type': 'attribute', 'children': ['290', '291']}; {'id': '290', 'type': 'identifier', 'children': [], 'value': 'pipe'}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'poll'}; {'id': '292', 'type': 'argument_list', 'children': []}, {'id': '293', 'type': 'while_statement', 'children': ['294', '295']}; {'id': '294', 'type': 'True', 'children': []}, {'id': '295', 'type': 'block', 'children': ['296', '311', '323']}; {'id': '296', 'type': 'expression_statement', 'children': ['297']}, {'id': '297', 'type': 'assignment', 'children': ['298', '302']}; {'id': '298', 'type': 'tuple_pattern', 'children': ['299', '300', '301']}, {'id': '299', 'type': 'identifier', 'children': [], 'value': 'readyToRead'}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'junk1'}, {'id': '301', 'type': 'identifier', 'children': [], 'value': 'junk2'}; {'id': '302', 'type': 'call', 'children': ['303', '306']}, {'id': '303', 'type': 'attribute', 'children': ['304', '305']}; {'id': '304', 'type': 'identifier', 'children': [], 'value': 'select'}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'select'}; {'id': '306', 'type': 'argument_list', 'children': ['307', '308', '309', '310']}, {'id': '307', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '308', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '309', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '310', 'type': 'float', 'children': [], 'value': '.005'}, {'id': '311', 'type': 'if_statement', 'children': ['312', '314']}; {'id': '312', 'type': 'not_operator', 'children': ['313']}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'readyToRead'}; {'id': '314', 'type': 'block', 'children': ['315', '322']}, {'id': '315', 'type': 'expression_statement', 'children': ['316']}; {'id': '316', 'type': 'call', 'children': ['317', '320']}, {'id': '317', 'type': 'attribute', 'children': ['318', '319']}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'time'}, {'id': '319', 'type': 'identifier', 'children': [], 'value': 'sleep'}; {'id': '320', 'type': 'argument_list', 'children': ['321']}, {'id': '321', 'type': 'float', 'children': [], 'value': '.01'}; {'id': '322', 'type': 'break_statement', 'children': []}, {'id': '323', 'type': 'for_statement', 'children': ['324', '325', '326']}; {'id': '324', 'type': 'identifier', 'children': [], 'value': 'readyStream'}, {'id': '325', 'type': 'identifier', 'children': [], 'value': 'readyToRead'}; {'id': '326', 'type': 'block', 'children': ['327', '337', '345', '360']}, {'id': '327', 'type': 'expression_statement', 'children': ['328']}; {'id': '328', 'type': 'assignment', 'children': ['329', '330']}, {'id': '329', 'type': 'identifier', 'children': [], 'value': 'retKey'}; {'id': '330', 'type': 'subscript', 'children': ['331', '332']}, {'id': '331', 'type': 'identifier', 'children': [], 'value': 'fileNoToKey'}; {'id': '332', 'type': 'call', 'children': ['333', '336']}, {'id': '333', 'type': 'attribute', 'children': ['334', '335']}; {'id': '334', 'type': 'identifier', 'children': [], 'value': 'readyStream'}, {'id': '335', 'type': 'identifier', 'children': [], 'value': 'fileno'}; {'id': '336', 'type': 'argument_list', 'children': []}, {'id': '337', 'type': 'expression_statement', 'children': ['338']}; {'id': '338', 'type': 'assignment', 'children': ['339', '340']}, {'id': '339', 'type': 'identifier', 'children': [], 'value': 'curRead'}; {'id': '340', 'type': 'call', 'children': ['341', '344']}, {'id': '341', 'type': 'attribute', 'children': ['342', '343']}; {'id': '342', 'type': 'identifier', 'children': [], 'value': 'readyStream'}, {'id': '343', 'type': 'identifier', 'children': [], 'value': 'read'}; {'id': '344', 'type': 'argument_list', 'children': []}, {'id': '345', 'type': 'if_statement', 'children': ['346', '351']}; {'id': '346', 'type': 'comparison_operator', 'children': ['347', '348'], 'value': 'in'}, {'id': '347', 'type': 'identifier', 'children': [], 'value': 'curRead'}; {'id': '348', 'type': 'tuple', 'children': ['349', '350']}, {'id': '349', 'type': 'string', 'children': [], 'value': "b''"}; {'id': '350', 'type': 'string', 'children': [], 'value': "''"}, {'id': '351', 'type': 'block', 'children': ['352', '359']}; {'id': '352', 'type': 'expression_statement', 'children': ['353']}, {'id': '353', 'type': 'call', 'children': ['354', '357']}; {'id': '354', 'type': 'attribute', 'children': ['355', '356']}, {'id': '355', 'type': 'identifier', 'children': [], 'value': 'streams'}; {'id': '356', 'type': 'identifier', 'children': [], 'value': 'remove'}, {'id': '357', 'type': 'argument_list', 'children': ['358']}; {'id': '358', 'type': 'identifier', 'children': [], 'value': 'readyStream'}, {'id': '359', 'type': 'continue_statement', 'children': []}; {'id': '360', 'type': 'expression_statement', 'children': ['361']}, {'id': '361', 'type': 'call', 'children': ['362', '367']}; {'id': '362', 'type': 'attribute', 'children': ['363', '366']}, {'id': '363', 'type': 'subscript', 'children': ['364', '365']}; {'id': '364', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '365', 'type': 'identifier', 'children': [], 'value': 'retKey'}; {'id': '366', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '367', 'type': 'argument_list', 'children': ['368']}; {'id': '368', 'type': 'identifier', 'children': [], 'value': 'curRead'}, {'id': '369', 'type': 'for_statement', 'children': ['370', '371', '379']}; {'id': '370', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '371', 'type': 'call', 'children': ['372', '373']}; {'id': '372', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '373', 'type': 'argument_list', 'children': ['374']}; {'id': '374', 'type': 'call', 'children': ['375', '378']}, {'id': '375', 'type': 'attribute', 'children': ['376', '377']}; {'id': '376', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '377', 'type': 'identifier', 'children': [], 'value': 'keys'}; {'id': '378', 'type': 'argument_list', 'children': []}, {'id': '379', 'type': 'block', 'children': ['380', '393']}; {'id': '380', 'type': 'expression_statement', 'children': ['381']}, {'id': '381', 'type': 'assignment', 'children': ['382', '385']}; {'id': '382', 'type': 'subscript', 'children': ['383', '384']}, {'id': '383', 'type': 'identifier', 'children': [], 'value': 'ret'}; {'id': '384', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '385', 'type': 'call', 'children': ['386', '389']}; {'id': '386', 'type': 'attribute', 'children': ['387', '388']}, {'id': '387', 'type': 'string', 'children': [], 'value': "b''"}; {'id': '388', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '389', 'type': 'argument_list', 'children': ['390']}; {'id': '390', 'type': 'subscript', 'children': ['391', '392']}, {'id': '391', 'type': 'identifier', 'children': [], 'value': 'ret'}; {'id': '392', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '393', 'type': 'if_statement', 'children': ['394', '395']}; {'id': '394', 'type': 'identifier', 'children': [], 'value': 'encoding'}, {'id': '395', 'type': 'block', 'children': ['396']}; {'id': '396', 'type': 'expression_statement', 'children': ['397']}, {'id': '397', 'type': 'assignment', 'children': ['398', '401']}; {'id': '398', 'type': 'subscript', 'children': ['399', '400']}, {'id': '399', 'type': 'identifier', 'children': [], 'value': 'ret'}; {'id': '400', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '401', 'type': 'call', 'children': ['402', '407']}; {'id': '402', 'type': 'attribute', 'children': ['403', '406']}, {'id': '403', 'type': 'subscript', 'children': ['404', '405']}; {'id': '404', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '405', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '406', 'type': 'identifier', 'children': [], 'value': 'decode'}, {'id': '407', 'type': 'argument_list', 'children': ['408']}; {'id': '408', 'type': 'identifier', 'children': [], 'value': 'encoding'}, {'id': '409', 'type': 'expression_statement', 'children': ['410']}; {'id': '410', 'type': 'assignment', 'children': ['411', '414']}, {'id': '411', 'type': 'subscript', 'children': ['412', '413']}; {'id': '412', 'type': 'identifier', 'children': [], 'value': 'ret'}, {'id': '413', 'type': 'string', 'children': [], 'value': "'returnCode'"}; {'id': '414', 'type': 'identifier', 'children': [], 'value': 'returnCode'}, {'id': '415', 'type': 'return_statement', 'children': ['416']} | def runGetResults(cmd, stdout=True, stderr=True, encoding=sys.getdefaultencoding()):
'''
runGetResults - Simple method to run a command and return the results of the execution as a dict.
@param cmd <str/list> - String of command and arguments, or list of command and arguments
If cmd is a string, the command will be executed as if ran exactly as written in a shell. This mode supports shell-isms like '&&' and '|'
If cmd is a list, the first element will be the executable, and further elements are arguments that will be passed to that executable.
@param stdout <True/False> - Default True, Whether to gather and include program's stdout data in results.
If False, that data the program prints to stdout will just be output to the current tty and not recorded.
If True, it will NOT be output to the tty, and will be recorded under the key "stdout" in the return dict.
@param stderr <True/False or "stdout"/subprocess.STDOUT> - Default True, Whether to gather and include program's stderr data in results, or to combine with "stdout" data.
If False, the data the program prints to stderr will just be output to the current tty and not recorded
If True, it will NOT be output to the tty, and will be recorded under the key "stderr" in the return dict.
If "stdout" or subprocess.STDOUT - stderr data will be blended with stdout data. This requires that stdout=True.
@param encoding <None/str> - Default sys.getdefaultencoding(), the program's output will automatically be decoded using the provided codec (e.x. "utf-8" or "ascii").
If None or False-ish, data will not be decoded (i.e. in python3 will be "bytes" type)
If unsure, leave this as it's default value, or provide "utf-8"
@return <dict> - Dict of results. Has following keys:
'returnCode' - <int> - Always present, included the integer return-code from the command.
'stdout' <unciode/str/bytes (depending on
'stderr' <unicode/str/bytes (depending on
@raises - SimpleCommandFailure if it cannot launch the given command, for reasons such as: cannot find the executable, or no permission to execute, etc
'''
if stderr in ('stdout', subprocess.STDOUT):
stderr = subprocess.STDOUT
elif stderr == True or stderr == subprocess.PIPE:
stderr = subprocess.PIPE
else:
stderr = None
if stdout == True or stdout == subprocess.STDOUT:
stdout = subprocess.PIPE
else:
stdout = None
if stderr == subprocess.PIPE:
raise ValueError('Cannot redirect stderr to stdout if stdout is not captured.')
if issubclass(cmd.__class__, (list, tuple)):
shell = False
else:
shell = True
try:
pipe = subprocess.Popen(cmd, stdout=stdout, stderr=stderr, shell=shell)
except Exception as e:
try:
if shell is True:
cmdStr = ' '.join(cmd)
else:
cmdStr = cmd
except:
cmdStr = repr(cmd)
raise SimpleCommandFailure('Failed to execute "%s": %s' %(cmdStr, str(e)), returnCode=255)
streams = []
fileNoToKey = {}
ret = {}
if stdout == subprocess.PIPE:
streams.append(pipe.stdout)
fileNoToKey[pipe.stdout.fileno()] = 'stdout'
ret['stdout'] = []
if stderr == subprocess.PIPE:
streams.append(pipe.stderr)
fileNoToKey[pipe.stderr.fileno()] = 'stderr'
ret['stderr'] = []
returnCode = None
time.sleep(.02)
while returnCode is None or streams:
returnCode = pipe.poll()
while True:
(readyToRead, junk1, junk2) = select.select(streams, [], [], .005)
if not readyToRead:
time.sleep(.01)
break
for readyStream in readyToRead:
retKey = fileNoToKey[readyStream.fileno()]
curRead = readyStream.read()
if curRead in (b'', ''):
streams.remove(readyStream)
continue
ret[retKey].append(curRead)
for key in list(ret.keys()):
ret[key] = b''.join(ret[key])
if encoding:
ret[key] = ret[key].decode(encoding)
ret['returnCode'] = returnCode
return ret |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_pathway_feature_permutation'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'pathway_feature_tuples'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'permutation_max_iters'}; {'id': '6', 'type': 'block', 'children': ['7', '24', '31', '38', '42', '46', '245', '252']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '12']}, {'id': '9', 'type': 'pattern_list', 'children': ['10', '11']}; {'id': '10', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'features'}; {'id': '12', 'type': 'list_comprehension', 'children': ['13', '17']}, {'id': '13', 'type': 'call', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '15', 'type': 'argument_list', 'children': ['16']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'elements_at_position'}, {'id': '17', 'type': 'for_in_clause', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'elements_at_position'}, {'id': '19', 'type': 'call', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 'zip'}, {'id': '21', 'type': 'argument_list', 'children': ['22']}; {'id': '22', 'type': 'list_splat', 'children': ['23']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'pathway_feature_tuples'}; {'id': '24', 'type': 'expression_statement', 'children': ['25']}, {'id': '25', 'type': 'assignment', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'original_pathways'}, {'id': '27', 'type': 'subscript', 'children': ['28', '29']}; {'id': '28', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '29', 'type': 'slice', 'children': ['30']}; {'id': '30', 'type': 'colon', 'children': []}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'call', 'children': ['33', '36']}, {'id': '33', 'type': 'attribute', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'random'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': 'shuffle'}; {'id': '36', 'type': 'argument_list', 'children': ['37']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '38', 'type': 'expression_statement', 'children': ['39']}, {'id': '39', 'type': 'assignment', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'feature_block_locations'}, {'id': '41', 'type': 'dictionary', 'children': []}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '45', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '46', 'type': 'while_statement', 'children': ['47', '53']}, {'id': '47', 'type': 'comparison_operator', 'children': ['48', '49'], 'value': '<'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '49', 'type': 'call', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '53', 'type': 'block', 'children': ['54', '58', '64', '70', '233', '237']}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'starting_index'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '58', 'type': 'expression_statement', 'children': ['59']}, {'id': '59', 'type': 'assignment', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'current_feature'}, {'id': '61', 'type': 'subscript', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'features'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'pathway_set'}, {'id': '67', 'type': 'call', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '69', 'type': 'argument_list', 'children': []}; {'id': '70', 'type': 'while_statement', 'children': ['71', '83']}, {'id': '71', 'type': 'boolean_operator', 'children': ['72', '78'], 'value': 'and'}; {'id': '72', 'type': 'comparison_operator', 'children': ['73', '74'], 'value': '<'}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '74', 'type': 'call', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '76', 'type': 'argument_list', 'children': ['77']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '78', 'type': 'comparison_operator', 'children': ['79', '82'], 'value': '=='}, {'id': '79', 'type': 'subscript', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'features'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'current_feature'}, {'id': '83', 'type': 'block', 'children': ['84', '229']}; {'id': '84', 'type': 'if_statement', 'children': ['85', '90', '100']}, {'id': '85', 'type': 'comparison_operator', 'children': ['86', '89'], 'value': 'not'}; {'id': '86', 'type': 'subscript', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'pathway_set'}; {'id': '90', 'type': 'block', 'children': ['91']}, {'id': '91', 'type': 'expression_statement', 'children': ['92']}; {'id': '92', 'type': 'call', 'children': ['93', '96']}, {'id': '93', 'type': 'attribute', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'pathway_set'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'add'}; {'id': '96', 'type': 'argument_list', 'children': ['97']}, {'id': '97', 'type': 'subscript', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '100', 'type': 'else_clause', 'children': ['101']}, {'id': '101', 'type': 'block', 'children': ['102', '106', '110', '208', '215', '223']}; {'id': '102', 'type': 'expression_statement', 'children': ['103']}, {'id': '103', 'type': 'assignment', 'children': ['104', '105']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'k'}, {'id': '105', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}, {'id': '109', 'type': 'None', 'children': []}; {'id': '110', 'type': 'while_statement', 'children': ['111', '112']}, {'id': '111', 'type': 'True', 'children': []}; {'id': '112', 'type': 'block', 'children': ['113', '129', '135', '141', '185', '189']}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'assignment', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '116', 'type': 'call', 'children': ['117', '120']}, {'id': '117', 'type': 'attribute', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'random'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'choice'}; {'id': '120', 'type': 'argument_list', 'children': ['121']}, {'id': '121', 'type': 'call', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '125']}; {'id': '124', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '125', 'type': 'call', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '127', 'type': 'argument_list', 'children': ['128']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '129', 'type': 'expression_statement', 'children': ['130']}; {'id': '130', 'type': 'assignment', 'children': ['131', '132']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}; {'id': '132', 'type': 'subscript', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '135', 'type': 'expression_statement', 'children': ['136']}; {'id': '136', 'type': 'assignment', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'random_feature'}; {'id': '138', 'type': 'subscript', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'features'}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '141', 'type': 'if_statement', 'children': ['142', '152']}; {'id': '142', 'type': 'parenthesized_expression', 'children': ['143']}, {'id': '143', 'type': 'boolean_operator', 'children': ['144', '149'], 'value': 'and'}; {'id': '144', 'type': 'comparison_operator', 'children': ['145', '146'], 'value': '!='}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}; {'id': '146', 'type': 'subscript', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '149', 'type': 'comparison_operator', 'children': ['150', '151'], 'value': 'not'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'pathway_set'}; {'id': '152', 'type': 'block', 'children': ['153', '159', '168', '177']}, {'id': '153', 'type': 'if_statement', 'children': ['154', '157']}; {'id': '154', 'type': 'comparison_operator', 'children': ['155', '156'], 'value': 'not'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'random_feature'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'feature_block_locations'}, {'id': '157', 'type': 'block', 'children': ['158']}; {'id': '158', 'type': 'break_statement', 'children': []}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'assignment', 'children': ['161', '164', '165']}, {'id': '161', 'type': 'pattern_list', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'feature_block_start'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'feature_block_end'}; {'id': '164', 'type': 'line_continuation', 'children': [], 'value': '\\'}, {'id': '165', 'type': 'subscript', 'children': ['166', '167']}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'feature_block_locations'}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'random_feature'}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'pathway_block'}, {'id': '171', 'type': 'subscript', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '173', 'type': 'slice', 'children': ['174', '175', '176']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'feature_block_start'}, {'id': '175', 'type': 'colon', 'children': []}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'feature_block_end'}, {'id': '177', 'type': 'if_statement', 'children': ['178', '183']}; {'id': '178', 'type': 'comparison_operator', 'children': ['179', '182'], 'value': 'not'}, {'id': '179', 'type': 'subscript', 'children': ['180', '181']}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'pathway_block'}, {'id': '183', 'type': 'block', 'children': ['184']}; {'id': '184', 'type': 'break_statement', 'children': []}, {'id': '185', 'type': 'expression_statement', 'children': ['186']}; {'id': '186', 'type': 'augmented_assignment', 'children': ['187', '188'], 'value': '+='}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '188', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '189', 'type': 'if_statement', 'children': ['190', '193']}; {'id': '190', 'type': 'comparison_operator', 'children': ['191', '192'], 'value': '>'}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'k'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'permutation_max_iters'}, {'id': '193', 'type': 'block', 'children': ['194', '206']}; {'id': '194', 'type': 'expression_statement', 'children': ['195']}, {'id': '195', 'type': 'call', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'print'}, {'id': '197', 'type': 'argument_list', 'children': ['198']}; {'id': '198', 'type': 'call', 'children': ['199', '204']}, {'id': '199', 'type': 'attribute', 'children': ['200', '203']}; {'id': '200', 'type': 'concatenated_string', 'children': ['201', '202']}, {'id': '201', 'type': 'string', 'children': [], 'value': '"Permutation step: reached the maximum "'}; {'id': '202', 'type': 'string', 'children': [], 'value': '"number of iterations {0}."'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '204', 'type': 'argument_list', 'children': ['205']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'permutation_max_iters'}; {'id': '206', 'type': 'return_statement', 'children': ['207']}, {'id': '207', 'type': 'None', 'children': []}; {'id': '208', 'type': 'expression_statement', 'children': ['209']}, {'id': '209', 'type': 'call', 'children': ['210', '213']}; {'id': '210', 'type': 'attribute', 'children': ['211', '212']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'pathway_set'}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'add'}, {'id': '213', 'type': 'argument_list', 'children': ['214']}; {'id': '214', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}, {'id': '215', 'type': 'expression_statement', 'children': ['216']}; {'id': '216', 'type': 'assignment', 'children': ['217', '220']}, {'id': '217', 'type': 'subscript', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '220', 'type': 'subscript', 'children': ['221', '222']}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'pathways'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '223', 'type': 'expression_statement', 'children': ['224']}; {'id': '224', 'type': 'assignment', 'children': ['225', '228']}, {'id': '225', 'type': 'subscript', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'random_pathway'}, {'id': '229', 'type': 'expression_statement', 'children': ['230']}; {'id': '230', 'type': 'augmented_assignment', 'children': ['231', '232'], 'value': '+='}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '232', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '233', 'type': 'expression_statement', 'children': ['234']}; {'id': '234', 'type': 'assignment', 'children': ['235', '236']}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'ending_index'}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '237', 'type': 'expression_statement', 'children': ['238']}; {'id': '238', 'type': 'assignment', 'children': ['239', '242']}, {'id': '239', 'type': 'subscript', 'children': ['240', '241']}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'feature_block_locations'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'current_feature'}; {'id': '242', 'type': 'tuple', 'children': ['243', '244']}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'starting_index'}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'ending_index'}, {'id': '245', 'type': 'if_statement', 'children': ['246', '249']}; {'id': '246', 'type': 'comparison_operator', 'children': ['247', '248'], 'value': '=='}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'original_pathways'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'pathways'}, {'id': '249', 'type': 'block', 'children': ['250']}; {'id': '250', 'type': 'return_statement', 'children': ['251']}, {'id': '251', 'type': 'None', 'children': []}; {'id': '252', 'type': 'return_statement', 'children': ['253']}, {'id': '253', 'type': 'call', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '255', 'type': 'argument_list', 'children': ['256']}; {'id': '256', 'type': 'call', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'zip'}; {'id': '258', 'type': 'argument_list', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'pathways'} | def _pathway_feature_permutation(pathway_feature_tuples,
permutation_max_iters):
pathways, features = [list(elements_at_position)
for elements_at_position in
zip(*pathway_feature_tuples)]
original_pathways = pathways[:]
random.shuffle(pathways)
feature_block_locations = {}
i = 0
while i < len(pathways):
starting_index = i
current_feature = features[i]
pathway_set = set()
while i < len(pathways) and features[i] == current_feature:
if pathways[i] not in pathway_set:
pathway_set.add(pathways[i])
else:
k = 0
random_pathway = None
while True:
j = random.choice(range(0, len(pathways)))
random_pathway = pathways[j]
random_feature = features[j]
if (random_pathway != pathways[i] and
random_pathway not in pathway_set):
if random_feature not in feature_block_locations:
break
feature_block_start, feature_block_end = \
feature_block_locations[random_feature]
pathway_block = pathways[feature_block_start:
feature_block_end]
if pathways[i] not in pathway_block:
break
k += 1
if k > permutation_max_iters:
print("Permutation step: reached the maximum "
"number of iterations {0}.".format(
permutation_max_iters))
return None
pathway_set.add(random_pathway)
pathways[j] = pathways[i]
pathways[i] = random_pathway
i += 1
ending_index = i
feature_block_locations[current_feature] = (
starting_index, ending_index)
if original_pathways == pathways:
return None
return list(zip(pathways, features)) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'get_field_infos'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'code'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'free_format'}; {'id': '6', 'type': 'block', 'children': ['7', '11', '15', '22', '26', '190']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'assignment', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '10', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '11', 'type': 'expression_statement', 'children': ['12']}; {'id': '12', 'type': 'assignment', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'field_infos'}; {'id': '14', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '15', 'type': 'expression_statement', 'children': ['16']}; {'id': '16', 'type': 'assignment', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'lines'}; {'id': '18', 'type': 'call', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': '_clean_code'}; {'id': '20', 'type': 'argument_list', 'children': ['21']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'code'}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'previous_offset'}, {'id': '25', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '26', 'type': 'for_statement', 'children': ['27', '28', '33']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'process_cobol'}; {'id': '30', 'type': 'argument_list', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'lines'}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'free_format'}, {'id': '33', 'type': 'block', 'children': ['34', '40', '48', '56', '64', '72', '80', '88', '112', '123', '134', '145', '151', '170', '177']}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '37', 'type': 'call', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'PicFieldInfo'}, {'id': '39', 'type': 'argument_list', 'children': []}; {'id': '40', 'type': 'expression_statement', 'children': ['41']}, {'id': '41', 'type': 'assignment', 'children': ['42', '45']}; {'id': '42', 'type': 'attribute', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '45', 'type': 'subscript', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '47', 'type': 'string', 'children': [], 'value': '"name"'}; {'id': '48', 'type': 'expression_statement', 'children': ['49']}, {'id': '49', 'type': 'assignment', 'children': ['50', '53']}; {'id': '50', 'type': 'attribute', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'level'}, {'id': '53', 'type': 'subscript', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '55', 'type': 'string', 'children': [], 'value': '"level"'}; {'id': '56', 'type': 'expression_statement', 'children': ['57']}, {'id': '57', 'type': 'assignment', 'children': ['58', '61']}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'pic'}, {'id': '61', 'type': 'subscript', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '63', 'type': 'string', 'children': [], 'value': '"pic"'}; {'id': '64', 'type': 'expression_statement', 'children': ['65']}, {'id': '65', 'type': 'assignment', 'children': ['66', '69']}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'occurs'}, {'id': '69', 'type': 'subscript', 'children': ['70', '71']}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '71', 'type': 'string', 'children': [], 'value': '"occurs"'}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '77']}; {'id': '74', 'type': 'attribute', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'redefines'}, {'id': '77', 'type': 'subscript', 'children': ['78', '79']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '79', 'type': 'string', 'children': [], 'value': '"redefines"'}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '85']}; {'id': '82', 'type': 'attribute', 'children': ['83', '84']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'indexed_by'}, {'id': '85', 'type': 'subscript', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'row'}, {'id': '87', 'type': 'string', 'children': [], 'value': '"indexed_by"'}; {'id': '88', 'type': 'if_statement', 'children': ['89', '92']}, {'id': '89', 'type': 'attribute', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'redefines'}; {'id': '92', 'type': 'block', 'children': ['93']}, {'id': '93', 'type': 'for_statement', 'children': ['94', '95', '96']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'fib'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'field_infos'}; {'id': '96', 'type': 'block', 'children': ['97']}, {'id': '97', 'type': 'if_statement', 'children': ['98', '105']}; {'id': '98', 'type': 'comparison_operator', 'children': ['99', '102'], 'value': '=='}, {'id': '99', 'type': 'attribute', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'fib'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '102', 'type': 'attribute', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'redefines'}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'offset'}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'fib'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '112', 'type': 'if_statement', 'children': ['113', '118']}, {'id': '113', 'type': 'comparison_operator', 'children': ['114', '117'], 'value': '=='}; {'id': '114', 'type': 'attribute', 'children': ['115', '116']}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'level'}, {'id': '117', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '118', 'type': 'block', 'children': ['119']}, {'id': '119', 'type': 'expression_statement', 'children': ['120']}; {'id': '120', 'type': 'assignment', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '122', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '123', 'type': 'if_statement', 'children': ['124', '129']}; {'id': '124', 'type': 'comparison_operator', 'children': ['125', '128'], 'value': '=='}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'level'}; {'id': '128', 'type': 'integer', 'children': [], 'value': '78'}, {'id': '129', 'type': 'block', 'children': ['130']}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'assignment', 'children': ['132', '133']}; {'id': '132', 'type': 'identifier', 'children': [], 'value': 'offset'}, {'id': '133', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '134', 'type': 'if_statement', 'children': ['135', '140']}, {'id': '135', 'type': 'comparison_operator', 'children': ['136', '139'], 'value': '=='}; {'id': '136', 'type': 'attribute', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'level'}, {'id': '139', 'type': 'integer', 'children': [], 'value': '77'}; {'id': '140', 'type': 'block', 'children': ['141']}, {'id': '141', 'type': 'expression_statement', 'children': ['142']}; {'id': '142', 'type': 'assignment', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '144', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '145', 'type': 'expression_statement', 'children': ['146']}; {'id': '146', 'type': 'assignment', 'children': ['147', '150']}, {'id': '147', 'type': 'attribute', 'children': ['148', '149']}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'offset'}, {'id': '151', 'type': 'if_statement', 'children': ['152', '157', '164']}; {'id': '152', 'type': 'comparison_operator', 'children': ['153', '156'], 'value': '=='}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'level'}; {'id': '156', 'type': 'integer', 'children': [], 'value': '88'}, {'id': '157', 'type': 'block', 'children': ['158']}; {'id': '158', 'type': 'expression_statement', 'children': ['159']}, {'id': '159', 'type': 'assignment', 'children': ['160', '163']}; {'id': '160', 'type': 'attribute', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'fi'}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'offset'}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'previous_offset'}; {'id': '164', 'type': 'else_clause', 'children': ['165']}, {'id': '165', 'type': 'block', 'children': ['166']}; {'id': '166', 'type': 'expression_statement', 'children': ['167']}, {'id': '167', 'type': 'assignment', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'previous_offset'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'offset'}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'call', 'children': ['172', '175']}; {'id': '172', 'type': 'attribute', 'children': ['173', '174']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'field_infos'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'fi'}, {'id': '177', 'type': 'if_statement', 'children': ['178', '181']}; {'id': '178', 'type': 'subscript', 'children': ['179', '180']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '180', 'type': 'string', 'children': [], 'value': "'pic'"}, {'id': '181', 'type': 'block', 'children': ['182']}; {'id': '182', 'type': 'expression_statement', 'children': ['183']}, {'id': '183', 'type': 'augmented_assignment', 'children': ['184', '185'], 'value': '+='}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'offset'}, {'id': '185', 'type': 'subscript', 'children': ['186', '189']}; {'id': '186', 'type': 'subscript', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'row'}; {'id': '188', 'type': 'string', 'children': [], 'value': "'pic_info'"}, {'id': '189', 'type': 'string', 'children': [], 'value': "'length'"}; {'id': '190', 'type': 'return_statement', 'children': ['191']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'field_infos'} | def get_field_infos(code, free_format):
offset = 0
field_infos = []
lines = _clean_code(code)
previous_offset = 0
for row in process_cobol(lines, free_format):
fi = PicFieldInfo()
fi.name = row["name"]
fi.level = row["level"]
fi.pic = row["pic"]
fi.occurs = row["occurs"]
fi.redefines = row["redefines"]
fi.indexed_by = row["indexed_by"]
if fi.redefines:
for fib in field_infos:
if fib.name == fi.redefines:
offset = fib.offset
if fi.level == 1:
offset = 1
if fi.level == 78:
offset = 0
if fi.level == 77:
offset = 1
fi.offset = offset
if fi.level == 88:
fi.offset = previous_offset
else:
previous_offset = offset
field_infos.append(fi)
if row['pic']:
offset += row['pic_info']['length']
return field_infos |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_advanced_acronym_detection'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6', '7']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'acronyms'}; {'id': '8', 'type': 'block', 'children': ['9', '23', '27', '40', '151', '166', '172', '184', '219']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'acstr'}; {'id': '12', 'type': 'call', 'children': ['13', '16']}, {'id': '13', 'type': 'attribute', 'children': ['14', '15']}; {'id': '14', 'type': 'string', 'children': [], 'value': "''"}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'join'}; {'id': '16', 'type': 'argument_list', 'children': ['17']}, {'id': '17', 'type': 'subscript', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '19', 'type': 'slice', 'children': ['20', '21', '22']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '21', 'type': 'colon', 'children': []}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'range_list'}; {'id': '26', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '27', 'type': 'expression_statement', 'children': ['28']}; {'id': '28', 'type': 'assignment', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'not_range'}; {'id': '30', 'type': 'call', 'children': ['31', '32']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'call', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'range'}, {'id': '35', 'type': 'argument_list', 'children': ['36']}; {'id': '36', 'type': 'call', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '38', 'type': 'argument_list', 'children': ['39']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'acstr'}; {'id': '40', 'type': 'for_statement', 'children': ['41', '42', '43']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'acronym'}; {'id': '42', 'type': 'identifier', 'children': [], 'value': 'acronyms'}, {'id': '43', 'type': 'block', 'children': ['44', '56', '60']}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'rac'}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'regex'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'compile'}, {'id': '51', 'type': 'argument_list', 'children': ['52']}; {'id': '52', 'type': 'call', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'unicode'}; {'id': '54', 'type': 'argument_list', 'children': ['55']}, {'id': '55', 'type': 'identifier', 'children': [], 'value': 'acronym'}; {'id': '56', 'type': 'expression_statement', 'children': ['57']}, {'id': '57', 'type': 'assignment', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '59', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '60', 'type': 'while_statement', 'children': ['61', '62']}, {'id': '61', 'type': 'True', 'children': []}; {'id': '62', 'type': 'block', 'children': ['63', '73', '78', '94', '98', '102', '124']}, {'id': '63', 'type': 'expression_statement', 'children': ['64']}; {'id': '64', 'type': 'assignment', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '66', 'type': 'call', 'children': ['67', '70']}, {'id': '67', 'type': 'attribute', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'rac'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'search'}; {'id': '70', 'type': 'argument_list', 'children': ['71', '72']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'acstr'}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '73', 'type': 'if_statement', 'children': ['74', '76']}; {'id': '74', 'type': 'not_operator', 'children': ['75']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '76', 'type': 'block', 'children': ['77']}, {'id': '77', 'type': 'break_statement', 'children': []}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '83']}; {'id': '80', 'type': 'pattern_list', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'a'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '83', 'type': 'expression_list', 'children': ['84', '89']}; {'id': '84', 'type': 'call', 'children': ['85', '88']}, {'id': '85', 'type': 'attribute', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'm'}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 'start'}; {'id': '88', 'type': 'argument_list', 'children': []}, {'id': '89', 'type': 'call', 'children': ['90', '93']}; {'id': '90', 'type': 'attribute', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'm'}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'end'}, {'id': '93', 'type': 'argument_list', 'children': []}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'n'}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'b'}; {'id': '98', 'type': 'expression_statement', 'children': ['99']}, {'id': '99', 'type': 'assignment', 'children': ['100', '101']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'ok'}, {'id': '101', 'type': 'True', 'children': []}; {'id': '102', 'type': 'for_statement', 'children': ['103', '104', '105']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '105', 'type': 'block', 'children': ['106']}; {'id': '106', 'type': 'if_statement', 'children': ['107', '118']}, {'id': '107', 'type': 'boolean_operator', 'children': ['108', '113'], 'value': 'and'}; {'id': '108', 'type': 'comparison_operator', 'children': ['109', '110'], 'value': '<'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'a'}; {'id': '110', 'type': 'subscript', 'children': ['111', '112']}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '112', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '113', 'type': 'comparison_operator', 'children': ['114', '115'], 'value': '>'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '115', 'type': 'subscript', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '117', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '118', 'type': 'block', 'children': ['119', '123']}, {'id': '119', 'type': 'expression_statement', 'children': ['120']}; {'id': '120', 'type': 'assignment', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'ok'}; {'id': '122', 'type': 'False', 'children': []}, {'id': '123', 'type': 'break_statement', 'children': []}; {'id': '124', 'type': 'if_statement', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'ok'}; {'id': '126', 'type': 'block', 'children': ['127', '136']}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'call', 'children': ['129', '132']}, {'id': '129', 'type': 'attribute', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '132', 'type': 'argument_list', 'children': ['133']}, {'id': '133', 'type': 'tuple', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'a'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'b'}; {'id': '136', 'type': 'for_statement', 'children': ['137', '138', '143']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '138', 'type': 'call', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'xrange'}; {'id': '140', 'type': 'argument_list', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'a'}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'b'}, {'id': '143', 'type': 'block', 'children': ['144']}; {'id': '144', 'type': 'expression_statement', 'children': ['145']}, {'id': '145', 'type': 'call', 'children': ['146', '149']}; {'id': '146', 'type': 'attribute', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'not_range'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'remove'}, {'id': '149', 'type': 'argument_list', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '151', 'type': 'for_statement', 'children': ['152', '153', '154']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'nr'}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'not_range'}; {'id': '154', 'type': 'block', 'children': ['155']}, {'id': '155', 'type': 'expression_statement', 'children': ['156']}; {'id': '156', 'type': 'call', 'children': ['157', '160']}, {'id': '157', 'type': 'attribute', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '160', 'type': 'argument_list', 'children': ['161']}, {'id': '161', 'type': 'tuple', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'nr'}, {'id': '163', 'type': 'binary_operator', 'children': ['164', '165'], 'value': '+'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'nr'}, {'id': '165', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '166', 'type': 'expression_statement', 'children': ['167']}, {'id': '167', 'type': 'call', 'children': ['168', '171']}; {'id': '168', 'type': 'attribute', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'range_list'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'sort'}, {'id': '171', 'type': 'argument_list', 'children': []}; {'id': '172', 'type': 'for_statement', 'children': ['173', '174', '179']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': '_'}; {'id': '174', 'type': 'call', 'children': ['175', '176']}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'xrange'}; {'id': '176', 'type': 'argument_list', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '179', 'type': 'block', 'children': ['180']}; {'id': '180', 'type': 'delete_statement', 'children': ['181']}, {'id': '181', 'type': 'subscript', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '184', 'type': 'for_statement', 'children': ['185', '186', '193']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '186', 'type': 'call', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'xrange'}; {'id': '188', 'type': 'argument_list', 'children': ['189']}, {'id': '189', 'type': 'call', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '191', 'type': 'argument_list', 'children': ['192']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '193', 'type': 'block', 'children': ['194', '200']}; {'id': '194', 'type': 'expression_statement', 'children': ['195']}, {'id': '195', 'type': 'assignment', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'r'}, {'id': '197', 'type': 'subscript', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'j'}; {'id': '200', 'type': 'expression_statement', 'children': ['201']}, {'id': '201', 'type': 'call', 'children': ['202', '205']}; {'id': '202', 'type': 'attribute', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'insert'}, {'id': '205', 'type': 'argument_list', 'children': ['206', '209']}; {'id': '206', 'type': 'binary_operator', 'children': ['207', '208'], 'value': '+'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'j'}, {'id': '209', 'type': 'subscript', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'acstr'}, {'id': '211', 'type': 'slice', 'children': ['212', '215', '216']}; {'id': '212', 'type': 'subscript', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '214', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '215', 'type': 'colon', 'children': []}; {'id': '216', 'type': 'subscript', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'r'}; {'id': '218', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '219', 'type': 'return_statement', 'children': ['220']}; {'id': '220', 'type': 'binary_operator', 'children': ['221', '227'], 'value': '-'}, {'id': '221', 'type': 'binary_operator', 'children': ['222', '223'], 'value': '+'}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '223', 'type': 'call', 'children': ['224', '225']}; {'id': '224', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '225', 'type': 'argument_list', 'children': ['226']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'range_list'}, {'id': '227', 'type': 'integer', 'children': [], 'value': '1'} | def _advanced_acronym_detection(s, i, words, acronyms):
acstr = ''.join(words[s:i])
range_list = []
not_range = set(range(len(acstr)))
for acronym in acronyms:
rac = regex.compile(unicode(acronym))
n = 0
while True:
m = rac.search(acstr, n)
if not m:
break
a, b = m.start(), m.end()
n = b
ok = True
for r in range_list:
if a < r[1] and b > r[0]:
ok = False
break
if ok:
range_list.append((a, b))
for j in xrange(a, b):
not_range.remove(j)
for nr in not_range:
range_list.append((nr, nr + 1))
range_list.sort()
for _ in xrange(s, i):
del words[s]
for j in xrange(len(range_list)):
r = range_list[j]
words.insert(s + j, acstr[r[0]:r[1]])
return s + len(range_list) - 1 |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': '_separate_words'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '5', 'type': 'block', 'children': ['6', '10', '14', '18', '22', '31', '35', '54', '200']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '9', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'assignment', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '13', 'type': 'string', 'children': [], 'value': '""'}; {'id': '14', 'type': 'expression_statement', 'children': ['15']}, {'id': '15', 'type': 'assignment', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '17', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '18', 'type': 'expression_statement', 'children': ['19']}, {'id': '19', 'type': 'assignment', 'children': ['20', '21']}; {'id': '20', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '21', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '22', 'type': 'expression_statement', 'children': ['23']}, {'id': '23', 'type': 'assignment', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '25', 'type': 'subscript', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '27', 'type': 'slice', 'children': ['28', '29', '30']}; {'id': '28', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '29', 'type': 'colon', 'children': []}; {'id': '30', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '31', 'type': 'expression_statement', 'children': ['32']}; {'id': '32', 'type': 'assignment', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'was_upper'}; {'id': '34', 'type': 'False', 'children': []}, {'id': '35', 'type': 'if_statement', 'children': ['36', '41']}; {'id': '36', 'type': 'call', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'isupper'}; {'id': '40', 'type': 'argument_list', 'children': []}, {'id': '41', 'type': 'block', 'children': ['42', '50']}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'string'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '49', 'type': 'argument_list', 'children': []}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 'was_upper'}, {'id': '53', 'type': 'True', 'children': []}; {'id': '54', 'type': 'while_statement', 'children': ['55', '61']}, {'id': '55', 'type': 'comparison_operator', 'children': ['56', '57'], 'value': '<='}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '59', 'type': 'argument_list', 'children': ['60']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '61', 'type': 'block', 'children': ['62', '73', '77', '141', '192', '196']}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '65', 'type': 'subscript', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '67', 'type': 'slice', 'children': ['68', '69', '70']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '69', 'type': 'colon', 'children': []}; {'id': '70', 'type': 'binary_operator', 'children': ['71', '72'], 'value': '+'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '72', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '73', 'type': 'expression_statement', 'children': ['74']}; {'id': '74', 'type': 'assignment', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '76', 'type': 'False', 'children': []}, {'id': '77', 'type': 'if_statement', 'children': ['78', '84', '135']}; {'id': '78', 'type': 'comparison_operator', 'children': ['79', '80'], 'value': '<'}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '80', 'type': 'call', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'len'}; {'id': '82', 'type': 'argument_list', 'children': ['83']}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'string'}; {'id': '84', 'type': 'block', 'children': ['85']}, {'id': '85', 'type': 'if_statement', 'children': ['86', '92', '97', '116']}; {'id': '86', 'type': 'call', 'children': ['87', '90']}, {'id': '87', 'type': 'attribute', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'UPPER'}, {'id': '89', 'type': 'identifier', 'children': [], 'value': 'match'}; {'id': '90', 'type': 'argument_list', 'children': ['91']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '92', 'type': 'block', 'children': ['93']}, {'id': '93', 'type': 'expression_statement', 'children': ['94']}; {'id': '94', 'type': 'assignment', 'children': ['95', '96']}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '96', 'type': 'True', 'children': []}, {'id': '97', 'type': 'elif_clause', 'children': ['98', '111']}; {'id': '98', 'type': 'boolean_operator', 'children': ['99', '105'], 'value': 'and'}, {'id': '99', 'type': 'call', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'NOTSEP'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'match'}, {'id': '103', 'type': 'argument_list', 'children': ['104']}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'c'}, {'id': '105', 'type': 'call', 'children': ['106', '109']}; {'id': '106', 'type': 'attribute', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'SEP'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'match'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '111', 'type': 'block', 'children': ['112']}; {'id': '112', 'type': 'expression_statement', 'children': ['113']}, {'id': '113', 'type': 'assignment', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '115', 'type': 'True', 'children': []}; {'id': '116', 'type': 'elif_clause', 'children': ['117', '130']}, {'id': '117', 'type': 'boolean_operator', 'children': ['118', '124'], 'value': 'and'}; {'id': '118', 'type': 'call', 'children': ['119', '122']}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'SEP'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'match'}; {'id': '122', 'type': 'argument_list', 'children': ['123']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'NOTSEP'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'match'}; {'id': '128', 'type': 'argument_list', 'children': ['129']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'p'}; {'id': '130', 'type': 'block', 'children': ['131']}, {'id': '131', 'type': 'expression_statement', 'children': ['132']}; {'id': '132', 'type': 'assignment', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '134', 'type': 'True', 'children': []}, {'id': '135', 'type': 'else_clause', 'children': ['136']}; {'id': '136', 'type': 'block', 'children': ['137']}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'assignment', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '140', 'type': 'True', 'children': []}, {'id': '141', 'type': 'if_statement', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '143', 'type': 'block', 'children': ['144', '188']}; {'id': '144', 'type': 'if_statement', 'children': ['145', '151', '164']}, {'id': '145', 'type': 'call', 'children': ['146', '149']}; {'id': '146', 'type': 'attribute', 'children': ['147', '148']}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'NOTSEP'}; {'id': '148', 'type': 'identifier', 'children': [], 'value': 'match'}, {'id': '149', 'type': 'argument_list', 'children': ['150']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '151', 'type': 'block', 'children': ['152']}; {'id': '152', 'type': 'expression_statement', 'children': ['153']}, {'id': '153', 'type': 'call', 'children': ['154', '157']}; {'id': '154', 'type': 'attribute', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '157', 'type': 'argument_list', 'children': ['158']}; {'id': '158', 'type': 'subscript', 'children': ['159', '160']}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'string'}; {'id': '160', 'type': 'slice', 'children': ['161', '162', '163']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '162', 'type': 'colon', 'children': []}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '164', 'type': 'else_clause', 'children': ['165']}, {'id': '165', 'type': 'block', 'children': ['166', '181']}; {'id': '166', 'type': 'if_statement', 'children': ['167', '169']}, {'id': '167', 'type': 'not_operator', 'children': ['168']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '169', 'type': 'block', 'children': ['170']}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'assignment', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '173', 'type': 'subscript', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '175', 'type': 'slice', 'children': ['176', '177', '178']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '177', 'type': 'colon', 'children': []}; {'id': '178', 'type': 'binary_operator', 'children': ['179', '180'], 'value': '+'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '180', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '181', 'type': 'expression_statement', 'children': ['182']}; {'id': '182', 'type': 'call', 'children': ['183', '186']}, {'id': '183', 'type': 'attribute', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '186', 'type': 'argument_list', 'children': ['187']}, {'id': '187', 'type': 'None', 'children': []}; {'id': '188', 'type': 'expression_statement', 'children': ['189']}, {'id': '189', 'type': 'assignment', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '192', 'type': 'expression_statement', 'children': ['193']}, {'id': '193', 'type': 'augmented_assignment', 'children': ['194', '195'], 'value': '+='}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '195', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '196', 'type': 'expression_statement', 'children': ['197']}, {'id': '197', 'type': 'assignment', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'p'}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'c'}; {'id': '200', 'type': 'return_statement', 'children': ['201']}, {'id': '201', 'type': 'expression_list', 'children': ['202', '203', '204']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'separator'} | def _separate_words(string):
words = []
separator = ""
i = 1
s = 0
p = string[0:1]
was_upper = False
if string.isupper():
string = string.lower()
was_upper = True
while i <= len(string):
c = string[i:i + 1]
split = False
if i < len(string):
if UPPER.match(c):
split = True
elif NOTSEP.match(c) and SEP.match(p):
split = True
elif SEP.match(c) and NOTSEP.match(p):
split = True
else:
split = True
if split:
if NOTSEP.match(p):
words.append(string[s:i])
else:
if not separator:
separator = string[s:s + 1]
words.append(None)
s = i
i += 1
p = c
return words, separator, was_upper |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '11']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'parse_case'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '8']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'string'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'acronyms'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'default_parameter', 'children': ['9', '10']}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'preserve_case'}; {'id': '10', 'type': 'False', 'children': []}, {'id': '11', 'type': 'block', 'children': ['12', '22', '46', '50', '54', '114', '127', '139', '148', '176']}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '18']}; {'id': '14', 'type': 'pattern_list', 'children': ['15', '16', '17']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'separator'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'was_upper'}; {'id': '18', 'type': 'call', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': '_separate_words'}; {'id': '20', 'type': 'argument_list', 'children': ['21']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'string'}; {'id': '22', 'type': 'if_statement', 'children': ['23', '24', '36']}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'acronyms'}; {'id': '24', 'type': 'block', 'children': ['25', '32']}, {'id': '25', 'type': 'expression_statement', 'children': ['26']}; {'id': '26', 'type': 'assignment', 'children': ['27', '28']}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'acronyms'}; {'id': '28', 'type': 'call', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': '_sanitize_acronyms'}; {'id': '30', 'type': 'argument_list', 'children': ['31']}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'acronyms'}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'check_acronym'}, {'id': '35', 'type': 'identifier', 'children': [], 'value': '_advanced_acronym_detection'}; {'id': '36', 'type': 'else_clause', 'children': ['37']}, {'id': '37', 'type': 'block', 'children': ['38', '42']}; {'id': '38', 'type': 'expression_statement', 'children': ['39']}, {'id': '39', 'type': 'assignment', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'acronyms'}, {'id': '41', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'check_acronym'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': '_simple_acronym_detection'}; {'id': '46', 'type': 'expression_statement', 'children': ['47']}, {'id': '47', 'type': 'assignment', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '49', 'type': 'integer', 'children': [], 'value': '0'}; {'id': '50', 'type': 'expression_statement', 'children': ['51']}, {'id': '51', 'type': 'assignment', 'children': ['52', '53']}; {'id': '52', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '53', 'type': 'None', 'children': []}; {'id': '54', 'type': 'while_statement', 'children': ['55', '61']}, {'id': '55', 'type': 'comparison_operator', 'children': ['56', '57'], 'value': '<'}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '57', 'type': 'call', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'len'}, {'id': '59', 'type': 'argument_list', 'children': ['60']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '61', 'type': 'block', 'children': ['62', '68', '110']}; {'id': '62', 'type': 'expression_statement', 'children': ['63']}, {'id': '63', 'type': 'assignment', 'children': ['64', '65']}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'word'}, {'id': '65', 'type': 'subscript', 'children': ['66', '67']}; {'id': '66', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'i'}; {'id': '68', 'type': 'if_statement', 'children': ['69', '79', '89']}, {'id': '69', 'type': 'boolean_operator', 'children': ['70', '73'], 'value': 'and'}; {'id': '70', 'type': 'comparison_operator', 'children': ['71', '72'], 'value': 'is'}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'word'}; {'id': '72', 'type': 'None', 'children': []}, {'id': '73', 'type': 'call', 'children': ['74', '77']}; {'id': '74', 'type': 'attribute', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'UPPER'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'match'}, {'id': '77', 'type': 'argument_list', 'children': ['78']}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'word'}, {'id': '79', 'type': 'block', 'children': ['80']}; {'id': '80', 'type': 'if_statement', 'children': ['81', '84']}, {'id': '81', 'type': 'comparison_operator', 'children': ['82', '83'], 'value': 'is'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '83', 'type': 'None', 'children': []}; {'id': '84', 'type': 'block', 'children': ['85']}, {'id': '85', 'type': 'expression_statement', 'children': ['86']}; {'id': '86', 'type': 'assignment', 'children': ['87', '88']}, {'id': '87', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '89', 'type': 'elif_clause', 'children': ['90', '93']}; {'id': '90', 'type': 'comparison_operator', 'children': ['91', '92'], 'value': 'is'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '92', 'type': 'None', 'children': []}, {'id': '93', 'type': 'block', 'children': ['94', '106']}; {'id': '94', 'type': 'expression_statement', 'children': ['95']}, {'id': '95', 'type': 'assignment', 'children': ['96', '97']}; {'id': '96', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '97', 'type': 'binary_operator', 'children': ['98', '105'], 'value': '+'}; {'id': '98', 'type': 'call', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'check_acronym'}; {'id': '100', 'type': 'argument_list', 'children': ['101', '102', '103', '104']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'acronyms'}, {'id': '105', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '106', 'type': 'expression_statement', 'children': ['107']}, {'id': '107', 'type': 'assignment', 'children': ['108', '109']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '109', 'type': 'None', 'children': []}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'augmented_assignment', 'children': ['112', '113'], 'value': '+='}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '113', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '114', 'type': 'if_statement', 'children': ['115', '118']}, {'id': '115', 'type': 'comparison_operator', 'children': ['116', '117'], 'value': 'is'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 's'}, {'id': '117', 'type': 'None', 'children': []}; {'id': '118', 'type': 'block', 'children': ['119']}, {'id': '119', 'type': 'expression_statement', 'children': ['120']}; {'id': '120', 'type': 'call', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'check_acronym'}; {'id': '122', 'type': 'argument_list', 'children': ['123', '124', '125', '126']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 's'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'i'}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'acronyms'}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'assignment', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '130', 'type': 'list_comprehension', 'children': ['131', '132', '135']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '132', 'type': 'for_in_clause', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '135', 'type': 'if_clause', 'children': ['136']}; {'id': '136', 'type': 'comparison_operator', 'children': ['137', '138'], 'value': 'is'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'w'}; {'id': '138', 'type': 'None', 'children': []}, {'id': '139', 'type': 'expression_statement', 'children': ['140']}; {'id': '140', 'type': 'assignment', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'case_type'}; {'id': '142', 'type': 'call', 'children': ['143', '144']}, {'id': '143', 'type': 'identifier', 'children': [], 'value': '_determine_case'}; {'id': '144', 'type': 'argument_list', 'children': ['145', '146', '147']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'was_upper'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'string'}; {'id': '148', 'type': 'if_statement', 'children': ['149', '150', '166']}, {'id': '149', 'type': 'identifier', 'children': [], 'value': 'preserve_case'}; {'id': '150', 'type': 'block', 'children': ['151']}, {'id': '151', 'type': 'if_statement', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'was_upper'}, {'id': '153', 'type': 'block', 'children': ['154']}; {'id': '154', 'type': 'expression_statement', 'children': ['155']}, {'id': '155', 'type': 'assignment', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '157', 'type': 'list_comprehension', 'children': ['158', '163']}; {'id': '158', 'type': 'call', 'children': ['159', '162']}, {'id': '159', 'type': 'attribute', 'children': ['160', '161']}; {'id': '160', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'upper'}; {'id': '162', 'type': 'argument_list', 'children': []}, {'id': '163', 'type': 'for_in_clause', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'w'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'words'}; {'id': '166', 'type': 'else_clause', 'children': ['167']}, {'id': '167', 'type': 'block', 'children': ['168']}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '171', 'type': 'call', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': '_normalize_words'}, {'id': '173', 'type': 'argument_list', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '175', 'type': 'identifier', 'children': [], 'value': 'acronyms'}; {'id': '176', 'type': 'return_statement', 'children': ['177']}, {'id': '177', 'type': 'expression_list', 'children': ['178', '179', '180']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'words'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'case_type'} | def parse_case(string, acronyms=None, preserve_case=False):
words, separator, was_upper = _separate_words(string)
if acronyms:
acronyms = _sanitize_acronyms(acronyms)
check_acronym = _advanced_acronym_detection
else:
acronyms = []
check_acronym = _simple_acronym_detection
i = 0
s = None
while i < len(words):
word = words[i]
if word is not None and UPPER.match(word):
if s is None:
s = i
elif s is not None:
i = check_acronym(s, i, words, acronyms) + 1
s = None
i += 1
if s is not None:
check_acronym(s, i, words, acronyms)
words = [w for w in words if w is not None]
case_type = _determine_case(was_upper, words, string)
if preserve_case:
if was_upper:
words = [w.upper() for w in words]
else:
words = _normalize_words(words, acronyms)
return words, case_type, separator |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'listen'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'block', 'children': ['6', '17', '21', '42', '52', '66', '73']}; {'id': '6', 'type': 'expression_statement', 'children': ['7']}, {'id': '7', 'type': 'assignment', 'children': ['8', '9']}; {'id': '8', 'type': 'identifier', 'children': [], 'value': 'db'}, {'id': '9', 'type': 'call', 'children': ['10', '15']}; {'id': '10', 'type': 'attribute', 'children': ['11', '14']}, {'id': '11', 'type': 'attribute', 'children': ['12', '13']}; {'id': '12', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'get_database'}, {'id': '15', 'type': 'argument_list', 'children': ['16']}; {'id': '16', 'type': 'string', 'children': [], 'value': "'control'"}, {'id': '17', 'type': 'expression_statement', 'children': ['18']}; {'id': '18', 'type': 'assignment', 'children': ['19', '20']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '20', 'type': 'dictionary', 'children': []}, {'id': '21', 'type': 'if_statement', 'children': ['22', '29']}; {'id': '22', 'type': 'comparison_operator', 'children': ['23', '24'], 'value': 'in'}, {'id': '23', 'type': 'string', 'children': [], 'value': "'shard_hint'"}; {'id': '24', 'type': 'subscript', 'children': ['25', '28']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '28', 'type': 'string', 'children': [], 'value': "'control'"}, {'id': '29', 'type': 'block', 'children': ['30']}; {'id': '30', 'type': 'expression_statement', 'children': ['31']}, {'id': '31', 'type': 'assignment', 'children': ['32', '35']}; {'id': '32', 'type': 'subscript', 'children': ['33', '34']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '34', 'type': 'string', 'children': [], 'value': "'shard_hint'"}, {'id': '35', 'type': 'subscript', 'children': ['36', '41']}; {'id': '36', 'type': 'subscript', 'children': ['37', '40']}, {'id': '37', 'type': 'attribute', 'children': ['38', '39']}; {'id': '38', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '40', 'type': 'string', 'children': [], 'value': "'control'"}, {'id': '41', 'type': 'string', 'children': [], 'value': "'shard_hint'"}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'pubsub'}, {'id': '45', 'type': 'call', 'children': ['46', '49']}; {'id': '46', 'type': 'attribute', 'children': ['47', '48']}, {'id': '47', 'type': 'identifier', 'children': [], 'value': 'db'}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'pubsub'}, {'id': '49', 'type': 'argument_list', 'children': ['50']}; {'id': '50', 'type': 'dictionary_splat', 'children': ['51']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '52', 'type': 'expression_statement', 'children': ['53']}, {'id': '53', 'type': 'assignment', 'children': ['54', '55']}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'channel'}, {'id': '55', 'type': 'call', 'children': ['56', '63']}; {'id': '56', 'type': 'attribute', 'children': ['57', '62']}, {'id': '57', 'type': 'subscript', 'children': ['58', '61']}; {'id': '58', 'type': 'attribute', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '61', 'type': 'string', 'children': [], 'value': "'control'"}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '63', 'type': 'argument_list', 'children': ['64', '65']}; {'id': '64', 'type': 'string', 'children': [], 'value': "'channel'"}, {'id': '65', 'type': 'string', 'children': [], 'value': "'control'"}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'call', 'children': ['68', '71']}; {'id': '68', 'type': 'attribute', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'pubsub'}; {'id': '70', 'type': 'identifier', 'children': [], 'value': 'subscribe'}, {'id': '71', 'type': 'argument_list', 'children': ['72']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'channel'}, {'id': '73', 'type': 'for_statement', 'children': ['74', '75', '80']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '75', 'type': 'call', 'children': ['76', '79']}; {'id': '76', 'type': 'attribute', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'pubsub'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'listen'}, {'id': '79', 'type': 'argument_list', 'children': []}; {'id': '80', 'type': 'block', 'children': ['81']}, {'id': '81', 'type': 'if_statement', 'children': ['82', '96']}; {'id': '82', 'type': 'parenthesized_expression', 'children': ['83']}, {'id': '83', 'type': 'boolean_operator', 'children': ['84', '91'], 'value': 'and'}; {'id': '84', 'type': 'comparison_operator', 'children': ['85', '88'], 'value': 'in'}, {'id': '85', 'type': 'subscript', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '87', 'type': 'string', 'children': [], 'value': "'type'"}; {'id': '88', 'type': 'tuple', 'children': ['89', '90']}, {'id': '89', 'type': 'string', 'children': [], 'value': "'pmessage'"}; {'id': '90', 'type': 'string', 'children': [], 'value': "'message'"}, {'id': '91', 'type': 'comparison_operator', 'children': ['92', '95'], 'value': '=='}; {'id': '92', 'type': 'subscript', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'msg'}; {'id': '94', 'type': 'string', 'children': [], 'value': "'channel'"}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'channel'}; {'id': '96', 'type': 'block', 'children': ['97', '111', '116', '133', '171', '185', '197']}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '103']}, {'id': '99', 'type': 'pattern_list', 'children': ['100', '101', '102']}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '101', 'type': 'identifier', 'children': [], 'value': '_sep'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'args'}, {'id': '103', 'type': 'call', 'children': ['104', '109']}; {'id': '104', 'type': 'attribute', 'children': ['105', '108']}, {'id': '105', 'type': 'subscript', 'children': ['106', '107']}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'msg'}, {'id': '107', 'type': 'string', 'children': [], 'value': "'data'"}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'partition'}, {'id': '109', 'type': 'argument_list', 'children': ['110']}; {'id': '110', 'type': 'string', 'children': [], 'value': "':'"}, {'id': '111', 'type': 'if_statement', 'children': ['112', '114']}; {'id': '112', 'type': 'not_operator', 'children': ['113']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '114', 'type': 'block', 'children': ['115']}, {'id': '115', 'type': 'continue_statement', 'children': []}; {'id': '116', 'type': 'if_statement', 'children': ['117', '122']}, {'id': '117', 'type': 'comparison_operator', 'children': ['118', '121'], 'value': '=='}; {'id': '118', 'type': 'subscript', 'children': ['119', '120']}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '120', 'type': 'integer', 'children': [], 'value': '0'}, {'id': '121', 'type': 'string', 'children': [], 'value': "'_'"}; {'id': '122', 'type': 'block', 'children': ['123', '132']}, {'id': '123', 'type': 'expression_statement', 'children': ['124']}; {'id': '124', 'type': 'call', 'children': ['125', '128']}, {'id': '125', 'type': 'attribute', 'children': ['126', '127']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'LOG'}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '128', 'type': 'argument_list', 'children': ['129']}, {'id': '129', 'type': 'binary_operator', 'children': ['130', '131'], 'value': '%'}; {'id': '130', 'type': 'string', 'children': [], 'value': '"Cannot call internal command %r"'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '132', 'type': 'continue_statement', 'children': []}, {'id': '133', 'type': 'if_statement', 'children': ['134', '139', '148']}; {'id': '134', 'type': 'comparison_operator', 'children': ['135', '136'], 'value': 'in'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '136', 'type': 'attribute', 'children': ['137', '138']}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': '_commands'}, {'id': '139', 'type': 'block', 'children': ['140']}; {'id': '140', 'type': 'expression_statement', 'children': ['141']}, {'id': '141', 'type': 'assignment', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'func'}, {'id': '143', 'type': 'subscript', 'children': ['144', '147']}; {'id': '144', 'type': 'attribute', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': '_commands'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '148', 'type': 'else_clause', 'children': ['149']}, {'id': '149', 'type': 'block', 'children': ['150', '163']}; {'id': '150', 'type': 'expression_statement', 'children': ['151']}, {'id': '151', 'type': 'assignment', 'children': ['152', '153']}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'func'}, {'id': '153', 'type': 'call', 'children': ['154', '157']}; {'id': '154', 'type': 'attribute', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'utils'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'find_entrypoint'}, {'id': '157', 'type': 'argument_list', 'children': ['158', '159', '160']}; {'id': '158', 'type': 'string', 'children': [], 'value': "'turnstile.command'"}, {'id': '159', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '160', 'type': 'keyword_argument', 'children': ['161', '162']}, {'id': '161', 'type': 'identifier', 'children': [], 'value': 'compat'}; {'id': '162', 'type': 'False', 'children': []}, {'id': '163', 'type': 'expression_statement', 'children': ['164']}; {'id': '164', 'type': 'assignment', 'children': ['165', '170']}, {'id': '165', 'type': 'subscript', 'children': ['166', '169']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': '_commands'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'func'}, {'id': '171', 'type': 'if_statement', 'children': ['172', '174']}; {'id': '172', 'type': 'not_operator', 'children': ['173']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'func'}; {'id': '174', 'type': 'block', 'children': ['175', '184']}, {'id': '175', 'type': 'expression_statement', 'children': ['176']}; {'id': '176', 'type': 'call', 'children': ['177', '180']}, {'id': '177', 'type': 'attribute', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'LOG'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'error'}; {'id': '180', 'type': 'argument_list', 'children': ['181']}, {'id': '181', 'type': 'binary_operator', 'children': ['182', '183'], 'value': '%'}; {'id': '182', 'type': 'string', 'children': [], 'value': '"No such command %r"'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'command'}; {'id': '184', 'type': 'continue_statement', 'children': []}, {'id': '185', 'type': 'expression_statement', 'children': ['186']}; {'id': '186', 'type': 'assignment', 'children': ['187', '188']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'arglist'}; {'id': '188', 'type': 'conditional_expression', 'children': ['189', '195', '196'], 'value': 'if'}, {'id': '189', 'type': 'call', 'children': ['190', '193']}; {'id': '190', 'type': 'attribute', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '193', 'type': 'argument_list', 'children': ['194']}; {'id': '194', 'type': 'string', 'children': [], 'value': "':'"}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'args'}; {'id': '196', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '197', 'type': 'try_statement', 'children': ['198', '206']}; {'id': '198', 'type': 'block', 'children': ['199']}, {'id': '199', 'type': 'expression_statement', 'children': ['200']}; {'id': '200', 'type': 'call', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'func'}; {'id': '202', 'type': 'argument_list', 'children': ['203', '204']}, {'id': '203', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '204', 'type': 'list_splat', 'children': ['205']}, {'id': '205', 'type': 'identifier', 'children': [], 'value': 'arglist'}; {'id': '206', 'type': 'except_clause', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'Exception'}; {'id': '208', 'type': 'block', 'children': ['209', '220']}, {'id': '209', 'type': 'expression_statement', 'children': ['210']}; {'id': '210', 'type': 'call', 'children': ['211', '214']}, {'id': '211', 'type': 'attribute', 'children': ['212', '213']}; {'id': '212', 'type': 'identifier', 'children': [], 'value': 'LOG'}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'exception'}; {'id': '214', 'type': 'argument_list', 'children': ['215']}, {'id': '215', 'type': 'binary_operator', 'children': ['216', '217'], 'value': '%'}; {'id': '216', 'type': 'string', 'children': [], 'value': '"Failed to execute command %r arguments %r"'}, {'id': '217', 'type': 'tuple', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'command'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'arglist'} | def listen(self):
db = self.config.get_database('control')
kwargs = {}
if 'shard_hint' in self.config['control']:
kwargs['shard_hint'] = self.config['control']['shard_hint']
pubsub = db.pubsub(**kwargs)
channel = self.config['control'].get('channel', 'control')
pubsub.subscribe(channel)
for msg in pubsub.listen():
if (msg['type'] in ('pmessage', 'message') and
msg['channel'] == channel):
command, _sep, args = msg['data'].partition(':')
if not command:
continue
if command[0] == '_':
LOG.error("Cannot call internal command %r" % command)
continue
if command in self._commands:
func = self._commands[command]
else:
func = utils.find_entrypoint('turnstile.command', command,
compat=False)
self._commands[command] = func
if not func:
LOG.error("No such command %r" % command)
continue
arglist = args.split(':') if args else []
try:
func(self, *arglist)
except Exception:
LOG.exception("Failed to execute command %r arguments %r" %
(command, arglist))
continue |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '5']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'initialize'}, {'id': '3', 'type': 'parameters', 'children': ['4']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '5', 'type': 'block', 'children': ['6', '34', '38', '64', '80', '84', '88', '92', '199', '208', '228', '309', '316']}; {'id': '6', 'type': 'if_statement', 'children': ['7', '10', '26']}, {'id': '7', 'type': 'comparison_operator', 'children': ['8', '9'], 'value': 'in'}; {'id': '8', 'type': 'string', 'children': [], 'value': "'redis_client'"}, {'id': '9', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '10', 'type': 'block', 'children': ['11']}, {'id': '11', 'type': 'expression_statement', 'children': ['12']}; {'id': '12', 'type': 'assignment', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'client'}; {'id': '14', 'type': 'call', 'children': ['15', '18']}, {'id': '15', 'type': 'attribute', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'utils'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'find_entrypoint'}; {'id': '18', 'type': 'argument_list', 'children': ['19', '20', '23']}, {'id': '19', 'type': 'string', 'children': [], 'value': "'turnstile.redis_client'"}; {'id': '20', 'type': 'subscript', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '22', 'type': 'string', 'children': [], 'value': "'redis_client'"}, {'id': '23', 'type': 'keyword_argument', 'children': ['24', '25']}; {'id': '24', 'type': 'identifier', 'children': [], 'value': 'required'}, {'id': '25', 'type': 'True', 'children': []}; {'id': '26', 'type': 'else_clause', 'children': ['27']}, {'id': '27', 'type': 'block', 'children': ['28']}; {'id': '28', 'type': 'expression_statement', 'children': ['29']}, {'id': '29', 'type': 'assignment', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'client'}, {'id': '31', 'type': 'attribute', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'redis'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'StrictRedis'}; {'id': '34', 'type': 'expression_statement', 'children': ['35']}, {'id': '35', 'type': 'assignment', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '37', 'type': 'dictionary', 'children': []}; {'id': '38', 'type': 'for_statement', 'children': ['39', '42', '47']}, {'id': '39', 'type': 'pattern_list', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'cfg_var'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'type_'}; {'id': '42', 'type': 'call', 'children': ['43', '46']}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'REDIS_CONFIGS'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '46', 'type': 'argument_list', 'children': []}, {'id': '47', 'type': 'block', 'children': ['48']}; {'id': '48', 'type': 'if_statement', 'children': ['49', '52']}, {'id': '49', 'type': 'comparison_operator', 'children': ['50', '51'], 'value': 'in'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'cfg_var'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '52', 'type': 'block', 'children': ['53']}, {'id': '53', 'type': 'expression_statement', 'children': ['54']}; {'id': '54', 'type': 'assignment', 'children': ['55', '58']}, {'id': '55', 'type': 'subscript', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'cfg_var'}; {'id': '58', 'type': 'call', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'type_'}; {'id': '60', 'type': 'argument_list', 'children': ['61']}, {'id': '61', 'type': 'subscript', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'cfg_var'}; {'id': '64', 'type': 'if_statement', 'children': ['65', '72']}, {'id': '65', 'type': 'boolean_operator', 'children': ['66', '69'], 'value': 'and'}; {'id': '66', 'type': 'comparison_operator', 'children': ['67', '68'], 'value': 'not'}, {'id': '67', 'type': 'string', 'children': [], 'value': "'host'"}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '69', 'type': 'comparison_operator', 'children': ['70', '71'], 'value': 'not'}; {'id': '70', 'type': 'string', 'children': [], 'value': "'unix_socket_path'"}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '72', 'type': 'block', 'children': ['73']}, {'id': '73', 'type': 'raise_statement', 'children': ['74']}; {'id': '74', 'type': 'call', 'children': ['75', '78']}, {'id': '75', 'type': 'attribute', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'redis'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'ConnectionError'}; {'id': '78', 'type': 'argument_list', 'children': ['79']}, {'id': '79', 'type': 'string', 'children': [], 'value': '"No host specified for redis database"'}; {'id': '80', 'type': 'expression_statement', 'children': ['81']}, {'id': '81', 'type': 'assignment', 'children': ['82', '83']}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'cpool_class'}, {'id': '83', 'type': 'None', 'children': []}; {'id': '84', 'type': 'expression_statement', 'children': ['85']}, {'id': '85', 'type': 'assignment', 'children': ['86', '87']}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '87', 'type': 'dictionary', 'children': []}; {'id': '88', 'type': 'expression_statement', 'children': ['89']}, {'id': '89', 'type': 'assignment', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'extra_kwargs'}, {'id': '91', 'type': 'dictionary', 'children': []}; {'id': '92', 'type': 'for_statement', 'children': ['93', '96', '101']}, {'id': '93', 'type': 'pattern_list', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '95', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '96', 'type': 'call', 'children': ['97', '100']}, {'id': '97', 'type': 'attribute', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'config'}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'items'}; {'id': '100', 'type': 'argument_list', 'children': []}, {'id': '101', 'type': 'block', 'children': ['102']}; {'id': '102', 'type': 'if_statement', 'children': ['103', '109', '184']}, {'id': '103', 'type': 'call', 'children': ['104', '107']}; {'id': '104', 'type': 'attribute', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'startswith'}, {'id': '107', 'type': 'argument_list', 'children': ['108']}; {'id': '108', 'type': 'string', 'children': [], 'value': "'connection_pool.'"}, {'id': '109', 'type': 'block', 'children': ['110', '122']}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'assignment', 'children': ['112', '116']}; {'id': '112', 'type': 'pattern_list', 'children': ['113', '114', '115']}, {'id': '113', 'type': 'identifier', 'children': [], 'value': '_dummy'}; {'id': '114', 'type': 'identifier', 'children': [], 'value': '_sep'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'varname'}; {'id': '116', 'type': 'call', 'children': ['117', '120']}, {'id': '117', 'type': 'attribute', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'partition'}; {'id': '120', 'type': 'argument_list', 'children': ['121']}, {'id': '121', 'type': 'string', 'children': [], 'value': "'.'"}; {'id': '122', 'type': 'if_statement', 'children': ['123', '126', '142', '156', '176']}, {'id': '123', 'type': 'comparison_operator', 'children': ['124', '125'], 'value': '=='}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'varname'}, {'id': '125', 'type': 'string', 'children': [], 'value': "'connection_class'"}; {'id': '126', 'type': 'block', 'children': ['127']}, {'id': '127', 'type': 'expression_statement', 'children': ['128']}; {'id': '128', 'type': 'assignment', 'children': ['129', '132']}, {'id': '129', 'type': 'subscript', 'children': ['130', '131']}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'varname'}; {'id': '132', 'type': 'call', 'children': ['133', '136']}, {'id': '133', 'type': 'attribute', 'children': ['134', '135']}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'utils'}, {'id': '135', 'type': 'identifier', 'children': [], 'value': 'find_entrypoint'}; {'id': '136', 'type': 'argument_list', 'children': ['137', '138', '139']}, {'id': '137', 'type': 'string', 'children': [], 'value': "'turnstile.connection_class'"}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '139', 'type': 'keyword_argument', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'required'}, {'id': '141', 'type': 'True', 'children': []}; {'id': '142', 'type': 'elif_clause', 'children': ['143', '146']}, {'id': '143', 'type': 'comparison_operator', 'children': ['144', '145'], 'value': '=='}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'varname'}, {'id': '145', 'type': 'string', 'children': [], 'value': "'max_connections'"}; {'id': '146', 'type': 'block', 'children': ['147']}, {'id': '147', 'type': 'expression_statement', 'children': ['148']}; {'id': '148', 'type': 'assignment', 'children': ['149', '152']}, {'id': '149', 'type': 'subscript', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'varname'}; {'id': '152', 'type': 'call', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'int'}; {'id': '154', 'type': 'argument_list', 'children': ['155']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '156', 'type': 'elif_clause', 'children': ['157', '160']}, {'id': '157', 'type': 'comparison_operator', 'children': ['158', '159'], 'value': '=='}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'varname'}, {'id': '159', 'type': 'string', 'children': [], 'value': "'parser_class'"}; {'id': '160', 'type': 'block', 'children': ['161']}, {'id': '161', 'type': 'expression_statement', 'children': ['162']}; {'id': '162', 'type': 'assignment', 'children': ['163', '166']}, {'id': '163', 'type': 'subscript', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'varname'}; {'id': '166', 'type': 'call', 'children': ['167', '170']}, {'id': '167', 'type': 'attribute', 'children': ['168', '169']}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'utils'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'find_entrypoint'}; {'id': '170', 'type': 'argument_list', 'children': ['171', '172', '173']}, {'id': '171', 'type': 'string', 'children': [], 'value': "'turnstile.parser_class'"}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '173', 'type': 'keyword_argument', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'required'}, {'id': '175', 'type': 'True', 'children': []}; {'id': '176', 'type': 'else_clause', 'children': ['177']}, {'id': '177', 'type': 'block', 'children': ['178']}; {'id': '178', 'type': 'expression_statement', 'children': ['179']}, {'id': '179', 'type': 'assignment', 'children': ['180', '183']}; {'id': '180', 'type': 'subscript', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'varname'}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '184', 'type': 'elif_clause', 'children': ['185', '192']}, {'id': '185', 'type': 'boolean_operator', 'children': ['186', '189'], 'value': 'and'}; {'id': '186', 'type': 'comparison_operator', 'children': ['187', '188'], 'value': 'not'}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'REDIS_CONFIGS'}, {'id': '189', 'type': 'comparison_operator', 'children': ['190', '191'], 'value': 'not'}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'REDIS_EXCLUDES'}; {'id': '192', 'type': 'block', 'children': ['193']}, {'id': '193', 'type': 'expression_statement', 'children': ['194']}; {'id': '194', 'type': 'assignment', 'children': ['195', '198']}, {'id': '195', 'type': 'subscript', 'children': ['196', '197']}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'extra_kwargs'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '199', 'type': 'if_statement', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '201', 'type': 'block', 'children': ['202']}; {'id': '202', 'type': 'expression_statement', 'children': ['203']}, {'id': '203', 'type': 'assignment', 'children': ['204', '205']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'cpool_class'}, {'id': '205', 'type': 'attribute', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'redis'}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'ConnectionPool'}; {'id': '208', 'type': 'if_statement', 'children': ['209', '212']}, {'id': '209', 'type': 'comparison_operator', 'children': ['210', '211'], 'value': 'in'}; {'id': '210', 'type': 'string', 'children': [], 'value': "'connection_pool'"}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '212', 'type': 'block', 'children': ['213']}, {'id': '213', 'type': 'expression_statement', 'children': ['214']}; {'id': '214', 'type': 'assignment', 'children': ['215', '216']}, {'id': '215', 'type': 'identifier', 'children': [], 'value': 'cpool_class'}; {'id': '216', 'type': 'call', 'children': ['217', '220']}, {'id': '217', 'type': 'attribute', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'utils'}, {'id': '219', 'type': 'identifier', 'children': [], 'value': 'find_entrypoint'}; {'id': '220', 'type': 'argument_list', 'children': ['221', '222', '225']}, {'id': '221', 'type': 'string', 'children': [], 'value': "'turnstile.connection_pool'"}; {'id': '222', 'type': 'subscript', 'children': ['223', '224']}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'config'}; {'id': '224', 'type': 'string', 'children': [], 'value': "'connection_pool'"}, {'id': '225', 'type': 'keyword_argument', 'children': ['226', '227']}; {'id': '226', 'type': 'identifier', 'children': [], 'value': 'required'}, {'id': '227', 'type': 'True', 'children': []}; {'id': '228', 'type': 'if_statement', 'children': ['229', '230']}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'cpool_class'}; {'id': '230', 'type': 'block', 'children': ['231', '238', '296']}, {'id': '231', 'type': 'expression_statement', 'children': ['232']}; {'id': '232', 'type': 'call', 'children': ['233', '236']}, {'id': '233', 'type': 'attribute', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '235', 'type': 'identifier', 'children': [], 'value': 'update'}; {'id': '236', 'type': 'argument_list', 'children': ['237']}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'kwargs'}; {'id': '238', 'type': 'if_statement', 'children': ['239', '242']}, {'id': '239', 'type': 'comparison_operator', 'children': ['240', '241'], 'value': 'not'}; {'id': '240', 'type': 'string', 'children': [], 'value': "'connection_class'"}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '242', 'type': 'block', 'children': ['243']}, {'id': '243', 'type': 'if_statement', 'children': ['244', '247', '286']}; {'id': '244', 'type': 'comparison_operator', 'children': ['245', '246'], 'value': 'in'}, {'id': '245', 'type': 'string', 'children': [], 'value': "'unix_socket_path'"}; {'id': '246', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '247', 'type': 'block', 'children': ['248', '257', '266', '274', '278']}; {'id': '248', 'type': 'if_statement', 'children': ['249', '252']}, {'id': '249', 'type': 'comparison_operator', 'children': ['250', '251'], 'value': 'in'}; {'id': '250', 'type': 'string', 'children': [], 'value': "'host'"}, {'id': '251', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '252', 'type': 'block', 'children': ['253']}, {'id': '253', 'type': 'delete_statement', 'children': ['254']}; {'id': '254', 'type': 'subscript', 'children': ['255', '256']}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '256', 'type': 'string', 'children': [], 'value': "'host'"}, {'id': '257', 'type': 'if_statement', 'children': ['258', '261']}; {'id': '258', 'type': 'comparison_operator', 'children': ['259', '260'], 'value': 'in'}, {'id': '259', 'type': 'string', 'children': [], 'value': "'port'"}; {'id': '260', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '261', 'type': 'block', 'children': ['262']}; {'id': '262', 'type': 'delete_statement', 'children': ['263']}, {'id': '263', 'type': 'subscript', 'children': ['264', '265']}; {'id': '264', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '265', 'type': 'string', 'children': [], 'value': "'port'"}; {'id': '266', 'type': 'expression_statement', 'children': ['267']}, {'id': '267', 'type': 'assignment', 'children': ['268', '271']}; {'id': '268', 'type': 'subscript', 'children': ['269', '270']}, {'id': '269', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '270', 'type': 'string', 'children': [], 'value': "'path'"}, {'id': '271', 'type': 'subscript', 'children': ['272', '273']}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '273', 'type': 'string', 'children': [], 'value': "'unix_socket_path'"}; {'id': '274', 'type': 'delete_statement', 'children': ['275']}, {'id': '275', 'type': 'subscript', 'children': ['276', '277']}; {'id': '276', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '277', 'type': 'string', 'children': [], 'value': "'unix_socket_path'"}; {'id': '278', 'type': 'expression_statement', 'children': ['279']}, {'id': '279', 'type': 'assignment', 'children': ['280', '283']}; {'id': '280', 'type': 'subscript', 'children': ['281', '282']}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '282', 'type': 'string', 'children': [], 'value': "'connection_class'"}, {'id': '283', 'type': 'attribute', 'children': ['284', '285']}; {'id': '284', 'type': 'identifier', 'children': [], 'value': 'redis'}, {'id': '285', 'type': 'identifier', 'children': [], 'value': 'UnixDomainSocketConnection'}; {'id': '286', 'type': 'else_clause', 'children': ['287']}, {'id': '287', 'type': 'block', 'children': ['288']}; {'id': '288', 'type': 'expression_statement', 'children': ['289']}, {'id': '289', 'type': 'assignment', 'children': ['290', '293']}; {'id': '290', 'type': 'subscript', 'children': ['291', '292']}, {'id': '291', 'type': 'identifier', 'children': [], 'value': 'cpool'}; {'id': '292', 'type': 'string', 'children': [], 'value': "'connection_class'"}, {'id': '293', 'type': 'attribute', 'children': ['294', '295']}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'redis'}, {'id': '295', 'type': 'identifier', 'children': [], 'value': 'Connection'}; {'id': '296', 'type': 'expression_statement', 'children': ['297']}, {'id': '297', 'type': 'assignment', 'children': ['298', '299']}; {'id': '298', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '299', 'type': 'call', 'children': ['300', '301']}; {'id': '300', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '301', 'type': 'argument_list', 'children': ['302']}; {'id': '302', 'type': 'keyword_argument', 'children': ['303', '304']}, {'id': '303', 'type': 'identifier', 'children': [], 'value': 'connection_pool'}; {'id': '304', 'type': 'call', 'children': ['305', '306']}, {'id': '305', 'type': 'identifier', 'children': [], 'value': 'cpool_class'}; {'id': '306', 'type': 'argument_list', 'children': ['307']}, {'id': '307', 'type': 'dictionary_splat', 'children': ['308']}; {'id': '308', 'type': 'identifier', 'children': [], 'value': 'cpool'}, {'id': '309', 'type': 'expression_statement', 'children': ['310']}; {'id': '310', 'type': 'call', 'children': ['311', '314']}, {'id': '311', 'type': 'attribute', 'children': ['312', '313']}; {'id': '312', 'type': 'identifier', 'children': [], 'value': 'kwargs'}, {'id': '313', 'type': 'identifier', 'children': [], 'value': 'update'}; {'id': '314', 'type': 'argument_list', 'children': ['315']}, {'id': '315', 'type': 'identifier', 'children': [], 'value': 'extra_kwargs'}; {'id': '316', 'type': 'return_statement', 'children': ['317']}, {'id': '317', 'type': 'call', 'children': ['318', '319']}; {'id': '318', 'type': 'identifier', 'children': [], 'value': 'client'}, {'id': '319', 'type': 'argument_list', 'children': ['320']}; {'id': '320', 'type': 'dictionary_splat', 'children': ['321']}, {'id': '321', 'type': 'identifier', 'children': [], 'value': 'kwargs'} | def initialize(config):
if 'redis_client' in config:
client = utils.find_entrypoint('turnstile.redis_client',
config['redis_client'], required=True)
else:
client = redis.StrictRedis
kwargs = {}
for cfg_var, type_ in REDIS_CONFIGS.items():
if cfg_var in config:
kwargs[cfg_var] = type_(config[cfg_var])
if 'host' not in kwargs and 'unix_socket_path' not in kwargs:
raise redis.ConnectionError("No host specified for redis database")
cpool_class = None
cpool = {}
extra_kwargs = {}
for key, value in config.items():
if key.startswith('connection_pool.'):
_dummy, _sep, varname = key.partition('.')
if varname == 'connection_class':
cpool[varname] = utils.find_entrypoint(
'turnstile.connection_class', value, required=True)
elif varname == 'max_connections':
cpool[varname] = int(value)
elif varname == 'parser_class':
cpool[varname] = utils.find_entrypoint(
'turnstile.parser_class', value, required=True)
else:
cpool[varname] = value
elif key not in REDIS_CONFIGS and key not in REDIS_EXCLUDES:
extra_kwargs[key] = value
if cpool:
cpool_class = redis.ConnectionPool
if 'connection_pool' in config:
cpool_class = utils.find_entrypoint('turnstile.connection_pool',
config['connection_pool'],
required=True)
if cpool_class:
cpool.update(kwargs)
if 'connection_class' not in cpool:
if 'unix_socket_path' in cpool:
if 'host' in cpool:
del cpool['host']
if 'port' in cpool:
del cpool['port']
cpool['path'] = cpool['unix_socket_path']
del cpool['unix_socket_path']
cpool['connection_class'] = redis.UnixDomainSocketConnection
else:
cpool['connection_class'] = redis.Connection
kwargs = dict(connection_pool=cpool_class(**cpool))
kwargs.update(extra_kwargs)
return client(**kwargs) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '8']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'read_attributes'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'default_parameter', 'children': ['6', '7']}; {'id': '6', 'type': 'identifier', 'children': [], 'value': 'attributes'}, {'id': '7', 'type': 'None', 'children': []}; {'id': '8', 'type': 'block', 'children': ['9', '11', '24', '32', '41', '68', '148', '155', '167', '205']}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'string', 'children': [], 'value': '\'\'\'\n Collect read attributes across reads in this PileupCollection into a\n pandas.DataFrame.\n Valid attributes are the following properties of a pysam.AlignedSegment\n instance. See:\n http://pysam.readthedocs.org/en/latest/api.html\n for the meaning of these attributes.\n * cigarstring\n * flag\n * inferred_length\n * is_duplicate\n * is_paired\n * is_proper_pair\n * is_qcfail\n * is_read1\n * is_read2\n * is_reverse\n * is_secondary\n * is_unmapped\n * mapping_quality\n * mate_is_reverse\n * mate_is_unmapped\n * next_reference_id\n * next_reference_start\n * query_alignment_end\n * query_alignment_length\n * query_alignment_qualities\n * query_alignment_sequence\n * query_alignment_start\n * query_length\n * query_name\n * reference_end\n * reference_id\n * reference_length\n * reference_start\n * template_length\n (Note: the above list is parsed into the _READ_ATTRIBUTE_NAMES class\n variable, so be careful when modifying it.)\n Additionally, for alignment "tags" (arbitrary key values associated\n with an alignment), a column of the form "TAG_{tag name}" is\n included.\n Finally, the column "pysam_alignment_record" gives the underlying\n `pysam.AlignedSegment` instances.\n Parameters\n ----------\n attributes (optional): list of strings\n List of columns to include. If unspecified, all columns are\n included in the result.\n Returns\n ----------\n pandas.DataFrame of read attributes.\n \'\'\''}, {'id': '11', 'type': 'function_definition', 'children': ['12', '13', '15']}; {'id': '12', 'type': 'function_name', 'children': [], 'value': 'include'}, {'id': '13', 'type': 'parameters', 'children': ['14']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'attribute'}, {'id': '15', 'type': 'block', 'children': ['16']}; {'id': '16', 'type': 'return_statement', 'children': ['17']}, {'id': '17', 'type': 'boolean_operator', 'children': ['18', '21'], 'value': 'or'}; {'id': '18', 'type': 'comparison_operator', 'children': ['19', '20'], 'value': 'is'}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'attributes'}; {'id': '20', 'type': 'None', 'children': []}, {'id': '21', 'type': 'comparison_operator', 'children': ['22', '23'], 'value': 'in'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'attribute'}, {'id': '23', 'type': 'identifier', 'children': [], 'value': 'attributes'}; {'id': '24', 'type': 'expression_statement', 'children': ['25']}, {'id': '25', 'type': 'assignment', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'reads'}, {'id': '27', 'type': 'call', 'children': ['28', '31']}; {'id': '28', 'type': 'attribute', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'reads'}, {'id': '31', 'type': 'argument_list', 'children': []}; {'id': '32', 'type': 'expression_statement', 'children': ['33']}, {'id': '33', 'type': 'assignment', 'children': ['34', '35']}; {'id': '34', 'type': 'identifier', 'children': [], 'value': 'possible_column_names'}, {'id': '35', 'type': 'call', 'children': ['36', '37']}; {'id': '36', 'type': 'identifier', 'children': [], 'value': 'list'}, {'id': '37', 'type': 'argument_list', 'children': ['38']}; {'id': '38', 'type': 'attribute', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'PileupCollection'}; {'id': '40', 'type': 'identifier', 'children': [], 'value': '_READ_ATTRIBUTE_NAMES'}, {'id': '41', 'type': 'expression_statement', 'children': ['42']}; {'id': '42', 'type': 'assignment', 'children': ['43', '44']}, {'id': '43', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '44', 'type': 'call', 'children': ['45', '46']}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}; {'id': '46', 'type': 'generator_expression', 'children': ['47', '58', '63']}, {'id': '47', 'type': 'tuple', 'children': ['48', '49']}; {'id': '48', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '49', 'type': 'list_comprehension', 'children': ['50', '55']}; {'id': '50', 'type': 'call', 'children': ['51', '52']}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'getattr'}; {'id': '52', 'type': 'argument_list', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'read'}; {'id': '54', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '55', 'type': 'for_in_clause', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'read'}, {'id': '57', 'type': 'identifier', 'children': [], 'value': 'reads'}; {'id': '58', 'type': 'for_in_clause', 'children': ['59', '60']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '60', 'type': 'attribute', 'children': ['61', '62']}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'PileupCollection'}; {'id': '62', 'type': 'identifier', 'children': [], 'value': '_READ_ATTRIBUTE_NAMES'}, {'id': '63', 'type': 'if_clause', 'children': ['64']}; {'id': '64', 'type': 'call', 'children': ['65', '66']}, {'id': '65', 'type': 'identifier', 'children': [], 'value': 'include'}; {'id': '66', 'type': 'argument_list', 'children': ['67']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '68', 'type': 'if_statement', 'children': ['69', '70']}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'reads'}; {'id': '70', 'type': 'block', 'children': ['71', '86', '107']}, {'id': '71', 'type': 'expression_statement', 'children': ['72']}; {'id': '72', 'type': 'assignment', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'tag_dicts'}; {'id': '74', 'type': 'list_comprehension', 'children': ['75', '83']}, {'id': '75', 'type': 'call', 'children': ['76', '77']}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'dict'}, {'id': '77', 'type': 'argument_list', 'children': ['78']}; {'id': '78', 'type': 'call', 'children': ['79', '82']}, {'id': '79', 'type': 'attribute', 'children': ['80', '81']}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'get_tags'}; {'id': '82', 'type': 'argument_list', 'children': []}, {'id': '83', 'type': 'for_in_clause', 'children': ['84', '85']}; {'id': '84', 'type': 'identifier', 'children': [], 'value': 'x'}, {'id': '85', 'type': 'identifier', 'children': [], 'value': 'reads'}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'tag_keys'}, {'id': '89', 'type': 'call', 'children': ['90', '93']}; {'id': '90', 'type': 'attribute', 'children': ['91', '92']}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'union'}, {'id': '93', 'type': 'argument_list', 'children': ['94']}; {'id': '94', 'type': 'list_splat', 'children': ['95']}, {'id': '95', 'type': 'list_comprehension', 'children': ['96', '104']}; {'id': '96', 'type': 'call', 'children': ['97', '98']}, {'id': '97', 'type': 'identifier', 'children': [], 'value': 'set'}; {'id': '98', 'type': 'argument_list', 'children': ['99']}, {'id': '99', 'type': 'call', 'children': ['100', '103']}; {'id': '100', 'type': 'attribute', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'keys'}, {'id': '103', 'type': 'argument_list', 'children': []}; {'id': '104', 'type': 'for_in_clause', 'children': ['105', '106']}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'item'}; {'id': '106', 'type': 'identifier', 'children': [], 'value': 'tag_dicts'}, {'id': '107', 'type': 'for_statement', 'children': ['108', '109', '113']}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'tag_key'}, {'id': '109', 'type': 'call', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'sorted'}, {'id': '111', 'type': 'argument_list', 'children': ['112']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'tag_keys'}, {'id': '113', 'type': 'block', 'children': ['114', '120', '127']}; {'id': '114', 'type': 'expression_statement', 'children': ['115']}, {'id': '115', 'type': 'assignment', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'column_name'}, {'id': '117', 'type': 'binary_operator', 'children': ['118', '119'], 'value': '%'}; {'id': '118', 'type': 'string', 'children': [], 'value': '"TAG_%s"'}, {'id': '119', 'type': 'identifier', 'children': [], 'value': 'tag_key'}; {'id': '120', 'type': 'expression_statement', 'children': ['121']}, {'id': '121', 'type': 'call', 'children': ['122', '125']}; {'id': '122', 'type': 'attribute', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'possible_column_names'}; {'id': '124', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '125', 'type': 'argument_list', 'children': ['126']}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'column_name'}, {'id': '127', 'type': 'if_statement', 'children': ['128', '132']}; {'id': '128', 'type': 'call', 'children': ['129', '130']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'include'}; {'id': '130', 'type': 'argument_list', 'children': ['131']}, {'id': '131', 'type': 'identifier', 'children': [], 'value': 'column_name'}; {'id': '132', 'type': 'block', 'children': ['133']}, {'id': '133', 'type': 'expression_statement', 'children': ['134']}; {'id': '134', 'type': 'assignment', 'children': ['135', '138']}, {'id': '135', 'type': 'subscript', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'column_name'}; {'id': '138', 'type': 'list_comprehension', 'children': ['139', '145']}, {'id': '139', 'type': 'call', 'children': ['140', '143']}; {'id': '140', 'type': 'attribute', 'children': ['141', '142']}, {'id': '141', 'type': 'identifier', 'children': [], 'value': 'd'}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '143', 'type': 'argument_list', 'children': ['144']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'tag_key'}, {'id': '145', 'type': 'for_in_clause', 'children': ['146', '147']}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'd'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'tag_dicts'}; {'id': '148', 'type': 'expression_statement', 'children': ['149']}, {'id': '149', 'type': 'call', 'children': ['150', '153']}; {'id': '150', 'type': 'attribute', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'possible_column_names'}; {'id': '152', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '153', 'type': 'argument_list', 'children': ['154']}; {'id': '154', 'type': 'string', 'children': [], 'value': '"pysam_alignment_record"'}, {'id': '155', 'type': 'if_statement', 'children': ['156', '160']}; {'id': '156', 'type': 'call', 'children': ['157', '158']}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'include'}; {'id': '158', 'type': 'argument_list', 'children': ['159']}, {'id': '159', 'type': 'string', 'children': [], 'value': '"pysam_alignment_record"'}; {'id': '160', 'type': 'block', 'children': ['161']}, {'id': '161', 'type': 'expression_statement', 'children': ['162']}; {'id': '162', 'type': 'assignment', 'children': ['163', '166']}, {'id': '163', 'type': 'subscript', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '165', 'type': 'string', 'children': [], 'value': '"pysam_alignment_record"'}; {'id': '166', 'type': 'identifier', 'children': [], 'value': 'reads'}, {'id': '167', 'type': 'if_statement', 'children': ['168', '171']}; {'id': '168', 'type': 'comparison_operator', 'children': ['169', '170'], 'value': 'is'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'attributes'}; {'id': '170', 'type': 'None', 'children': []}, {'id': '171', 'type': 'block', 'children': ['172', '195']}; {'id': '172', 'type': 'for_statement', 'children': ['173', '174', '175']}, {'id': '173', 'type': 'identifier', 'children': [], 'value': 'attribute'}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'attributes'}, {'id': '175', 'type': 'block', 'children': ['176']}; {'id': '176', 'type': 'if_statement', 'children': ['177', '180']}, {'id': '177', 'type': 'comparison_operator', 'children': ['178', '179'], 'value': 'not'}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'attribute'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '180', 'type': 'block', 'children': ['181']}, {'id': '181', 'type': 'raise_statement', 'children': ['182']}; {'id': '182', 'type': 'call', 'children': ['183', '184']}, {'id': '183', 'type': 'identifier', 'children': [], 'value': 'ValueError'}; {'id': '184', 'type': 'argument_list', 'children': ['185']}, {'id': '185', 'type': 'binary_operator', 'children': ['186', '187'], 'value': '%'}; {'id': '186', 'type': 'string', 'children': [], 'value': '"No such attribute: %s. Valid attributes are: %s"'}, {'id': '187', 'type': 'tuple', 'children': ['188', '189']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'attribute'}, {'id': '189', 'type': 'call', 'children': ['190', '193']}; {'id': '190', 'type': 'attribute', 'children': ['191', '192']}, {'id': '191', 'type': 'string', 'children': [], 'value': '" "'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'join'}, {'id': '193', 'type': 'argument_list', 'children': ['194']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'possible_column_names'}, {'id': '195', 'type': 'assert_statement', 'children': ['196']}; {'id': '196', 'type': 'comparison_operator', 'children': ['197', '201'], 'value': '=='}, {'id': '197', 'type': 'call', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '199', 'type': 'argument_list', 'children': ['200']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'attributes'}, {'id': '201', 'type': 'call', 'children': ['202', '203']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'set'}, {'id': '203', 'type': 'argument_list', 'children': ['204']}; {'id': '204', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '205', 'type': 'return_statement', 'children': ['206']}; {'id': '206', 'type': 'call', 'children': ['207', '210']}, {'id': '207', 'type': 'attribute', 'children': ['208', '209']}; {'id': '208', 'type': 'identifier', 'children': [], 'value': 'pandas'}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'DataFrame'}; {'id': '210', 'type': 'argument_list', 'children': ['211']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'result'} | def read_attributes(self, attributes=None):
'''
Collect read attributes across reads in this PileupCollection into a
pandas.DataFrame.
Valid attributes are the following properties of a pysam.AlignedSegment
instance. See:
http://pysam.readthedocs.org/en/latest/api.html
for the meaning of these attributes.
* cigarstring
* flag
* inferred_length
* is_duplicate
* is_paired
* is_proper_pair
* is_qcfail
* is_read1
* is_read2
* is_reverse
* is_secondary
* is_unmapped
* mapping_quality
* mate_is_reverse
* mate_is_unmapped
* next_reference_id
* next_reference_start
* query_alignment_end
* query_alignment_length
* query_alignment_qualities
* query_alignment_sequence
* query_alignment_start
* query_length
* query_name
* reference_end
* reference_id
* reference_length
* reference_start
* template_length
(Note: the above list is parsed into the _READ_ATTRIBUTE_NAMES class
variable, so be careful when modifying it.)
Additionally, for alignment "tags" (arbitrary key values associated
with an alignment), a column of the form "TAG_{tag name}" is
included.
Finally, the column "pysam_alignment_record" gives the underlying
`pysam.AlignedSegment` instances.
Parameters
----------
attributes (optional): list of strings
List of columns to include. If unspecified, all columns are
included in the result.
Returns
----------
pandas.DataFrame of read attributes.
'''
def include(attribute):
return attributes is None or attribute in attributes
reads = self.reads()
possible_column_names = list(PileupCollection._READ_ATTRIBUTE_NAMES)
result = OrderedDict(
(name, [getattr(read, name) for read in reads])
for name in PileupCollection._READ_ATTRIBUTE_NAMES
if include(name))
if reads:
tag_dicts = [dict(x.get_tags()) for x in reads]
tag_keys = set.union(
*[set(item.keys()) for item in tag_dicts])
for tag_key in sorted(tag_keys):
column_name = "TAG_%s" % tag_key
possible_column_names.append(column_name)
if include(column_name):
result[column_name] = [d.get(tag_key) for d in tag_dicts]
possible_column_names.append("pysam_alignment_record")
if include("pysam_alignment_record"):
result["pysam_alignment_record"] = reads
if attributes is not None:
for attribute in attributes:
if attribute not in result:
raise ValueError(
"No such attribute: %s. Valid attributes are: %s"
% (attribute, " ".join(possible_column_names)))
assert set(attributes) == set(result)
return pandas.DataFrame(result) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'group_by_allele'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '16', '20', '24', '170', '186', '262', '284']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'string', 'children': [], 'value': '\'\'\'\n Split the PileupCollection by the alleles suggested by the reads at the\n specified locus.\n If a read has an insertion immediately following the locus, then the\n insertion is included in the allele. For example, if locus is the\n 1-base range [5,6), one allele might be "AGA", indicating that at\n locus 5 some read has an "A" followed by a 2-base insertion ("GA"). If\n a read has a deletion at the specified locus, the allele is the empty\n string.\n The given locus may include any number of bases. If the locus includes\n multiple bases, then the alleles consist of all bases aligning to that\n range in any read. Note that only sequences actually sequenced in a\n particular read are included. For example, if one read has "ATT" at a\n locus and another read has "GCC", then the alleles are "ATT" and\n "GCC", but not "GTT". That is, the bases in each allele are phased. For\n this reason, only reads that overlap the entire locus are included.\n If the locus is an empty interval (e.g. [5,5) ), then the alleles\n consist only of inserted bases. In this example, only bases inserted\n immediately after locus 5 would be included (but *not* the base\n actually at locus 5). In the previous insertion example, the allele\n would be "GA", indicating a 2-base insertion. Reads that have no\n insertion at that position (matches or deletions) would have the empty\n string as their allele.\n Parameters\n ----------\n locus : Locus\n The reference locus, encompassing 0 or more bases.\n Returns\n ----------\n A dict of string -> PileupCollection. The keys are nucleotide strings\n giving the bases sequenced at the locus, and the values are\n PileupCollection instances of the alignments that support that allele.\n \'\'\''}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '12', 'type': 'call', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'to_locus'}; {'id': '14', 'type': 'argument_list', 'children': ['15']}, {'id': '15', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '16', 'type': 'expression_statement', 'children': ['17']}, {'id': '17', 'type': 'assignment', 'children': ['18', '19']}; {'id': '18', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}, {'id': '19', 'type': 'None', 'children': []}; {'id': '20', 'type': 'expression_statement', 'children': ['21']}, {'id': '21', 'type': 'assignment', 'children': ['22', '23']}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'loci'}, {'id': '23', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '24', 'type': 'if_statement', 'children': ['25', '28', '114']}, {'id': '25', 'type': 'attribute', 'children': ['26', '27']}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '27', 'type': 'identifier', 'children': [], 'value': 'positions'}; {'id': '28', 'type': 'block', 'children': ['29']}, {'id': '29', 'type': 'for_statement', 'children': ['30', '31', '34']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'position'}, {'id': '31', 'type': 'attribute', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'positions'}; {'id': '34', 'type': 'block', 'children': ['35', '47', '54', '58', '110']}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'base_position'}; {'id': '38', 'type': 'call', 'children': ['39', '42']}, {'id': '39', 'type': 'attribute', 'children': ['40', '41']}; {'id': '40', 'type': 'identifier', 'children': [], 'value': 'Locus'}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'from_interbase_coordinates'}; {'id': '42', 'type': 'argument_list', 'children': ['43', '46']}, {'id': '43', 'type': 'attribute', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '45', 'type': 'identifier', 'children': [], 'value': 'contig'}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'position'}, {'id': '47', 'type': 'expression_statement', 'children': ['48']}; {'id': '48', 'type': 'call', 'children': ['49', '52']}, {'id': '49', 'type': 'attribute', 'children': ['50', '51']}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'loci'}, {'id': '51', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '52', 'type': 'argument_list', 'children': ['53']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'base_position'}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'new_read_to_allele'}, {'id': '57', 'type': 'dictionary', 'children': []}; {'id': '58', 'type': 'for_statement', 'children': ['59', '60', '65']}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'element'}; {'id': '60', 'type': 'subscript', 'children': ['61', '64']}, {'id': '61', 'type': 'attribute', 'children': ['62', '63']}; {'id': '62', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'base_position'}, {'id': '65', 'type': 'block', 'children': ['66', '70', '79', '96', '104']}; {'id': '66', 'type': 'expression_statement', 'children': ['67']}, {'id': '67', 'type': 'assignment', 'children': ['68', '69']}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'allele_prefix'}, {'id': '69', 'type': 'string', 'children': [], 'value': '""'}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'assignment', 'children': ['72', '73']}; {'id': '72', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '73', 'type': 'call', 'children': ['74', '75']}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'alignment_key'}, {'id': '75', 'type': 'argument_list', 'children': ['76']}; {'id': '76', 'type': 'attribute', 'children': ['77', '78']}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'element'}; {'id': '78', 'type': 'identifier', 'children': [], 'value': 'alignment'}, {'id': '79', 'type': 'if_statement', 'children': ['80', '83']}; {'id': '80', 'type': 'comparison_operator', 'children': ['81', '82'], 'value': 'is'}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}; {'id': '82', 'type': 'None', 'children': []}, {'id': '83', 'type': 'block', 'children': ['84']}; {'id': '84', 'type': 'try_statement', 'children': ['85', '92']}, {'id': '85', 'type': 'block', 'children': ['86']}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'allele_prefix'}, {'id': '89', 'type': 'subscript', 'children': ['90', '91']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}, {'id': '91', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '92', 'type': 'except_clause', 'children': ['93', '94']}, {'id': '93', 'type': 'identifier', 'children': [], 'value': 'KeyError'}; {'id': '94', 'type': 'block', 'children': ['95']}, {'id': '95', 'type': 'continue_statement', 'children': []}; {'id': '96', 'type': 'expression_statement', 'children': ['97']}, {'id': '97', 'type': 'assignment', 'children': ['98', '99']}; {'id': '98', 'type': 'identifier', 'children': [], 'value': 'allele'}, {'id': '99', 'type': 'binary_operator', 'children': ['100', '101'], 'value': '+'}; {'id': '100', 'type': 'identifier', 'children': [], 'value': 'allele_prefix'}, {'id': '101', 'type': 'attribute', 'children': ['102', '103']}; {'id': '102', 'type': 'identifier', 'children': [], 'value': 'element'}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'bases'}; {'id': '104', 'type': 'expression_statement', 'children': ['105']}, {'id': '105', 'type': 'assignment', 'children': ['106', '109']}; {'id': '106', 'type': 'subscript', 'children': ['107', '108']}, {'id': '107', 'type': 'identifier', 'children': [], 'value': 'new_read_to_allele'}; {'id': '108', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '109', 'type': 'identifier', 'children': [], 'value': 'allele'}; {'id': '110', 'type': 'expression_statement', 'children': ['111']}, {'id': '111', 'type': 'assignment', 'children': ['112', '113']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}, {'id': '113', 'type': 'identifier', 'children': [], 'value': 'new_read_to_allele'}; {'id': '114', 'type': 'else_clause', 'children': ['115']}, {'id': '115', 'type': 'block', 'children': ['116', '130', '137', '141']}; {'id': '116', 'type': 'expression_statement', 'children': ['117']}, {'id': '117', 'type': 'assignment', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'position_before'}, {'id': '119', 'type': 'call', 'children': ['120', '123']}; {'id': '120', 'type': 'attribute', 'children': ['121', '122']}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'Locus'}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'from_interbase_coordinates'}, {'id': '123', 'type': 'argument_list', 'children': ['124', '127']}; {'id': '124', 'type': 'attribute', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '126', 'type': 'identifier', 'children': [], 'value': 'contig'}, {'id': '127', 'type': 'attribute', 'children': ['128', '129']}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'start'}; {'id': '130', 'type': 'expression_statement', 'children': ['131']}, {'id': '131', 'type': 'call', 'children': ['132', '135']}; {'id': '132', 'type': 'attribute', 'children': ['133', '134']}, {'id': '133', 'type': 'identifier', 'children': [], 'value': 'loci'}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '135', 'type': 'argument_list', 'children': ['136']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'position_before'}, {'id': '137', 'type': 'expression_statement', 'children': ['138']}; {'id': '138', 'type': 'assignment', 'children': ['139', '140']}, {'id': '139', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}; {'id': '140', 'type': 'dictionary', 'children': []}, {'id': '141', 'type': 'for_statement', 'children': ['142', '143', '148']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'element'}, {'id': '143', 'type': 'subscript', 'children': ['144', '147']}; {'id': '144', 'type': 'attribute', 'children': ['145', '146']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '146', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '147', 'type': 'identifier', 'children': [], 'value': 'position_before'}; {'id': '148', 'type': 'block', 'children': ['149', '159']}, {'id': '149', 'type': 'expression_statement', 'children': ['150']}; {'id': '150', 'type': 'assignment', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'allele'}; {'id': '152', 'type': 'subscript', 'children': ['153', '156']}, {'id': '153', 'type': 'attribute', 'children': ['154', '155']}; {'id': '154', 'type': 'identifier', 'children': [], 'value': 'element'}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'bases'}; {'id': '156', 'type': 'slice', 'children': ['157', '158']}, {'id': '157', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '158', 'type': 'colon', 'children': []}, {'id': '159', 'type': 'expression_statement', 'children': ['160']}; {'id': '160', 'type': 'assignment', 'children': ['161', '169']}, {'id': '161', 'type': 'subscript', 'children': ['162', '163']}; {'id': '162', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}, {'id': '163', 'type': 'call', 'children': ['164', '165']}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'alignment_key'}, {'id': '165', 'type': 'argument_list', 'children': ['166']}; {'id': '166', 'type': 'attribute', 'children': ['167', '168']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'element'}; {'id': '168', 'type': 'identifier', 'children': [], 'value': 'alignment'}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'allele'}; {'id': '170', 'type': 'expression_statement', 'children': ['171']}, {'id': '171', 'type': 'assignment', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '173', 'type': 'call', 'children': ['174', '175']}; {'id': '174', 'type': 'identifier', 'children': [], 'value': 'defaultdict'}, {'id': '175', 'type': 'argument_list', 'children': ['176']}; {'id': '176', 'type': 'lambda', 'children': ['177']}, {'id': '177', 'type': 'call', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'PileupCollection'}, {'id': '179', 'type': 'argument_list', 'children': ['180', '183']}; {'id': '180', 'type': 'keyword_argument', 'children': ['181', '182']}, {'id': '181', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '182', 'type': 'dictionary', 'children': []}, {'id': '183', 'type': 'keyword_argument', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'parent'}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '186', 'type': 'for_statement', 'children': ['187', '188', '189']}, {'id': '187', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'loci'}, {'id': '189', 'type': 'block', 'children': ['190', '198']}; {'id': '190', 'type': 'expression_statement', 'children': ['191']}, {'id': '191', 'type': 'assignment', 'children': ['192', '193']}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'pileup'}, {'id': '193', 'type': 'subscript', 'children': ['194', '197']}; {'id': '194', 'type': 'attribute', 'children': ['195', '196']}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'self'}; {'id': '196', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '197', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '198', 'type': 'for_statement', 'children': ['199', '200', '203']}, {'id': '199', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '200', 'type': 'attribute', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'pileup'}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'elements'}, {'id': '203', 'type': 'block', 'children': ['204', '218']}; {'id': '204', 'type': 'expression_statement', 'children': ['205']}, {'id': '205', 'type': 'assignment', 'children': ['206', '207']}; {'id': '206', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '207', 'type': 'call', 'children': ['208', '211']}; {'id': '208', 'type': 'attribute', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'read_to_allele'}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 'get'}, {'id': '211', 'type': 'argument_list', 'children': ['212']}; {'id': '212', 'type': 'call', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'alignment_key'}; {'id': '214', 'type': 'argument_list', 'children': ['215']}, {'id': '215', 'type': 'attribute', 'children': ['216', '217']}; {'id': '216', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'alignment'}; {'id': '218', 'type': 'if_statement', 'children': ['219', '222']}, {'id': '219', 'type': 'comparison_operator', 'children': ['220', '221'], 'value': 'is'}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'key'}, {'id': '221', 'type': 'None', 'children': []}; {'id': '222', 'type': 'block', 'children': ['223']}, {'id': '223', 'type': 'if_statement', 'children': ['224', '231', '245']}; {'id': '224', 'type': 'comparison_operator', 'children': ['225', '226'], 'value': 'in'}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '226', 'type': 'attribute', 'children': ['227', '230']}, {'id': '227', 'type': 'subscript', 'children': ['228', '229']}; {'id': '228', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '231', 'type': 'block', 'children': ['232']}; {'id': '232', 'type': 'expression_statement', 'children': ['233']}, {'id': '233', 'type': 'call', 'children': ['234', '243']}; {'id': '234', 'type': 'attribute', 'children': ['235', '242']}, {'id': '235', 'type': 'subscript', 'children': ['236', '241']}; {'id': '236', 'type': 'attribute', 'children': ['237', '240']}, {'id': '237', 'type': 'subscript', 'children': ['238', '239']}; {'id': '238', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '239', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '240', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'append'}, {'id': '243', 'type': 'argument_list', 'children': ['244']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'e'}, {'id': '245', 'type': 'else_clause', 'children': ['246']}; {'id': '246', 'type': 'block', 'children': ['247']}, {'id': '247', 'type': 'expression_statement', 'children': ['248']}; {'id': '248', 'type': 'assignment', 'children': ['249', '256']}, {'id': '249', 'type': 'subscript', 'children': ['250', '255']}; {'id': '250', 'type': 'attribute', 'children': ['251', '254']}, {'id': '251', 'type': 'subscript', 'children': ['252', '253']}; {'id': '252', 'type': 'identifier', 'children': [], 'value': 'split'}, {'id': '253', 'type': 'identifier', 'children': [], 'value': 'key'}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '256', 'type': 'call', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'Pileup'}; {'id': '258', 'type': 'argument_list', 'children': ['259', '260']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '260', 'type': 'list', 'children': ['261'], 'value': '[e]'}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'e'}; {'id': '262', 'type': 'function_definition', 'children': ['263', '264', '266']}, {'id': '263', 'type': 'function_name', 'children': [], 'value': 'sorter'}; {'id': '264', 'type': 'parameters', 'children': ['265']}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'pair'}; {'id': '266', 'type': 'block', 'children': ['267', '273']}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'assignment', 'children': ['269', '272']}, {'id': '269', 'type': 'tuple_pattern', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'allele'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'pileup_collection'}; {'id': '272', 'type': 'identifier', 'children': [], 'value': 'pair'}, {'id': '273', 'type': 'return_statement', 'children': ['274']}; {'id': '274', 'type': 'tuple', 'children': ['275', '283']}, {'id': '275', 'type': 'binary_operator', 'children': ['276', '278'], 'value': '*'}; {'id': '276', 'type': 'unary_operator', 'children': ['277'], 'value': '-'}, {'id': '277', 'type': 'integer', 'children': [], 'value': '1'}; {'id': '278', 'type': 'call', 'children': ['279', '282']}, {'id': '279', 'type': 'attribute', 'children': ['280', '281']}; {'id': '280', 'type': 'identifier', 'children': [], 'value': 'pileup_collection'}, {'id': '281', 'type': 'identifier', 'children': [], 'value': 'num_reads'}; {'id': '282', 'type': 'argument_list', 'children': []}, {'id': '283', 'type': 'identifier', 'children': [], 'value': 'allele'}; {'id': '284', 'type': 'return_statement', 'children': ['285']}, {'id': '285', 'type': 'call', 'children': ['286', '287']}; {'id': '286', 'type': 'identifier', 'children': [], 'value': 'OrderedDict'}, {'id': '287', 'type': 'argument_list', 'children': ['288']}; {'id': '288', 'type': 'call', 'children': ['289', '290']}, {'id': '289', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '290', 'type': 'argument_list', 'children': ['291', '296']}, {'id': '291', 'type': 'call', 'children': ['292', '295']}; {'id': '292', 'type': 'attribute', 'children': ['293', '294']}, {'id': '293', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '294', 'type': 'identifier', 'children': [], 'value': 'items'}, {'id': '295', 'type': 'argument_list', 'children': []}; {'id': '296', 'type': 'keyword_argument', 'children': ['297', '298']}, {'id': '297', 'type': 'identifier', 'children': [], 'value': 'key'} | def group_by_allele(self, locus):
'''
Split the PileupCollection by the alleles suggested by the reads at the
specified locus.
If a read has an insertion immediately following the locus, then the
insertion is included in the allele. For example, if locus is the
1-base range [5,6), one allele might be "AGA", indicating that at
locus 5 some read has an "A" followed by a 2-base insertion ("GA"). If
a read has a deletion at the specified locus, the allele is the empty
string.
The given locus may include any number of bases. If the locus includes
multiple bases, then the alleles consist of all bases aligning to that
range in any read. Note that only sequences actually sequenced in a
particular read are included. For example, if one read has "ATT" at a
locus and another read has "GCC", then the alleles are "ATT" and
"GCC", but not "GTT". That is, the bases in each allele are phased. For
this reason, only reads that overlap the entire locus are included.
If the locus is an empty interval (e.g. [5,5) ), then the alleles
consist only of inserted bases. In this example, only bases inserted
immediately after locus 5 would be included (but *not* the base
actually at locus 5). In the previous insertion example, the allele
would be "GA", indicating a 2-base insertion. Reads that have no
insertion at that position (matches or deletions) would have the empty
string as their allele.
Parameters
----------
locus : Locus
The reference locus, encompassing 0 or more bases.
Returns
----------
A dict of string -> PileupCollection. The keys are nucleotide strings
giving the bases sequenced at the locus, and the values are
PileupCollection instances of the alignments that support that allele.
'''
locus = to_locus(locus)
read_to_allele = None
loci = []
if locus.positions:
for position in locus.positions:
base_position = Locus.from_interbase_coordinates(
locus.contig, position)
loci.append(base_position)
new_read_to_allele = {}
for element in self.pileups[base_position]:
allele_prefix = ""
key = alignment_key(element.alignment)
if read_to_allele is not None:
try:
allele_prefix = read_to_allele[key]
except KeyError:
continue
allele = allele_prefix + element.bases
new_read_to_allele[key] = allele
read_to_allele = new_read_to_allele
else:
position_before = Locus.from_interbase_coordinates(
locus.contig, locus.start)
loci.append(position_before)
read_to_allele = {}
for element in self.pileups[position_before]:
allele = element.bases[1:]
read_to_allele[alignment_key(element.alignment)] = allele
split = defaultdict(lambda: PileupCollection(pileups={}, parent=self))
for locus in loci:
pileup = self.pileups[locus]
for e in pileup.elements:
key = read_to_allele.get(alignment_key(e.alignment))
if key is not None:
if locus in split[key].pileups:
split[key].pileups[locus].append(e)
else:
split[key].pileups[locus] = Pileup(locus, [e])
def sorter(pair):
(allele, pileup_collection) = pair
return (-1 * pileup_collection.num_reads(), allele)
return OrderedDict(sorted(split.items(), key=sorter)) |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '9']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'from_bam'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5', '6']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'loci'}; {'id': '6', 'type': 'default_parameter', 'children': ['7', '8']}, {'id': '7', 'type': 'identifier', 'children': [], 'value': 'normalized_contig_names'}; {'id': '8', 'type': 'True', 'children': []}, {'id': '9', 'type': 'block', 'children': ['10', '12', '23', '27', '46']}; {'id': '10', 'type': 'expression_statement', 'children': ['11']}, {'id': '11', 'type': 'string', 'children': [], 'value': "'''\n Create a PileupCollection for a set of loci from a BAM file.\n Parameters\n ----------\n pysam_samfile : `pysam.Samfile` instance, or filename string\n to a BAM file. The BAM file must be indexed.\n loci : list of Locus instances\n Loci to collect pileups for.\n normalized_contig_names : whether the contig names have been normalized\n (e.g. pyensembl removes the 'chr' prefix). Set to true to\n de-normalize the names when querying the BAM file.\n Returns\n ----------\n PileupCollection instance containing pileups for the specified loci.\n All alignments in the BAM file are included (e.g. duplicate reads,\n secondary alignments, etc.). See `PileupCollection.filter` if these\n need to be removed. \n '''"}; {'id': '12', 'type': 'expression_statement', 'children': ['13']}, {'id': '13', 'type': 'assignment', 'children': ['14', '15']}; {'id': '14', 'type': 'identifier', 'children': [], 'value': 'loci'}, {'id': '15', 'type': 'list_comprehension', 'children': ['16', '20']}; {'id': '16', 'type': 'call', 'children': ['17', '18']}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'to_locus'}; {'id': '18', 'type': 'argument_list', 'children': ['19']}, {'id': '19', 'type': 'identifier', 'children': [], 'value': 'obj'}; {'id': '20', 'type': 'for_in_clause', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'obj'}; {'id': '22', 'type': 'identifier', 'children': [], 'value': 'loci'}, {'id': '23', 'type': 'expression_statement', 'children': ['24']}; {'id': '24', 'type': 'assignment', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'close_on_completion'}; {'id': '26', 'type': 'False', 'children': []}, {'id': '27', 'type': 'if_statement', 'children': ['28', '34']}; {'id': '28', 'type': 'call', 'children': ['29', '32']}, {'id': '29', 'type': 'attribute', 'children': ['30', '31']}; {'id': '30', 'type': 'identifier', 'children': [], 'value': 'typechecks'}, {'id': '31', 'type': 'identifier', 'children': [], 'value': 'is_string'}; {'id': '32', 'type': 'argument_list', 'children': ['33']}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}; {'id': '34', 'type': 'block', 'children': ['35', '42']}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'Samfile'}; {'id': '40', 'type': 'argument_list', 'children': ['41']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}; {'id': '42', 'type': 'expression_statement', 'children': ['43']}, {'id': '43', 'type': 'assignment', 'children': ['44', '45']}; {'id': '44', 'type': 'identifier', 'children': [], 'value': 'close_on_completion'}, {'id': '45', 'type': 'True', 'children': []}; {'id': '46', 'type': 'try_statement', 'children': ['47', '262']}, {'id': '47', 'type': 'block', 'children': ['48', '90', '97', '128', '260']}; {'id': '48', 'type': 'if_statement', 'children': ['49', '50', '84']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'normalized_contig_names'}; {'id': '50', 'type': 'block', 'children': ['51', '55']}, {'id': '51', 'type': 'expression_statement', 'children': ['52']}; {'id': '52', 'type': 'assignment', 'children': ['53', '54']}, {'id': '53', 'type': 'identifier', 'children': [], 'value': 'chromosome_name_map'}; {'id': '54', 'type': 'dictionary', 'children': []}, {'id': '55', 'type': 'for_statement', 'children': ['56', '57', '60']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '57', 'type': 'attribute', 'children': ['58', '59']}; {'id': '58', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}, {'id': '59', 'type': 'identifier', 'children': [], 'value': 'references'}; {'id': '60', 'type': 'block', 'children': ['61', '72', '78']}, {'id': '61', 'type': 'expression_statement', 'children': ['62']}; {'id': '62', 'type': 'assignment', 'children': ['63', '64']}, {'id': '63', 'type': 'identifier', 'children': [], 'value': 'normalized'}; {'id': '64', 'type': 'call', 'children': ['65', '70']}, {'id': '65', 'type': 'attribute', 'children': ['66', '69']}; {'id': '66', 'type': 'attribute', 'children': ['67', '68']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'pyensembl'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '69', 'type': 'identifier', 'children': [], 'value': 'normalize_chromosome'}; {'id': '70', 'type': 'argument_list', 'children': ['71']}, {'id': '71', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '72', 'type': 'expression_statement', 'children': ['73']}, {'id': '73', 'type': 'assignment', 'children': ['74', '77']}; {'id': '74', 'type': 'subscript', 'children': ['75', '76']}, {'id': '75', 'type': 'identifier', 'children': [], 'value': 'chromosome_name_map'}; {'id': '76', 'type': 'identifier', 'children': [], 'value': 'normalized'}, {'id': '77', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '78', 'type': 'expression_statement', 'children': ['79']}, {'id': '79', 'type': 'assignment', 'children': ['80', '83']}; {'id': '80', 'type': 'subscript', 'children': ['81', '82']}, {'id': '81', 'type': 'identifier', 'children': [], 'value': 'chromosome_name_map'}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'name'}, {'id': '83', 'type': 'identifier', 'children': [], 'value': 'name'}; {'id': '84', 'type': 'else_clause', 'children': ['85']}, {'id': '85', 'type': 'block', 'children': ['86']}; {'id': '86', 'type': 'expression_statement', 'children': ['87']}, {'id': '87', 'type': 'assignment', 'children': ['88', '89']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'chromosome_name_map'}, {'id': '89', 'type': 'None', 'children': []}; {'id': '90', 'type': 'expression_statement', 'children': ['91']}, {'id': '91', 'type': 'assignment', 'children': ['92', '93']}; {'id': '92', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '93', 'type': 'call', 'children': ['94', '95']}; {'id': '94', 'type': 'identifier', 'children': [], 'value': 'PileupCollection'}, {'id': '95', 'type': 'argument_list', 'children': ['96']}; {'id': '96', 'type': 'dictionary', 'children': []}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'locus_iterator'}; {'id': '100', 'type': 'call', 'children': ['101', '106']}, {'id': '101', 'type': 'attribute', 'children': ['102', '105']}; {'id': '102', 'type': 'attribute', 'children': ['103', '104']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'itertools'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'chain'}, {'id': '105', 'type': 'identifier', 'children': [], 'value': 'from_iterable'}; {'id': '106', 'type': 'generator_expression', 'children': ['107', '122']}, {'id': '107', 'type': 'generator_expression', 'children': ['108', '117']}; {'id': '108', 'type': 'call', 'children': ['109', '112']}, {'id': '109', 'type': 'attribute', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'Locus'}, {'id': '111', 'type': 'identifier', 'children': [], 'value': 'from_interbase_coordinates'}; {'id': '112', 'type': 'argument_list', 'children': ['113', '116']}, {'id': '113', 'type': 'attribute', 'children': ['114', '115']}; {'id': '114', 'type': 'identifier', 'children': [], 'value': 'locus_interval'}, {'id': '115', 'type': 'identifier', 'children': [], 'value': 'contig'}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'pos'}, {'id': '117', 'type': 'for_in_clause', 'children': ['118', '119']}; {'id': '118', 'type': 'identifier', 'children': [], 'value': 'pos'}, {'id': '119', 'type': 'attribute', 'children': ['120', '121']}; {'id': '120', 'type': 'identifier', 'children': [], 'value': 'locus_interval'}, {'id': '121', 'type': 'identifier', 'children': [], 'value': 'positions'}; {'id': '122', 'type': 'for_in_clause', 'children': ['123', '124']}, {'id': '123', 'type': 'identifier', 'children': [], 'value': 'locus_interval'}; {'id': '124', 'type': 'call', 'children': ['125', '126']}, {'id': '125', 'type': 'identifier', 'children': [], 'value': 'sorted'}; {'id': '126', 'type': 'argument_list', 'children': ['127']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'loci'}; {'id': '128', 'type': 'for_statement', 'children': ['129', '130', '131']}, {'id': '129', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '130', 'type': 'identifier', 'children': [], 'value': 'locus_iterator'}, {'id': '131', 'type': 'block', 'children': ['132', '144', '180', '203', '216', '222', '229']}; {'id': '132', 'type': 'expression_statement', 'children': ['133']}, {'id': '133', 'type': 'assignment', 'children': ['134', '139']}; {'id': '134', 'type': 'subscript', 'children': ['135', '138']}, {'id': '135', 'type': 'attribute', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '138', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '139', 'type': 'call', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'Pileup'}, {'id': '141', 'type': 'argument_list', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '143', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '144', 'type': 'if_statement', 'children': ['145', '146', '172']}, {'id': '145', 'type': 'identifier', 'children': [], 'value': 'normalized_contig_names'}; {'id': '146', 'type': 'block', 'children': ['147']}, {'id': '147', 'type': 'try_statement', 'children': ['148', '157']}; {'id': '148', 'type': 'block', 'children': ['149']}, {'id': '149', 'type': 'expression_statement', 'children': ['150']}; {'id': '150', 'type': 'assignment', 'children': ['151', '152']}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'chromosome'}; {'id': '152', 'type': 'subscript', 'children': ['153', '154']}, {'id': '153', 'type': 'identifier', 'children': [], 'value': 'chromosome_name_map'}; {'id': '154', 'type': 'attribute', 'children': ['155', '156']}, {'id': '155', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'contig'}, {'id': '157', 'type': 'except_clause', 'children': ['158', '159']}; {'id': '158', 'type': 'identifier', 'children': [], 'value': 'KeyError'}, {'id': '159', 'type': 'block', 'children': ['160', '171']}; {'id': '160', 'type': 'expression_statement', 'children': ['161']}, {'id': '161', 'type': 'call', 'children': ['162', '165']}; {'id': '162', 'type': 'attribute', 'children': ['163', '164']}, {'id': '163', 'type': 'identifier', 'children': [], 'value': 'logging'}; {'id': '164', 'type': 'identifier', 'children': [], 'value': 'warn'}, {'id': '165', 'type': 'argument_list', 'children': ['166']}; {'id': '166', 'type': 'binary_operator', 'children': ['167', '168'], 'value': '%'}, {'id': '167', 'type': 'string', 'children': [], 'value': '"No such contig in bam: %s"'}; {'id': '168', 'type': 'attribute', 'children': ['169', '170']}, {'id': '169', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'contig'}, {'id': '171', 'type': 'continue_statement', 'children': []}; {'id': '172', 'type': 'else_clause', 'children': ['173']}, {'id': '173', 'type': 'block', 'children': ['174']}; {'id': '174', 'type': 'expression_statement', 'children': ['175']}, {'id': '175', 'type': 'assignment', 'children': ['176', '177']}; {'id': '176', 'type': 'identifier', 'children': [], 'value': 'chromosome'}, {'id': '177', 'type': 'attribute', 'children': ['178', '179']}; {'id': '178', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'contig'}; {'id': '180', 'type': 'expression_statement', 'children': ['181']}, {'id': '181', 'type': 'assignment', 'children': ['182', '183']}; {'id': '182', 'type': 'identifier', 'children': [], 'value': 'columns'}, {'id': '183', 'type': 'call', 'children': ['184', '187']}; {'id': '184', 'type': 'attribute', 'children': ['185', '186']}, {'id': '185', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}; {'id': '186', 'type': 'identifier', 'children': [], 'value': 'pileup'}, {'id': '187', 'type': 'argument_list', 'children': ['188', '189', '192', '197', '200']}; {'id': '188', 'type': 'identifier', 'children': [], 'value': 'chromosome'}, {'id': '189', 'type': 'attribute', 'children': ['190', '191']}; {'id': '190', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'position'}; {'id': '192', 'type': 'binary_operator', 'children': ['193', '196'], 'value': '+'}, {'id': '193', 'type': 'attribute', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '195', 'type': 'identifier', 'children': [], 'value': 'position'}; {'id': '196', 'type': 'integer', 'children': [], 'value': '1'}, {'id': '197', 'type': 'keyword_argument', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'truncate'}, {'id': '199', 'type': 'True', 'children': []}; {'id': '200', 'type': 'keyword_argument', 'children': ['201', '202']}, {'id': '201', 'type': 'identifier', 'children': [], 'value': 'stepper'}; {'id': '202', 'type': 'string', 'children': [], 'value': '"nofilter"'}, {'id': '203', 'type': 'try_statement', 'children': ['204', '212']}; {'id': '204', 'type': 'block', 'children': ['205']}, {'id': '205', 'type': 'expression_statement', 'children': ['206']}; {'id': '206', 'type': 'assignment', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'column'}; {'id': '208', 'type': 'call', 'children': ['209', '210']}, {'id': '209', 'type': 'identifier', 'children': [], 'value': 'next'}; {'id': '210', 'type': 'argument_list', 'children': ['211']}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'columns'}; {'id': '212', 'type': 'except_clause', 'children': ['213', '214']}, {'id': '213', 'type': 'identifier', 'children': [], 'value': 'StopIteration'}; {'id': '214', 'type': 'block', 'children': ['215']}, {'id': '215', 'type': 'continue_statement', 'children': []}; {'id': '216', 'type': 'expression_statement', 'children': ['217']}, {'id': '217', 'type': 'assignment', 'children': ['218', '219']}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'pileups'}, {'id': '219', 'type': 'attribute', 'children': ['220', '221']}; {'id': '220', 'type': 'identifier', 'children': [], 'value': 'column'}, {'id': '221', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '222', 'type': 'assert_statement', 'children': ['223']}, {'id': '223', 'type': 'comparison_operator', 'children': ['224', '228'], 'value': '=='}; {'id': '224', 'type': 'call', 'children': ['225', '226']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'list'}; {'id': '226', 'type': 'argument_list', 'children': ['227']}, {'id': '227', 'type': 'identifier', 'children': [], 'value': 'columns'}; {'id': '228', 'type': 'list', 'children': [], 'value': '[]'}, {'id': '229', 'type': 'for_statement', 'children': ['230', '231', '232']}; {'id': '230', 'type': 'identifier', 'children': [], 'value': 'pileup_read'}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '232', 'type': 'block', 'children': ['233']}, {'id': '233', 'type': 'if_statement', 'children': ['234', '238']}; {'id': '234', 'type': 'not_operator', 'children': ['235']}, {'id': '235', 'type': 'attribute', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'pileup_read'}, {'id': '237', 'type': 'identifier', 'children': [], 'value': 'is_refskip'}; {'id': '238', 'type': 'block', 'children': ['239', '249']}, {'id': '239', 'type': 'expression_statement', 'children': ['240']}; {'id': '240', 'type': 'assignment', 'children': ['241', '242']}, {'id': '241', 'type': 'identifier', 'children': [], 'value': 'element'}; {'id': '242', 'type': 'call', 'children': ['243', '246']}, {'id': '243', 'type': 'attribute', 'children': ['244', '245']}; {'id': '244', 'type': 'identifier', 'children': [], 'value': 'PileupElement'}, {'id': '245', 'type': 'identifier', 'children': [], 'value': 'from_pysam_alignment'}; {'id': '246', 'type': 'argument_list', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'locus'}; {'id': '248', 'type': 'identifier', 'children': [], 'value': 'pileup_read'}, {'id': '249', 'type': 'expression_statement', 'children': ['250']}; {'id': '250', 'type': 'call', 'children': ['251', '258']}, {'id': '251', 'type': 'attribute', 'children': ['252', '257']}; {'id': '252', 'type': 'subscript', 'children': ['253', '256']}, {'id': '253', 'type': 'attribute', 'children': ['254', '255']}; {'id': '254', 'type': 'identifier', 'children': [], 'value': 'result'}, {'id': '255', 'type': 'identifier', 'children': [], 'value': 'pileups'}; {'id': '256', 'type': 'identifier', 'children': [], 'value': 'locus'}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'append'}; {'id': '258', 'type': 'argument_list', 'children': ['259']}, {'id': '259', 'type': 'identifier', 'children': [], 'value': 'element'}; {'id': '260', 'type': 'return_statement', 'children': ['261']}, {'id': '261', 'type': 'identifier', 'children': [], 'value': 'result'}; {'id': '262', 'type': 'finally_clause', 'children': ['263']}, {'id': '263', 'type': 'block', 'children': ['264']}; {'id': '264', 'type': 'if_statement', 'children': ['265', '266']}, {'id': '265', 'type': 'identifier', 'children': [], 'value': 'close_on_completion'}; {'id': '266', 'type': 'block', 'children': ['267']}, {'id': '267', 'type': 'expression_statement', 'children': ['268']}; {'id': '268', 'type': 'call', 'children': ['269', '272']}, {'id': '269', 'type': 'attribute', 'children': ['270', '271']}; {'id': '270', 'type': 'identifier', 'children': [], 'value': 'pysam_samfile'}, {'id': '271', 'type': 'identifier', 'children': [], 'value': 'close'} | def from_bam(pysam_samfile, loci, normalized_contig_names=True):
'''
Create a PileupCollection for a set of loci from a BAM file.
Parameters
----------
pysam_samfile : `pysam.Samfile` instance, or filename string
to a BAM file. The BAM file must be indexed.
loci : list of Locus instances
Loci to collect pileups for.
normalized_contig_names : whether the contig names have been normalized
(e.g. pyensembl removes the 'chr' prefix). Set to true to
de-normalize the names when querying the BAM file.
Returns
----------
PileupCollection instance containing pileups for the specified loci.
All alignments in the BAM file are included (e.g. duplicate reads,
secondary alignments, etc.). See `PileupCollection.filter` if these
need to be removed.
'''
loci = [to_locus(obj) for obj in loci]
close_on_completion = False
if typechecks.is_string(pysam_samfile):
pysam_samfile = Samfile(pysam_samfile)
close_on_completion = True
try:
if normalized_contig_names:
chromosome_name_map = {}
for name in pysam_samfile.references:
normalized = pyensembl.locus.normalize_chromosome(name)
chromosome_name_map[normalized] = name
chromosome_name_map[name] = name
else:
chromosome_name_map = None
result = PileupCollection({})
locus_iterator = itertools.chain.from_iterable(
(Locus.from_interbase_coordinates(locus_interval.contig, pos)
for pos
in locus_interval.positions)
for locus_interval in sorted(loci))
for locus in locus_iterator:
result.pileups[locus] = Pileup(locus, [])
if normalized_contig_names:
try:
chromosome = chromosome_name_map[locus.contig]
except KeyError:
logging.warn("No such contig in bam: %s" % locus.contig)
continue
else:
chromosome = locus.contig
columns = pysam_samfile.pileup(
chromosome,
locus.position,
locus.position + 1,
truncate=True,
stepper="nofilter")
try:
column = next(columns)
except StopIteration:
continue
pileups = column.pileups
assert list(columns) == []
for pileup_read in pileups:
if not pileup_read.is_refskip:
element = PileupElement.from_pysam_alignment(
locus, pileup_read)
result.pileups[locus].append(element)
return result
finally:
if close_on_completion:
pysam_samfile.close() |
{'id': '0', 'type': 'module', 'children': ['1']}, {'id': '1', 'type': 'function_definition', 'children': ['2', '3', '6']}; {'id': '2', 'type': 'function_name', 'children': [], 'value': 'check_recommended_global_attributes'}, {'id': '3', 'type': 'parameters', 'children': ['4', '5']}; {'id': '4', 'type': 'identifier', 'children': [], 'value': 'self'}, {'id': '5', 'type': 'identifier', 'children': [], 'value': 'dataset'}; {'id': '6', 'type': 'block', 'children': ['7', '9', '19', '35', '44', '54', '66', '89', '138', '147', '168', '188', '196', '205', '214', '232', '254']}, {'id': '7', 'type': 'expression_statement', 'children': ['8']}; {'id': '8', 'type': 'string', 'children': [], 'value': '\'\'\'\n Check the global recommended attributes for 2.0 templates. These go an extra step besides\n just checking that they exist.\n :param netCDF4.Dataset dataset: An open netCDF dataset\n :id = "" ; //.................................................. RECOMMENDED - Should be a human readable unique identifier for data set. (ACDD)\n :naming_authority = "" ; //.................................... RECOMMENDED - Backward URL of institution (for example, gov.noaa.ncei). (ACDD)\n :history = "" ; //............................................. RECOMMENDED - Provides an audit trail for modifications to the original data. (ACDD)\n :source = "" ; //.............................................. RECOMMENDED - The method of production of the original data. (CF)\n :processing_level = "" ; //.................................... RECOMMENDED - Provide a description of the processing or quality control level of the data. (ACDD)\n :comment = "" ; //............................................. RECOMMENDED - Provide useful additional information here. (CF)\n :acknowledgment = "" ; //...................................... RECOMMENDED - A place to acknowledge various types of support for the project that produced this data. (ACDD)\n :license = "" ; //............................................. RECOMMENDED - Describe the restrictions to data access and distribution. (ACDD)\n :standard_name_vocabulary = "CF Standard Name Table vNN" ; //.. RECOMMENDED - If using CF standard name attribute for variables. Replace NN with the CF standard name table number (CF)\n :date_created = "" ; //........................................ RECOMMENDED - Creation date of this version of the data(netCDF). Use ISO 8601:2004 for date and time. (ACDD)\n :creator_name = "" ; //........................................ RECOMMENDED - The name of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)\n :creator_email = "" ; //....................................... RECOMMENDED - The email address of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)\n :creator_url = "" ; //......................................... RECOMMENDED - The URL of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)\n :institution = "" ; //......................................... RECOMMENDED -The name of the institution principally responsible for originating this data.. An institution attribute can be used for each variable if variables come from more than one institution. (CF/ACDD)\n :project = "" ; //............................................. RECOMMENDED - The name of the project(s) principally responsible for originating this data. Multiple projects can be separated by commas. (ACDD)\n :publisher_name = "" ; //...................................... RECOMMENDED - The name of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)\n :publisher_email = "" ; //..................................... RECOMMENDED - The email address of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)\n :publisher_url = "" ; //....................................... RECOMMENDED - The URL of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)\n :geospatial_bounds = "" ; //................................... RECOMMENDED - Describes the data\'s 2D or 3D geospatial extent in OGC\'s Well-Known Text (WKT) Geometry format. (ACDD)\n :geospatial_bounds_crs = "" ; //............................... RECOMMENDED - The coordinate reference system (CRS) of the point coordinates in the geospatial_bounds attribute. (ACDD)\n :geospatial_bounds_vertical_crs = "" ; //...................... RECOMMENDED - The vertical coordinate reference system (CRS) for the Z axis of the point coordinates in the geospatial_bounds attribute. (ACDD)\n :geospatial_lat_min = 0.0d ; //................................ RECOMMENDED - Describes a simple lower latitude limit. (ACDD)\n :geospatial_lat_max = 0.0d ; //................................ RECOMMENDED - Describes a simple upper latitude limit. (ACDD)\n :geospatial_lon_min = 0.0d ; //................................ RECOMMENDED - Describes a simple lower longitude limit. (ACDD)\n :geospatial_lon_max = 0.0d ; //................................ RECOMMENDED - Describes a simple upper longitude limit. (ACDD)\n :geospatial_vertical_min = 0.0d ; //........................... RECOMMENDED - Describes the numerically smaller vertical limit. (ACDD)\n :geospatial_vertical_max = 0.0d ; //........................... RECOMMENDED - Describes the numerically larger vertical limit. (ACDD)\n :geospatial_vertical_positive = "" ; //........................ RECOMMENDED - Use "up" or "down". (ACDD)\n :time_coverage_start = "" ; //................................. RECOMMENDED - Describes the time of the first data point in the data set. Use ISO 8601:2004 for date and time. (ACDD)\n :time_coverage_end = "" ; //................................... RECOMMENDED - Describes the time of the last data point in the data set. Use ISO 8601:2004 for date and time.(ACDD)\n :time_coverage_duration = "" ; //.............................. RECOMMENDED - Describes the duration of the data set. Use ISO 8601:2004 for date and time. (ACDD)\n :time_coverage_resolution = "" ; //............................ RECOMMENDED - Describes the targeted time period between each value in the data set. Use ISO 8601:2004 for date and time. (ACDD)\n :uuid = "" ; //................................................ RECOMMENDED - Machine readable unique identifier for each file. A new uuid is created whenever the file is changed. (NCEI)\n :sea_name = "" ; //............................................ RECOMMENDED - The names of the sea in which the data were collected. Use NCEI sea names table. (NCEI)\n \'\'\''}, {'id': '9', 'type': 'expression_statement', 'children': ['10']}; {'id': '10', 'type': 'assignment', 'children': ['11', '12']}, {'id': '11', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '12', 'type': 'call', 'children': ['13', '14']}, {'id': '13', 'type': 'identifier', 'children': [], 'value': 'TestCtx'}; {'id': '14', 'type': 'argument_list', 'children': ['15', '18']}, {'id': '15', 'type': 'attribute', 'children': ['16', '17']}; {'id': '16', 'type': 'identifier', 'children': [], 'value': 'BaseCheck'}, {'id': '17', 'type': 'identifier', 'children': [], 'value': 'MEDIUM'}; {'id': '18', 'type': 'string', 'children': [], 'value': "'Recommended global attributes'"}, {'id': '19', 'type': 'expression_statement', 'children': ['20']}; {'id': '20', 'type': 'assignment', 'children': ['21', '22']}, {'id': '21', 'type': 'identifier', 'children': [], 'value': 'sea_names'}; {'id': '22', 'type': 'list_comprehension', 'children': ['23', '28']}, {'id': '23', 'type': 'call', 'children': ['24', '27']}; {'id': '24', 'type': 'attribute', 'children': ['25', '26']}, {'id': '25', 'type': 'identifier', 'children': [], 'value': 'sn'}; {'id': '26', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '27', 'type': 'argument_list', 'children': []}; {'id': '28', 'type': 'for_in_clause', 'children': ['29', '30']}, {'id': '29', 'type': 'identifier', 'children': [], 'value': 'sn'}; {'id': '30', 'type': 'call', 'children': ['31', '34']}, {'id': '31', 'type': 'attribute', 'children': ['32', '33']}; {'id': '32', 'type': 'identifier', 'children': [], 'value': 'util'}, {'id': '33', 'type': 'identifier', 'children': [], 'value': 'get_sea_names'}; {'id': '34', 'type': 'argument_list', 'children': []}, {'id': '35', 'type': 'expression_statement', 'children': ['36']}; {'id': '36', 'type': 'assignment', 'children': ['37', '38']}, {'id': '37', 'type': 'identifier', 'children': [], 'value': 'sea_name'}; {'id': '38', 'type': 'call', 'children': ['39', '40']}, {'id': '39', 'type': 'identifier', 'children': [], 'value': 'getattr'}; {'id': '40', 'type': 'argument_list', 'children': ['41', '42', '43']}, {'id': '41', 'type': 'identifier', 'children': [], 'value': 'dataset'}; {'id': '42', 'type': 'string', 'children': [], 'value': "'sea_name'"}, {'id': '43', 'type': 'string', 'children': [], 'value': "''"}; {'id': '44', 'type': 'expression_statement', 'children': ['45']}, {'id': '45', 'type': 'assignment', 'children': ['46', '47']}; {'id': '46', 'type': 'identifier', 'children': [], 'value': 'sea_name'}, {'id': '47', 'type': 'call', 'children': ['48', '51']}; {'id': '48', 'type': 'attribute', 'children': ['49', '50']}, {'id': '49', 'type': 'identifier', 'children': [], 'value': 'sea_name'}; {'id': '50', 'type': 'identifier', 'children': [], 'value': 'replace'}, {'id': '51', 'type': 'argument_list', 'children': ['52', '53']}; {'id': '52', 'type': 'string', 'children': [], 'value': "', '"}, {'id': '53', 'type': 'string', 'children': [], 'value': "','"}; {'id': '54', 'type': 'expression_statement', 'children': ['55']}, {'id': '55', 'type': 'assignment', 'children': ['56', '57']}; {'id': '56', 'type': 'identifier', 'children': [], 'value': 'sea_name'}, {'id': '57', 'type': 'conditional_expression', 'children': ['58', '64', '65'], 'value': 'if'}; {'id': '58', 'type': 'call', 'children': ['59', '62']}, {'id': '59', 'type': 'attribute', 'children': ['60', '61']}; {'id': '60', 'type': 'identifier', 'children': [], 'value': 'sea_name'}, {'id': '61', 'type': 'identifier', 'children': [], 'value': 'split'}; {'id': '62', 'type': 'argument_list', 'children': ['63']}, {'id': '63', 'type': 'string', 'children': [], 'value': "','"}; {'id': '64', 'type': 'identifier', 'children': [], 'value': 'sea_name'}, {'id': '65', 'type': 'list', 'children': [], 'value': '[]'}; {'id': '66', 'type': 'for_statement', 'children': ['67', '68', '69']}, {'id': '67', 'type': 'identifier', 'children': [], 'value': 'sea'}; {'id': '68', 'type': 'identifier', 'children': [], 'value': 'sea_name'}, {'id': '69', 'type': 'block', 'children': ['70']}; {'id': '70', 'type': 'expression_statement', 'children': ['71']}, {'id': '71', 'type': 'call', 'children': ['72', '75']}; {'id': '72', 'type': 'attribute', 'children': ['73', '74']}, {'id': '73', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '74', 'type': 'identifier', 'children': [], 'value': 'assert_true'}, {'id': '75', 'type': 'argument_list', 'children': ['76', '83']}; {'id': '76', 'type': 'comparison_operator', 'children': ['77', '82'], 'value': 'in'}, {'id': '77', 'type': 'call', 'children': ['78', '81']}; {'id': '78', 'type': 'attribute', 'children': ['79', '80']}, {'id': '79', 'type': 'identifier', 'children': [], 'value': 'sea'}; {'id': '80', 'type': 'identifier', 'children': [], 'value': 'lower'}, {'id': '81', 'type': 'argument_list', 'children': []}; {'id': '82', 'type': 'identifier', 'children': [], 'value': 'sea_names'}, {'id': '83', 'type': 'call', 'children': ['84', '87']}; {'id': '84', 'type': 'attribute', 'children': ['85', '86']}, {'id': '85', 'type': 'string', 'children': [], 'value': "'sea_name attribute should exist and should be from the NODC sea names list: {} is not a valid sea name'"}; {'id': '86', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '87', 'type': 'argument_list', 'children': ['88']}; {'id': '88', 'type': 'identifier', 'children': [], 'value': 'sea'}, {'id': '89', 'type': 'for_statement', 'children': ['90', '91', '96']}; {'id': '90', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '91', 'type': 'list', 'children': ['92', '93', '94', '95'], 'value': "['time_coverage_start', 'time_coverage_end', 'date_created', 'date_modified']"}; {'id': '92', 'type': 'string', 'children': [], 'value': "'time_coverage_start'"}, {'id': '93', 'type': 'string', 'children': [], 'value': "'time_coverage_end'"}; {'id': '94', 'type': 'string', 'children': [], 'value': "'date_created'"}, {'id': '95', 'type': 'string', 'children': [], 'value': "'date_modified'"}; {'id': '96', 'type': 'block', 'children': ['97', '106']}, {'id': '97', 'type': 'expression_statement', 'children': ['98']}; {'id': '98', 'type': 'assignment', 'children': ['99', '100']}, {'id': '99', 'type': 'identifier', 'children': [], 'value': 'attr_value'}; {'id': '100', 'type': 'call', 'children': ['101', '102']}, {'id': '101', 'type': 'identifier', 'children': [], 'value': 'getattr'}; {'id': '102', 'type': 'argument_list', 'children': ['103', '104', '105']}, {'id': '103', 'type': 'identifier', 'children': [], 'value': 'dataset'}; {'id': '104', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '105', 'type': 'string', 'children': [], 'value': "''"}; {'id': '106', 'type': 'try_statement', 'children': ['107', '121']}, {'id': '107', 'type': 'block', 'children': ['108', '113']}; {'id': '108', 'type': 'expression_statement', 'children': ['109']}, {'id': '109', 'type': 'call', 'children': ['110', '111']}; {'id': '110', 'type': 'identifier', 'children': [], 'value': 'parse_datetime'}, {'id': '111', 'type': 'argument_list', 'children': ['112']}; {'id': '112', 'type': 'identifier', 'children': [], 'value': 'attr_value'}, {'id': '113', 'type': 'expression_statement', 'children': ['114']}; {'id': '114', 'type': 'call', 'children': ['115', '118']}, {'id': '115', 'type': 'attribute', 'children': ['116', '117']}; {'id': '116', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}, {'id': '117', 'type': 'identifier', 'children': [], 'value': 'assert_true'}; {'id': '118', 'type': 'argument_list', 'children': ['119', '120']}, {'id': '119', 'type': 'True', 'children': []}; {'id': '120', 'type': 'string', 'children': [], 'value': "''"}, {'id': '121', 'type': 'except_clause', 'children': ['122', '123']}; {'id': '122', 'type': 'identifier', 'children': [], 'value': 'ISO8601Error'}, {'id': '123', 'type': 'block', 'children': ['124']}; {'id': '124', 'type': 'expression_statement', 'children': ['125']}, {'id': '125', 'type': 'call', 'children': ['126', '129']}; {'id': '126', 'type': 'attribute', 'children': ['127', '128']}, {'id': '127', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '128', 'type': 'identifier', 'children': [], 'value': 'assert_true'}, {'id': '129', 'type': 'argument_list', 'children': ['130', '131']}; {'id': '130', 'type': 'False', 'children': []}, {'id': '131', 'type': 'call', 'children': ['132', '135']}; {'id': '132', 'type': 'attribute', 'children': ['133', '134']}, {'id': '133', 'type': 'string', 'children': [], 'value': "'{} should exist and be ISO-8601 format (example: PT1M30S), currently: {}'"}; {'id': '134', 'type': 'identifier', 'children': [], 'value': 'format'}, {'id': '135', 'type': 'argument_list', 'children': ['136', '137']}; {'id': '136', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '137', 'type': 'identifier', 'children': [], 'value': 'attr_value'}; {'id': '138', 'type': 'expression_statement', 'children': ['139']}, {'id': '139', 'type': 'assignment', 'children': ['140', '141']}; {'id': '140', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '141', 'type': 'call', 'children': ['142', '143']}; {'id': '142', 'type': 'identifier', 'children': [], 'value': 'getattr'}, {'id': '143', 'type': 'argument_list', 'children': ['144', '145', '146']}; {'id': '144', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '145', 'type': 'string', 'children': [], 'value': "'geospatial_vertical_positive'"}; {'id': '146', 'type': 'string', 'children': [], 'value': "''"}, {'id': '147', 'type': 'expression_statement', 'children': ['148']}; {'id': '148', 'type': 'call', 'children': ['149', '152']}, {'id': '149', 'type': 'attribute', 'children': ['150', '151']}; {'id': '150', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}, {'id': '151', 'type': 'identifier', 'children': [], 'value': 'assert_true'}; {'id': '152', 'type': 'argument_list', 'children': ['153', '162']}, {'id': '153', 'type': 'comparison_operator', 'children': ['154', '159'], 'value': 'in'}; {'id': '154', 'type': 'call', 'children': ['155', '158']}, {'id': '155', 'type': 'attribute', 'children': ['156', '157']}; {'id': '156', 'type': 'identifier', 'children': [], 'value': 'value'}, {'id': '157', 'type': 'identifier', 'children': [], 'value': 'lower'}; {'id': '158', 'type': 'argument_list', 'children': []}, {'id': '159', 'type': 'list', 'children': ['160', '161'], 'value': "['up', 'down']"}; {'id': '160', 'type': 'string', 'children': [], 'value': "'up'"}, {'id': '161', 'type': 'string', 'children': [], 'value': "'down'"}; {'id': '162', 'type': 'call', 'children': ['163', '166']}, {'id': '163', 'type': 'attribute', 'children': ['164', '165']}; {'id': '164', 'type': 'string', 'children': [], 'value': "'geospatial_vertical_positive attribute should be up or down: {}'"}, {'id': '165', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '166', 'type': 'argument_list', 'children': ['167']}, {'id': '167', 'type': 'identifier', 'children': [], 'value': 'value'}; {'id': '168', 'type': 'expression_statement', 'children': ['169']}, {'id': '169', 'type': 'assignment', 'children': ['170', '171']}; {'id': '170', 'type': 'identifier', 'children': [], 'value': 'ack_exists'}, {'id': '171', 'type': 'call', 'children': ['172', '173']}; {'id': '172', 'type': 'identifier', 'children': [], 'value': 'any'}, {'id': '173', 'type': 'argument_list', 'children': ['174']}; {'id': '174', 'type': 'generator_expression', 'children': ['175', '183']}, {'id': '175', 'type': 'comparison_operator', 'children': ['176', '182'], 'value': '!='}; {'id': '176', 'type': 'call', 'children': ['177', '178']}, {'id': '177', 'type': 'identifier', 'children': [], 'value': 'getattr'}; {'id': '178', 'type': 'argument_list', 'children': ['179', '180', '181']}, {'id': '179', 'type': 'identifier', 'children': [], 'value': 'dataset'}; {'id': '180', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '181', 'type': 'string', 'children': [], 'value': "''"}; {'id': '182', 'type': 'string', 'children': [], 'value': "''"}, {'id': '183', 'type': 'for_in_clause', 'children': ['184', '185']}; {'id': '184', 'type': 'identifier', 'children': [], 'value': 'attr'}, {'id': '185', 'type': 'list', 'children': ['186', '187'], 'value': "['acknowledgment', 'acknowledgement']"}; {'id': '186', 'type': 'string', 'children': [], 'value': "'acknowledgment'"}, {'id': '187', 'type': 'string', 'children': [], 'value': "'acknowledgement'"}; {'id': '188', 'type': 'expression_statement', 'children': ['189']}, {'id': '189', 'type': 'call', 'children': ['190', '193']}; {'id': '190', 'type': 'attribute', 'children': ['191', '192']}, {'id': '191', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '192', 'type': 'identifier', 'children': [], 'value': 'assert_true'}, {'id': '193', 'type': 'argument_list', 'children': ['194', '195']}; {'id': '194', 'type': 'identifier', 'children': [], 'value': 'ack_exists'}, {'id': '195', 'type': 'string', 'children': [], 'value': "'acknowledgement attribute should exist and not be empty'"}; {'id': '196', 'type': 'expression_statement', 'children': ['197']}, {'id': '197', 'type': 'assignment', 'children': ['198', '199']}; {'id': '198', 'type': 'identifier', 'children': [], 'value': 'standard_name_vocab'}, {'id': '199', 'type': 'call', 'children': ['200', '201']}; {'id': '200', 'type': 'identifier', 'children': [], 'value': 'getattr'}, {'id': '201', 'type': 'argument_list', 'children': ['202', '203', '204']}; {'id': '202', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '203', 'type': 'string', 'children': [], 'value': "'standard_name_vocabulary'"}; {'id': '204', 'type': 'string', 'children': [], 'value': "''"}, {'id': '205', 'type': 'expression_statement', 'children': ['206']}; {'id': '206', 'type': 'assignment', 'children': ['207', '208']}, {'id': '207', 'type': 'identifier', 'children': [], 'value': 'regex'}; {'id': '208', 'type': 'call', 'children': ['209', '212']}, {'id': '209', 'type': 'attribute', 'children': ['210', '211']}; {'id': '210', 'type': 'identifier', 'children': [], 'value': 're'}, {'id': '211', 'type': 'identifier', 'children': [], 'value': 'compile'}; {'id': '212', 'type': 'argument_list', 'children': ['213']}, {'id': '213', 'type': 'string', 'children': [], 'value': "r'[sS]tandard [nN]ame [tT]able'"}; {'id': '214', 'type': 'expression_statement', 'children': ['215']}, {'id': '215', 'type': 'call', 'children': ['216', '219']}; {'id': '216', 'type': 'attribute', 'children': ['217', '218']}, {'id': '217', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '218', 'type': 'identifier', 'children': [], 'value': 'assert_true'}, {'id': '219', 'type': 'argument_list', 'children': ['220', '226']}; {'id': '220', 'type': 'call', 'children': ['221', '224']}, {'id': '221', 'type': 'attribute', 'children': ['222', '223']}; {'id': '222', 'type': 'identifier', 'children': [], 'value': 'regex'}, {'id': '223', 'type': 'identifier', 'children': [], 'value': 'search'}; {'id': '224', 'type': 'argument_list', 'children': ['225']}, {'id': '225', 'type': 'identifier', 'children': [], 'value': 'standard_name_vocab'}; {'id': '226', 'type': 'call', 'children': ['227', '230']}, {'id': '227', 'type': 'attribute', 'children': ['228', '229']}; {'id': '228', 'type': 'string', 'children': [], 'value': '"standard_name_vocabulary doesn\'t contain \'Standard Name Table\': {}"'}, {'id': '229', 'type': 'identifier', 'children': [], 'value': 'format'}; {'id': '230', 'type': 'argument_list', 'children': ['231']}, {'id': '231', 'type': 'identifier', 'children': [], 'value': 'standard_name_vocab'}; {'id': '232', 'type': 'if_statement', 'children': ['233', '238']}, {'id': '233', 'type': 'call', 'children': ['234', '235']}; {'id': '234', 'type': 'identifier', 'children': [], 'value': 'hasattr'}, {'id': '235', 'type': 'argument_list', 'children': ['236', '237']}; {'id': '236', 'type': 'identifier', 'children': [], 'value': 'dataset'}, {'id': '237', 'type': 'string', 'children': [], 'value': "'comment'"}; {'id': '238', 'type': 'block', 'children': ['239']}, {'id': '239', 'type': 'expression_statement', 'children': ['240']}; {'id': '240', 'type': 'call', 'children': ['241', '244']}, {'id': '241', 'type': 'attribute', 'children': ['242', '243']}; {'id': '242', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}, {'id': '243', 'type': 'identifier', 'children': [], 'value': 'assert_true'}; {'id': '244', 'type': 'argument_list', 'children': ['245', '253']}, {'id': '245', 'type': 'comparison_operator', 'children': ['246', '252'], 'value': '!='}; {'id': '246', 'type': 'call', 'children': ['247', '248']}, {'id': '247', 'type': 'identifier', 'children': [], 'value': 'getattr'}; {'id': '248', 'type': 'argument_list', 'children': ['249', '250', '251']}, {'id': '249', 'type': 'identifier', 'children': [], 'value': 'dataset'}; {'id': '250', 'type': 'string', 'children': [], 'value': "'comment'"}, {'id': '251', 'type': 'string', 'children': [], 'value': "''"}; {'id': '252', 'type': 'string', 'children': [], 'value': "''"}, {'id': '253', 'type': 'string', 'children': [], 'value': "'comment attribute should not be empty if specified'"}; {'id': '254', 'type': 'return_statement', 'children': ['255']}, {'id': '255', 'type': 'call', 'children': ['256', '259']}; {'id': '256', 'type': 'attribute', 'children': ['257', '258']}, {'id': '257', 'type': 'identifier', 'children': [], 'value': 'recommended_ctx'}; {'id': '258', 'type': 'identifier', 'children': [], 'value': 'to_result'}, {'id': '259', 'type': 'argument_list', 'children': []} | def check_recommended_global_attributes(self, dataset):
'''
Check the global recommended attributes for 2.0 templates. These go an extra step besides
just checking that they exist.
:param netCDF4.Dataset dataset: An open netCDF dataset
:id = "" ; //.................................................. RECOMMENDED - Should be a human readable unique identifier for data set. (ACDD)
:naming_authority = "" ; //.................................... RECOMMENDED - Backward URL of institution (for example, gov.noaa.ncei). (ACDD)
:history = "" ; //............................................. RECOMMENDED - Provides an audit trail for modifications to the original data. (ACDD)
:source = "" ; //.............................................. RECOMMENDED - The method of production of the original data. (CF)
:processing_level = "" ; //.................................... RECOMMENDED - Provide a description of the processing or quality control level of the data. (ACDD)
:comment = "" ; //............................................. RECOMMENDED - Provide useful additional information here. (CF)
:acknowledgment = "" ; //...................................... RECOMMENDED - A place to acknowledge various types of support for the project that produced this data. (ACDD)
:license = "" ; //............................................. RECOMMENDED - Describe the restrictions to data access and distribution. (ACDD)
:standard_name_vocabulary = "CF Standard Name Table vNN" ; //.. RECOMMENDED - If using CF standard name attribute for variables. Replace NN with the CF standard name table number (CF)
:date_created = "" ; //........................................ RECOMMENDED - Creation date of this version of the data(netCDF). Use ISO 8601:2004 for date and time. (ACDD)
:creator_name = "" ; //........................................ RECOMMENDED - The name of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)
:creator_email = "" ; //....................................... RECOMMENDED - The email address of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)
:creator_url = "" ; //......................................... RECOMMENDED - The URL of the person (or other creator type specified by the creator_type attribute) principally responsible for creating this data. (ACDD)
:institution = "" ; //......................................... RECOMMENDED -The name of the institution principally responsible for originating this data.. An institution attribute can be used for each variable if variables come from more than one institution. (CF/ACDD)
:project = "" ; //............................................. RECOMMENDED - The name of the project(s) principally responsible for originating this data. Multiple projects can be separated by commas. (ACDD)
:publisher_name = "" ; //...................................... RECOMMENDED - The name of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)
:publisher_email = "" ; //..................................... RECOMMENDED - The email address of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)
:publisher_url = "" ; //....................................... RECOMMENDED - The URL of the person (or other entity specified by the publisher_type attribute) responsible for publishing the data file or product to users, with its current metadata and format. (ACDD)
:geospatial_bounds = "" ; //................................... RECOMMENDED - Describes the data's 2D or 3D geospatial extent in OGC's Well-Known Text (WKT) Geometry format. (ACDD)
:geospatial_bounds_crs = "" ; //............................... RECOMMENDED - The coordinate reference system (CRS) of the point coordinates in the geospatial_bounds attribute. (ACDD)
:geospatial_bounds_vertical_crs = "" ; //...................... RECOMMENDED - The vertical coordinate reference system (CRS) for the Z axis of the point coordinates in the geospatial_bounds attribute. (ACDD)
:geospatial_lat_min = 0.0d ; //................................ RECOMMENDED - Describes a simple lower latitude limit. (ACDD)
:geospatial_lat_max = 0.0d ; //................................ RECOMMENDED - Describes a simple upper latitude limit. (ACDD)
:geospatial_lon_min = 0.0d ; //................................ RECOMMENDED - Describes a simple lower longitude limit. (ACDD)
:geospatial_lon_max = 0.0d ; //................................ RECOMMENDED - Describes a simple upper longitude limit. (ACDD)
:geospatial_vertical_min = 0.0d ; //........................... RECOMMENDED - Describes the numerically smaller vertical limit. (ACDD)
:geospatial_vertical_max = 0.0d ; //........................... RECOMMENDED - Describes the numerically larger vertical limit. (ACDD)
:geospatial_vertical_positive = "" ; //........................ RECOMMENDED - Use "up" or "down". (ACDD)
:time_coverage_start = "" ; //................................. RECOMMENDED - Describes the time of the first data point in the data set. Use ISO 8601:2004 for date and time. (ACDD)
:time_coverage_end = "" ; //................................... RECOMMENDED - Describes the time of the last data point in the data set. Use ISO 8601:2004 for date and time.(ACDD)
:time_coverage_duration = "" ; //.............................. RECOMMENDED - Describes the duration of the data set. Use ISO 8601:2004 for date and time. (ACDD)
:time_coverage_resolution = "" ; //............................ RECOMMENDED - Describes the targeted time period between each value in the data set. Use ISO 8601:2004 for date and time. (ACDD)
:uuid = "" ; //................................................ RECOMMENDED - Machine readable unique identifier for each file. A new uuid is created whenever the file is changed. (NCEI)
:sea_name = "" ; //............................................ RECOMMENDED - The names of the sea in which the data were collected. Use NCEI sea names table. (NCEI)
'''
recommended_ctx = TestCtx(BaseCheck.MEDIUM, 'Recommended global attributes')
sea_names = [sn.lower() for sn in util.get_sea_names()]
sea_name = getattr(dataset, 'sea_name', '')
sea_name = sea_name.replace(', ', ',')
sea_name = sea_name.split(',') if sea_name else []
for sea in sea_name:
recommended_ctx.assert_true(
sea.lower() in sea_names,
'sea_name attribute should exist and should be from the NODC sea names list: {} is not a valid sea name'.format(sea)
)
for attr in ['time_coverage_start', 'time_coverage_end', 'date_created', 'date_modified']:
attr_value = getattr(dataset, attr, '')
try:
parse_datetime(attr_value)
recommended_ctx.assert_true(True, '')
except ISO8601Error:
recommended_ctx.assert_true(False, '{} should exist and be ISO-8601 format (example: PT1M30S), currently: {}'.format(attr, attr_value))
value = getattr(dataset, 'geospatial_vertical_positive', '')
recommended_ctx.assert_true(value.lower() in ['up', 'down'], 'geospatial_vertical_positive attribute should be up or down: {}'.format(value))
ack_exists = any((getattr(dataset, attr, '') != '' for attr in ['acknowledgment', 'acknowledgement']))
recommended_ctx.assert_true(ack_exists, 'acknowledgement attribute should exist and not be empty')
standard_name_vocab = getattr(dataset, 'standard_name_vocabulary', '')
regex = re.compile(r'[sS]tandard [nN]ame [tT]able')
recommended_ctx.assert_true(regex.search(standard_name_vocab),
"standard_name_vocabulary doesn't contain 'Standard Name Table': {}".format(standard_name_vocab))
if hasattr(dataset, 'comment'):
recommended_ctx.assert_true(getattr(dataset, 'comment', '') != '', 'comment attribute should not be empty if specified')
return recommended_ctx.to_result() |
Subsets and Splits