sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:serialize_to_normalized_pretty_json; 3, parameters; 3, 4; 4, identifier:py_obj; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:json; 12, identifier:dumps; 13, argument_list; 13, 14; 13, 15; 13, 18; 13, 21; 14, identifier:py_obj; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:sort_keys; 17, True; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:indent; 20, integer:2; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:cls; 23, identifier:ToJsonCompatibleTypes
def serialize_to_normalized_pretty_json(py_obj): """Serialize a native object to normalized, pretty printed JSON. The JSON string is normalized by sorting any dictionary keys. Args: py_obj: object Any object that can be represented in JSON. Some types, such as datetimes are automatically converted to strings. Returns: str: normalized, pretty printed JSON string. """ return json.dumps(py_obj, sort_keys=True, indent=2, cls=ToJsonCompatibleTypes)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:serialize_to_normalized_compact_json; 3, parameters; 3, 4; 4, identifier:py_obj; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:json; 12, identifier:dumps; 13, argument_list; 13, 14; 13, 15; 13, 18; 13, 23; 14, identifier:py_obj; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:sort_keys; 17, True; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:separators; 20, tuple; 20, 21; 20, 22; 21, string:','; 22, string:':'; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:cls; 25, identifier:ToJsonCompatibleTypes
def serialize_to_normalized_compact_json(py_obj): """Serialize a native object to normalized, compact JSON. The JSON string is normalized by sorting any dictionary keys. It will be on a single line without whitespace between elements. Args: py_obj: object Any object that can be represented in JSON. Some types, such as datetimes are automatically converted to strings. Returns: str: normalized, compact JSON string. """ return json.dumps( py_obj, sort_keys=True, separators=(',', ':'), cls=ToJsonCompatibleTypes )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:topological_sort; 3, parameters; 3, 4; 4, identifier:unsorted_dict; 5, block; 5, 6; 5, 8; 5, 12; 5, 18; 5, 22; 5, 30; 5, 81; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:sorted_list; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:sorted_set; 15, call; 15, 16; 15, 17; 16, identifier:set; 17, argument_list; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:found; 21, True; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:unconnected_dict; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:unsorted_dict; 28, identifier:copy; 29, argument_list; 30, while_statement; 30, 31; 30, 32; 31, identifier:found; 32, block; 32, 33; 32, 37; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:found; 36, False; 37, for_statement; 37, 38; 37, 41; 37, 49; 38, pattern_list; 38, 39; 38, 40; 39, identifier:pid; 40, identifier:obsoletes_pid; 41, call; 41, 42; 41, 43; 42, identifier:list; 43, argument_list; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:unconnected_dict; 47, identifier:items; 48, argument_list; 49, block; 49, 50; 50, if_statement; 50, 51; 50, 58; 51, boolean_operator:or; 51, 52; 51, 55; 52, comparison_operator:is; 52, 53; 52, 54; 53, identifier:obsoletes_pid; 54, None; 55, comparison_operator:in; 55, 56; 55, 57; 56, identifier:obsoletes_pid; 57, identifier:sorted_set; 58, block; 58, 59; 58, 63; 58, 70; 58, 77; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:found; 62, True; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:sorted_list; 67, identifier:append; 68, argument_list; 68, 69; 69, identifier:pid; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:sorted_set; 74, identifier:add; 75, argument_list; 75, 76; 76, identifier:pid; 77, delete_statement; 77, 78; 78, subscript; 78, 79; 78, 80; 79, identifier:unconnected_dict; 80, identifier:pid; 81, return_statement; 81, 82; 82, expression_list; 82, 83; 82, 84; 83, identifier:sorted_list; 84, identifier:unconnected_dict
def topological_sort(unsorted_dict): """Sort objects by dependency. Sort a dict of obsoleting PID to obsoleted PID to a list of PIDs in order of obsolescence. Args: unsorted_dict : dict Dict that holds obsolescence information. Each ``key/value`` pair establishes that the PID in ``key`` identifies an object that obsoletes an object identifies by the PID in ``value``. Returns: tuple of sorted_list, unconnected_dict : ``sorted_list``: A list of PIDs ordered so that all PIDs that obsolete an object are listed after the object they obsolete. ``unconnected_dict``: A dict of PID to obsoleted PID of any objects that could not be added to a revision chain. These items will have obsoletes PIDs that directly or indirectly reference a PID that could not be sorted. Notes: ``obsoletes_dict`` is modified by the sort and on return holds any items that could not be sorted. The sort works by repeatedly iterating over an unsorted list of PIDs and moving PIDs to the sorted list as they become available. A PID is available to be moved to the sorted list if it does not obsolete a PID or if the PID it obsoletes is already in the sorted list. """ sorted_list = [] sorted_set = set() found = True unconnected_dict = unsorted_dict.copy() while found: found = False for pid, obsoletes_pid in list(unconnected_dict.items()): if obsoletes_pid is None or obsoletes_pid in sorted_set: found = True sorted_list.append(pid) sorted_set.add(pid) del unconnected_dict[pid] return sorted_list, unconnected_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort_elements_by_child_values; 3, parameters; 3, 4; 3, 5; 4, identifier:obj_pyxb; 5, identifier:child_name_list; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:obj_pyxb; 13, identifier:sort; 14, argument_list; 14, 15; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:key; 17, lambda; 17, 18; 17, 20; 18, lambda_parameters; 18, 19; 19, identifier:x; 20, list_comprehension; 20, 21; 20, 29; 21, call; 21, 22; 21, 23; 22, identifier:get_auto; 23, argument_list; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:getattr; 26, argument_list; 26, 27; 26, 28; 27, identifier:x; 28, identifier:n; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:n; 31, identifier:child_name_list
def sort_elements_by_child_values(obj_pyxb, child_name_list): """In-place sort simple or complex elements in a PyXB object by values they contain in child elements. Args: obj_pyxb: PyXB object child_name_list: list of str List of element names that are direct children of the PyXB object. """ obj_pyxb.sort(key=lambda x: [get_auto(getattr(x, n)) for n in child_name_list])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:get_field_values; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 10; 3, 13; 4, identifier:self; 5, identifier:name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:maxvalues; 8, unary_operator:-; 8, 9; 9, integer:1; 10, default_parameter; 10, 11; 10, 12; 11, identifier:sort; 12, True; 13, dictionary_splat_pattern; 13, 14; 14, identifier:query_dict; 15, block; 15, 16; 15, 18; 15, 26; 15, 61; 15, 71; 15, 79; 15, 89; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:param_dict; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:query_dict; 24, identifier:copy; 25, argument_list; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:param_dict; 30, identifier:update; 31, argument_list; 31, 32; 32, dictionary; 32, 33; 32, 36; 32, 39; 32, 42; 32, 48; 32, 51; 33, pair; 33, 34; 33, 35; 34, string:'rows'; 35, string:'0'; 36, pair; 36, 37; 36, 38; 37, string:'facet'; 38, string:'true'; 39, pair; 39, 40; 39, 41; 40, string:'facet.field'; 41, identifier:name; 42, pair; 42, 43; 42, 44; 43, string:'facet.limit'; 44, call; 44, 45; 44, 46; 45, identifier:str; 46, argument_list; 46, 47; 47, identifier:maxvalues; 48, pair; 48, 49; 48, 50; 49, string:'facet.zeros'; 50, string:'false'; 51, pair; 51, 52; 51, 53; 52, string:'facet.sort'; 53, call; 53, 54; 53, 60; 54, attribute; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:str; 57, argument_list; 57, 58; 58, identifier:sort; 59, identifier:lower; 60, argument_list; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:resp_dict; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:self; 67, identifier:_post_query; 68, argument_list; 68, 69; 69, dictionary_splat; 69, 70; 70, identifier:param_dict; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:result_dict; 74, subscript; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:resp_dict; 77, string:'facet_counts'; 78, string:'facet_fields'; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:result_dict; 83, string:'numFound'; 84, subscript; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:resp_dict; 87, string:'response'; 88, string:'numFound'; 89, return_statement; 89, 90; 90, identifier:result_dict
def get_field_values(self, name, maxvalues=-1, sort=True, **query_dict): """Retrieve the unique values for a field, along with their usage counts. :param name: Name of field for which to retrieve values :type name: string :param sort: Sort the result :param maxvalues: Maximum number of values to retrieve. Default is -1, which causes retrieval of all values. :type maxvalues: int :returns: dict of {fieldname: [[value, count], ... ], } """ param_dict = query_dict.copy() param_dict.update( { 'rows': '0', 'facet': 'true', 'facet.field': name, 'facet.limit': str(maxvalues), 'facet.zeros': 'false', 'facet.sort': str(sort).lower(), } ) resp_dict = self._post_query(**param_dict) result_dict = resp_dict['facet_counts']['facet_fields'] result_dict['numFound'] = resp_dict['response']['numFound'] return result_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:bin; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:x; 5, identifier:bins; 6, default_parameter; 6, 7; 6, 8; 7, identifier:maxX; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:minX; 11, None; 12, block; 12, 13; 12, 15; 12, 28; 12, 41; 12, 65; 12, 66; 12, 67; 13, expression_statement; 13, 14; 14, string:''' bin signal x using 'binsN' bin. If minX, maxX are None, they default to the full range of the signal. If they are not None, everything above maxX gets assigned to binsN-1 and everything below minX gets assigned to 0, this is effectively the same as clipping x before passing it to 'bin' input: ----- x: signal to be binned, some sort of iterable bins: int, number of bins iterable, bin edges maxX: clips data above maxX minX: clips data below maxX output: ------ binnedX: x after being binned bins: bins used for binning. if input 'bins' is already an iterable it just returns the same iterable example: # make 10 bins of equal length spanning from x.min() to x.max() bin(x, 10) # use predefined bins such that each bin has the same number of points (maximize entropy) binsN = 10 percentiles = list(np.arange(0, 100.1, 100/binsN)) bins = np.percentile(x, percentiles) bin(x, bins) '''; 15, if_statement; 15, 16; 15, 19; 16, comparison_operator:is; 16, 17; 16, 18; 17, identifier:maxX; 18, None; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:maxX; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:x; 26, identifier:max; 27, argument_list; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:minX; 31, None; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:minX; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:x; 39, identifier:min; 40, argument_list; 41, if_statement; 41, 42; 41, 49; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:np; 46, identifier:iterable; 47, argument_list; 47, 48; 48, identifier:bins; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:bins; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:np; 56, identifier:linspace; 57, argument_list; 57, 58; 57, 59; 57, 62; 58, identifier:minX; 59, binary_operator:+; 59, 60; 59, 61; 60, identifier:maxX; 61, float:1e-5; 62, binary_operator:+; 62, 63; 62, 64; 63, identifier:bins; 64, integer:1; 65, comment; 66, comment; 67, return_statement; 67, 68; 68, expression_list; 68, 69; 68, 87; 69, call; 69, 70; 69, 83; 70, attribute; 70, 71; 70, 82; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:np; 74, identifier:digitize; 75, argument_list; 75, 76; 75, 81; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:x; 79, identifier:ravel; 80, argument_list; 81, identifier:bins; 82, identifier:reshape; 83, argument_list; 83, 84; 84, attribute; 84, 85; 84, 86; 85, identifier:x; 86, identifier:shape; 87, identifier:bins
def bin(x, bins, maxX=None, minX=None): ''' bin signal x using 'binsN' bin. If minX, maxX are None, they default to the full range of the signal. If they are not None, everything above maxX gets assigned to binsN-1 and everything below minX gets assigned to 0, this is effectively the same as clipping x before passing it to 'bin' input: ----- x: signal to be binned, some sort of iterable bins: int, number of bins iterable, bin edges maxX: clips data above maxX minX: clips data below maxX output: ------ binnedX: x after being binned bins: bins used for binning. if input 'bins' is already an iterable it just returns the same iterable example: # make 10 bins of equal length spanning from x.min() to x.max() bin(x, 10) # use predefined bins such that each bin has the same number of points (maximize entropy) binsN = 10 percentiles = list(np.arange(0, 100.1, 100/binsN)) bins = np.percentile(x, percentiles) bin(x, bins) ''' if maxX is None: maxX = x.max() if minX is None: minX = x.min() if not np.iterable(bins): bins = np.linspace(minX, maxX+1e-5, bins+1) # digitize works on 1d array but not nd arrays. # So I pass the flattened version of x and then reshape back into x's original shape return np.digitize(x.ravel(), bins).reshape(x.shape), bins
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 28; 2, function_name:update_item; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 4, identifier:self; 5, identifier:table_name; 6, identifier:key_dict; 7, default_parameter; 7, 8; 7, 9; 8, identifier:condition_expression; 9, None; 10, default_parameter; 10, 11; 10, 12; 11, identifier:update_expression; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:expression_attribute_names; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:expression_attribute_values; 18, None; 19, default_parameter; 19, 20; 19, 21; 20, identifier:return_consumed_capacity; 21, None; 22, default_parameter; 22, 23; 22, 24; 23, identifier:return_item_collection_metrics; 24, None; 25, default_parameter; 25, 26; 25, 27; 26, identifier:return_values; 27, None; 28, block; 28, 29; 28, 31; 28, 49; 28, 58; 28, 67; 28, 82; 28, 96; 28, 111; 28, 125; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:payload; 34, dictionary; 34, 35; 34, 38; 34, 46; 35, pair; 35, 36; 35, 37; 36, string:'TableName'; 37, identifier:table_name; 38, pair; 38, 39; 38, 40; 39, string:'Key'; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:utils; 43, identifier:marshall; 44, argument_list; 44, 45; 45, identifier:key_dict; 46, pair; 46, 47; 46, 48; 47, string:'UpdateExpression'; 48, identifier:update_expression; 49, if_statement; 49, 50; 49, 51; 50, identifier:condition_expression; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:payload; 56, string:'ConditionExpression'; 57, identifier:condition_expression; 58, if_statement; 58, 59; 58, 60; 59, identifier:expression_attribute_names; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:payload; 65, string:'ExpressionAttributeNames'; 66, identifier:expression_attribute_names; 67, if_statement; 67, 68; 67, 69; 68, identifier:expression_attribute_values; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 71, 76; 72, subscript; 72, 73; 72, 74; 73, identifier:payload; 74, string:'ExpressionAttributeValues'; 75, line_continuation:\; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:utils; 79, identifier:marshall; 80, argument_list; 80, 81; 81, identifier:expression_attribute_values; 82, if_statement; 82, 83; 82, 84; 83, identifier:return_consumed_capacity; 84, block; 84, 85; 84, 90; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 88; 87, identifier:_validate_return_consumed_capacity; 88, argument_list; 88, 89; 89, identifier:return_consumed_capacity; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:payload; 94, string:'ReturnConsumedCapacity'; 95, identifier:return_consumed_capacity; 96, if_statement; 96, 97; 96, 98; 97, identifier:return_item_collection_metrics; 98, block; 98, 99; 98, 104; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 102; 101, identifier:_validate_return_item_collection_metrics; 102, argument_list; 102, 103; 103, identifier:return_item_collection_metrics; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 105, 110; 106, subscript; 106, 107; 106, 108; 107, identifier:payload; 108, string:'ReturnItemCollectionMetrics'; 109, line_continuation:\; 110, identifier:return_item_collection_metrics; 111, if_statement; 111, 112; 111, 113; 112, identifier:return_values; 113, block; 113, 114; 113, 119; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:_validate_return_values; 117, argument_list; 117, 118; 118, identifier:return_values; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 124; 121, subscript; 121, 122; 121, 123; 122, identifier:payload; 123, string:'ReturnValues'; 124, identifier:return_values; 125, return_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:execute; 130, argument_list; 130, 131; 130, 132; 131, string:'UpdateItem'; 132, identifier:payload
def update_item(self, table_name, key_dict, condition_expression=None, update_expression=None, expression_attribute_names=None, expression_attribute_values=None, return_consumed_capacity=None, return_item_collection_metrics=None, return_values=None): """Invoke the `UpdateItem`_ function. Edits an existing item's attributes, or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values. You can also perform a conditional update on an existing item (insert a new attribute name-value pair if it doesn't exist, or replace an existing name-value pair if it has certain expected attribute values). :param str table_name: The name of the table that contains the item to update :param dict key_dict: A dictionary of key/value pairs that are used to define the primary key values for the item. For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key. :param str condition_expression: A condition that must be satisfied in order for a conditional *UpdateItem* operation to succeed. One of: ``attribute_exists``, ``attribute_not_exists``, ``attribute_type``, ``contains``, ``begins_with``, ``size``, ``=``, ``<>``, ``<``, ``>``, ``<=``, ``>=``, ``BETWEEN``, ``IN``, ``AND``, ``OR``, or ``NOT``. :param str update_expression: An expression that defines one or more attributes to be updated, the action to be performed on them, and new value(s) for them. :param dict expression_attribute_names: One or more substitution tokens for attribute names in an expression. :param dict expression_attribute_values: One or more values that can be substituted in an expression. :param str return_consumed_capacity: Determines the level of detail about provisioned throughput consumption that is returned in the response. See the `AWS documentation for ReturnConsumedCapacity <http://docs.aws.amazon.com/ amazondynamodb/latest/APIReference/API_UpdateItem.html#DDB-Update Item-request-ReturnConsumedCapacity>`_ for more information. :param str return_item_collection_metrics: Determines whether item collection metrics are returned. :param str return_values: Use ReturnValues if you want to get the item attributes as they appeared either before or after they were updated. See the `AWS documentation for ReturnValues <http://docs. aws.amazon.com/amazondynamodb/latest/APIReference/ API_UpdateItem.html#DDB-UpdateItem-request-ReturnValues>`_ :rtype: tornado.concurrent.Future .. _UpdateItem: http://docs.aws.amazon.com/amazondynamodb/ latest/APIReference/API_UpdateItem.html """ payload = {'TableName': table_name, 'Key': utils.marshall(key_dict), 'UpdateExpression': update_expression} if condition_expression: payload['ConditionExpression'] = condition_expression if expression_attribute_names: payload['ExpressionAttributeNames'] = expression_attribute_names if expression_attribute_values: payload['ExpressionAttributeValues'] = \ utils.marshall(expression_attribute_values) if return_consumed_capacity: _validate_return_consumed_capacity(return_consumed_capacity) payload['ReturnConsumedCapacity'] = return_consumed_capacity if return_item_collection_metrics: _validate_return_item_collection_metrics( return_item_collection_metrics) payload['ReturnItemCollectionMetrics'] = \ return_item_collection_metrics if return_values: _validate_return_values(return_values) payload['ReturnValues'] = return_values return self.execute('UpdateItem', payload)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 42; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 3, 36; 3, 39; 4, identifier:self; 5, identifier:table_name; 6, default_parameter; 6, 7; 6, 8; 7, identifier:index_name; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:consistent_read; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:key_condition_expression; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:filter_expression; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:expression_attribute_names; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:expression_attribute_values; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:projection_expression; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:select; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:exclusive_start_key; 32, None; 33, default_parameter; 33, 34; 33, 35; 34, identifier:limit; 35, None; 36, default_parameter; 36, 37; 36, 38; 37, identifier:scan_index_forward; 38, True; 39, default_parameter; 39, 40; 39, 41; 40, identifier:return_consumed_capacity; 41, None; 42, block; 42, 43; 42, 45; 42, 55; 42, 64; 42, 75; 42, 84; 42, 93; 42, 102; 42, 117; 42, 126; 42, 140; 42, 154; 42, 163; 42, 177; 43, expression_statement; 43, 44; 44, comment; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:payload; 48, dictionary; 48, 49; 48, 52; 49, pair; 49, 50; 49, 51; 50, string:'TableName'; 51, identifier:table_name; 52, pair; 52, 53; 52, 54; 53, string:'ScanIndexForward'; 54, identifier:scan_index_forward; 55, if_statement; 55, 56; 55, 57; 56, identifier:index_name; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, subscript; 60, 61; 60, 62; 61, identifier:payload; 62, string:'IndexName'; 63, identifier:index_name; 64, if_statement; 64, 65; 64, 68; 65, comparison_operator:is; 65, 66; 65, 67; 66, identifier:consistent_read; 67, None; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:payload; 73, string:'ConsistentRead'; 74, identifier:consistent_read; 75, if_statement; 75, 76; 75, 77; 76, identifier:key_condition_expression; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 83; 80, subscript; 80, 81; 80, 82; 81, identifier:payload; 82, string:'KeyConditionExpression'; 83, identifier:key_condition_expression; 84, if_statement; 84, 85; 84, 86; 85, identifier:filter_expression; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:payload; 91, string:'FilterExpression'; 92, identifier:filter_expression; 93, if_statement; 93, 94; 93, 95; 94, identifier:expression_attribute_names; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:payload; 100, string:'ExpressionAttributeNames'; 101, identifier:expression_attribute_names; 102, if_statement; 102, 103; 102, 104; 103, identifier:expression_attribute_values; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 106, 111; 107, subscript; 107, 108; 107, 109; 108, identifier:payload; 109, string:'ExpressionAttributeValues'; 110, line_continuation:\; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:utils; 114, identifier:marshall; 115, argument_list; 115, 116; 116, identifier:expression_attribute_values; 117, if_statement; 117, 118; 117, 119; 118, identifier:projection_expression; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:payload; 124, string:'ProjectionExpression'; 125, identifier:projection_expression; 126, if_statement; 126, 127; 126, 128; 127, identifier:select; 128, block; 128, 129; 128, 134; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 132; 131, identifier:_validate_select; 132, argument_list; 132, 133; 133, identifier:select; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 139; 136, subscript; 136, 137; 136, 138; 137, identifier:payload; 138, string:'Select'; 139, identifier:select; 140, if_statement; 140, 141; 140, 142; 141, identifier:exclusive_start_key; 142, block; 142, 143; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 148; 145, subscript; 145, 146; 145, 147; 146, identifier:payload; 147, string:'ExclusiveStartKey'; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:utils; 151, identifier:marshall; 152, argument_list; 152, 153; 153, identifier:exclusive_start_key; 154, if_statement; 154, 155; 154, 156; 155, identifier:limit; 156, block; 156, 157; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, subscript; 159, 160; 159, 161; 160, identifier:payload; 161, string:'Limit'; 162, identifier:limit; 163, if_statement; 163, 164; 163, 165; 164, identifier:return_consumed_capacity; 165, block; 165, 166; 165, 171; 166, expression_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:_validate_return_consumed_capacity; 169, argument_list; 169, 170; 170, identifier:return_consumed_capacity; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:payload; 175, string:'ReturnConsumedCapacity'; 176, identifier:return_consumed_capacity; 177, return_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:execute; 182, argument_list; 182, 183; 182, 184; 183, string:'Query'; 184, identifier:payload
def query(self, table_name, index_name=None, consistent_read=None, key_condition_expression=None, filter_expression=None, expression_attribute_names=None, expression_attribute_values=None, projection_expression=None, select=None, exclusive_start_key=None, limit=None, scan_index_forward=True, return_consumed_capacity=None): """A `Query`_ operation uses the primary key of a table or a secondary index to directly access items from that table or index. :param str table_name: The name of the table containing the requested items. :param bool consistent_read: Determines the read consistency model: If set to ``True``, then the operation uses strongly consistent reads; otherwise, the operation uses eventually consistent reads. Strongly consistent reads are not supported on global secondary indexes. If you query a global secondary index with ``consistent_read`` set to ``True``, you will receive a :exc:`~sprockets_dynamodb.exceptions.ValidationException`. :param dict exclusive_start_key: The primary key of the first item that this operation will evaluate. Use the value that was returned for ``LastEvaluatedKey`` in the previous operation. In a parallel scan, a *Scan* request that includes ``exclusive_start_key`` must specify the same segment whose previous *Scan* returned the corresponding value of ``LastEvaluatedKey``. :param dict expression_attribute_names: One or more substitution tokens for attribute names in an expression. :param dict expression_attribute_values: One or more values that can be substituted in an expression. :param str key_condition_expression: The condition that specifies the key value(s) for items to be retrieved by the *Query* action. The condition must perform an equality test on a single partition key value, but can optionally perform one of several comparison tests on a single sort key value. The partition key equality test is required. For examples see `KeyConditionExpression <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ Query.html#Query.KeyConditionExpressions>. :param str filter_expression: A string that contains conditions that DynamoDB applies after the *Query* operation, but before the data is returned to you. Items that do not satisfy the criteria are not returned. Note that a filter expression is applied after the items have already been read; the process of filtering does not consume any additional read capacity units. For more information, see `Filter Expressions <http://docs.aws.amazon.com/amazondynamodb/ latest/developerguide/QueryAndScan.html#FilteringResults>`_ in the Amazon DynamoDB Developer Guide. :param str projection_expression: :param str index_name: The name of a secondary index to query. This index can be any local secondary index or global secondary index. Note that if you use this parameter, you must also provide ``table_name``. :param int limit: The maximum number of items to evaluate (not necessarily the number of matching items). If DynamoDB processes the number of items up to the limit while processing the results, it stops the operation and returns the matching values up to that point, and a key in ``LastEvaluatedKey`` to apply in a subsequent operation, so that you can pick up where you left off. Also, if the processed data set size exceeds 1 MB before DynamoDB reaches this limit, it stops the operation and returns the matching values up to the limit, and a key in ``LastEvaluatedKey`` to apply in a subsequent operation to continue the operation. For more information, see `Query and Scan <http://docs.aws.amazon.com/amazo ndynamodb/latest/developerguide/QueryAndScan.html>`_ in the Amazon DynamoDB Developer Guide. :param str return_consumed_capacity: Determines the level of detail about provisioned throughput consumption that is returned in the response: - ``INDEXES``: The response includes the aggregate consumed capacity for the operation, together with consumed capacity for each table and secondary index that was accessed. Note that some operations, such as *GetItem* and *BatchGetItem*, do not access any indexes at all. In these cases, specifying ``INDEXES`` will only return consumed capacity information for table(s). - ``TOTAL``: The response includes only the aggregate consumed capacity for the operation. - ``NONE``: No consumed capacity details are included in the response. :param bool scan_index_forward: Specifies the order for index traversal: If ``True`` (default), the traversal is performed in ascending order; if ``False``, the traversal is performed in descending order. Items with the same partition key value are stored in sorted order by sort key. If the sort key data type is *Number*, the results are stored in numeric order. For type *String*, the results are stored in order of ASCII character code values. For type *Binary*, DynamoDB treats each byte of the binary data as unsigned. If set to ``True``, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If set to ``False``, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client. :param str select: The attributes to be returned in the result. You can retrieve all item attributes, specific item attributes, the count of matching items, or in the case of an index, some or all of the attributes projected into the index. Possible values are: - ``ALL_ATTRIBUTES``: Returns all of the item attributes from the specified table or index. If you query a local secondary index, then for each matching item in the index DynamoDB will fetch the entire item from the parent table. If the index is configured to project all item attributes, then all of the data can be obtained from the local secondary index, and no fetching is required. - ``ALL_PROJECTED_ATTRIBUTES``: Allowed only when querying an index. Retrieves all attributes that have been projected into the index. If the index is configured to project all attributes, this return value is equivalent to specifying ``ALL_ATTRIBUTES``. - ``COUNT``: Returns the number of matching items, rather than the matching items themselves. :rtype: dict .. _Query: http://docs.aws.amazon.com/amazondynamodb/ latest/APIReference/API_Query.html """ payload = {'TableName': table_name, 'ScanIndexForward': scan_index_forward} if index_name: payload['IndexName'] = index_name if consistent_read is not None: payload['ConsistentRead'] = consistent_read if key_condition_expression: payload['KeyConditionExpression'] = key_condition_expression if filter_expression: payload['FilterExpression'] = filter_expression if expression_attribute_names: payload['ExpressionAttributeNames'] = expression_attribute_names if expression_attribute_values: payload['ExpressionAttributeValues'] = \ utils.marshall(expression_attribute_values) if projection_expression: payload['ProjectionExpression'] = projection_expression if select: _validate_select(select) payload['Select'] = select if exclusive_start_key: payload['ExclusiveStartKey'] = utils.marshall(exclusive_start_key) if limit: payload['Limit'] = limit if return_consumed_capacity: _validate_return_consumed_capacity(return_consumed_capacity) payload['ReturnConsumedCapacity'] = return_consumed_capacity return self.execute('Query', payload)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:get_gae_versions; 3, parameters; 4, block; 4, 5; 4, 7; 4, 16; 4, 22; 4, 36; 4, 37; 4, 38; 4, 39; 4, 40; 4, 44; 4, 86; 5, expression_statement; 5, 6; 6, comment; 7, expression_statement; 7, 8; 8, assignment; 8, 9; 8, 10; 9, identifier:r; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:requests; 13, identifier:get; 14, argument_list; 14, 15; 15, identifier:SDK_RELEASES_URL; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:r; 20, identifier:raise_for_status; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:releases; 25, call; 25, 26; 25, 33; 26, attribute; 26, 27; 26, 32; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:r; 30, identifier:json; 31, argument_list; 32, identifier:get; 33, argument_list; 33, 34; 33, 35; 34, string:'items'; 35, dictionary; 36, comment; 37, comment; 38, comment; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:versions_and_urls; 43, list:[]; 44, for_statement; 44, 45; 44, 46; 44, 47; 45, identifier:release; 46, identifier:releases; 47, block; 47, 48; 47, 59; 47, 64; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:match; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:PYTHON_RELEASE_RE; 54, identifier:match; 55, argument_list; 55, 56; 56, subscript; 56, 57; 56, 58; 57, identifier:release; 58, string:'name'; 59, if_statement; 59, 60; 59, 62; 60, not_operator; 60, 61; 61, identifier:match; 62, block; 62, 63; 63, continue_statement; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:versions_and_urls; 68, identifier:append; 69, argument_list; 69, 70; 70, tuple; 70, 71; 70, 83; 71, list_comprehension; 71, 72; 71, 76; 72, call; 72, 73; 72, 74; 73, identifier:int; 74, argument_list; 74, 75; 75, identifier:x; 76, for_in_clause; 76, 77; 76, 78; 77, identifier:x; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:match; 81, identifier:groups; 82, argument_list; 83, subscript; 83, 84; 83, 85; 84, identifier:release; 85, string:'mediaLink'; 86, return_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:sorted; 89, argument_list; 89, 90; 89, 91; 90, identifier:versions_and_urls; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:key; 93, lambda; 93, 94; 93, 96; 94, lambda_parameters; 94, 95; 95, identifier:x; 96, subscript; 96, 97; 96, 98; 97, identifier:x; 98, integer:0
def get_gae_versions(): """Gets a list of all of the available Python SDK versions, sorted with the newest last.""" r = requests.get(SDK_RELEASES_URL) r.raise_for_status() releases = r.json().get('items', {}) # We only care about the Python releases, which all are in the format # "featured/google_appengine_{version}.zip". We'll extract the version # number so we can sort the list by version, and finally get the download # URL. versions_and_urls = [] for release in releases: match = PYTHON_RELEASE_RE.match(release['name']) if not match: continue versions_and_urls.append( ([int(x) for x in match.groups()], release['mediaLink'])) return sorted(versions_and_urls, key=lambda x: x[0])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 20; 2, function_name:_parse_options; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:options; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:List; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:str; 12, type; 12, 13; 13, generic_type; 13, 14; 13, 15; 14, identifier:Dict; 15, type_parameter; 15, 16; 15, 18; 16, type; 16, 17; 17, identifier:str; 18, type; 18, 19; 19, identifier:str; 20, block; 20, 21; 20, 23; 21, expression_statement; 21, 22; 22, comment; 23, try_statement; 23, 24; 23, 41; 24, block; 24, 25; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:dict; 28, generator_expression; 28, 29; 28, 38; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:i; 32, identifier:split; 33, argument_list; 33, 34; 33, 35; 34, string:'='; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:maxsplit; 37, integer:1; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:i; 40, identifier:options; 41, except_clause; 41, 42; 41, 43; 42, identifier:ValueError; 43, block; 43, 44; 44, raise_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:ArgumentError; 47, argument_list; 47, 48; 48, string:f'Option must be in format <key>=<value>, got: {options}'
def _parse_options(options: List[str]) -> Dict[str, str]: """ Parse repeatable CLI options >>> opts = _parse_options(['cluster.name=foo', 'CRATE_JAVA_OPTS="-Dxy=foo"']) >>> print(json.dumps(opts, sort_keys=True)) {"CRATE_JAVA_OPTS": "\\"-Dxy=foo\\"", "cluster.name": "foo"} """ try: return dict(i.split('=', maxsplit=1) for i in options) except ValueError: raise ArgumentError( f'Option must be in format <key>=<value>, got: {options}')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:collapse; 3, parameters; 3, 4; 3, 5; 4, identifier:l; 5, identifier:raw; 6, block; 6, 7; 6, 9; 6, 16; 6, 17; 6, 21; 6, 25; 6, 220; 6, 227; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:logger_ts; 13, identifier:info; 14, argument_list; 14, 15; 15, string:"enter collapse"; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:_master; 20, dictionary; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:_dsn; 24, string:""; 25, try_statement; 25, 26; 25, 27; 25, 192; 26, comment; 27, block; 27, 28; 27, 36; 27, 37; 27, 157; 27, 158; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:_pc; 31, subscript; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:l; 34, integer:0; 35, string:"mode"; 36, comment; 37, for_statement; 37, 38; 37, 39; 37, 40; 37, 41; 38, identifier:entry; 39, identifier:l; 40, comment; 41, block; 41, 42; 41, 48; 41, 52; 41, 56; 41, 57; 41, 146; 41, 147; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:dsn; 45, subscript; 45, 46; 45, 47; 46, identifier:entry; 47, string:'dataSetName'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:_dsn; 51, identifier:dsn; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:_current; 55, identifier:entry; 56, comment; 57, if_statement; 57, 58; 57, 61; 58, comparison_operator:not; 58, 59; 58, 60; 59, identifier:dsn; 60, identifier:_master; 61, block; 61, 62; 61, 74; 61, 84; 61, 96; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:logger_ts; 66, identifier:info; 67, argument_list; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, string:"collapsing: {}"; 71, identifier:format; 72, argument_list; 72, 73; 73, identifier:dsn; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:print; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, string:"collapsing: {}"; 81, identifier:format; 82, argument_list; 82, 83; 83, identifier:dsn; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, pattern_list; 86, 87; 86, 88; 87, identifier:_master; 88, identifier:_current; 89, call; 89, 90; 89, 91; 90, identifier:_collapse_root; 91, argument_list; 91, 92; 91, 93; 91, 94; 91, 95; 92, identifier:_master; 93, identifier:_current; 94, identifier:dsn; 95, identifier:_pc; 96, try_statement; 96, 97; 96, 129; 97, block; 97, 98; 97, 110; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 105; 100, subscript; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:_master; 103, identifier:dsn; 104, string:"paleoData"; 105, subscript; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:raw; 108, identifier:dsn; 109, string:"paleoData"; 110, if_statement; 110, 111; 110, 116; 111, comparison_operator:in; 111, 112; 111, 113; 112, string:"chronData"; 113, subscript; 113, 114; 113, 115; 114, identifier:raw; 115, identifier:dsn; 116, block; 116, 117; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 124; 119, subscript; 119, 120; 119, 123; 120, subscript; 120, 121; 120, 122; 121, identifier:_master; 122, identifier:dsn; 123, string:"chronData"; 124, subscript; 124, 125; 124, 128; 125, subscript; 125, 126; 125, 127; 126, identifier:raw; 127, identifier:dsn; 128, string:"chronData"; 129, except_clause; 129, 130; 129, 134; 130, as_pattern; 130, 131; 130, 132; 131, identifier:KeyError; 132, as_pattern_target; 132, 133; 133, identifier:e; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:print; 138, argument_list; 138, 139; 139, call; 139, 140; 139, 143; 140, attribute; 140, 141; 140, 142; 141, string:"collapse: Could not collapse an object the dataset: {}, {}"; 142, identifier:format; 143, argument_list; 143, 144; 143, 145; 144, identifier:dsn; 145, identifier:e; 146, comment; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:_master; 150, call; 150, 151; 150, 152; 151, identifier:_collapse_pc; 152, argument_list; 152, 153; 152, 154; 152, 155; 152, 156; 153, identifier:_master; 154, identifier:_current; 155, identifier:dsn; 156, identifier:_pc; 157, comment; 158, if_statement; 158, 159; 158, 165; 158, 177; 159, comparison_operator:==; 159, 160; 159, 164; 160, call; 160, 161; 160, 162; 161, identifier:len; 162, argument_list; 162, 163; 163, identifier:_master; 164, integer:1; 165, block; 165, 166; 165, 172; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:_master; 169, subscript; 169, 170; 169, 171; 170, identifier:_master; 171, identifier:_dsn; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 175; 174, identifier:print; 175, argument_list; 175, 176; 176, string:"Created LiPD data: 1 dataset"; 177, else_clause; 177, 178; 178, block; 178, 179; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 182; 181, identifier:print; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, string:"Created LiPD data: {} datasets"; 186, identifier:format; 187, argument_list; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:len; 190, argument_list; 190, 191; 191, identifier:_master; 192, except_clause; 192, 193; 192, 197; 193, as_pattern; 193, 194; 193, 195; 194, identifier:Exception; 195, as_pattern_target; 195, 196; 196, identifier:e; 197, block; 197, 198; 197, 208; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 201; 200, identifier:print; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, string:"Error: Unable to collapse time series, {}"; 205, identifier:format; 206, argument_list; 206, 207; 207, identifier:e; 208, expression_statement; 208, 209; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:logger_ts; 212, identifier:error; 213, argument_list; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, string:"collapse: Exception: {}"; 217, identifier:format; 218, argument_list; 218, 219; 219, identifier:e; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:logger_ts; 224, identifier:info; 225, argument_list; 225, 226; 226, string:"exit collapse"; 227, return_statement; 227, 228; 228, identifier:_master
def collapse(l, raw): """ LiPD Version 1.3 Main function to initiate time series to LiPD conversion Each object has a: "paleoNumber" or "chronNumber" "tableNumber" "modelNumber" "time_id" "mode" - chronData or paleoData "tableType" - "meas" "ens" "summ" :param list l: Time series :return dict _master: LiPD data, sorted by dataset name """ logger_ts.info("enter collapse") # LiPD data (in progress), sorted dataset name _master = {} _dsn = "" try: # Determine if we're collapsing a paleo or chron time series _pc = l[0]["mode"] # Loop the time series for entry in l: # Get notable keys dsn = entry['dataSetName'] _dsn = dsn _current = entry # Since root items are the same in each column of the same dataset, we only need these steps the first time. if dsn not in _master: logger_ts.info("collapsing: {}".format(dsn)) print("collapsing: {}".format(dsn)) _master, _current = _collapse_root(_master, _current, dsn, _pc) try: _master[dsn]["paleoData"] = raw[dsn]["paleoData"] if "chronData" in raw[dsn]: _master[dsn]["chronData"] = raw[dsn]["chronData"] except KeyError as e: print("collapse: Could not collapse an object the dataset: {}, {}".format(dsn, e)) # Collapse pc, calibration, and interpretation _master = _collapse_pc(_master, _current, dsn, _pc) # The result combined into a single dataset. Remove the extra layer on the data. if len(_master) == 1: _master = _master[_dsn] print("Created LiPD data: 1 dataset") else: print("Created LiPD data: {} datasets".format(len(_master))) except Exception as e: print("Error: Unable to collapse time series, {}".format(e)) logger_ts.error("collapse: Exception: {}".format(e)) logger_ts.info("exit collapse") return _master
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:collapseTs; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:ts; 6, None; 7, block; 7, 8; 7, 10; 7, 11; 7, 13; 7, 17; 7, 98; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, global_statement; 11, 12; 12, identifier:_timeseries_data; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:_d; 16, dictionary; 17, if_statement; 17, 18; 17, 20; 17, 26; 18, not_operator; 18, 19; 19, identifier:ts; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:print; 24, argument_list; 24, 25; 25, string:"Error: Time series data not provided. Pass time series into the function."; 26, else_clause; 26, 27; 26, 28; 27, comment; 28, block; 28, 29; 29, try_statement; 29, 30; 29, 70; 30, block; 30, 31; 30, 41; 30, 55; 30, 63; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:_raw; 34, subscript; 34, 35; 34, 36; 35, identifier:_timeseries_data; 36, subscript; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:ts; 39, integer:0; 40, string:"time_id"; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:print; 44, argument_list; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:mode_ts; 47, argument_list; 47, 48; 47, 49; 47, 52; 48, string:"collapse"; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:mode; 51, string:""; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:ts; 54, identifier:ts; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:_d; 58, call; 58, 59; 58, 60; 59, identifier:collapse; 60, argument_list; 60, 61; 60, 62; 61, identifier:ts; 62, identifier:_raw; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:_d; 66, call; 66, 67; 66, 68; 67, identifier:rm_empty_fields; 68, argument_list; 68, 69; 69, identifier:_d; 70, except_clause; 70, 71; 70, 75; 71, as_pattern; 71, 72; 71, 73; 72, identifier:Exception; 73, as_pattern_target; 73, 74; 74, identifier:e; 75, block; 75, 76; 75, 86; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:print; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"Error: Unable to collapse the time series: {}"; 83, identifier:format; 84, argument_list; 84, 85; 85, identifier:e; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:logger_start; 90, identifier:error; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, string:"collapseTs: unable to collapse the time series: {}"; 95, identifier:format; 96, argument_list; 96, 97; 97, identifier:e; 98, return_statement; 98, 99; 99, identifier:_d
def collapseTs(ts=None): """ Collapse a time series back into LiPD record form. | Example | 1. D = lipd.readLipd() | 2. ts = lipd.extractTs(D) | 3. New_D = lipd.collapseTs(ts) _timeseries_data is sorted by time_id, and then by dataSetName _timeseries_data[10103341]["ODP1098B"] = {data} :param list ts: Time series :return dict: Metadata """ # Retrieve the associated raw data according to the "time_id" found in each object. Match it in _timeseries_data global _timeseries_data _d = {} if not ts: print("Error: Time series data not provided. Pass time series into the function.") else: # Send time series list through to be collapsed. try: _raw = _timeseries_data[ts[0]["time_id"]] print(mode_ts("collapse", mode="", ts=ts)) _d = collapse(ts, _raw) _d = rm_empty_fields(_d) except Exception as e: print("Error: Unable to collapse the time series: {}".format(e)) logger_start.error("collapseTs: unable to collapse the time series: {}".format(e)) return _d
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:display_results; 3, parameters; 3, 4; 3, 5; 4, identifier:results; 5, default_parameter; 5, 6; 5, 7; 6, identifier:detailed; 7, False; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 22; 8, 96; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, if_statement; 13, 14; 13, 16; 14, not_operator; 14, 15; 15, identifier:detailed; 16, block; 16, 17; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:print; 20, argument_list; 20, 21; 21, string:'FILENAME......................................... STATUS..........'; 22, for_statement; 22, 23; 22, 24; 22, 25; 23, identifier:entry; 24, identifier:results; 25, block; 25, 26; 26, try_statement; 26, 27; 26, 68; 27, block; 27, 28; 28, if_statement; 28, 29; 28, 30; 28, 51; 29, identifier:detailed; 30, block; 30, 31; 30, 43; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:print; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, string:"\n{}"; 38, identifier:format; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 42; 41, identifier:entry; 42, string:"filename"; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:print; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:create_detailed_results; 49, argument_list; 49, 50; 50, identifier:entry; 51, else_clause; 51, 52; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:print; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, string:"{:<50}{}"; 60, identifier:format; 61, argument_list; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:entry; 64, string:"filename"; 65, subscript; 65, 66; 65, 67; 66, identifier:entry; 67, string:"status"; 68, except_clause; 68, 69; 68, 73; 69, as_pattern; 69, 70; 69, 71; 70, identifier:Exception; 71, as_pattern_target; 71, 72; 72, identifier:e; 73, block; 73, 74; 73, 86; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:logger_validator_api; 78, identifier:debug; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"display_results: Exception: {}"; 83, identifier:format; 84, argument_list; 84, 85; 85, identifier:e; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:print; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, string:"Error: display_results: {}"; 93, identifier:format; 94, argument_list; 94, 95; 95, identifier:e; 96, return_statement
def display_results(results, detailed=False): """ Display the results from the validator in a brief or detailed output :param list results: API results, sorted by dataset name (multiple) :param bool detailed: Detailed results on or off :return none: """ # print("\nVALIDATOR RESULTS") # print("======================\n") if not detailed: print('FILENAME......................................... STATUS..........') for entry in results: try: if detailed: print("\n{}".format(entry["filename"])) print(create_detailed_results(entry)) else: print("{:<50}{}".format(entry["filename"], entry["status"])) except Exception as e: logger_validator_api.debug("display_results: Exception: {}".format(e)) print("Error: display_results: {}".format(e)) return
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:call_validator_api; 3, parameters; 3, 4; 3, 5; 4, identifier:dsn; 5, identifier:api_data; 6, block; 6, 7; 6, 9; 6, 15; 6, 252; 6, 272; 6, 278; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:_filename; 12, binary_operator:+; 12, 13; 12, 14; 13, identifier:dsn; 14, string:".lpd"; 15, try_statement; 15, 16; 15, 17; 15, 18; 15, 141; 15, 175; 15, 213; 16, comment; 17, comment; 18, block; 18, 19; 18, 28; 18, 29; 18, 39; 18, 40; 18, 41; 18, 42; 18, 54; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:api_data; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:json; 25, identifier:dumps; 26, argument_list; 26, 27; 27, identifier:api_data; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:payload; 32, dictionary; 32, 33; 32, 36; 33, pair; 33, 34; 33, 35; 34, string:'json_payload'; 35, identifier:api_data; 36, pair; 36, 37; 36, 38; 37, string:'apikey'; 38, string:'lipd_linked'; 39, comment; 40, comment; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:response; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:requests; 48, identifier:post; 49, argument_list; 49, 50; 49, 51; 50, string:'http://www.lipd.net/api/validator'; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:data; 53, identifier:payload; 54, if_statement; 54, 55; 54, 60; 54, 77; 54, 100; 54, 125; 54, 126; 54, 127; 54, 128; 55, comparison_operator:==; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:response; 58, identifier:status_code; 59, integer:413; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:result; 64, dictionary; 64, 65; 64, 68; 64, 71; 64, 74; 65, pair; 65, 66; 65, 67; 66, string:"dat"; 67, dictionary; 68, pair; 68, 69; 68, 70; 69, string:"feedback"; 70, dictionary; 71, pair; 71, 72; 71, 73; 72, string:"filename"; 73, identifier:_filename; 74, pair; 74, 75; 74, 76; 75, string:"status"; 76, string:"HTTP 413: Request Entity Too Large"; 77, elif_clause; 77, 78; 77, 83; 78, comparison_operator:==; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:response; 81, identifier:status_code; 82, integer:404; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:result; 87, dictionary; 87, 88; 87, 91; 87, 94; 87, 97; 88, pair; 88, 89; 88, 90; 89, string:"dat"; 90, dictionary; 91, pair; 91, 92; 91, 93; 92, string:"feedback"; 93, dictionary; 94, pair; 94, 95; 94, 96; 95, string:"filename"; 96, identifier:_filename; 97, pair; 97, 98; 97, 99; 98, string:"status"; 99, string:"HTTP 404: Not Found"; 100, elif_clause; 100, 101; 100, 106; 101, comparison_operator:==; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:response; 104, identifier:status_code; 105, integer:400; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:result; 110, dictionary; 110, 111; 110, 114; 110, 117; 110, 120; 111, pair; 111, 112; 111, 113; 112, string:"dat"; 113, dictionary; 114, pair; 114, 115; 114, 116; 115, string:"feedback"; 116, dictionary; 117, pair; 117, 118; 117, 119; 118, string:"filename"; 119, identifier:_filename; 120, pair; 120, 121; 120, 122; 121, string:"status"; 122, attribute; 122, 123; 122, 124; 123, identifier:response; 124, identifier:text; 125, comment; 126, comment; 127, comment; 128, else_clause; 128, 129; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:result; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:json; 136, identifier:loads; 137, argument_list; 137, 138; 138, attribute; 138, 139; 138, 140; 139, identifier:response; 140, identifier:text; 141, except_clause; 141, 142; 141, 146; 142, as_pattern; 142, 143; 142, 144; 143, identifier:TypeError; 144, as_pattern_target; 144, 145; 145, identifier:e; 146, block; 146, 147; 146, 159; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:logger_validator_api; 151, identifier:warning; 152, argument_list; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, string:"get_validator_results: TypeError: {}"; 156, identifier:format; 157, argument_list; 157, 158; 158, identifier:e; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:result; 162, dictionary; 162, 163; 162, 166; 162, 169; 162, 172; 163, pair; 163, 164; 163, 165; 164, string:"dat"; 165, dictionary; 166, pair; 166, 167; 166, 168; 167, string:"feedback"; 168, dictionary; 169, pair; 169, 170; 169, 171; 170, string:"filename"; 171, identifier:_filename; 172, pair; 172, 173; 172, 174; 173, string:"status"; 174, string:"JSON DECODE ERROR"; 175, except_clause; 175, 176; 175, 184; 176, as_pattern; 176, 177; 176, 182; 177, attribute; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:requests; 180, identifier:exceptions; 181, identifier:ConnectionError; 182, as_pattern_target; 182, 183; 183, identifier:e; 184, block; 184, 185; 184, 197; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:logger_validator_api; 189, identifier:warning; 190, argument_list; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, string:"get_validator_results: ConnectionError: {}"; 194, identifier:format; 195, argument_list; 195, 196; 196, identifier:e; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:result; 200, dictionary; 200, 201; 200, 204; 200, 207; 200, 210; 201, pair; 201, 202; 201, 203; 202, string:"dat"; 203, dictionary; 204, pair; 204, 205; 204, 206; 205, string:"feedback"; 206, dictionary; 207, pair; 207, 208; 207, 209; 208, string:"filename"; 209, identifier:_filename; 210, pair; 210, 211; 210, 212; 211, string:"status"; 212, string:"UNABLE TO REACH SERVER"; 213, except_clause; 213, 214; 213, 218; 214, as_pattern; 214, 215; 214, 216; 215, identifier:Exception; 216, as_pattern_target; 216, 217; 217, identifier:e; 218, block; 218, 219; 218, 231; 219, expression_statement; 219, 220; 220, call; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:logger_validator_api; 223, identifier:debug; 224, argument_list; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, string:"get_validator_results: Exception: {}"; 228, identifier:format; 229, argument_list; 229, 230; 230, identifier:e; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 234; 233, identifier:result; 234, dictionary; 234, 235; 234, 238; 234, 241; 234, 244; 235, pair; 235, 236; 235, 237; 236, string:"dat"; 237, dictionary; 238, pair; 238, 239; 238, 240; 239, string:"feedback"; 240, dictionary; 241, pair; 241, 242; 241, 243; 242, string:"filename"; 243, identifier:_filename; 244, pair; 244, 245; 244, 246; 245, string:"status"; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:"ERROR BEFORE VALIDATION, {}"; 249, identifier:format; 250, argument_list; 250, 251; 251, identifier:e; 252, if_statement; 252, 253; 252, 255; 253, not_operator; 253, 254; 254, identifier:result; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:result; 259, dictionary; 259, 260; 259, 263; 259, 266; 259, 269; 260, pair; 260, 261; 260, 262; 261, string:"dat"; 262, dictionary; 263, pair; 263, 264; 263, 265; 264, string:"feedback"; 265, dictionary; 266, pair; 266, 267; 266, 268; 267, string:"filename"; 268, identifier:_filename; 269, pair; 269, 270; 269, 271; 270, string:"status"; 271, string:"EMPTY RESPONSE"; 272, expression_statement; 272, 273; 273, assignment; 273, 274; 273, 277; 274, subscript; 274, 275; 274, 276; 275, identifier:result; 276, string:"filename"; 277, identifier:_filename; 278, return_statement; 278, 279; 279, identifier:result
def call_validator_api(dsn, api_data): """ Single call to the lipd.net validator API 'api_data' format: [ {"type": "csv", "filenameFull": /path/to/filename.csv, "data": "", ...}, {"type": "json", "filenameFull": /path/to/metadata.jsonld, "data": "", ...}, ... ] Result format: {"dat": <dict>, "feedback": <dict>, "filename": "", "status": ""} :param str dsn: Dataset name :param list api_data: Prepared payload for one LiPD dataset. All the sorted files (txt, jsonld, csv), API formatted :return list result: Validator result for one file """ _filename = dsn + ".lpd" try: # Contact server and send LiPD metadata as the payload # print("Sending request to LiPD.net validator...\n") api_data = json.dumps(api_data) # The payload that is going to be sent with the JSON request payload = {'json_payload': api_data, 'apikey': 'lipd_linked'} # Development Link # response = requests.post('http://localhost:3000/api/validator', data=payload) # Production Link response = requests.post('http://www.lipd.net/api/validator', data=payload) if response.status_code == 413: result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "HTTP 413: Request Entity Too Large"} elif response.status_code == 404: result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "HTTP 404: Not Found"} elif response.status_code == 400: result = {"dat": {}, "feedback": {}, "filename": _filename, "status": response.text} # For an example of the JSON Response, reference the "sample_data_response" below # Convert JSON string into a Python dictionary # print("Converting response to json...\n") else: result = json.loads(response.text) except TypeError as e: logger_validator_api.warning("get_validator_results: TypeError: {}".format(e)) result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "JSON DECODE ERROR"} except requests.exceptions.ConnectionError as e: logger_validator_api.warning("get_validator_results: ConnectionError: {}".format(e)) result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "UNABLE TO REACH SERVER"} except Exception as e: logger_validator_api.debug("get_validator_results: Exception: {}".format(e)) result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "ERROR BEFORE VALIDATION, {}".format(e)} if not result: result = {"dat": {}, "feedback": {}, "filename": _filename, "status": "EMPTY RESPONSE"} result["filename"] = _filename return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:create_back_links; 3, parameters; 3, 4; 4, identifier:env; 5, block; 5, 6; 5, 8; 5, 16; 5, 22; 5, 151; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 14; 9, subscript; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:env; 12, identifier:needs_workflow; 13, string:'backlink_creation'; 14, block; 14, 15; 15, return_statement; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:needs; 19, attribute; 19, 20; 19, 21; 20, identifier:env; 21, identifier:needs_all_needs; 22, for_statement; 22, 23; 22, 26; 22, 31; 23, pattern_list; 23, 24; 23, 25; 24, identifier:key; 25, identifier:need; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:needs; 29, identifier:items; 30, argument_list; 31, block; 31, 32; 32, for_statement; 32, 33; 32, 34; 32, 37; 33, identifier:link; 34, subscript; 34, 35; 34, 36; 35, identifier:need; 36, string:"links"; 37, block; 37, 38; 37, 49; 37, 69; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:link_main; 41, subscript; 41, 42; 41, 48; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:link; 45, identifier:split; 46, argument_list; 46, 47; 47, string:'.'; 48, integer:0; 49, try_statement; 49, 50; 49, 62; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:link_part; 54, subscript; 54, 55; 54, 61; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:link; 58, identifier:split; 59, argument_list; 59, 60; 60, string:'.'; 61, integer:1; 62, except_clause; 62, 63; 62, 64; 63, identifier:IndexError; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:link_part; 68, None; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:in; 70, 71; 70, 72; 71, identifier:link_main; 72, identifier:needs; 73, block; 73, 74; 73, 94; 73, 95; 74, if_statement; 74, 75; 74, 82; 75, comparison_operator:not; 75, 76; 75, 77; 76, identifier:key; 77, subscript; 77, 78; 77, 81; 78, subscript; 78, 79; 78, 80; 79, identifier:needs; 80, identifier:link_main; 81, string:"links_back"; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 92; 85, attribute; 85, 86; 85, 91; 86, subscript; 86, 87; 86, 90; 87, subscript; 87, 88; 87, 89; 88, identifier:needs; 89, identifier:link_main; 90, string:"links_back"; 91, identifier:append; 92, argument_list; 92, 93; 93, identifier:key; 94, comment; 95, if_statement; 95, 96; 95, 99; 96, comparison_operator:is; 96, 97; 96, 98; 97, identifier:link_part; 98, None; 99, block; 99, 100; 100, if_statement; 100, 101; 100, 108; 101, comparison_operator:in; 101, 102; 101, 103; 102, identifier:link_part; 103, subscript; 103, 104; 103, 107; 104, subscript; 104, 105; 104, 106; 105, identifier:needs; 106, identifier:link_main; 107, string:'parts'; 108, block; 108, 109; 108, 136; 109, if_statement; 109, 110; 109, 123; 110, comparison_operator:not; 110, 111; 110, 112; 111, string:'links_back'; 112, call; 112, 113; 112, 122; 113, attribute; 113, 114; 113, 121; 114, subscript; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, subscript; 116, 117; 116, 118; 117, identifier:needs; 118, identifier:link_main; 119, string:'parts'; 120, identifier:link_part; 121, identifier:keys; 122, argument_list; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 135; 126, subscript; 126, 127; 126, 134; 127, subscript; 127, 128; 127, 133; 128, subscript; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:needs; 131, identifier:link_main; 132, string:'parts'; 133, identifier:link_part; 134, string:'links_back'; 135, list:[]; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 149; 138, attribute; 138, 139; 138, 148; 139, subscript; 139, 140; 139, 147; 140, subscript; 140, 141; 140, 146; 141, subscript; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:needs; 144, identifier:link_main; 145, string:'parts'; 146, identifier:link_part; 147, string:'links_back'; 148, identifier:append; 149, argument_list; 149, 150; 150, identifier:key; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 158; 153, subscript; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:env; 156, identifier:needs_workflow; 157, string:'backlink_creation'; 158, True
def create_back_links(env): """ Create back-links in all found needs. But do this only once, as all needs are already collected and this sorting is for all needs and not only for the ones of the current document. :param env: sphinx enviroment :return: None """ if env.needs_workflow['backlink_creation']: return needs = env.needs_all_needs for key, need in needs.items(): for link in need["links"]: link_main = link.split('.')[0] try: link_part = link.split('.')[1] except IndexError: link_part = None if link_main in needs: if key not in needs[link_main]["links_back"]: needs[link_main]["links_back"].append(key) # Handling of links to need_parts inside a need if link_part is not None: if link_part in needs[link_main]['parts']: if 'links_back' not in needs[link_main]['parts'][link_part].keys(): needs[link_main]['parts'][link_part]['links_back'] = [] needs[link_main]['parts'][link_part]['links_back'].append(key) env.needs_workflow['backlink_creation'] = True
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:CaseGroups; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:unicode_dir; 6, identifier:_UNICODE_DIR; 7, block; 7, 8; 7, 10; 7, 11; 7, 15; 7, 60; 7, 69; 7, 77; 7, 87; 7, 93; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:togroup; 14, dictionary; 15, function_definition; 15, 16; 15, 17; 15, 20; 16, function_name:DoLine; 17, parameters; 17, 18; 17, 19; 18, identifier:codes; 19, identifier:fields; 20, block; 20, 21; 20, 23; 20, 31; 20, 39; 20, 46; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 30; 25, tuple_pattern; 25, 26; 25, 27; 25, 28; 25, 29; 26, identifier:_; 27, identifier:foldtype; 28, identifier:lower; 29, identifier:_; 30, identifier:fields; 31, if_statement; 31, 32; 31, 37; 32, comparison_operator:not; 32, 33; 32, 34; 33, identifier:foldtype; 34, tuple; 34, 35; 34, 36; 35, string:"C"; 36, string:"S"; 37, block; 37, 38; 38, return_statement; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:lower; 42, call; 42, 43; 42, 44; 43, identifier:_UInt; 44, argument_list; 44, 45; 45, identifier:lower; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 58; 48, attribute; 48, 49; 48, 57; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:togroup; 52, identifier:setdefault; 53, argument_list; 53, 54; 53, 55; 54, identifier:lower; 55, list:[lower]; 55, 56; 56, identifier:lower; 57, identifier:extend; 58, argument_list; 58, 59; 59, identifier:codes; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:ReadUnicodeTable; 63, argument_list; 63, 64; 63, 67; 63, 68; 64, binary_operator:+; 64, 65; 64, 66; 65, identifier:unicode_dir; 66, string:"/CaseFolding.txt"; 67, integer:4; 68, identifier:DoLine; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:groups; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:togroup; 75, identifier:values; 76, argument_list; 77, for_statement; 77, 78; 77, 79; 77, 80; 78, identifier:g; 79, identifier:groups; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:g; 85, identifier:sort; 86, argument_list; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:groups; 91, identifier:sort; 92, argument_list; 93, return_statement; 93, 94; 94, expression_list; 94, 95; 94, 96; 95, identifier:togroup; 96, identifier:groups
def CaseGroups(unicode_dir=_UNICODE_DIR): """Returns list of Unicode code groups equivalent under case folding. Each group is a sorted list of code points, and the list of groups is sorted by first code point in the group. Args: unicode_dir: Unicode data directory Returns: list of Unicode code groups """ # Dict mapping lowercase code point to fold-equivalent group. togroup = {} def DoLine(codes, fields): """Process single CaseFolding.txt line, updating togroup.""" (_, foldtype, lower, _) = fields if foldtype not in ("C", "S"): return lower = _UInt(lower) togroup.setdefault(lower, [lower]).extend(codes) ReadUnicodeTable(unicode_dir+"/CaseFolding.txt", 4, DoLine) groups = togroup.values() for g in groups: g.sort() groups.sort() return togroup, groups
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:naturalsortkey; 3, parameters; 3, 4; 4, identifier:s; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, list_comprehension; 9, 10; 9, 21; 10, conditional_expression:if; 10, 11; 10, 15; 10, 20; 11, call; 11, 12; 11, 13; 12, identifier:int; 13, argument_list; 13, 14; 14, identifier:part; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:part; 18, identifier:isdigit; 19, argument_list; 20, identifier:part; 21, for_in_clause; 21, 22; 21, 23; 22, identifier:part; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:re; 26, identifier:split; 27, argument_list; 27, 28; 27, 29; 28, string:'([0-9]+)'; 29, identifier:s
def naturalsortkey(s): """Natural sort order""" return [int(part) if part.isdigit() else part for part in re.split('([0-9]+)', s)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:max_variance_genes; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:data; 5, default_parameter; 5, 6; 5, 7; 6, identifier:nbins; 7, integer:5; 8, default_parameter; 8, 9; 8, 10; 9, identifier:frac; 10, float:0.2; 11, block; 11, 12; 11, 14; 11, 15; 11, 16; 11, 17; 11, 18; 11, 19; 11, 20; 11, 21; 11, 22; 11, 23; 11, 27; 11, 64; 11, 72; 11, 85; 11, 94; 11, 189; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, comment; 16, comment; 17, comment; 18, comment; 19, comment; 20, comment; 21, comment; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:indices; 26, list:[]; 27, if_statement; 27, 28; 27, 34; 27, 44; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:sparse; 31, identifier:issparse; 32, argument_list; 32, 33; 33, identifier:data; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, pattern_list; 37, 38; 37, 39; 38, identifier:means; 39, identifier:var; 40, call; 40, 41; 40, 42; 41, identifier:sparse_mean_var; 42, argument_list; 42, 43; 43, identifier:data; 44, else_clause; 44, 45; 45, block; 45, 46; 45, 55; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:means; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:data; 52, identifier:mean; 53, argument_list; 53, 54; 54, integer:1; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:var; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:data; 61, identifier:var; 62, argument_list; 62, 63; 63, integer:1; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:mean_indices; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:means; 70, identifier:argsort; 71, argument_list; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:n_elements; 75, call; 75, 76; 75, 77; 76, identifier:int; 77, argument_list; 77, 78; 78, binary_operator:/; 78, 79; 78, 84; 79, subscript; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:data; 82, identifier:shape; 83, integer:0; 84, identifier:nbins; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:frac_elements; 88, call; 88, 89; 88, 90; 89, identifier:int; 90, argument_list; 90, 91; 91, binary_operator:*; 91, 92; 91, 93; 92, identifier:n_elements; 93, identifier:frac; 94, for_statement; 94, 95; 94, 96; 94, 100; 95, identifier:i; 96, call; 96, 97; 96, 98; 97, identifier:range; 98, argument_list; 98, 99; 99, identifier:nbins; 100, block; 100, 101; 100, 117; 100, 134; 100, 140; 100, 148; 100, 161; 100, 167; 100, 168; 100, 182; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:bin_i; 104, subscript; 104, 105; 104, 106; 105, identifier:mean_indices; 106, slice; 106, 107; 106, 110; 106, 111; 107, binary_operator:*; 107, 108; 107, 109; 108, identifier:i; 109, identifier:n_elements; 110, colon; 111, binary_operator:*; 111, 112; 111, 116; 112, parenthesized_expression; 112, 113; 113, binary_operator:+; 113, 114; 113, 115; 114, identifier:i; 115, integer:1; 116, identifier:n_elements; 117, if_statement; 117, 118; 117, 123; 118, comparison_operator:==; 118, 119; 118, 120; 119, identifier:i; 120, binary_operator:-; 120, 121; 120, 122; 121, identifier:nbins; 122, integer:1; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:bin_i; 127, subscript; 127, 128; 127, 129; 128, identifier:mean_indices; 129, slice; 129, 130; 129, 133; 130, binary_operator:*; 130, 131; 130, 132; 131, identifier:i; 132, identifier:n_elements; 133, colon; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:var_i; 137, subscript; 137, 138; 137, 139; 138, identifier:var; 139, identifier:bin_i; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:var_sorted; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:var_i; 146, identifier:argsort; 147, argument_list; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:top_var_indices; 151, subscript; 151, 152; 151, 153; 152, identifier:var_sorted; 153, slice; 153, 154; 153, 160; 154, binary_operator:-; 154, 155; 154, 159; 155, call; 155, 156; 155, 157; 156, identifier:len; 157, argument_list; 157, 158; 158, identifier:bin_i; 159, identifier:frac_elements; 160, colon; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:ind; 164, subscript; 164, 165; 164, 166; 165, identifier:bin_i; 166, identifier:top_var_indices; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:ind; 171, list_comprehension; 171, 172; 171, 173; 171, 176; 172, identifier:index; 173, for_in_clause; 173, 174; 173, 175; 174, identifier:index; 175, identifier:ind; 176, if_clause; 176, 177; 177, comparison_operator:>; 177, 178; 177, 181; 178, subscript; 178, 179; 178, 180; 179, identifier:var; 180, identifier:index; 181, integer:0; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:indices; 186, identifier:extend; 187, argument_list; 187, 188; 188, identifier:ind; 189, return_statement; 189, 190; 190, identifier:indices
def max_variance_genes(data, nbins=5, frac=0.2): """ This function identifies the genes that have the max variance across a number of bins sorted by mean. Args: data (array): genes x cells nbins (int): number of bins to sort genes by mean expression level. Default: 10. frac (float): fraction of genes to return per bin - between 0 and 1. Default: 0.1 Returns: list of gene indices (list of ints) """ # TODO: profile, make more efficient for large matrices # 8000 cells: 0.325 seconds # top time: sparse.csc_tocsr, csc_matvec, astype, copy, mul_scalar # 73233 cells: 5.347 seconds, 4.762 s in sparse_var # csc_tocsr: 1.736 s # copy: 1.028 s # astype: 0.999 s # there is almost certainly something superlinear in this method # maybe it's to_csr? indices = [] if sparse.issparse(data): means, var = sparse_mean_var(data) else: means = data.mean(1) var = data.var(1) mean_indices = means.argsort() n_elements = int(data.shape[0]/nbins) frac_elements = int(n_elements*frac) for i in range(nbins): bin_i = mean_indices[i*n_elements : (i+1)*n_elements] if i==nbins-1: bin_i = mean_indices[i*n_elements :] var_i = var[bin_i] var_sorted = var_i.argsort() top_var_indices = var_sorted[len(bin_i) - frac_elements:] ind = bin_i[top_var_indices] # filter out genes with zero variance ind = [index for index in ind if var[index]>0] indices.extend(ind) return indices
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:unique; 3, parameters; 3, 4; 4, identifier:ar; 5, block; 5, 6; 5, 8; 5, 14; 5, 32; 6, expression_statement; 6, 7; 7, comment; 8, import_statement; 8, 9; 9, aliased_import; 9, 10; 9, 13; 10, dotted_name; 10, 11; 10, 12; 11, identifier:dask; 12, identifier:array; 13, identifier:da; 14, if_statement; 14, 15; 14, 24; 15, call; 15, 16; 15, 17; 16, identifier:isinstance; 17, argument_list; 17, 18; 17, 19; 18, identifier:ar; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:da; 22, identifier:core; 23, identifier:Array; 24, block; 24, 25; 25, return_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:da; 29, identifier:unique; 30, argument_list; 30, 31; 31, identifier:ar; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:_unique; 35, argument_list; 35, 36; 36, identifier:ar
def unique(ar): r"""Find the unique elements of an array. It uses ``dask.array.unique`` if necessary. Args: ar (array_like): Input array. Returns: array_like: the sorted unique elements. """ import dask.array as da if isinstance(ar, da.core.Array): return da.unique(ar) return _unique(ar)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:polls_get; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 7, 16; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, subscript; 12, 13; 12, 14; 13, identifier:kwargs; 14, string:'_return_http_data_only'; 15, True; 16, if_statement; 16, 17; 16, 23; 16, 32; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:kwargs; 20, identifier:get; 21, argument_list; 21, 22; 22, string:'callback'; 23, block; 23, 24; 24, return_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:polls_get_with_http_info; 29, argument_list; 29, 30; 30, dictionary_splat; 30, 31; 31, identifier:kwargs; 32, else_clause; 32, 33; 33, block; 33, 34; 33, 45; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 38; 36, tuple_pattern; 36, 37; 37, identifier:data; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:polls_get_with_http_info; 42, argument_list; 42, 43; 43, dictionary_splat; 43, 44; 44, identifier:kwargs; 45, return_statement; 45, 46; 46, identifier:data
def polls_get(self, **kwargs): """ Polls A Poll on Pollster is a collection of questions and responses published by a reputable survey house. This endpoint provides raw data from the survey house, plus Pollster-provided metadata about each question. Pollster editors don't include every question when they enter Polls, and they don't necessarily enter every subpopulation for the responses they _do_ enter. They make editorial decisions about which questions belong in the database. The response will contain a maximum of 25 Poll objects, even if the database contains more than 25 polls. Use the `next_cursor` parameter to fetch the rest, 25 Polls at a time. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please define a `callback` function to be invoked when receiving the response. >>> def callback_function(response): >>> pprint(response) >>> >>> thread = api.polls_get(callback=callback_function) :param callback function: The callback function for asynchronous request. (optional) :param str cursor: Special string to index into the Array :param str tags: Comma-separated list of Question tag names; only Polls containing Questions with any of the given tags will be returned. :param str question: Question slug; only Polls that ask that Question will be returned. :param str sort: If `updated_at`, sort the most recently updated Poll first. (This can cause race conditions when used with `cursor`.) Otherwise, sort by most recently _entered_ Poll first. :return: InlineResponse2003 If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('callback'): return self.polls_get_with_http_info(**kwargs) else: (data) = self.polls_get_with_http_info(**kwargs) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_sorted_actions; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 37; 5, 65; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 33; 9, identifier:a; 10, call; 10, 11; 10, 12; 11, identifier:filter; 12, argument_list; 12, 13; 12, 30; 13, lambda; 13, 14; 13, 16; 14, lambda_parameters; 14, 15; 15, identifier:_; 16, boolean_operator:and; 16, 17; 16, 21; 16, 22; 17, not_operator; 17, 18; 18, attribute; 18, 19; 18, 20; 19, identifier:_; 20, identifier:last; 21, line_continuation:\; 22, not_operator; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:is_action; 27, argument_list; 27, 28; 27, 29; 28, identifier:_; 29, string:'parsers'; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_actions; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, yield; 35, 36; 36, identifier:a; 37, for_statement; 37, 38; 37, 39; 37, 61; 38, identifier:a; 39, call; 39, 40; 39, 41; 40, identifier:filter; 41, argument_list; 41, 42; 41, 58; 42, lambda; 42, 43; 42, 45; 43, lambda_parameters; 43, 44; 44, identifier:_; 45, boolean_operator:and; 45, 46; 45, 49; 45, 50; 46, attribute; 46, 47; 46, 48; 47, identifier:_; 48, identifier:last; 49, line_continuation:\; 50, not_operator; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:is_action; 55, argument_list; 55, 56; 55, 57; 56, identifier:_; 57, string:'parsers'; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:_actions; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, yield; 63, 64; 64, identifier:a; 65, for_statement; 65, 66; 65, 67; 65, 83; 66, identifier:a; 67, call; 67, 68; 67, 69; 68, identifier:filter; 69, argument_list; 69, 70; 69, 80; 70, lambda; 70, 71; 70, 73; 71, lambda_parameters; 71, 72; 72, identifier:_; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:is_action; 77, argument_list; 77, 78; 77, 79; 78, identifier:_; 79, string:'parsers'; 80, attribute; 80, 81; 80, 82; 81, identifier:self; 82, identifier:_actions; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, yield; 85, 86; 86, identifier:a
def _sorted_actions(self): """ Generate the sorted list of actions based on the "last" attribute. """ for a in filter(lambda _: not _.last and \ not self.is_action(_, 'parsers'), self._actions): yield a for a in filter(lambda _: _.last and \ not self.is_action(_, 'parsers'), self._actions): yield a for a in filter(lambda _: self.is_action(_, 'parsers'), self._actions): yield a
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:eigendecomp; 3, parameters; 3, 4; 3, 5; 4, identifier:G; 5, identifier:d; 6, block; 6, 7; 6, 9; 6, 17; 6, 30; 6, 31; 6, 32; 6, 33; 6, 34; 6, 43; 6, 58; 6, 64; 6, 88; 6, 96; 6, 111; 6, 130; 6, 138; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:N; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:G; 15, identifier:shape; 16, integer:0; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:lamda; 21, identifier:u; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:np; 26, identifier:linalg; 27, identifier:eig; 28, argument_list; 28, 29; 29, identifier:G; 30, comment; 31, comment; 32, comment; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:lamda; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:np; 40, identifier:real; 41, argument_list; 41, 42; 42, identifier:lamda; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:indices; 46, subscript; 46, 47; 46, 53; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:np; 50, identifier:argsort; 51, argument_list; 51, 52; 52, identifier:lamda; 53, slice; 53, 54; 53, 55; 53, 56; 54, colon; 55, colon; 56, unary_operator:-; 56, 57; 57, integer:1; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:lamda_sorted; 61, subscript; 61, 62; 61, 63; 62, identifier:lamda; 63, identifier:indices; 64, assert_statement; 64, 65; 64, 78; 65, call; 65, 66; 65, 77; 66, attribute; 66, 67; 66, 76; 67, parenthesized_expression; 67, 68; 68, comparison_operator:>; 68, 69; 68, 74; 69, subscript; 69, 70; 69, 71; 70, identifier:lamda_sorted; 71, slice; 71, 72; 71, 73; 72, colon; 73, identifier:d; 74, unary_operator:-; 74, 75; 75, float:1e-10; 76, identifier:all; 77, argument_list; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, string:"{} not all positive!"; 81, identifier:format; 82, argument_list; 82, 83; 83, subscript; 83, 84; 83, 85; 84, identifier:lamda_sorted; 85, slice; 85, 86; 85, 87; 86, colon; 87, identifier:d; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:u; 91, subscript; 91, 92; 91, 93; 91, 95; 92, identifier:u; 93, slice; 93, 94; 94, colon; 95, identifier:indices; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:factor; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:np; 102, identifier:empty; 103, argument_list; 103, 104; 103, 106; 104, tuple; 104, 105; 105, identifier:N; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:dtype; 108, attribute; 108, 109; 108, 110; 109, identifier:lamda; 110, identifier:dtype; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:np; 115, identifier:sqrt; 116, argument_list; 116, 117; 116, 122; 117, subscript; 117, 118; 117, 119; 118, identifier:lamda_sorted; 119, slice; 119, 120; 119, 121; 120, colon; 121, identifier:d; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:out; 124, subscript; 124, 125; 124, 126; 125, identifier:factor; 126, slice; 126, 127; 126, 128; 126, 129; 127, integer:0; 128, colon; 129, identifier:d; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 137; 132, subscript; 132, 133; 132, 134; 133, identifier:factor; 134, slice; 134, 135; 134, 136; 135, identifier:d; 136, colon; 137, float:0.0; 138, return_statement; 138, 139; 139, expression_list; 139, 140; 139, 141; 140, identifier:factor; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:np; 144, identifier:real; 145, argument_list; 145, 146; 146, identifier:u
def eigendecomp(G, d): """ Computes sorted eigendecomposition of G. :param G: Matrix :param d: rank :return factor: vector of square root d of eigenvalues (biggest to smallest). :return u: matrix with colums equal to the normalized eigenvectors corresponding to sorted eigenvalues. """ N = G.shape[0] lamda, u = np.linalg.eig(G) # test decomposition of G. #G_hat = np.dot(np.dot(u, np.diag(lamda)), u.T) #assert np.linalg.norm(G_hat - G) < 1e-10, 'decomposition not exact: err {}'.format(np.linalg.norm(G_hat - G)) # sort the eigenvalues in decreasing order lamda = np.real(lamda) indices = np.argsort(lamda)[::-1] lamda_sorted = lamda[indices] assert (lamda_sorted[ :d] > -1e-10).all(), "{} not all positive!".format(lamda_sorted[:d]) u = u[:, indices] factor = np.empty((N,), dtype=lamda.dtype) np.sqrt(lamda_sorted[:d], out=factor[0:d]) factor[d:] = 0.0 return factor, np.real(u)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:get_astrom; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:official; 6, string:'%'; 7, default_parameter; 7, 8; 7, 9; 8, identifier:provisional; 9, string:'%'; 10, block; 10, 11; 10, 13; 10, 17; 10, 21; 10, 38; 10, 45; 10, 52; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:sql; 16, string:"SELECT m.* FROM measure m "; 17, expression_statement; 17, 18; 18, augmented_assignment:+=; 18, 19; 18, 20; 19, identifier:sql; 20, string:"LEFT JOIN object o ON m.provisional LIKE o.provisional "; 21, if_statement; 21, 22; 21, 24; 21, 29; 22, not_operator; 22, 23; 23, identifier:official; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, augmented_assignment:+=; 26, 27; 26, 28; 27, identifier:sql; 28, string:"WHERE o.official IS NULL"; 29, else_clause; 29, 30; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, augmented_assignment:+=; 32, 33; 32, 34; 33, identifier:sql; 34, binary_operator:%; 34, 35; 34, 36; 35, string:"WHERE o.official LIKE '%s' "; 36, tuple; 36, 37; 37, identifier:official; 38, expression_statement; 38, 39; 39, augmented_assignment:+=; 39, 40; 39, 41; 40, identifier:sql; 41, binary_operator:%; 41, 42; 41, 43; 42, string:" AND m.provisional LIKE '%s' "; 43, tuple; 43, 44; 44, identifier:provisional; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:cfeps; 49, identifier:execute; 50, argument_list; 50, 51; 51, identifier:sql; 52, return_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:mk_dict; 55, argument_list; 55, 56; 55, 61; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:cfeps; 59, identifier:fetchall; 60, argument_list; 61, attribute; 61, 62; 61, 63; 62, identifier:cfeps; 63, identifier:description
def get_astrom(official='%',provisional='%'): """Query the measure table for all measurements of a particular object. Default is to return all the astrometry in the measure table, sorted by mjdate""" sql= "SELECT m.* FROM measure m " sql+="LEFT JOIN object o ON m.provisional LIKE o.provisional " if not official: sql+="WHERE o.official IS NULL" else: sql+="WHERE o.official LIKE '%s' " % ( official, ) sql+=" AND m.provisional LIKE '%s' " % ( provisional, ) cfeps.execute(sql) return mk_dict(cfeps.fetchall(), cfeps.description)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sorted_by_field; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:issues; 5, default_parameter; 5, 6; 5, 7; 6, identifier:field; 7, string:'closed_at'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:reverse; 10, False; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:sorted; 17, argument_list; 17, 18; 17, 19; 17, 27; 18, identifier:issues; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:key; 21, lambda; 21, 22; 21, 24; 22, lambda_parameters; 22, 23; 23, identifier:i; 24, subscript; 24, 25; 24, 26; 25, identifier:i; 26, identifier:field; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:reverse; 29, identifier:reverse
def sorted_by_field(issues, field='closed_at', reverse=False): """Return a list of issues sorted by closing date date.""" return sorted(issues, key = lambda i:i[field], reverse=reverse)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:order_by; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:key_selector; 7, identifier:identity; 8, block; 8, 9; 8, 11; 8, 25; 8, 49; 9, expression_statement; 9, 10; 10, string:'''Sorts by a key in ascending order. Introduces a primary sorting order to the sequence. Additional sort criteria should be specified by subsequent calls to then_by() and then_by_descending(). Calling order_by() or order_by_descending() on the results of a call to order_by() will introduce a new primary ordering which will override any already established ordering. This method performs a stable sort. The order of two elements with the same key will be preserved. Note: This method uses deferred execution. Args: key_selector: A unary function which extracts a key from each element using which the result will be ordered. Returns: An OrderedQueryable over the sorted elements. Raises: ValueError: If the Queryable is closed. TypeError: If the key_selector is not callable. '''; 11, if_statement; 11, 12; 11, 17; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:closed; 16, argument_list; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:ValueError; 21, argument_list; 21, 22; 22, concatenated_string; 22, 23; 22, 24; 23, string:"Attempt to call order_by() on a "; 24, string:"closed Queryable."; 25, if_statement; 25, 26; 25, 31; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:is_callable; 29, argument_list; 29, 30; 30, identifier:key_selector; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:TypeError; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, concatenated_string; 38, 39; 38, 40; 39, string:"order_by() parameter key_selector={key_selector} "; 40, string:"is not callable"; 41, identifier:format; 42, argument_list; 42, 43; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:key_selector; 45, call; 45, 46; 45, 47; 46, identifier:repr; 47, argument_list; 47, 48; 48, identifier:key_selector; 49, return_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:_create_ordered; 54, argument_list; 54, 55; 54, 59; 54, 61; 55, call; 55, 56; 55, 57; 56, identifier:iter; 57, argument_list; 57, 58; 58, identifier:self; 59, unary_operator:-; 59, 60; 60, integer:1; 61, identifier:key_selector
def order_by(self, key_selector=identity): '''Sorts by a key in ascending order. Introduces a primary sorting order to the sequence. Additional sort criteria should be specified by subsequent calls to then_by() and then_by_descending(). Calling order_by() or order_by_descending() on the results of a call to order_by() will introduce a new primary ordering which will override any already established ordering. This method performs a stable sort. The order of two elements with the same key will be preserved. Note: This method uses deferred execution. Args: key_selector: A unary function which extracts a key from each element using which the result will be ordered. Returns: An OrderedQueryable over the sorted elements. Raises: ValueError: If the Queryable is closed. TypeError: If the key_selector is not callable. ''' if self.closed(): raise ValueError("Attempt to call order_by() on a " "closed Queryable.") if not is_callable(key_selector): raise TypeError("order_by() parameter key_selector={key_selector} " "is not callable".format(key_selector=repr(key_selector))) return self._create_ordered(iter(self), -1, key_selector)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:then_by; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:key_selector; 7, identifier:identity; 8, block; 8, 9; 8, 11; 8, 25; 8, 49; 8, 61; 9, expression_statement; 9, 10; 10, string:'''Introduce subsequent ordering to the sequence with an optional key. The returned sequence will be sorted in ascending order by the selected key. Note: This method uses deferred execution. Args: key_selector: A unary function the only positional argument to which is the element value from which the key will be selected. The return value should be the key from that element. Returns: An OrderedQueryable over the sorted items. Raises: ValueError: If the OrderedQueryable is closed(). TypeError: If key_selector is not callable. '''; 11, if_statement; 11, 12; 11, 17; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:closed; 16, argument_list; 17, block; 17, 18; 18, raise_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:ValueError; 21, argument_list; 21, 22; 22, concatenated_string; 22, 23; 22, 24; 23, string:"Attempt to call then_by() on a "; 24, string:"closed OrderedQueryable."; 25, if_statement; 25, 26; 25, 31; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:is_callable; 29, argument_list; 29, 30; 30, identifier:key_selector; 31, block; 31, 32; 32, raise_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:TypeError; 35, argument_list; 35, 36; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, concatenated_string; 38, 39; 38, 40; 39, string:"then_by() parameter key_selector={key_selector} "; 40, string:"is not callable"; 41, identifier:format; 42, argument_list; 42, 43; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:key_selector; 45, call; 45, 46; 45, 47; 46, identifier:repr; 47, argument_list; 47, 48; 48, identifier:key_selector; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 56; 51, attribute; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:_funcs; 55, identifier:append; 56, argument_list; 56, 57; 57, tuple; 57, 58; 57, 60; 58, unary_operator:-; 58, 59; 59, integer:1; 60, identifier:key_selector; 61, return_statement; 61, 62; 62, identifier:self
def then_by(self, key_selector=identity): '''Introduce subsequent ordering to the sequence with an optional key. The returned sequence will be sorted in ascending order by the selected key. Note: This method uses deferred execution. Args: key_selector: A unary function the only positional argument to which is the element value from which the key will be selected. The return value should be the key from that element. Returns: An OrderedQueryable over the sorted items. Raises: ValueError: If the OrderedQueryable is closed(). TypeError: If key_selector is not callable. ''' if self.closed(): raise ValueError("Attempt to call then_by() on a " "closed OrderedQueryable.") if not is_callable(key_selector): raise TypeError("then_by() parameter key_selector={key_selector} " "is not callable".format(key_selector=repr(key_selector))) self._funcs.append((-1, key_selector)) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 15; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_matches; 14, identifier:sort_values; 15, argument_list; 15, 16; 15, 37; 15, 46; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:by; 18, list:[constants.SIZE_FIELDNAME, constants.NGRAM_FIELDNAME, constants.COUNT_FIELDNAME, constants.LABEL_FIELDNAME, constants.WORK_FIELDNAME, constants.SIGLUM_FIELDNAME]; 18, 19; 18, 22; 18, 25; 18, 28; 18, 31; 18, 34; 19, attribute; 19, 20; 19, 21; 20, identifier:constants; 21, identifier:SIZE_FIELDNAME; 22, attribute; 22, 23; 22, 24; 23, identifier:constants; 24, identifier:NGRAM_FIELDNAME; 25, attribute; 25, 26; 25, 27; 26, identifier:constants; 27, identifier:COUNT_FIELDNAME; 28, attribute; 28, 29; 28, 30; 29, identifier:constants; 30, identifier:LABEL_FIELDNAME; 31, attribute; 31, 32; 31, 33; 32, identifier:constants; 33, identifier:WORK_FIELDNAME; 34, attribute; 34, 35; 34, 36; 35, identifier:constants; 36, identifier:SIGLUM_FIELDNAME; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:ascending; 39, list:[False, True, False, True, True, True]; 39, 40; 39, 41; 39, 42; 39, 43; 39, 44; 39, 45; 40, False; 41, True; 42, False; 43, True; 44, True; 45, True; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:inplace; 48, True
def sort(self): """Sorts all results rows. Sorts by: size (descending), n-gram, count (descending), label, text name, siglum. """ self._matches.sort_values( by=[constants.SIZE_FIELDNAME, constants.NGRAM_FIELDNAME, constants.COUNT_FIELDNAME, constants.LABEL_FIELDNAME, constants.WORK_FIELDNAME, constants.SIGLUM_FIELDNAME], ascending=[False, True, False, True, True, True], inplace=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_set_labels; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:catalogue; 6, block; 6, 7; 6, 9; 6, 96; 7, expression_statement; 7, 8; 8, comment; 9, with_statement; 9, 10; 9, 15; 10, with_clause; 10, 11; 11, with_item; 11, 12; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_conn; 15, block; 15, 16; 15, 29; 15, 33; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_conn; 22, identifier:execute; 23, argument_list; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:constants; 26, identifier:UPDATE_LABELS_SQL; 27, list:['']; 27, 28; 28, string:''; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:labels; 32, dictionary; 33, for_statement; 33, 34; 33, 37; 33, 42; 34, pattern_list; 34, 35; 34, 36; 35, identifier:work; 36, identifier:label; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:catalogue; 40, identifier:items; 41, argument_list; 42, block; 42, 43; 42, 57; 42, 72; 42, 82; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 50; 45, attribute; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:_conn; 49, identifier:execute; 50, argument_list; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:constants; 53, identifier:UPDATE_LABEL_SQL; 54, list:[label, work]; 54, 55; 54, 56; 55, identifier:label; 56, identifier:work; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:cursor; 60, call; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:_conn; 65, identifier:execute; 66, argument_list; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:constants; 69, identifier:SELECT_TEXT_TOKEN_COUNT_SQL; 70, list:[work]; 70, 71; 71, identifier:work; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:token_count; 75, subscript; 75, 76; 75, 81; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:cursor; 79, identifier:fetchone; 80, argument_list; 81, string:'token_count'; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:labels; 86, identifier:label; 87, binary_operator:+; 87, 88; 87, 95; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:labels; 91, identifier:get; 92, argument_list; 92, 93; 92, 94; 93, identifier:label; 94, integer:0; 95, identifier:token_count; 96, return_statement; 96, 97; 97, identifier:labels
def _set_labels(self, catalogue): """Returns a dictionary of the unique labels in `catalogue` and the count of all tokens associated with each, and sets the record of each Text to its corresponding label. Texts that do not have a label specified are set to the empty string. Token counts are included in the results to allow for semi-accurate sorting based on corpora size. :param catalogue: catalogue matching filenames to labels :type catalogue: `Catalogue` :rtype: `dict` """ with self._conn: self._conn.execute(constants.UPDATE_LABELS_SQL, ['']) labels = {} for work, label in catalogue.items(): self._conn.execute(constants.UPDATE_LABEL_SQL, [label, work]) cursor = self._conn.execute( constants.SELECT_TEXT_TOKEN_COUNT_SQL, [work]) token_count = cursor.fetchone()['token_count'] labels[label] = labels.get(label, 0) + token_count return labels
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_fn_args; 3, parameters; 3, 4; 3, 5; 3, 9; 4, identifier:fn; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:kwargs; 7, type; 7, 8; 8, identifier:dict; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:prefix; 11, type; 11, 12; 12, identifier:str; 13, None; 14, block; 14, 15; 14, 17; 14, 24; 14, 31; 14, 40; 14, 52; 14, 70; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:all_args; 20, call; 20, 21; 20, 22; 21, identifier:get_all_args; 22, argument_list; 22, 23; 23, identifier:fn; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:required_args; 27, call; 27, 28; 27, 29; 28, identifier:get_required_args; 29, argument_list; 29, 30; 30, identifier:fn; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:fn_kwargs; 34, call; 34, 35; 34, 36; 35, identifier:pick_kwargs; 36, argument_list; 36, 37; 36, 38; 36, 39; 37, identifier:kwargs; 38, identifier:all_args; 39, identifier:prefix; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:missing_args; 43, list_comprehension; 43, 44; 43, 45; 43, 48; 44, identifier:arg; 45, for_in_clause; 45, 46; 45, 47; 46, identifier:arg; 47, identifier:required_args; 48, if_clause; 48, 49; 49, comparison_operator:not; 49, 50; 49, 51; 50, identifier:arg; 51, identifier:fn_kwargs; 52, if_statement; 52, 53; 52, 54; 53, identifier:missing_args; 54, block; 54, 55; 55, raise_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:ValueError; 58, argument_list; 58, 59; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, concatenated_string; 61, 62; 61, 63; 62, string:'The following args are missing for the function '; 63, string:'{}: {}.'; 64, identifier:format; 65, argument_list; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:fn; 68, identifier:__name__; 69, identifier:missing_args; 70, return_statement; 70, 71; 71, identifier:fn_kwargs
def get_fn_args(fn, kwargs: dict, prefix: str = None): """ Given function and a dict of kwargs return a dict containing only the args used by the function. If prefix is specified, also search for args that begin with '{prefix}__'. Removes prefix in returned dict. Raises ValueError if a required arg is missing from the kwargs. Raises ValueError if both prefixed and unprefixed arg are given in kwargs. >>> from pprint import pprint as p # Use pprint to sort dict keys >>> kwargs = {'a': 1, 'b': 2, 'c': 3, 'x__d': 4} >>> def foo(a, b=10): return a + b >>> p(get_fn_args(foo, kwargs)) {'a': 1, 'b': 2} >>> def bar(a, b, d): return a + b + d >>> p(get_fn_args(bar, kwargs, prefix='x')) {'a': 1, 'b': 2, 'd': 4} >>> p(get_fn_args(bar, kwargs)) Traceback (most recent call last): ValueError: The following args are missing for the function bar: ['d'] """ all_args = get_all_args(fn) required_args = get_required_args(fn) fn_kwargs = pick_kwargs(kwargs, all_args, prefix) missing_args = [arg for arg in required_args if arg not in fn_kwargs] if missing_args: raise ValueError( 'The following args are missing for the function ' '{}: {}.'.format(fn.__name__, missing_args) ) return fn_kwargs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:pick_kwargs; 3, parameters; 3, 4; 3, 8; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:kwargs; 6, type; 6, 7; 7, identifier:dict; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:required_args; 10, type; 10, 11; 11, identifier:list; 12, typed_default_parameter; 12, 13; 12, 14; 12, 16; 13, identifier:prefix; 14, type; 14, 15; 15, identifier:str; 16, None; 17, block; 17, 18; 17, 20; 17, 40; 17, 44; 17, 88; 17, 100; 17, 115; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:picked; 23, dictionary_comprehension; 23, 24; 23, 27; 23, 36; 24, pair; 24, 25; 24, 26; 25, identifier:k; 26, identifier:v; 27, for_in_clause; 27, 28; 27, 31; 28, pattern_list; 28, 29; 28, 30; 29, identifier:k; 30, identifier:v; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:kwargs; 34, identifier:items; 35, argument_list; 36, if_clause; 36, 37; 37, comparison_operator:in; 37, 38; 37, 39; 38, identifier:k; 39, identifier:required_args; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:prefixed; 43, dictionary; 44, if_statement; 44, 45; 44, 46; 45, identifier:prefix; 46, block; 46, 47; 46, 53; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:prefix; 50, binary_operator:+; 50, 51; 50, 52; 51, identifier:prefix; 52, string:'__'; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:prefixed; 56, dictionary_comprehension; 56, 57; 56, 64; 56, 73; 57, pair; 57, 58; 57, 63; 58, call; 58, 59; 58, 60; 59, identifier:_remove_prefix; 60, argument_list; 60, 61; 60, 62; 61, identifier:k; 62, identifier:prefix; 63, identifier:v; 64, for_in_clause; 64, 65; 64, 68; 65, pattern_list; 65, 66; 65, 67; 66, identifier:k; 67, identifier:v; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:kwargs; 71, identifier:items; 72, argument_list; 73, if_clause; 73, 74; 74, boolean_operator:and; 74, 75; 74, 81; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:k; 78, identifier:startswith; 79, argument_list; 79, 80; 80, identifier:prefix; 81, comparison_operator:in; 81, 82; 81, 87; 82, call; 82, 83; 82, 84; 83, identifier:_remove_prefix; 84, argument_list; 84, 85; 84, 86; 85, identifier:k; 86, identifier:prefix; 87, identifier:required_args; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:conflicting_args; 91, list_comprehension; 91, 92; 91, 93; 91, 96; 92, identifier:k; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:k; 95, identifier:picked; 96, if_clause; 96, 97; 97, comparison_operator:in; 97, 98; 97, 99; 98, identifier:k; 99, identifier:prefixed; 100, if_statement; 100, 101; 100, 102; 101, identifier:conflicting_args; 102, block; 102, 103; 103, raise_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:ValueError; 106, argument_list; 106, 107; 107, call; 107, 108; 107, 113; 108, attribute; 108, 109; 108, 112; 109, concatenated_string; 109, 110; 109, 111; 110, string:'Both prefixed and unprefixed args were specified '; 111, string:'for the following parameters: {}'; 112, identifier:format; 113, argument_list; 113, 114; 114, identifier:conflicting_args; 115, return_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:tz; 119, identifier:merge; 120, argument_list; 120, 121; 120, 122; 121, identifier:picked; 122, identifier:prefixed
def pick_kwargs(kwargs: dict, required_args: list, prefix: str = None): """ Given a dict of kwargs and a list of required_args, return a dict containing only the args in required_args. If prefix is specified, also search for args that begin with '{prefix}__'. Removes prefix in returned dict. Raises ValueError if both prefixed and unprefixed arg are given in kwargs. >>> from pprint import pprint as p # Use pprint to sort dict keys >>> kwargs = {'a': 1, 'b': 2, 'c': 3, 'x__d': 4} >>> p(pick_kwargs(kwargs, ['a', 'd'])) {'a': 1} >>> p(pick_kwargs(kwargs, ['a', 'd'], prefix='x')) {'a': 1, 'd': 4} >>> pick_kwargs({'a': 1, 'x__a': 2}, ['a'], prefix='x') Traceback (most recent call last): ValueError: Both prefixed and unprefixed args were specified for the following parameters: ['a'] """ picked = {k: v for k, v in kwargs.items() if k in required_args} prefixed = {} if prefix: prefix = prefix + '__' prefixed = { _remove_prefix(k, prefix): v for k, v in kwargs.items() if k.startswith(prefix) and _remove_prefix(k, prefix) in required_args } conflicting_args = [k for k in picked if k in prefixed] if conflicting_args: raise ValueError( 'Both prefixed and unprefixed args were specified ' 'for the following parameters: {}'.format(conflicting_args) ) return tz.merge(picked, prefixed)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:search; 3, parameters; 3, 4; 3, 5; 4, identifier:query; 5, default_parameter; 5, 6; 5, 7; 6, identifier:query_type; 7, identifier:DEFAULT_QUERY_TYPE; 8, block; 8, 9; 8, 11; 8, 12; 8, 21; 8, 22; 8, 38; 8, 76; 8, 77; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 17; 14, pattern_list; 14, 15; 14, 16; 15, identifier:statement; 16, identifier:arguments; 17, call; 17, 18; 17, 19; 18, identifier:_build_search; 19, argument_list; 19, 20; 20, identifier:query; 21, comment; 22, if_statement; 22, 23; 22, 30; 23, boolean_operator:and; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:statement; 26, None; 27, comparison_operator:is; 27, 28; 27, 29; 28, identifier:arguments; 29, None; 30, block; 30, 31; 31, return_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:QueryResults; 34, argument_list; 34, 35; 34, 36; 34, 37; 35, list:[]; 36, list:[]; 37, string:'AND'; 38, with_statement; 38, 39; 38, 47; 39, with_clause; 39, 40; 40, with_item; 40, 41; 41, as_pattern; 41, 42; 41, 45; 42, call; 42, 43; 42, 44; 43, identifier:db_connect; 44, argument_list; 45, as_pattern_target; 45, 46; 46, identifier:db_connection; 47, block; 47, 48; 48, with_statement; 48, 49; 48, 59; 49, with_clause; 49, 50; 50, with_item; 50, 51; 51, as_pattern; 51, 52; 51, 57; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:db_connection; 55, identifier:cursor; 56, argument_list; 57, as_pattern_target; 57, 58; 58, identifier:cursor; 59, block; 59, 60; 59, 68; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:cursor; 64, identifier:execute; 65, argument_list; 65, 66; 65, 67; 66, identifier:statement; 67, identifier:arguments; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:search_results; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:cursor; 74, identifier:fetchall; 75, argument_list; 76, comment; 77, return_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:QueryResults; 80, argument_list; 80, 81; 80, 82; 80, 83; 81, identifier:search_results; 82, identifier:query; 83, identifier:query_type
def search(query, query_type=DEFAULT_QUERY_TYPE): """Search database using parsed query. Executes a database search query from the given ``query`` (a ``Query`` object) and optionally accepts a list of search weights. By default, the search results are ordered by weight. :param query: containing terms, filters, and sorts. :type query: Query :returns: a sequence of records that match the query conditions :rtype: QueryResults (which is a sequence of QueryRecord objects) """ # Build the SQL statement. statement, arguments = _build_search(query) # Execute the SQL. if statement is None and arguments is None: return QueryResults([], [], 'AND') with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute(statement, arguments) search_results = cursor.fetchall() # Wrap the SQL results. return QueryResults(search_results, query, query_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_tdms_files; 3, parameters; 3, 4; 4, identifier:directory; 5, block; 5, 6; 5, 8; 5, 21; 5, 22; 5, 41; 5, 42; 5, 60; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:path; 11, call; 11, 12; 11, 20; 12, attribute; 12, 13; 12, 19; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:pathlib; 16, identifier:Path; 17, argument_list; 17, 18; 18, identifier:directory; 19, identifier:resolve; 20, argument_list; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:tdmslist; 25, list_comprehension; 25, 26; 25, 27; 25, 35; 26, identifier:r; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:r; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:path; 32, identifier:rglob; 33, argument_list; 33, 34; 34, string:"*.tdms"; 35, if_clause; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:r; 39, identifier:is_file; 40, argument_list; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:tdmslist; 45, list_comprehension; 45, 46; 45, 47; 45, 50; 46, identifier:r; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:r; 49, identifier:tdmslist; 50, if_clause; 50, 51; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:r; 56, identifier:name; 57, identifier:endswith; 58, argument_list; 58, 59; 59, string:"_traces.tdms"; 60, return_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:sorted; 63, argument_list; 63, 64; 64, identifier:tdmslist
def get_tdms_files(directory): """Recursively find projects based on '.tdms' file endings Searches the `directory` recursively and return a sorted list of all found '.tdms' project files, except fluorescence data trace files which end with `_traces.tdms`. """ path = pathlib.Path(directory).resolve() # get all tdms files tdmslist = [r for r in path.rglob("*.tdms") if r.is_file()] # exclude traces files tdmslist = [r for r in tdmslist if not r.name.endswith("_traces.tdms")] return sorted(tdmslist)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:footrule_dist; 3, parameters; 3, 4; 3, 5; 4, identifier:params1; 5, default_parameter; 5, 6; 5, 7; 6, identifier:params2; 7, None; 8, block; 8, 9; 8, 11; 8, 25; 8, 35; 8, 70; 9, expression_statement; 9, 10; 10, comment; 11, assert_statement; 11, 12; 12, boolean_operator:or; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:params2; 15, None; 16, comparison_operator:==; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:len; 19, argument_list; 19, 20; 20, identifier:params1; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:params2; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ranks1; 28, call; 28, 29; 28, 30; 29, identifier:rankdata; 30, argument_list; 30, 31; 30, 32; 31, identifier:params1; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:method; 34, string:"average"; 35, if_statement; 35, 36; 35, 39; 35, 58; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:params2; 38, None; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ranks2; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:np; 46, identifier:arange; 47, argument_list; 47, 48; 47, 49; 47, 55; 48, integer:1; 49, binary_operator:+; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, identifier:params1; 54, integer:1; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:dtype; 57, identifier:float; 58, else_clause; 58, 59; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:ranks2; 63, call; 63, 64; 63, 65; 64, identifier:rankdata; 65, argument_list; 65, 66; 65, 67; 66, identifier:params2; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:method; 69, string:"average"; 70, return_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:np; 74, identifier:sum; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:abs; 80, argument_list; 80, 81; 81, binary_operator:-; 81, 82; 81, 83; 82, identifier:ranks1; 83, identifier:ranks2
def footrule_dist(params1, params2=None): r"""Compute Spearman's footrule distance between two models. This function computes Spearman's footrule distance between the rankings induced by two parameter vectors. Let :math:`\sigma_i` be the rank of item ``i`` in the model described by ``params1``, and :math:`\tau_i` be its rank in the model described by ``params2``. Spearman's footrule distance is defined by .. math:: \sum_{i=1}^N | \sigma_i - \tau_i | By convention, items with the lowest parameters are ranked first (i.e., sorted using the natural order). If the argument ``params2`` is ``None``, the second model is assumed to rank the items by their index: item ``0`` has rank 1, item ``1`` has rank 2, etc. Parameters ---------- params1 : array_like Parameters of the first model. params2 : array_like, optional Parameters of the second model. Returns ------- dist : float Spearman's footrule distance. """ assert params2 is None or len(params1) == len(params2) ranks1 = rankdata(params1, method="average") if params2 is None: ranks2 = np.arange(1, len(params1) + 1, dtype=float) else: ranks2 = rankdata(params2, method="average") return np.sum(np.abs(ranks1 - ranks2))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:kendalltau_dist; 3, parameters; 3, 4; 3, 5; 4, identifier:params1; 5, default_parameter; 5, 6; 5, 7; 6, identifier:params2; 7, None; 8, block; 8, 9; 8, 11; 8, 25; 8, 35; 8, 70; 8, 80; 8, 87; 8, 98; 9, expression_statement; 9, 10; 10, comment; 11, assert_statement; 11, 12; 12, boolean_operator:or; 12, 13; 12, 16; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:params2; 15, None; 16, comparison_operator:==; 16, 17; 16, 21; 17, call; 17, 18; 17, 19; 18, identifier:len; 19, argument_list; 19, 20; 20, identifier:params1; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:params2; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ranks1; 28, call; 28, 29; 28, 30; 29, identifier:rankdata; 30, argument_list; 30, 31; 30, 32; 31, identifier:params1; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:method; 34, string:"ordinal"; 35, if_statement; 35, 36; 35, 39; 35, 58; 36, comparison_operator:is; 36, 37; 36, 38; 37, identifier:params2; 38, None; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:ranks2; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:np; 46, identifier:arange; 47, argument_list; 47, 48; 47, 49; 47, 55; 48, integer:1; 49, binary_operator:+; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, identifier:params1; 54, integer:1; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:dtype; 57, identifier:float; 58, else_clause; 58, 59; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:ranks2; 63, call; 63, 64; 63, 65; 64, identifier:rankdata; 65, argument_list; 65, 66; 65, 67; 66, identifier:params2; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:method; 69, string:"ordinal"; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, pattern_list; 72, 73; 72, 74; 73, identifier:tau; 74, identifier:_; 75, call; 75, 76; 75, 77; 76, identifier:kendalltau; 77, argument_list; 77, 78; 77, 79; 78, identifier:ranks1; 79, identifier:ranks2; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:n_items; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, identifier:params1; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:n_pairs; 90, binary_operator:/; 90, 91; 90, 97; 91, binary_operator:*; 91, 92; 91, 93; 92, identifier:n_items; 93, parenthesized_expression; 93, 94; 94, binary_operator:-; 94, 95; 94, 96; 95, identifier:n_items; 96, integer:1; 97, integer:2; 98, return_statement; 98, 99; 99, call; 99, 100; 99, 101; 100, identifier:round; 101, argument_list; 101, 102; 102, binary_operator:/; 102, 103; 102, 109; 103, parenthesized_expression; 103, 104; 104, binary_operator:-; 104, 105; 104, 106; 105, identifier:n_pairs; 106, binary_operator:*; 106, 107; 106, 108; 107, identifier:n_pairs; 108, identifier:tau; 109, integer:2
def kendalltau_dist(params1, params2=None): r"""Compute the Kendall tau distance between two models. This function computes the Kendall tau distance between the rankings induced by two parameter vectors. Let :math:`\sigma_i` be the rank of item ``i`` in the model described by ``params1``, and :math:`\tau_i` be its rank in the model described by ``params2``. The Kendall tau distance is defined as the number of pairwise disagreements between the two rankings, i.e., .. math:: \sum_{i=1}^N \sum_{j=1}^N \mathbf{1} \{ \sigma_i > \sigma_j \wedge \tau_i < \tau_j \} By convention, items with the lowest parameters are ranked first (i.e., sorted using the natural order). If the argument ``params2`` is ``None``, the second model is assumed to rank the items by their index: item ``0`` has rank 1, item ``1`` has rank 2, etc. If some values are equal within a parameter vector, all items are given a distinct rank, corresponding to the order in which the values occur. Parameters ---------- params1 : array_like Parameters of the first model. params2 : array_like, optional Parameters of the second model. Returns ------- dist : float Kendall tau distance. """ assert params2 is None or len(params1) == len(params2) ranks1 = rankdata(params1, method="ordinal") if params2 is None: ranks2 = np.arange(1, len(params1) + 1, dtype=float) else: ranks2 = rankdata(params2, method="ordinal") tau, _ = kendalltau(ranks1, ranks2) n_items = len(params1) n_pairs = n_items * (n_items - 1) / 2 return round((n_pairs - n_pairs * tau) / 2)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:parse_sort_string; 3, parameters; 3, 4; 4, identifier:sort; 5, block; 5, 6; 5, 8; 5, 16; 5, 25; 5, 29; 5, 102; 5, 109; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 12; 9, comparison_operator:is; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, block; 12, 13; 13, return_statement; 13, 14; 14, list:['_score']; 14, 15; 15, string:'_score'; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:l; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:sort; 22, identifier:rsplit; 23, argument_list; 23, 24; 24, string:','; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:sortlist; 28, list:[]; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:se; 31, identifier:l; 32, block; 32, 33; 32, 41; 32, 55; 32, 75; 32, 83; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:se; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:se; 39, identifier:strip; 40, argument_list; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:order; 44, conditional_expression:if; 44, 45; 44, 46; 44, 54; 45, string:'desc'; 46, comparison_operator:==; 46, 47; 46, 53; 47, subscript; 47, 48; 47, 49; 48, identifier:se; 49, slice; 49, 50; 49, 51; 49, 52; 50, integer:0; 51, colon; 52, integer:1; 53, string:'-'; 54, string:'asc'; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:field; 58, conditional_expression:if; 58, 59; 58, 64; 58, 74; 59, subscript; 59, 60; 59, 61; 60, identifier:se; 61, slice; 61, 62; 61, 63; 62, integer:1; 63, colon; 64, comparison_operator:in; 64, 65; 64, 71; 65, subscript; 65, 66; 65, 67; 66, identifier:se; 67, slice; 67, 68; 67, 69; 67, 70; 68, integer:0; 69, colon; 70, integer:1; 71, list:['-', '+']; 71, 72; 71, 73; 72, string:'-'; 73, string:'+'; 74, identifier:se; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:field; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:field; 81, identifier:strip; 82, argument_list; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:sortlist; 87, identifier:append; 88, argument_list; 88, 89; 89, dictionary; 89, 90; 90, pair; 90, 91; 90, 92; 91, identifier:field; 92, dictionary; 92, 93; 92, 96; 92, 99; 93, pair; 93, 94; 93, 95; 94, string:"order"; 95, identifier:order; 96, pair; 96, 97; 96, 98; 97, string:"unmapped_type"; 98, string:"string"; 99, pair; 99, 100; 99, 101; 100, string:"missing"; 101, string:"_last"; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:sortlist; 106, identifier:append; 107, argument_list; 107, 108; 108, string:'_score'; 109, return_statement; 109, 110; 110, identifier:sortlist
def parse_sort_string(sort): """ Parse a sort string for use with elasticsearch :param: sort: the sort string """ if sort is None: return ['_score'] l = sort.rsplit(',') sortlist = [] for se in l: se = se.strip() order = 'desc' if se[0:1] == '-' else 'asc' field = se[1:] if se[0:1] in ['-', '+'] else se field = field.strip() sortlist.append({field: {"order": order, "unmapped_type": "string", "missing": "_last"}}) sortlist.append('_score') return sortlist
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_config_dir; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:path; 5, default_parameter; 5, 6; 5, 7; 6, identifier:pattern; 7, string:"*.config"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:configspec; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:allow_errors; 13, False; 14, block; 14, 15; 14, 17; 14, 26; 14, 39; 14, 57; 14, 64; 14, 70; 14, 138; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:logger; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:logging; 23, identifier:getLogger; 24, argument_list; 24, 25; 25, identifier:__name__; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:logger; 30, identifier:debug; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, string:"Loading all files matching {0} in {1}"; 35, identifier:format; 36, argument_list; 36, 37; 36, 38; 37, identifier:pattern; 38, identifier:path; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:files; 42, call; 42, 43; 42, 56; 43, attribute; 43, 44; 43, 55; 44, call; 44, 45; 44, 46; 45, identifier:Globber; 46, argument_list; 46, 47; 46, 48; 46, 52; 47, identifier:path; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:include; 50, list:[pattern]; 50, 51; 51, identifier:pattern; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:recursive; 54, False; 55, identifier:glob; 56, argument_list; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:files; 60, call; 60, 61; 60, 62; 61, identifier:sorted; 62, argument_list; 62, 63; 63, identifier:files; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:config; 67, call; 67, 68; 67, 69; 68, identifier:ConfigObj; 69, argument_list; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:filename; 72, identifier:files; 73, block; 73, 74; 73, 86; 73, 119; 73, 131; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:logger; 78, identifier:debug; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"- Loading config for {0}"; 83, identifier:format; 84, argument_list; 84, 85; 85, identifier:filename; 86, try_statement; 86, 87; 86, 98; 87, block; 87, 88; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:conf; 91, call; 91, 92; 91, 93; 92, identifier:ConfigObj; 93, argument_list; 93, 94; 93, 95; 94, identifier:filename; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:configspec; 97, identifier:configspec; 98, except_clause; 98, 99; 98, 100; 98, 101; 99, identifier:ConfigObjError; 100, identifier:coe; 101, block; 101, 102; 101, 118; 102, expression_statement; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:logger; 106, identifier:error; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, string:"An error occurred while parsing {0}: {1}"; 111, identifier:format; 112, argument_list; 112, 113; 112, 114; 113, identifier:filename; 114, call; 114, 115; 114, 116; 115, identifier:str; 116, argument_list; 116, 117; 117, identifier:coe; 118, continue_statement; 119, if_statement; 119, 120; 119, 121; 120, identifier:configspec; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:conf; 126, identifier:validate; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:Validator; 130, argument_list; 131, expression_statement; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:config; 135, identifier:merge; 136, argument_list; 136, 137; 137, identifier:conf; 138, return_statement; 138, 139; 139, identifier:config
def get_config_dir(path, pattern="*.config", configspec=None, allow_errors=False): """ Load an entire directory of configuration files, merging them into one. This function will load multiple configuration files matching the given pattern, in the given path, and merge them. The found files are first sorted alphabetically, and then loaded and merged. A good practice is to use ConfigObj sections, for easy loading of information like per-host configuration. Parameters ---------- path: string Absolute path to a directory of ConfigObj files pattern: string Globbing pattern used to find files. Defaults to *.config. configspec: ConfigObj Used to sanitize the values in the resulting ConfigObj. Validation errors are currently not exposed to the caller. allow_errors: boolean If False, errors raised by ConfigObj are not caught. If True, errors raise by ConfigObj are caught, and an error is logged using logger. """ logger = logging.getLogger(__name__) logger.debug("Loading all files matching {0} in {1}".format(pattern, path)) files = Globber(path, include=[pattern], recursive=False).glob() files = sorted(files) config = ConfigObj() for filename in files: logger.debug("- Loading config for {0}".format(filename)) try: conf = ConfigObj(filename, configspec=configspec) except ConfigObjError, coe: logger.error("An error occurred while parsing {0}: {1}".format(filename, str(coe))) continue if configspec: conf.validate(Validator()) config.merge(conf) return config
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:is_first_instance_aws; 3, parameters; 4, block; 4, 5; 4, 7; 4, 54; 4, 118; 4, 180; 5, expression_statement; 5, 6; 6, comment; 7, try_statement; 7, 8; 7, 9; 7, 38; 8, comment; 9, block; 9, 10; 9, 26; 9, 32; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:instance_details; 13, call; 13, 14; 13, 25; 14, attribute; 14, 15; 14, 24; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:requests; 18, identifier:get; 19, argument_list; 19, 20; 19, 21; 20, string:'http://169.254.169.254/latest/dynamic/instance-identity/document'; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:timeout; 23, integer:5; 24, identifier:json; 25, argument_list; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:instance_id; 29, subscript; 29, 30; 29, 31; 30, identifier:instance_details; 31, string:'instanceId'; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:instance_region; 35, subscript; 35, 36; 35, 37; 36, identifier:instance_details; 37, string:'region'; 38, except_clause; 38, 39; 38, 48; 39, as_pattern; 39, 40; 39, 46; 40, tuple; 40, 41; 40, 44; 40, 45; 41, attribute; 41, 42; 41, 43; 42, identifier:requests; 43, identifier:RequestException; 44, identifier:ValueError; 45, identifier:KeyError; 46, as_pattern_target; 46, 47; 47, identifier:e; 48, block; 48, 49; 49, raise_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:StackInterrogationException; 52, argument_list; 52, 53; 53, identifier:e; 54, try_statement; 54, 55; 54, 56; 54, 100; 54, 111; 55, comment; 56, block; 56, 57; 56, 69; 56, 81; 56, 90; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:autoscaling_client; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:boto3; 63, identifier:client; 64, argument_list; 64, 65; 64, 66; 65, string:'autoscaling'; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:region_name; 68, identifier:instance_region; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:response; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:autoscaling_client; 75, identifier:describe_auto_scaling_instances; 76, argument_list; 76, 77; 77, keyword_argument; 77, 78; 77, 79; 78, identifier:InstanceIds; 79, list:[instance_id]; 79, 80; 80, identifier:instance_id; 81, assert_statement; 81, 82; 82, comparison_operator:==; 82, 83; 82, 89; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:response; 88, string:'AutoScalingInstances'; 89, integer:1; 90, expression_statement; 90, 91; 91, assignment; 91, 92; 91, 93; 92, identifier:autoscaling_group; 93, subscript; 93, 94; 93, 99; 94, subscript; 94, 95; 94, 98; 95, subscript; 95, 96; 95, 97; 96, identifier:response; 97, string:'AutoScalingInstances'; 98, integer:0; 99, string:'AutoScalingGroupName'; 100, except_clause; 100, 101; 100, 105; 101, as_pattern; 101, 102; 101, 103; 102, identifier:ClientError; 103, as_pattern_target; 103, 104; 104, identifier:e; 105, block; 105, 106; 106, raise_statement; 106, 107; 107, call; 107, 108; 107, 109; 108, identifier:StackInterrogationException; 109, argument_list; 109, 110; 110, identifier:e; 111, except_clause; 111, 112; 111, 113; 112, identifier:AssertionError; 113, block; 113, 114; 114, raise_statement; 114, 115; 115, call; 115, 116; 115, 117; 116, identifier:InstanceNotInAsgException; 117, argument_list; 118, try_statement; 118, 119; 118, 120; 118, 121; 118, 167; 119, comment; 120, comment; 121, block; 121, 122; 121, 134; 121, 143; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 125; 124, identifier:response; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:autoscaling_client; 128, identifier:describe_auto_scaling_groups; 129, argument_list; 129, 130; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:AutoScalingGroupNames; 132, list:[autoscaling_group]; 132, 133; 133, identifier:autoscaling_group; 134, assert_statement; 134, 135; 135, comparison_operator:==; 135, 136; 135, 142; 136, call; 136, 137; 136, 138; 137, identifier:len; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 141; 140, identifier:response; 141, string:'AutoScalingGroups'; 142, integer:1; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:autoscaling_group_instance_ids; 146, call; 146, 147; 146, 148; 147, identifier:sorted; 148, generator_expression; 148, 149; 148, 152; 148, 161; 149, subscript; 149, 150; 149, 151; 150, identifier:instance; 151, string:'InstanceId'; 152, for_in_clause; 152, 153; 152, 154; 153, identifier:instance; 154, subscript; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 159; 156, subscript; 156, 157; 156, 158; 157, identifier:response; 158, string:'AutoScalingGroups'; 159, integer:0; 160, string:'Instances'; 161, if_clause; 161, 162; 162, comparison_operator:==; 162, 163; 162, 166; 163, subscript; 163, 164; 163, 165; 164, identifier:instance; 165, string:'LifecycleState'; 166, string:'InService'; 167, except_clause; 167, 168; 167, 174; 168, as_pattern; 168, 169; 168, 172; 169, tuple; 169, 170; 169, 171; 170, identifier:ClientError; 171, identifier:AssertionError; 172, as_pattern_target; 172, 173; 173, identifier:e; 174, block; 174, 175; 175, raise_statement; 175, 176; 176, call; 176, 177; 176, 178; 177, identifier:StackInterrogationException; 178, argument_list; 178, 179; 179, identifier:e; 180, return_statement; 180, 181; 181, call; 181, 182; 181, 183; 182, identifier:bool; 183, argument_list; 183, 184; 184, boolean_operator:and; 184, 185; 184, 186; 185, identifier:autoscaling_group_instance_ids; 186, comparison_operator:==; 186, 187; 186, 190; 187, subscript; 187, 188; 187, 189; 188, identifier:autoscaling_group_instance_ids; 189, integer:0; 190, identifier:instance_id
def is_first_instance_aws(): """ Returns True if the current instance is the first instance in the ASG group, sorted by instance_id. """ try: # get instance id and aws region instance_details = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document', timeout=5).json() instance_id = instance_details['instanceId'] instance_region = instance_details['region'] except (requests.RequestException, ValueError, KeyError) as e: raise StackInterrogationException(e) try: # get instance's autoscaling group autoscaling_client = boto3.client('autoscaling', region_name=instance_region) response = autoscaling_client.describe_auto_scaling_instances(InstanceIds=[instance_id]) assert len(response['AutoScalingInstances']) == 1 autoscaling_group = response['AutoScalingInstances'][0]['AutoScalingGroupName'] except ClientError as e: raise StackInterrogationException(e) except AssertionError: raise InstanceNotInAsgException() try: # list in-service instances in autoscaling group # instances being launched or terminated should not be considered response = autoscaling_client.describe_auto_scaling_groups(AutoScalingGroupNames=[autoscaling_group]) assert len(response['AutoScalingGroups']) == 1 autoscaling_group_instance_ids = sorted( instance['InstanceId'] for instance in response['AutoScalingGroups'][0]['Instances'] if instance['LifecycleState'] == 'InService' ) except (ClientError, AssertionError) as e: raise StackInterrogationException(e) return bool(autoscaling_group_instance_ids and autoscaling_group_instance_ids[0] == instance_id)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:creation_ordered; 3, parameters; 3, 4; 4, identifier:class_to_decorate; 5, block; 5, 6; 5, 8; 5, 22; 5, 28; 5, 69; 5, 76; 5, 77; 5, 92; 5, 99; 5, 108; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:next_index; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:functools; 14, identifier:partial; 15, argument_list; 15, 16; 15, 17; 16, identifier:next; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:itertools; 20, identifier:count; 21, argument_list; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:__init__orig; 25, attribute; 25, 26; 25, 27; 26, identifier:class_to_decorate; 27, identifier:__init__; 28, decorated_definition; 28, 29; 28, 40; 29, decorator; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:functools; 33, identifier:wraps; 34, argument_list; 34, 35; 34, 36; 35, identifier:__init__orig; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:assigned; 38, list:['__doc__']; 38, 39; 39, string:'__doc__'; 40, function_definition; 40, 41; 40, 42; 40, 48; 41, function_name:__init__; 42, parameters; 42, 43; 42, 44; 42, 46; 43, identifier:self; 44, list_splat_pattern; 44, 45; 45, identifier:args; 46, dictionary_splat_pattern; 46, 47; 47, identifier:kwargs; 48, block; 48, 49; 48, 60; 49, expression_statement; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:object; 53, identifier:__setattr__; 54, argument_list; 54, 55; 54, 56; 54, 57; 55, identifier:self; 56, string:'_index'; 57, call; 57, 58; 57, 59; 58, identifier:next_index; 59, argument_list; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 63; 62, identifier:__init__orig; 63, argument_list; 63, 64; 63, 65; 63, 67; 64, identifier:self; 65, list_splat; 65, 66; 66, identifier:args; 67, dictionary_splat; 67, 68; 68, identifier:kwargs; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:setattr; 72, argument_list; 72, 73; 72, 74; 72, 75; 73, identifier:class_to_decorate; 74, string:'__init__'; 75, identifier:__init__; 76, comment; 77, function_definition; 77, 78; 77, 79; 77, 82; 78, function_name:__lt__; 79, parameters; 79, 80; 79, 81; 80, identifier:self; 81, identifier:other; 82, block; 82, 83; 82, 91; 83, return_statement; 83, 84; 84, comparison_operator:<; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:_index; 88, attribute; 88, 89; 88, 90; 89, identifier:other; 90, identifier:_index; 91, comment; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:setattr; 95, argument_list; 95, 96; 95, 97; 95, 98; 96, identifier:class_to_decorate; 97, string:'__lt__'; 98, identifier:__lt__; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:class_to_decorate; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:functools; 105, identifier:total_ordering; 106, argument_list; 106, 107; 107, identifier:class_to_decorate; 108, return_statement; 108, 109; 109, identifier:class_to_decorate
def creation_ordered(class_to_decorate): """ Class decorator that ensures that instances will be ordered after creation order when sorted. :type class_to_decorate: class :rtype: class """ next_index = functools.partial(next, itertools.count()) __init__orig = class_to_decorate.__init__ @functools.wraps(__init__orig, assigned=['__doc__']) def __init__(self, *args, **kwargs): object.__setattr__(self, '_index', next_index()) __init__orig(self, *args, **kwargs) setattr(class_to_decorate, '__init__', __init__) # noinspection PyProtectedMember def __lt__(self, other): return self._index < other._index # pragma: no mutate setattr(class_to_decorate, '__lt__', __lt__) class_to_decorate = functools.total_ordering(class_to_decorate) return class_to_decorate
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:get_members; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:cls; 5, default_parameter; 5, 6; 5, 7; 6, identifier:member_class; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:is_member; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort_key; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:_parameter; 16, None; 17, block; 17, 18; 17, 20; 17, 34; 17, 40; 17, 85; 17, 174; 17, 180; 17, 238; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 28; 21, boolean_operator:and; 21, 22; 21, 25; 22, comparison_operator:is; 22, 23; 22, 24; 23, identifier:member_class; 24, None; 25, comparison_operator:is; 25, 26; 25, 27; 26, identifier:is_member; 27, None; 28, block; 28, 29; 29, raise_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:TypeError; 32, argument_list; 32, 33; 33, string:"get_members either needs a member_class parameter or an is_member check function (or both)"; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:members; 37, call; 37, 38; 37, 39; 38, identifier:OrderedDict; 39, argument_list; 40, for_statement; 40, 41; 40, 42; 40, 45; 41, identifier:base; 42, attribute; 42, 43; 42, 44; 43, identifier:cls; 44, identifier:__bases__; 45, block; 45, 46; 45, 78; 46, if_statement; 46, 47; 46, 50; 46, 67; 47, comparison_operator:is; 47, 48; 47, 49; 48, identifier:_parameter; 49, None; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:inherited_members; 54, call; 54, 55; 54, 56; 55, identifier:get_members; 56, argument_list; 56, 57; 56, 58; 56, 61; 56, 64; 57, identifier:base; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:member_class; 60, identifier:member_class; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:is_member; 63, identifier:is_member; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:sort_key; 66, identifier:sort_key; 67, else_clause; 67, 68; 67, 69; 68, comment; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:inherited_members; 73, call; 73, 74; 73, 75; 74, identifier:get_declared; 75, argument_list; 75, 76; 75, 77; 76, identifier:base; 77, identifier:_parameter; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:members; 82, identifier:update; 83, argument_list; 83, 84; 84, identifier:inherited_members; 85, function_definition; 85, 86; 85, 87; 85, 88; 86, function_name:generate_member_bindings; 87, parameters; 88, block; 88, 89; 89, for_statement; 89, 90; 89, 91; 89, 94; 90, identifier:name; 91, attribute; 91, 92; 91, 93; 92, identifier:cls; 93, identifier:__dict__; 94, block; 94, 95; 94, 104; 94, 112; 95, if_statement; 95, 96; 95, 102; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:name; 99, identifier:startswith; 100, argument_list; 100, 101; 101, string:'__'; 102, block; 102, 103; 103, continue_statement; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:obj; 107, call; 107, 108; 107, 109; 108, identifier:getattr; 109, argument_list; 109, 110; 109, 111; 110, identifier:cls; 111, identifier:name; 112, if_statement; 112, 113; 112, 122; 112, 128; 112, 143; 113, boolean_operator:and; 113, 114; 113, 117; 114, comparison_operator:is; 114, 115; 114, 116; 115, identifier:member_class; 116, None; 117, call; 117, 118; 117, 119; 118, identifier:isinstance; 119, argument_list; 119, 120; 119, 121; 120, identifier:obj; 121, identifier:member_class; 122, block; 122, 123; 123, expression_statement; 123, 124; 124, yield; 124, 125; 125, expression_list; 125, 126; 125, 127; 126, identifier:name; 127, identifier:obj; 128, elif_clause; 128, 129; 128, 137; 129, boolean_operator:and; 129, 130; 129, 133; 130, comparison_operator:is; 130, 131; 130, 132; 131, identifier:is_member; 132, None; 133, call; 133, 134; 133, 135; 134, identifier:is_member; 135, argument_list; 135, 136; 136, identifier:obj; 137, block; 137, 138; 138, expression_statement; 138, 139; 139, yield; 139, 140; 140, expression_list; 140, 141; 140, 142; 141, identifier:name; 142, identifier:obj; 143, elif_clause; 143, 144; 143, 165; 144, boolean_operator:and; 144, 145; 144, 158; 145, boolean_operator:and; 145, 146; 145, 152; 146, comparison_operator:is; 146, 147; 146, 151; 147, call; 147, 148; 147, 149; 148, identifier:type; 149, argument_list; 149, 150; 150, identifier:obj; 151, identifier:tuple; 152, comparison_operator:==; 152, 153; 152, 157; 153, call; 153, 154; 153, 155; 154, identifier:len; 155, argument_list; 155, 156; 156, identifier:obj; 157, integer:1; 158, call; 158, 159; 158, 160; 159, identifier:isinstance; 160, argument_list; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:obj; 163, integer:0; 164, identifier:member_class; 165, block; 165, 166; 165, 173; 166, raise_statement; 166, 167; 167, call; 167, 168; 167, 169; 168, identifier:TypeError; 169, argument_list; 169, 170; 170, binary_operator:%; 170, 171; 170, 172; 171, string:"'%s' is a one-tuple containing what we are looking for. Trailing comma much? Don't... just don't."; 172, identifier:name; 173, comment; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:bindings; 177, call; 177, 178; 177, 179; 178, identifier:generate_member_bindings; 179, argument_list; 180, if_statement; 180, 181; 180, 184; 180, 229; 181, comparison_operator:is; 181, 182; 181, 183; 182, identifier:sort_key; 183, None; 184, block; 184, 185; 184, 222; 185, try_statement; 185, 186; 185, 205; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:sorted_bindings; 190, call; 190, 191; 190, 192; 191, identifier:sorted; 192, argument_list; 192, 193; 192, 194; 193, identifier:bindings; 194, keyword_argument; 194, 195; 194, 196; 195, identifier:key; 196, lambda; 196, 197; 196, 199; 197, lambda_parameters; 197, 198; 198, identifier:x; 199, call; 199, 200; 199, 201; 200, identifier:sort_key; 201, argument_list; 201, 202; 202, subscript; 202, 203; 202, 204; 203, identifier:x; 204, integer:1; 205, except_clause; 205, 206; 205, 207; 206, identifier:AttributeError; 207, block; 207, 208; 208, if_statement; 208, 209; 208, 212; 208, 218; 209, comparison_operator:is; 209, 210; 209, 211; 210, identifier:sort_key; 211, identifier:default_sort_key; 212, block; 212, 213; 213, raise_statement; 213, 214; 214, call; 214, 215; 214, 216; 215, identifier:TypeError; 216, argument_list; 216, 217; 217, string:'Missing member ordering definition. Use @creation_ordered or specify sort_key'; 218, else_clause; 218, 219; 218, 220; 219, comment; 220, block; 220, 221; 221, raise_statement; 222, expression_statement; 222, 223; 223, call; 223, 224; 223, 227; 224, attribute; 224, 225; 224, 226; 225, identifier:members; 226, identifier:update; 227, argument_list; 227, 228; 228, identifier:sorted_bindings; 229, else_clause; 229, 230; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:members; 235, identifier:update; 236, argument_list; 236, 237; 237, identifier:bindings; 238, return_statement; 238, 239; 239, identifier:members
def get_members(cls, member_class=None, is_member=None, sort_key=None, _parameter=None): """ Collect all class level attributes matching the given criteria. :param class member_class: Class(es) to collect :param is_member: Function to determine if an object should be collected :param sort_key: Function to invoke on members to obtain ordering (Default is to use ordering from `creation_ordered`) :type is_member: (object) -> bool :type sort_key: (object) -> object """ if member_class is None and is_member is None: raise TypeError("get_members either needs a member_class parameter or an is_member check function (or both)") members = OrderedDict() for base in cls.__bases__: if _parameter is None: inherited_members = get_members(base, member_class=member_class, is_member=is_member, sort_key=sort_key) else: # When user by @declarative, only traverse up the class inheritance to the decorated class. inherited_members = get_declared(base, _parameter) members.update(inherited_members) def generate_member_bindings(): for name in cls.__dict__: if name.startswith('__'): continue obj = getattr(cls, name) if member_class is not None and isinstance(obj, member_class): yield name, obj elif is_member is not None and is_member(obj): yield name, obj elif type(obj) is tuple and len(obj) == 1 and isinstance(obj[0], member_class): raise TypeError("'%s' is a one-tuple containing what we are looking for. Trailing comma much? Don't... just don't." % name) # pragma: no mutate bindings = generate_member_bindings() if sort_key is not None: try: sorted_bindings = sorted(bindings, key=lambda x: sort_key(x[1])) except AttributeError: if sort_key is default_sort_key: raise TypeError('Missing member ordering definition. Use @creation_ordered or specify sort_key') else: # pragma: no covererage raise members.update(sorted_bindings) else: members.update(bindings) return members
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:sort_field; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:field_name; 6, identifier:direction; 7, default_parameter; 7, 8; 7, 9; 8, identifier:field_vals; 9, None; 10, block; 10, 11; 10, 13; 10, 22; 10, 53; 10, 72; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:field_name; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_normalize_field_name; 20, argument_list; 20, 21; 21, identifier:field_name; 22, if_statement; 22, 23; 22, 26; 22, 31; 22, 41; 23, comparison_operator:>; 23, 24; 23, 25; 24, identifier:direction; 25, integer:0; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:direction; 30, integer:1; 31, elif_clause; 31, 32; 31, 35; 32, comparison_operator:<; 32, 33; 32, 34; 33, identifier:direction; 34, integer:0; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:direction; 39, unary_operator:-; 39, 40; 40, integer:1; 41, else_clause; 41, 42; 42, block; 42, 43; 43, raise_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:ValueError; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, string:"direction {} is undefined"; 50, identifier:format; 51, argument_list; 51, 52; 52, identifier:direction; 53, expression_statement; 53, 54; 54, call; 54, 55; 54, 60; 55, attribute; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:fields_sort; 59, identifier:append; 60, argument_list; 60, 61; 60, 62; 61, identifier:field_name; 62, list:[direction, field_name, list(field_vals) if field_vals else field_vals]; 62, 63; 62, 64; 62, 65; 63, identifier:direction; 64, identifier:field_name; 65, conditional_expression:if; 65, 66; 65, 70; 65, 71; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 68, 69; 69, identifier:field_vals; 70, identifier:field_vals; 71, identifier:field_vals; 72, return_statement; 72, 73; 73, identifier:self
def sort_field(self, field_name, direction, field_vals=None): """ sort this query by field_name in directrion field_name -- string -- the field to sort on direction -- integer -- negative for DESC, positive for ASC field_vals -- list -- the order the rows should be returned in """ field_name = self._normalize_field_name(field_name) if direction > 0: direction = 1 elif direction < 0: direction = -1 else: raise ValueError("direction {} is undefined".format(direction)) self.fields_sort.append(field_name, [direction, field_name, list(field_vals) if field_vals else field_vals]) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:check_photometry_categorize; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:x; 5, identifier:y; 6, identifier:levels; 7, default_parameter; 7, 8; 7, 9; 8, identifier:tags; 9, None; 10, block; 10, 11; 10, 13; 10, 22; 10, 31; 10, 39; 10, 45; 10, 46; 10, 63; 10, 67; 10, 71; 10, 88; 10, 92; 10, 116; 10, 190; 11, expression_statement; 11, 12; 12, string:'''Put every point in its category. levels must be sorted.'''; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:x; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:numpy; 19, identifier:asarray; 20, argument_list; 20, 21; 21, identifier:x; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:y; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:numpy; 28, identifier:asarray; 29, argument_list; 29, 30; 30, identifier:y; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:ys; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:y; 37, identifier:copy; 38, argument_list; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:ys; 43, identifier:sort; 44, argument_list; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:m; 49, call; 49, 50; 49, 62; 50, attribute; 50, 51; 50, 61; 51, subscript; 51, 52; 51, 53; 52, identifier:ys; 53, slice; 53, 54; 53, 60; 54, binary_operator://; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:ys; 59, integer:2; 60, colon; 61, identifier:mean; 62, argument_list; 63, expression_statement; 63, 64; 64, augmented_assignment:/=; 64, 65; 64, 66; 65, identifier:y; 66, identifier:m; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:m; 70, float:1.0; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:s; 74, call; 74, 75; 74, 87; 75, attribute; 75, 76; 75, 86; 76, subscript; 76, 77; 76, 78; 77, identifier:ys; 78, slice; 78, 79; 78, 85; 79, binary_operator://; 79, 80; 79, 84; 80, call; 80, 81; 80, 82; 81, identifier:len; 82, argument_list; 82, 83; 83, identifier:ys; 84, integer:2; 85, colon; 86, identifier:std; 87, argument_list; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:result; 91, list:[]; 92, if_statement; 92, 93; 92, 96; 93, comparison_operator:is; 93, 94; 93, 95; 94, identifier:tags; 95, None; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:tags; 100, call; 100, 101; 100, 102; 101, identifier:list; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:six; 107, identifier:moves; 108, identifier:range; 109, argument_list; 109, 110; 110, binary_operator:+; 110, 111; 110, 115; 111, call; 111, 112; 111, 113; 112, identifier:len; 113, argument_list; 113, 114; 114, identifier:levels; 115, integer:1; 116, for_statement; 116, 117; 116, 120; 116, 125; 116, 175; 117, pattern_list; 117, 118; 117, 119; 118, identifier:l; 119, identifier:t; 120, call; 120, 121; 120, 122; 121, identifier:zip; 122, argument_list; 122, 123; 122, 124; 123, identifier:levels; 124, identifier:tags; 125, block; 125, 126; 125, 132; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:indc; 129, comparison_operator:<; 129, 130; 129, 131; 130, identifier:y; 131, identifier:l; 132, if_statement; 132, 133; 132, 138; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:indc; 136, identifier:any; 137, argument_list; 138, block; 138, 139; 138, 145; 138, 151; 138, 161; 138, 168; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:x1; 142, subscript; 142, 143; 142, 144; 143, identifier:x; 144, identifier:indc; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:y1; 148, subscript; 148, 149; 148, 150; 149, identifier:y; 150, identifier:indc; 151, expression_statement; 151, 152; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:result; 155, identifier:append; 156, argument_list; 156, 157; 157, tuple; 157, 158; 157, 159; 157, 160; 158, identifier:x1; 159, identifier:y1; 160, identifier:t; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:x; 164, subscript; 164, 165; 164, 166; 165, identifier:x; 166, unary_operator:~; 166, 167; 167, identifier:indc; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:y; 171, subscript; 171, 172; 171, 173; 172, identifier:y; 173, unary_operator:~; 173, 174; 174, identifier:indc; 175, else_clause; 175, 176; 176, block; 176, 177; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:result; 181, identifier:append; 182, argument_list; 182, 183; 183, tuple; 183, 184; 183, 185; 183, 186; 184, identifier:x; 185, identifier:y; 186, subscript; 186, 187; 186, 188; 187, identifier:tags; 188, unary_operator:-; 188, 189; 189, integer:1; 190, return_statement; 190, 191; 191, expression_list; 191, 192; 191, 193; 192, identifier:result; 193, tuple; 193, 194; 193, 195; 194, identifier:m; 195, identifier:s
def check_photometry_categorize(x, y, levels, tags=None): '''Put every point in its category. levels must be sorted.''' x = numpy.asarray(x) y = numpy.asarray(y) ys = y.copy() ys.sort() # Mean of the upper half m = ys[len(ys) // 2:].mean() y /= m m = 1.0 s = ys[len(ys) // 2:].std() result = [] if tags is None: tags = list(six.moves.range(len(levels) + 1)) for l, t in zip(levels, tags): indc = y < l if indc.any(): x1 = x[indc] y1 = y[indc] result.append((x1, y1, t)) x = x[~indc] y = y[~indc] else: result.append((x, y, tags[-1])) return result, (m, s)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_normalize_sort_SQL; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:field_name; 6, identifier:field_vals; 7, identifier:sort_dir_str; 8, block; 8, 9; 8, 11; 8, 15; 8, 47; 8, 62; 8, 66; 8, 94; 8, 101; 8, 110; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:fvi; 14, None; 15, if_statement; 15, 16; 15, 19; 15, 31; 16, comparison_operator:==; 16, 17; 16, 18; 17, identifier:sort_dir_str; 18, string:'ASC'; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:fvi; 23, generator_expression; 23, 24; 23, 25; 24, identifier:t; 25, for_in_clause; 25, 26; 25, 27; 26, identifier:t; 27, call; 27, 28; 27, 29; 28, identifier:enumerate; 29, argument_list; 29, 30; 30, identifier:field_vals; 31, else_clause; 31, 32; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:fvi; 36, generator_expression; 36, 37; 36, 38; 37, identifier:t; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:t; 40, call; 40, 41; 40, 42; 41, identifier:enumerate; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:reversed; 45, argument_list; 45, 46; 46, identifier:field_vals; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:query_sort_str; 50, list:[' CASE {}'.format(self._normalize_name(field_name))]; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, string:' CASE {}'; 54, identifier:format; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:_normalize_name; 60, argument_list; 60, 61; 61, identifier:field_name; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:query_args; 65, list:[]; 66, for_statement; 66, 67; 66, 70; 66, 71; 67, pattern_list; 67, 68; 67, 69; 68, identifier:i; 69, identifier:v; 70, identifier:fvi; 71, block; 71, 72; 71, 87; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:query_sort_str; 76, identifier:append; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, string:' WHEN {} THEN {}'; 81, identifier:format; 82, argument_list; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:val_placeholder; 86, identifier:i; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:query_args; 91, identifier:append; 92, argument_list; 92, 93; 93, identifier:v; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:query_sort_str; 98, identifier:append; 99, argument_list; 99, 100; 100, string:' END'; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:query_sort_str; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, string:"\n"; 107, identifier:join; 108, argument_list; 108, 109; 109, identifier:query_sort_str; 110, return_statement; 110, 111; 111, expression_list; 111, 112; 111, 113; 112, identifier:query_sort_str; 113, identifier:query_args
def _normalize_sort_SQL(self, field_name, field_vals, sort_dir_str): """ allow sorting by a set of values http://stackoverflow.com/questions/3303851/sqlite-and-custom-order-by """ fvi = None if sort_dir_str == 'ASC': fvi = (t for t in enumerate(field_vals)) else: fvi = (t for t in enumerate(reversed(field_vals))) query_sort_str = [' CASE {}'.format(self._normalize_name(field_name))] query_args = [] for i, v in fvi: query_sort_str.append(' WHEN {} THEN {}'.format(self.val_placeholder, i)) query_args.append(v) query_sort_str.append(' END') query_sort_str = "\n".join(query_sort_str) return query_sort_str, query_args
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_process; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:segments; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 12; 6, 13; 6, 25; 6, 36; 6, 37; 6, 56; 6, 65; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 18; 15, pattern_list; 15, 16; 15, 17; 16, identifier:mlh; 17, identifier:mlw; 18, expression_list; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:max_line_height; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:max_line_width; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:s; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:segments; 31, identifier:astype; 32, argument_list; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:numpy; 35, identifier:uint32; 36, comment; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:order; 40, binary_operator:+; 40, 41; 40, 51; 41, binary_operator:*; 41, 42; 41, 43; 42, identifier:mlw; 43, parenthesized_expression; 43, 44; 44, binary_operator://; 44, 45; 44, 50; 45, subscript; 45, 46; 45, 47; 45, 49; 46, identifier:s; 47, slice; 47, 48; 48, colon; 49, integer:1; 50, identifier:mlh; 51, subscript; 51, 52; 51, 53; 51, 55; 52, identifier:s; 53, slice; 53, 54; 54, colon; 55, integer:0; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:sort_order; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:numpy; 62, identifier:argsort; 63, argument_list; 63, 64; 64, identifier:order; 65, return_statement; 65, 66; 66, subscript; 66, 67; 66, 68; 67, identifier:segments; 68, identifier:sort_order
def _process(self, segments): """sort segments in read order - left to right, up to down""" # sort_f= lambda r: max_line_width*(r[1]/max_line_height)+r[0] # segments= sorted(segments, key=sort_f) # segments= segments_to_numpy( segments ) # return segments mlh, mlw = self.max_line_height, self.max_line_width s = segments.astype(numpy.uint32) # prevent overflows order = mlw * (s[:, 1] // mlh) + s[:, 0] sort_order = numpy.argsort(order) return segments[sort_order]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:flatten; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:rho; 5, identifier:pval; 6, default_parameter; 6, 7; 6, 8; 7, identifier:sortby; 8, string:"cor"; 9, block; 9, 10; 9, 12; 9, 20; 9, 32; 9, 65; 9, 82; 9, 135; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:n; 15, subscript; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:rho; 18, identifier:shape; 19, integer:0; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:idx; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:np; 26, identifier:triu_indices; 27, argument_list; 27, 28; 27, 29; 28, identifier:n; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:k; 31, integer:1; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:tab; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:pd; 38, identifier:DataFrame; 39, argument_list; 39, 40; 39, 47; 40, keyword_argument; 40, 41; 40, 42; 41, identifier:columns; 42, list:['i', 'j', 'cor', 'pval']; 42, 43; 42, 44; 42, 45; 42, 46; 43, string:'i'; 44, string:'j'; 45, string:'cor'; 46, string:'pval'; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:data; 49, subscript; 49, 50; 49, 53; 49, 56; 49, 59; 49, 62; 50, attribute; 50, 51; 50, 52; 51, identifier:np; 52, identifier:c_; 53, subscript; 53, 54; 53, 55; 54, identifier:idx; 55, integer:0; 56, subscript; 56, 57; 56, 58; 57, identifier:idx; 58, integer:1; 59, subscript; 59, 60; 59, 61; 60, identifier:rho; 61, identifier:idx; 62, subscript; 62, 63; 62, 64; 63, identifier:pval; 64, identifier:idx; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 72; 67, subscript; 67, 68; 67, 69; 68, identifier:tab; 69, list:['i', "j"]; 69, 70; 69, 71; 70, string:'i'; 71, string:"j"; 72, call; 72, 73; 72, 80; 73, attribute; 73, 74; 73, 79; 74, subscript; 74, 75; 74, 76; 75, identifier:tab; 76, list:['i', "j"]; 76, 77; 76, 78; 77, string:'i'; 78, string:"j"; 79, identifier:astype; 80, argument_list; 80, 81; 81, identifier:int; 82, if_statement; 82, 83; 82, 86; 82, 115; 83, comparison_operator:==; 83, 84; 83, 85; 84, identifier:sortby; 85, string:"cor"; 86, block; 86, 87; 86, 100; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:tab; 91, string:'abscor'; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:abs; 96, argument_list; 96, 97; 97, subscript; 97, 98; 97, 99; 98, identifier:tab; 99, string:'cor'; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:tab; 104, identifier:sort_values; 105, argument_list; 105, 106; 105, 109; 105, 112; 106, keyword_argument; 106, 107; 106, 108; 107, identifier:by; 108, string:'abscor'; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:inplace; 111, True; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:ascending; 114, False; 115, elif_clause; 115, 116; 115, 119; 116, comparison_operator:==; 116, 117; 116, 118; 117, identifier:sortby; 118, string:"pval"; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:tab; 124, identifier:sort_values; 125, argument_list; 125, 126; 125, 129; 125, 132; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:by; 128, string:'pval'; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:inplace; 131, True; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:ascending; 134, True; 135, return_statement; 135, 136; 136, subscript; 136, 137; 136, 138; 137, identifier:tab; 138, list:["i", "j", "cor", "pval"]; 138, 139; 138, 140; 138, 141; 138, 142; 139, string:"i"; 140, string:"j"; 141, string:"cor"; 142, string:"pval"
def flatten(rho, pval, sortby="cor"): """Flatten correlation and p-value matrix Parameters: ----------- rho : ndarray Correlation Matrix pval : ndarray Matrix with p-values sortby : str sort the output table by - "cor" the highest absolute correlation coefficient - "pval" the lowest p-value Return: ------- tab : ndarray Table with (i, j, cor, pval) rows Example: -------- from korr import pearson, flatten rho, pval = pearson(X) tab = flatten(rho, pval, sortby="pval") tab.values """ n = rho.shape[0] idx = np.triu_indices(n, k=1) tab = pd.DataFrame( columns=['i', 'j', 'cor', 'pval'], data=np.c_[idx[0], idx[1], rho[idx], pval[idx]]) tab[['i', "j"]] = tab[['i', "j"]].astype(int) if sortby == "cor": tab['abscor'] = np.abs(tab['cor']) tab.sort_values(by='abscor', inplace=True, ascending=False) elif sortby == "pval": tab.sort_values(by='pval', inplace=True, ascending=True) return tab[["i", "j", "cor", "pval"]]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:has_code; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:state; 5, identifier:text; 6, default_parameter; 6, 7; 6, 8; 7, identifier:incorrect_msg; 8, string:"The checker expected to find `{{text}}` in your command."; 9, default_parameter; 9, 10; 9, 11; 10, identifier:fixed; 11, False; 12, block; 12, 13; 12, 15; 12, 21; 12, 22; 12, 37; 12, 63; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:stu_code; 18, attribute; 18, 19; 18, 20; 19, identifier:state; 20, identifier:student_code; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:res; 25, conditional_expression:if; 25, 26; 25, 29; 25, 30; 26, comparison_operator:in; 26, 27; 26, 28; 27, identifier:text; 28, identifier:stu_code; 29, identifier:fixed; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:re; 33, identifier:search; 34, argument_list; 34, 35; 34, 36; 35, identifier:text; 36, identifier:stu_code; 37, if_statement; 37, 38; 37, 40; 38, not_operator; 38, 39; 39, identifier:res; 40, block; 40, 41; 40, 56; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:_msg; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:state; 47, identifier:build_message; 48, argument_list; 48, 49; 48, 50; 49, identifier:incorrect_msg; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:fmt_kwargs; 52, dictionary; 52, 53; 53, pair; 53, 54; 53, 55; 54, string:'text'; 55, identifier:text; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:state; 60, identifier:do_test; 61, argument_list; 61, 62; 62, identifier:_msg; 63, return_statement; 63, 64; 64, identifier:state
def has_code(state, text, incorrect_msg="The checker expected to find `{{text}}` in your command.", fixed=False): """Check whether the student code contains text. This function is a simpler override of the `has_code` function in protowhat, because ``ast_node._get_text()`` is not implemented in the OSH parser Using ``has_code()`` should be a last resort. It is always better to look at the result of code or the side effects they had on the state of your program. Args: state: State instance describing student and solution code. Can be omitted if used with Ex(). text : text that student code must contain. Can be a regex pattern or a simple string. incorrect_msg: if specified, this overrides the automatically generated feedback message in case ``text`` is not found in the student code. fixed: whether to match ``text`` exactly, rather than using regular expressions. :Example: Suppose the solution requires you to do: :: git push origin master The following SCT can be written: :: Ex().has_code(r'git\\s+push\\s+origin\\s+master') Submissions that would pass: :: git push origin master git push origin master Submissions that would fail: :: git push --force origin master """ stu_code = state.student_code # either simple text matching or regex test res = text in stu_code if fixed else re.search(text, stu_code) if not res: _msg = state.build_message(incorrect_msg, fmt_kwargs={ 'text': text }) state.do_test(_msg) return state
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:topports; 3, parameters; 3, 4; 3, 7; 3, 10; 3, 13; 4, default_parameter; 4, 5; 4, 6; 5, identifier:sort_by; 6, string:'records'; 7, default_parameter; 7, 8; 7, 9; 8, identifier:limit; 9, integer:10; 10, default_parameter; 10, 11; 10, 12; 11, identifier:date; 12, None; 13, default_parameter; 13, 14; 13, 15; 14, identifier:return_format; 15, None; 16, block; 16, 17; 16, 19; 16, 34; 16, 69; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:uri; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, string:'/'; 25, identifier:join; 26, argument_list; 26, 27; 27, list:['topports', sort_by, str(limit)]; 27, 28; 27, 29; 27, 30; 28, string:'topports'; 29, identifier:sort_by; 30, call; 30, 31; 30, 32; 31, identifier:str; 32, argument_list; 32, 33; 33, identifier:limit; 34, if_statement; 34, 35; 34, 36; 35, identifier:date; 36, block; 36, 37; 37, try_statement; 37, 38; 37, 55; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:uri; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, string:'/'; 45, identifier:join; 46, argument_list; 46, 47; 47, list:[uri, date.strftime("%Y-%m-%d")]; 47, 48; 47, 49; 48, identifier:uri; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:date; 52, identifier:strftime; 53, argument_list; 53, 54; 54, string:"%Y-%m-%d"; 55, except_clause; 55, 56; 55, 57; 56, identifier:AttributeError; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:uri; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:'/'; 64, identifier:join; 65, argument_list; 65, 66; 66, list:[uri, date]; 66, 67; 66, 68; 67, identifier:uri; 68, identifier:date; 69, return_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:_get; 72, argument_list; 72, 73; 72, 74; 73, identifier:uri; 74, identifier:return_format
def topports(sort_by='records', limit=10, date=None, return_format=None): """Information about top ports for a particular date with return limit. :param sort_by: one of 'records', 'targets', 'sources' :param limit: number of records to be returned :param date: an optional string in 'Y-M-D' format or datetime.date() object """ uri = '/'.join(['topports', sort_by, str(limit)]) if date: try: uri = '/'.join([uri, date.strftime("%Y-%m-%d")]) except AttributeError: uri = '/'.join([uri, date]) return _get(uri, return_format)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:all_nodes_that_receive; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:service; 5, default_parameter; 5, 6; 5, 7; 6, identifier:service_configuration; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:run_only; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:deploy_to_only; 13, False; 14, block; 14, 15; 14, 17; 14, 23; 14, 34; 14, 42; 14, 53; 14, 62; 14, 90; 15, expression_statement; 15, 16; 16, comment; 17, assert_statement; 17, 18; 18, not_operator; 18, 19; 19, parenthesized_expression; 19, 20; 20, boolean_operator:and; 20, 21; 20, 22; 21, identifier:run_only; 22, identifier:deploy_to_only; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:service_configuration; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:service_configuration; 31, call; 31, 32; 31, 33; 32, identifier:read_services_configuration; 33, argument_list; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:runs_on; 37, subscript; 37, 38; 37, 41; 38, subscript; 38, 39; 38, 40; 39, identifier:service_configuration; 40, identifier:service; 41, string:'runs_on'; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:deployed_to; 45, call; 45, 46; 45, 51; 46, attribute; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:service_configuration; 49, identifier:service; 50, identifier:get; 51, argument_list; 51, 52; 52, string:'deployed_to'; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:deployed_to; 56, None; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:deployed_to; 61, list:[]; 62, if_statement; 62, 63; 62, 64; 62, 69; 62, 76; 63, identifier:run_only; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:result; 68, identifier:runs_on; 69, elif_clause; 69, 70; 69, 71; 70, identifier:deploy_to_only; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:result; 75, identifier:deployed_to; 76, else_clause; 76, 77; 77, block; 77, 78; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:result; 81, binary_operator:|; 81, 82; 81, 86; 82, call; 82, 83; 82, 84; 83, identifier:set; 84, argument_list; 84, 85; 85, identifier:runs_on; 86, call; 86, 87; 86, 88; 87, identifier:set; 88, argument_list; 88, 89; 89, identifier:deployed_to; 90, return_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:list; 93, argument_list; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:sorted; 96, argument_list; 96, 97; 97, identifier:result
def all_nodes_that_receive(service, service_configuration=None, run_only=False, deploy_to_only=False): """If run_only, returns only the services that are in the runs_on list. If deploy_to_only, returns only the services in the deployed_to list. If neither, both are returned, duplicates stripped. Results are always sorted. """ assert not (run_only and deploy_to_only) if service_configuration is None: service_configuration = read_services_configuration() runs_on = service_configuration[service]['runs_on'] deployed_to = service_configuration[service].get('deployed_to') if deployed_to is None: deployed_to = [] if run_only: result = runs_on elif deploy_to_only: result = deployed_to else: result = set(runs_on) | set(deployed_to) return list(sorted(result))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_cuthill_mckee; 3, parameters; 3, 4; 3, 5; 4, identifier:vertices; 5, identifier:vertices_neighbours; 6, block; 6, 7; 6, 9; 6, 27; 6, 43; 6, 51; 6, 56; 6, 64; 6, 131; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:vertices_degrees; 12, dictionary_comprehension; 12, 13; 12, 24; 13, pair; 13, 14; 13, 15; 14, identifier:v; 15, call; 15, 16; 15, 17; 16, identifier:sum; 17, argument_list; 17, 18; 18, call; 18, 19; 18, 20; 19, identifier:itervalues; 20, argument_list; 20, 21; 21, subscript; 21, 22; 21, 23; 22, identifier:vertices_neighbours; 23, identifier:v; 24, for_in_clause; 24, 25; 24, 26; 25, identifier:v; 26, identifier:vertices; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:peripheral_vertex; 30, call; 30, 31; 30, 32; 31, identifier:min; 32, argument_list; 32, 33; 32, 34; 33, identifier:vertices; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:key; 36, parenthesized_expression; 36, 37; 37, lambda; 37, 38; 37, 40; 38, lambda_parameters; 38, 39; 39, identifier:v; 40, subscript; 40, 41; 40, 42; 41, identifier:vertices_degrees; 42, identifier:v; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:visited; 46, call; 46, 47; 46, 48; 47, identifier:set; 48, argument_list; 48, 49; 49, list:[peripheral_vertex]; 49, 50; 50, identifier:peripheral_vertex; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:cm_order; 54, list:[peripheral_vertex]; 54, 55; 55, identifier:peripheral_vertex; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:previous_layer; 59, call; 59, 60; 59, 61; 60, identifier:set; 61, argument_list; 61, 62; 62, list:[peripheral_vertex]; 62, 63; 63, identifier:peripheral_vertex; 64, while_statement; 64, 65; 64, 74; 65, comparison_operator:<; 65, 66; 65, 70; 66, call; 66, 67; 66, 68; 67, identifier:len; 68, argument_list; 68, 69; 69, identifier:cm_order; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:vertices; 74, block; 74, 75; 74, 81; 74, 94; 74, 101; 74, 108; 74, 127; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:adjacent; 78, call; 78, 79; 78, 80; 79, identifier:set; 80, argument_list; 81, for_statement; 81, 82; 81, 83; 81, 84; 82, identifier:vertex; 83, identifier:previous_layer; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:adjacent; 89, identifier:update; 90, argument_list; 90, 91; 91, subscript; 91, 92; 91, 93; 92, identifier:vertices_neighbours; 93, identifier:vertex; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:adjacent; 98, identifier:difference_update; 99, argument_list; 99, 100; 100, identifier:visited; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:visited; 105, identifier:update; 106, argument_list; 106, 107; 107, identifier:adjacent; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:cm_order; 112, identifier:extend; 113, argument_list; 113, 114; 114, call; 114, 115; 114, 116; 115, identifier:sorted; 116, argument_list; 116, 117; 116, 118; 117, identifier:adjacent; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:key; 120, parenthesized_expression; 120, 121; 121, lambda; 121, 122; 121, 124; 122, lambda_parameters; 122, 123; 123, identifier:v; 124, subscript; 124, 125; 124, 126; 125, identifier:vertices_degrees; 126, identifier:v; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:previous_layer; 130, identifier:adjacent; 131, return_statement; 131, 132; 132, identifier:cm_order
def _cuthill_mckee(vertices, vertices_neighbours): """Yield the Cuthill-McKee order for a connected, undirected graph. `Wikipedia <https://en.wikipedia.org/wiki/Cuthill%E2%80%93McKee_algorithm>`_ provides a good introduction to the Cuthill-McKee algorithm. The RCM algorithm attempts to order vertices in a graph such that their adjacency matrix's bandwidth is reduced. In brief the RCM algorithm is a breadth-first search with the following tweaks: * The search starts from the vertex with the lowest degree. * Vertices discovered in each layer of the search are sorted by ascending order of their degree in the output. .. warning:: This function must not be called on a disconnected or empty graph. Returns ------- [vertex, ...] """ vertices_degrees = {v: sum(itervalues(vertices_neighbours[v])) for v in vertices} peripheral_vertex = min(vertices, key=(lambda v: vertices_degrees[v])) visited = set([peripheral_vertex]) cm_order = [peripheral_vertex] previous_layer = set([peripheral_vertex]) while len(cm_order) < len(vertices): adjacent = set() for vertex in previous_layer: adjacent.update(vertices_neighbours[vertex]) adjacent.difference_update(visited) visited.update(adjacent) cm_order.extend(sorted(adjacent, key=(lambda v: vertices_degrees[v]))) previous_layer = adjacent return cm_order
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:write_csv; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:path; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 26; 8, 27; 8, 28; 8, 41; 8, 54; 8, 65; 8, 73; 8, 79; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:sort_sections; 17, argument_list; 17, 18; 18, list:['Root', 'Contacts', 'Documentation', 'References', 'Resources', 'Citations', 'Schema']; 18, 19; 18, 20; 18, 21; 18, 22; 18, 23; 18, 24; 18, 25; 19, string:'Root'; 20, string:'Contacts'; 21, string:'Documentation'; 22, string:'References'; 23, string:'Resources'; 24, string:'Citations'; 25, string:'Schema'; 26, comment; 27, comment; 28, if_statement; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:description; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:description; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:description; 41, if_statement; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:abstract; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:description; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:abstract; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:t; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:self; 61, string:'Root'; 62, identifier:get_or_new_term; 63, argument_list; 63, 64; 64, string:'Root.Modified'; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:t; 69, identifier:value; 70, call; 70, 71; 70, 72; 71, identifier:datetime_now; 72, argument_list; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:sort_by_term; 78, argument_list; 79, return_statement; 79, 80; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, call; 82, 83; 82, 84; 83, identifier:super; 84, argument_list; 85, identifier:write_csv; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:str; 89, argument_list; 89, 90; 90, identifier:path
def write_csv(self, path=None): """Write CSV file. Sorts the sections before calling the superclass write_csv""" # Sort the Sections self.sort_sections(['Root', 'Contacts', 'Documentation', 'References', 'Resources', 'Citations', 'Schema']) # Sort Terms in the root section # Re-wrap the description and abstract if self.description: self.description = self.description if self.abstract: self.description = self.abstract t = self['Root'].get_or_new_term('Root.Modified') t.value = datetime_now() self.sort_by_term() return super().write_csv(str(path))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:validate_functions; 3, parameters; 3, 4; 3, 8; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:ast; 6, type; 6, 7; 7, identifier:BELAst; 8, identifier:bo; 9, block; 9, 10; 9, 12; 9, 101; 9, 102; 9, 121; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 18; 13, call; 13, 14; 13, 15; 14, identifier:isinstance; 15, argument_list; 15, 16; 15, 17; 16, identifier:ast; 17, identifier:Function; 18, block; 18, 19; 18, 26; 18, 42; 18, 48; 18, 61; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:log; 23, identifier:debug; 24, argument_list; 24, 25; 25, string:f"Validating: {ast.name}, {ast.function_type}, {ast.args}"; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:function_signatures; 29, subscript; 29, 30; 29, 41; 30, subscript; 30, 31; 30, 38; 31, subscript; 31, 32; 31, 37; 32, subscript; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:bo; 35, identifier:spec; 36, string:"functions"; 37, string:"signatures"; 38, attribute; 38, 39; 38, 40; 39, identifier:ast; 40, identifier:name; 41, string:"signatures"; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:function_name; 45, attribute; 45, 46; 45, 47; 46, identifier:ast; 47, identifier:name; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, tuple_pattern; 50, 51; 50, 52; 51, identifier:valid_function; 52, identifier:messages; 53, call; 53, 54; 53, 55; 54, identifier:check_function_args; 55, argument_list; 55, 56; 55, 59; 55, 60; 56, attribute; 56, 57; 56, 58; 57, identifier:ast; 58, identifier:args; 59, identifier:function_signatures; 60, identifier:function_name; 61, if_statement; 61, 62; 61, 64; 62, not_operator; 62, 63; 63, identifier:valid_function; 64, block; 64, 65; 64, 74; 64, 95; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:message; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, string:", "; 71, identifier:join; 72, argument_list; 72, 73; 73, identifier:messages; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:bo; 79, identifier:validation_messages; 80, identifier:append; 81, argument_list; 81, 82; 82, tuple; 82, 83; 82, 84; 83, string:"ERROR"; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, string:"Invalid BEL Statement function {} - problem with function signatures: {}"; 87, identifier:format; 88, argument_list; 88, 89; 88, 94; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:ast; 92, identifier:to_string; 93, argument_list; 94, identifier:message; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:bo; 99, identifier:parse_valid; 100, False; 101, comment; 102, if_statement; 102, 103; 102, 108; 103, call; 103, 104; 103, 105; 104, identifier:hasattr; 105, argument_list; 105, 106; 105, 107; 106, identifier:ast; 107, string:"args"; 108, block; 108, 109; 109, for_statement; 109, 110; 109, 111; 109, 114; 110, identifier:arg; 111, attribute; 111, 112; 111, 113; 112, identifier:ast; 113, identifier:args; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 118; 117, identifier:validate_functions; 118, argument_list; 118, 119; 118, 120; 119, identifier:arg; 120, identifier:bo; 121, return_statement; 121, 122; 122, identifier:bo
def validate_functions(ast: BELAst, bo): """Recursively validate function signatures Determine if function matches one of the available signatures. Also, 1. Add entity types to AST NSArg, e.g. Abundance, ... 2. Add optional to AST Arg (optional means it is not a fixed, required argument and needs to be sorted for canonicalization, e.g. reactants(A, B, C) ) Args: bo: bel object Returns: bel object """ if isinstance(ast, Function): log.debug(f"Validating: {ast.name}, {ast.function_type}, {ast.args}") function_signatures = bo.spec["functions"]["signatures"][ast.name]["signatures"] function_name = ast.name (valid_function, messages) = check_function_args( ast.args, function_signatures, function_name ) if not valid_function: message = ", ".join(messages) bo.validation_messages.append( ( "ERROR", "Invalid BEL Statement function {} - problem with function signatures: {}".format( ast.to_string(), message ), ) ) bo.parse_valid = False # Recursively process every NSArg by processing BELAst and Functions if hasattr(ast, "args"): for arg in ast.args: validate_functions(arg, bo) return bo
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 1, 16; 2, function_name:hash_nanopub; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:nanopub; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:Mapping; 9, type_parameter; 9, 10; 9, 12; 10, type; 10, 11; 11, identifier:str; 12, type; 12, 13; 13, identifier:Any; 14, type; 14, 15; 15, identifier:str; 16, block; 16, 17; 16, 19; 16, 23; 16, 24; 16, 45; 16, 66; 16, 67; 16, 194; 16, 195; 16, 199; 16, 284; 16, 291; 16, 298; 16, 299; 16, 303; 16, 349; 16, 356; 16, 363; 16, 380; 17, expression_statement; 17, 18; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:hash_list; 22, list:[]; 23, comment; 24, expression_statement; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:hash_list; 28, identifier:append; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 44; 31, attribute; 31, 32; 31, 43; 32, call; 32, 33; 32, 40; 33, attribute; 33, 34; 33, 39; 34, subscript; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:nanopub; 37, string:"nanopub"; 38, string:"type"; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, string:"name"; 42, string:""; 43, identifier:strip; 44, argument_list; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:hash_list; 49, identifier:append; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 65; 52, attribute; 52, 53; 52, 64; 53, call; 53, 54; 53, 61; 54, attribute; 54, 55; 54, 60; 55, subscript; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:nanopub; 58, string:"nanopub"; 59, string:"type"; 60, identifier:get; 61, argument_list; 61, 62; 61, 63; 62, string:"version"; 63, string:""; 64, identifier:strip; 65, argument_list; 66, comment; 67, if_statement; 67, 68; 67, 79; 67, 126; 67, 160; 68, call; 68, 69; 68, 76; 69, attribute; 69, 70; 69, 75; 70, subscript; 70, 71; 70, 74; 71, subscript; 71, 72; 71, 73; 72, identifier:nanopub; 73, string:"nanopub"; 74, string:"citation"; 75, identifier:get; 76, argument_list; 76, 77; 76, 78; 77, string:"database"; 78, False; 79, block; 79, 80; 79, 103; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:hash_list; 84, identifier:append; 85, argument_list; 85, 86; 86, call; 86, 87; 86, 102; 87, attribute; 87, 88; 87, 101; 88, call; 88, 89; 88, 98; 89, attribute; 89, 90; 89, 97; 90, subscript; 90, 91; 90, 96; 91, subscript; 91, 92; 91, 95; 92, subscript; 92, 93; 92, 94; 93, identifier:nanopub; 94, string:"nanopub"; 95, string:"citation"; 96, string:"database"; 97, identifier:get; 98, argument_list; 98, 99; 98, 100; 99, string:"name"; 100, string:""; 101, identifier:strip; 102, argument_list; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:hash_list; 107, identifier:append; 108, argument_list; 108, 109; 109, call; 109, 110; 109, 125; 110, attribute; 110, 111; 110, 124; 111, call; 111, 112; 111, 121; 112, attribute; 112, 113; 112, 120; 113, subscript; 113, 114; 113, 119; 114, subscript; 114, 115; 114, 118; 115, subscript; 115, 116; 115, 117; 116, identifier:nanopub; 117, string:"nanopub"; 118, string:"citation"; 119, string:"database"; 120, identifier:get; 121, argument_list; 121, 122; 121, 123; 122, string:"id"; 123, string:""; 124, identifier:strip; 125, argument_list; 126, elif_clause; 126, 127; 126, 138; 127, call; 127, 128; 127, 135; 128, attribute; 128, 129; 128, 134; 129, subscript; 129, 130; 129, 133; 130, subscript; 130, 131; 130, 132; 131, identifier:nanopub; 132, string:"nanopub"; 133, string:"citation"; 134, identifier:get; 135, argument_list; 135, 136; 135, 137; 136, string:"uri"; 137, False; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:hash_list; 143, identifier:append; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 159; 146, attribute; 146, 147; 146, 158; 147, call; 147, 148; 147, 155; 148, attribute; 148, 149; 148, 154; 149, subscript; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:nanopub; 152, string:"nanopub"; 153, string:"citation"; 154, identifier:get; 155, argument_list; 155, 156; 155, 157; 156, string:"uri"; 157, string:""; 158, identifier:strip; 159, argument_list; 160, elif_clause; 160, 161; 160, 172; 161, call; 161, 162; 161, 169; 162, attribute; 162, 163; 162, 168; 163, subscript; 163, 164; 163, 167; 164, subscript; 164, 165; 164, 166; 165, identifier:nanopub; 166, string:"nanopub"; 167, string:"citation"; 168, identifier:get; 169, argument_list; 169, 170; 169, 171; 170, string:"reference"; 171, False; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:hash_list; 177, identifier:append; 178, argument_list; 178, 179; 179, call; 179, 180; 179, 193; 180, attribute; 180, 181; 180, 192; 181, call; 181, 182; 181, 189; 182, attribute; 182, 183; 182, 188; 183, subscript; 183, 184; 183, 187; 184, subscript; 184, 185; 184, 186; 185, identifier:nanopub; 186, string:"nanopub"; 187, string:"citation"; 188, identifier:get; 189, argument_list; 189, 190; 189, 191; 190, string:"reference"; 191, string:""; 192, identifier:strip; 193, argument_list; 194, comment; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:assertions; 198, list:[]; 199, for_statement; 199, 200; 199, 201; 199, 206; 200, identifier:assertion; 201, subscript; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:nanopub; 204, string:"nanopub"; 205, string:"assertions"; 206, block; 206, 207; 206, 223; 206, 239; 207, if_statement; 207, 208; 207, 216; 208, comparison_operator:is; 208, 209; 208, 215; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:assertion; 212, identifier:get; 213, argument_list; 213, 214; 214, string:"relation"; 215, None; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 222; 219, subscript; 219, 220; 219, 221; 220, identifier:assertion; 221, string:"relation"; 222, string:""; 223, if_statement; 223, 224; 223, 232; 224, comparison_operator:is; 224, 225; 224, 231; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:assertion; 228, identifier:get; 229, argument_list; 229, 230; 230, string:"object"; 231, None; 232, block; 232, 233; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 238; 235, subscript; 235, 236; 235, 237; 236, identifier:assertion; 237, string:"object"; 238, string:""; 239, expression_statement; 239, 240; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:assertions; 243, identifier:append; 244, argument_list; 244, 245; 245, call; 245, 246; 245, 283; 246, attribute; 246, 247; 246, 282; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, string:" "; 250, identifier:join; 251, argument_list; 251, 252; 252, tuple; 252, 253; 252, 260; 252, 271; 253, call; 253, 254; 253, 259; 254, attribute; 254, 255; 254, 258; 255, subscript; 255, 256; 255, 257; 256, identifier:assertion; 257, string:"subject"; 258, identifier:strip; 259, argument_list; 260, call; 260, 261; 260, 270; 261, attribute; 261, 262; 261, 269; 262, call; 262, 263; 262, 266; 263, attribute; 263, 264; 263, 265; 264, identifier:assertion; 265, identifier:get; 266, argument_list; 266, 267; 266, 268; 267, string:"relation"; 268, string:""; 269, identifier:strip; 270, argument_list; 271, call; 271, 272; 271, 281; 272, attribute; 272, 273; 272, 280; 273, call; 273, 274; 273, 277; 274, attribute; 274, 275; 274, 276; 275, identifier:assertion; 276, identifier:get; 277, argument_list; 277, 278; 277, 279; 278, string:"object"; 279, string:""; 280, identifier:strip; 281, argument_list; 282, identifier:strip; 283, argument_list; 284, expression_statement; 284, 285; 285, assignment; 285, 286; 285, 287; 286, identifier:assertions; 287, call; 287, 288; 287, 289; 288, identifier:sorted; 289, argument_list; 289, 290; 290, identifier:assertions; 291, expression_statement; 291, 292; 292, call; 292, 293; 292, 296; 293, attribute; 293, 294; 293, 295; 294, identifier:hash_list; 295, identifier:extend; 296, argument_list; 296, 297; 297, identifier:assertions; 298, comment; 299, expression_statement; 299, 300; 300, assignment; 300, 301; 300, 302; 301, identifier:annotations; 302, list:[]; 303, for_statement; 303, 304; 303, 305; 303, 310; 304, identifier:anno; 305, subscript; 305, 306; 305, 309; 306, subscript; 306, 307; 306, 308; 307, identifier:nanopub; 308, string:"nanopub"; 309, string:"annotations"; 310, block; 310, 311; 311, expression_statement; 311, 312; 312, call; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:annotations; 315, identifier:append; 316, argument_list; 316, 317; 317, call; 317, 318; 317, 348; 318, attribute; 318, 319; 318, 347; 319, call; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, string:" "; 322, identifier:join; 323, argument_list; 323, 324; 324, tuple; 324, 325; 324, 336; 325, call; 325, 326; 325, 335; 326, attribute; 326, 327; 326, 334; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:anno; 330, identifier:get; 331, argument_list; 331, 332; 331, 333; 332, string:"type"; 333, string:""; 334, identifier:strip; 335, argument_list; 336, call; 336, 337; 336, 346; 337, attribute; 337, 338; 337, 345; 338, call; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:anno; 341, identifier:get; 342, argument_list; 342, 343; 342, 344; 343, string:"id"; 344, string:""; 345, identifier:strip; 346, argument_list; 347, identifier:strip; 348, argument_list; 349, expression_statement; 349, 350; 350, assignment; 350, 351; 350, 352; 351, identifier:annotations; 352, call; 352, 353; 352, 354; 353, identifier:sorted; 354, argument_list; 354, 355; 355, identifier:annotations; 356, expression_statement; 356, 357; 357, call; 357, 358; 357, 361; 358, attribute; 358, 359; 358, 360; 359, identifier:hash_list; 360, identifier:extend; 361, argument_list; 361, 362; 362, identifier:annotations; 363, expression_statement; 363, 364; 364, assignment; 364, 365; 364, 366; 365, identifier:np_string; 366, call; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, string:" "; 369, identifier:join; 370, argument_list; 370, 371; 371, list_comprehension; 371, 372; 371, 377; 372, call; 372, 373; 372, 376; 373, attribute; 373, 374; 373, 375; 374, identifier:l; 375, identifier:lower; 376, argument_list; 377, for_in_clause; 377, 378; 377, 379; 378, identifier:l; 379, identifier:hash_list; 380, return_statement; 380, 381; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, string:"{:x}"; 384, identifier:format; 385, argument_list; 385, 386; 386, call; 386, 387; 386, 388; 387, identifier:CityHash64; 388, argument_list; 388, 389; 389, identifier:np_string
def hash_nanopub(nanopub: Mapping[str, Any]) -> str: """Create CityHash64 from nanopub for duplicate check TODO - check that this hash value is consistent between C# and Python running on laptop and server Build string to hash Collect flat array of (all values.strip()): nanopub.type.name nanopub.type.version One of: nanopub.citation.database.name nanopub.citation.database.id OR nanopub.citation.database.uri OR nanopub.citation.database.reference Extend with sorted list of assertions (SRO as single string with space between S, R and O) Extend with sorted list of annotations (nanopub.annotations.type + ' ' + nanopub.annotations.id) Convert array to string by joining array elements separated by a space Create CityHash64(str) and return """ hash_list = [] # Type hash_list.append(nanopub["nanopub"]["type"].get("name", "").strip()) hash_list.append(nanopub["nanopub"]["type"].get("version", "").strip()) # Citation if nanopub["nanopub"]["citation"].get("database", False): hash_list.append( nanopub["nanopub"]["citation"]["database"].get("name", "").strip() ) hash_list.append( nanopub["nanopub"]["citation"]["database"].get("id", "").strip() ) elif nanopub["nanopub"]["citation"].get("uri", False): hash_list.append(nanopub["nanopub"]["citation"].get("uri", "").strip()) elif nanopub["nanopub"]["citation"].get("reference", False): hash_list.append(nanopub["nanopub"]["citation"].get("reference", "").strip()) # Assertions assertions = [] for assertion in nanopub["nanopub"]["assertions"]: if assertion.get("relation") is None: assertion["relation"] = "" if assertion.get("object") is None: assertion["object"] = "" assertions.append( " ".join( ( assertion["subject"].strip(), assertion.get("relation", "").strip(), assertion.get("object", "").strip(), ) ).strip() ) assertions = sorted(assertions) hash_list.extend(assertions) # Annotations annotations = [] for anno in nanopub["nanopub"]["annotations"]: annotations.append( " ".join((anno.get("type", "").strip(), anno.get("id", "").strip())).strip() ) annotations = sorted(annotations) hash_list.extend(annotations) np_string = " ".join([l.lower() for l in hash_list]) return "{:x}".format(CityHash64(np_string))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:build_listing; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 101; 5, 121; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 13; 9, function_name:func_entry; 10, parameters; 10, 11; 10, 12; 11, identifier:name; 12, identifier:func; 13, block; 13, 14; 13, 26; 13, 27; 13, 53; 13, 54; 13, 74; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 20; 16, pattern_list; 16, 17; 16, 18; 16, 19; 17, identifier:args; 18, identifier:varargs; 19, identifier:defaults; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_get_arg_spec; 24, argument_list; 24, 25; 25, identifier:func; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:params; 30, list_comprehension; 30, 31; 30, 46; 30, 49; 31, dictionary; 31, 32; 31, 38; 31, 43; 32, pair; 32, 33; 32, 34; 33, string:'name'; 34, call; 34, 35; 34, 36; 35, identifier:str; 36, argument_list; 36, 37; 37, identifier:a; 38, pair; 38, 39; 38, 40; 39, string:'optional'; 40, comparison_operator:in; 40, 41; 40, 42; 41, identifier:a; 42, identifier:defaults; 43, pair; 43, 44; 43, 45; 44, string:'vararg'; 45, False; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:a; 48, identifier:args; 49, if_clause; 49, 50; 50, comparison_operator:!=; 50, 51; 50, 52; 51, identifier:a; 52, string:'ctx'; 53, comment; 54, if_statement; 54, 55; 54, 56; 55, identifier:varargs; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, augmented_assignment:+=; 58, 59; 58, 60; 59, identifier:params; 60, list:[{'name': str(varargs), 'optional': False, 'vararg': True}]; 60, 61; 61, dictionary; 61, 62; 61, 68; 61, 71; 62, pair; 62, 63; 62, 64; 63, string:'name'; 64, call; 64, 65; 64, 66; 65, identifier:str; 66, argument_list; 66, 67; 67, identifier:varargs; 68, pair; 68, 69; 68, 70; 69, string:'optional'; 70, False; 71, pair; 71, 72; 71, 73; 72, string:'vararg'; 73, True; 74, return_statement; 74, 75; 75, dictionary; 75, 76; 75, 86; 75, 98; 76, pair; 76, 77; 76, 78; 77, string:'name'; 78, call; 78, 79; 78, 80; 79, identifier:str; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:name; 84, identifier:upper; 85, argument_list; 86, pair; 86, 87; 86, 88; 87, string:'description'; 88, call; 88, 89; 88, 97; 89, attribute; 89, 90; 89, 96; 90, call; 90, 91; 90, 92; 91, identifier:str; 92, argument_list; 92, 93; 93, attribute; 93, 94; 93, 95; 94, identifier:func; 95, identifier:__doc__; 96, identifier:strip; 97, argument_list; 98, pair; 98, 99; 98, 100; 99, string:'params'; 100, identifier:params; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:listing; 104, list_comprehension; 104, 105; 104, 110; 105, call; 105, 106; 105, 107; 106, identifier:func_entry; 107, argument_list; 107, 108; 107, 109; 108, identifier:f_name; 109, identifier:f; 110, for_in_clause; 110, 111; 110, 114; 111, pattern_list; 111, 112; 111, 113; 112, identifier:f_name; 113, identifier:f; 114, call; 114, 115; 114, 120; 115, attribute; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:_functions; 119, identifier:items; 120, argument_list; 121, return_statement; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:sorted; 124, argument_list; 124, 125; 124, 126; 125, identifier:listing; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:key; 128, lambda; 128, 129; 128, 131; 129, lambda_parameters; 129, 130; 130, identifier:l; 131, subscript; 131, 132; 131, 133; 132, identifier:l; 133, string:'name'
def build_listing(self): """ Builds a listing of all functions sorted A-Z, with their names and descriptions """ def func_entry(name, func): args, varargs, defaults = self._get_arg_spec(func) # add regular arguments params = [{'name': str(a), 'optional': a in defaults, 'vararg': False} for a in args if a != 'ctx'] # add possible variable argument if varargs: params += [{'name': str(varargs), 'optional': False, 'vararg': True}] return {'name': str(name.upper()), 'description': str(func.__doc__).strip(), 'params': params} listing = [func_entry(f_name, f) for f_name, f in self._functions.items()] return sorted(listing, key=lambda l: l['name'])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_url_qsl; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:cls; 5, identifier:raw_url; 6, dictionary_splat_pattern; 6, 7; 7, identifier:kwargs; 8, block; 8, 9; 8, 11; 8, 18; 8, 27; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:parsed_url; 14, call; 14, 15; 14, 16; 15, identifier:urlparse; 16, argument_list; 16, 17; 17, identifier:raw_url; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:qsl; 21, call; 21, 22; 21, 23; 22, identifier:parse_qsl; 23, argument_list; 23, 24; 24, attribute; 24, 25; 24, 26; 25, identifier:parsed_url; 26, identifier:query; 27, return_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:cls; 31, identifier:_join_url; 32, argument_list; 32, 33; 32, 34; 33, identifier:parsed_url; 34, call; 34, 35; 34, 36; 35, identifier:sorted; 36, argument_list; 36, 37; 36, 38; 37, identifier:qsl; 38, dictionary_splat; 38, 39; 39, identifier:kwargs
def sort_url_qsl(cls, raw_url, **kwargs): """Do nothing but sort the params of url. raw_url: the raw url to be sorted; kwargs: (optional) same kwargs for ``sorted``. """ parsed_url = urlparse(raw_url) qsl = parse_qsl(parsed_url.query) return cls._join_url(parsed_url, sorted(qsl, **kwargs))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:get_sorted_series_files; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:startpath; 7, string:""; 8, default_parameter; 8, 9; 8, 10; 9, identifier:series_number; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:return_files_with_info; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort_keys; 16, string:"SliceLocation"; 17, default_parameter; 17, 18; 17, 19; 18, identifier:return_files; 19, True; 20, default_parameter; 20, 21; 20, 22; 21, identifier:remove_doubled_slice_locations; 22, True; 23, block; 23, 24; 23, 26; 23, 35; 23, 36; 23, 55; 23, 65; 23, 77; 23, 89; 23, 93; 23, 118; 23, 122; 23, 132; 23, 142; 23, 177; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:dcmdir; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:files_with_info; 33, slice; 33, 34; 34, colon; 35, comment; 36, if_statement; 36, 37; 36, 40; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:series_number; 39, None; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:dcmdir; 44, list_comprehension; 44, 45; 44, 46; 44, 49; 45, identifier:line; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:line; 48, identifier:dcmdir; 49, if_clause; 49, 50; 50, comparison_operator:==; 50, 51; 50, 54; 51, subscript; 51, 52; 51, 53; 52, identifier:line; 53, string:'SeriesNumber'; 54, identifier:series_number; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:dcmdir; 58, call; 58, 59; 58, 60; 59, identifier:sort_list_of_dicts; 60, argument_list; 60, 61; 60, 62; 61, identifier:dcmdir; 62, keyword_argument; 62, 63; 62, 64; 63, identifier:keys; 64, identifier:sort_keys; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:logger; 69, identifier:debug; 70, argument_list; 70, 71; 71, binary_operator:+; 71, 72; 71, 73; 72, string:'SeriesNumber: '; 73, call; 73, 74; 73, 75; 74, identifier:str; 75, argument_list; 75, 76; 76, identifier:series_number; 77, if_statement; 77, 78; 77, 79; 78, identifier:remove_doubled_slice_locations; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:dcmdir; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:_remove_doubled_slice_locations; 87, argument_list; 87, 88; 88, identifier:dcmdir; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:filelist; 92, list:[]; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:onefile; 95, identifier:dcmdir; 96, block; 96, 97; 96, 117; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:filelist; 101, identifier:append; 102, argument_list; 102, 103; 103, call; 103, 104; 103, 109; 104, attribute; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:os; 107, identifier:path; 108, identifier:join; 109, argument_list; 109, 110; 109, 111; 109, 114; 110, identifier:startpath; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:dirpath; 114, subscript; 114, 115; 114, 116; 115, identifier:onefile; 116, string:'filename'; 117, comment; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:retval; 121, list:[]; 122, if_statement; 122, 123; 122, 124; 123, identifier:return_files; 124, block; 124, 125; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:retval; 129, identifier:append; 130, argument_list; 130, 131; 131, identifier:filelist; 132, if_statement; 132, 133; 132, 134; 133, identifier:return_files_with_info; 134, block; 134, 135; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:retval; 139, identifier:append; 140, argument_list; 140, 141; 141, identifier:dcmdir; 142, if_statement; 142, 143; 142, 149; 142, 154; 142, 168; 143, comparison_operator:==; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:retval; 148, integer:0; 149, block; 149, 150; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:retval; 153, None; 154, elif_clause; 154, 155; 154, 161; 155, comparison_operator:==; 155, 156; 155, 160; 156, call; 156, 157; 156, 158; 157, identifier:len; 158, argument_list; 158, 159; 159, identifier:retval; 160, integer:1; 161, block; 161, 162; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 165; 164, identifier:retval; 165, subscript; 165, 166; 165, 167; 166, identifier:retval; 167, integer:0; 168, else_clause; 168, 169; 169, block; 169, 170; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:retval; 173, call; 173, 174; 173, 175; 174, identifier:tuple; 175, argument_list; 175, 176; 176, identifier:retval; 177, return_statement; 177, 178; 178, identifier:retval
def get_sorted_series_files(self, startpath="", series_number=None, return_files_with_info=False, sort_keys="SliceLocation", return_files=True, remove_doubled_slice_locations=True): """ Function returns sorted list of dicom files. File paths are organized by SeriesUID, StudyUID and FrameUID :param startpath: path prefix. E.g. "~/data" :param series_number: ID of series used for filtering the data :param return_files_with_info: return more complex information about sorted files :param return_files: return simple list of sorted files :type sort_keys: One key or list of keys used for sorting method by the order of keys. """ dcmdir = self.files_with_info[:] # select sublist with SeriesNumber if series_number is not None: dcmdir = [ line for line in dcmdir if line['SeriesNumber'] == series_number ] dcmdir = sort_list_of_dicts(dcmdir, keys=sort_keys) logger.debug('SeriesNumber: ' + str(series_number)) if remove_doubled_slice_locations: dcmdir = self._remove_doubled_slice_locations(dcmdir) filelist = [] for onefile in dcmdir: filelist.append(os.path.join(startpath, self.dirpath, onefile['filename'])) # head, tail = os.path.split(onefile['filename']) retval = [] if return_files: retval.append(filelist) if return_files_with_info: retval.append(dcmdir) if len(retval) == 0: retval = None elif len(retval) == 1: retval = retval[0] else: retval = tuple(retval) return retval
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:guess_interval; 3, parameters; 3, 4; 3, 5; 4, identifier:nums; 5, default_parameter; 5, 6; 5, 7; 6, identifier:accuracy; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 17; 8, 31; 8, 43; 8, 67; 8, 79; 8, 86; 8, 97; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 14; 12, not_operator; 12, 13; 13, identifier:nums; 14, block; 14, 15; 15, return_statement; 15, 16; 16, integer:0; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:nums; 20, call; 20, 21; 20, 22; 21, identifier:sorted; 22, argument_list; 22, 23; 23, list_comprehension; 23, 24; 23, 28; 24, call; 24, 25; 24, 26; 25, identifier:int; 26, argument_list; 26, 27; 27, identifier:i; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:i; 30, identifier:nums; 31, if_statement; 31, 32; 31, 38; 32, comparison_operator:==; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:len; 35, argument_list; 35, 36; 36, identifier:nums; 37, integer:1; 38, block; 38, 39; 39, return_statement; 39, 40; 40, subscript; 40, 41; 40, 42; 41, identifier:nums; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:diffs; 46, list_comprehension; 46, 47; 46, 56; 47, binary_operator:-; 47, 48; 47, 53; 48, subscript; 48, 49; 48, 50; 49, identifier:nums; 50, binary_operator:+; 50, 51; 50, 52; 51, identifier:i; 52, integer:1; 53, subscript; 53, 54; 53, 55; 54, identifier:nums; 55, identifier:i; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:i; 58, call; 58, 59; 58, 60; 59, identifier:range; 60, argument_list; 60, 61; 61, binary_operator:-; 61, 62; 61, 66; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:nums; 66, integer:1; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:diffs; 70, list_comprehension; 70, 71; 70, 72; 70, 75; 71, identifier:item; 72, for_in_clause; 72, 73; 72, 74; 73, identifier:item; 74, identifier:diffs; 75, if_clause; 75, 76; 76, comparison_operator:>=; 76, 77; 76, 78; 77, identifier:item; 78, identifier:accuracy; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:sorted_diff; 82, call; 82, 83; 82, 84; 83, identifier:sorted; 84, argument_list; 84, 85; 85, identifier:diffs; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:result; 89, subscript; 89, 90; 89, 91; 90, identifier:sorted_diff; 91, binary_operator://; 91, 92; 91, 96; 92, call; 92, 93; 92, 94; 93, identifier:len; 94, argument_list; 94, 95; 95, identifier:diffs; 96, integer:2; 97, return_statement; 97, 98; 98, identifier:result
def guess_interval(nums, accuracy=0): """Given a seq of number, return the median, only calculate interval >= accuracy. :: from torequests.utils import guess_interval import random seq = [random.randint(1, 100) for i in range(20)] print(guess_interval(seq, 5)) # sorted_seq: [2, 10, 12, 19, 19, 29, 30, 32, 38, 40, 41, 54, 62, 69, 75, 79, 82, 88, 97, 99] # diffs: [8, 7, 10, 6, 13, 8, 7, 6, 6, 9] # median: 8 """ if not nums: return 0 nums = sorted([int(i) for i in nums]) if len(nums) == 1: return nums[0] diffs = [nums[i + 1] - nums[i] for i in range(len(nums) - 1)] diffs = [item for item in diffs if item >= accuracy] sorted_diff = sorted(diffs) result = sorted_diff[len(diffs) // 2] return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sort_list_of_dicts; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:lst_of_dct; 5, identifier:keys; 6, default_parameter; 6, 7; 6, 8; 7, identifier:reverse; 8, False; 9, dictionary_splat_pattern; 9, 10; 10, identifier:sort_args; 11, block; 11, 12; 11, 14; 11, 27; 11, 28; 11, 29; 11, 62; 12, expression_statement; 12, 13; 13, comment; 14, if_statement; 14, 15; 14, 21; 15, comparison_operator:!=; 15, 16; 15, 20; 16, call; 16, 17; 16, 18; 17, identifier:type; 18, argument_list; 18, 19; 19, identifier:keys; 20, identifier:list; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:keys; 25, list:[keys]; 25, 26; 26, identifier:keys; 27, comment; 28, comment; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:lst_of_dct; 33, identifier:sort; 34, argument_list; 34, 35; 34, 57; 34, 60; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:key; 37, lambda; 37, 38; 37, 40; 38, lambda_parameters; 38, 39; 39, identifier:x; 40, list_comprehension; 40, 41; 40, 54; 41, parenthesized_expression; 41, 42; 42, conditional_expression:if; 42, 43; 42, 48; 42, 51; 43, tuple; 43, 44; 43, 45; 44, False; 45, subscript; 45, 46; 45, 47; 46, identifier:x; 47, identifier:key; 48, comparison_operator:in; 48, 49; 48, 50; 49, identifier:key; 50, identifier:x; 51, tuple; 51, 52; 51, 53; 52, True; 53, integer:0; 54, for_in_clause; 54, 55; 54, 56; 55, identifier:key; 56, identifier:keys; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:reverse; 59, identifier:reverse; 60, dictionary_splat; 60, 61; 61, identifier:sort_args; 62, return_statement; 62, 63; 63, identifier:lst_of_dct
def sort_list_of_dicts(lst_of_dct, keys, reverse=False, **sort_args): """ Sort list of dicts by one or multiple keys. If the key is not available, sort these to the end. :param lst_of_dct: input structure. List of dicts. :param keys: one or more keys in list :param reverse: :param sort_args: :return: """ if type(keys) != list: keys = [keys] # dcmdir = lst_of_dct[:] # lst_of_dct.sort(key=lambda x: [x[key] for key in keys], reverse=reverse, **sort_args) lst_of_dct.sort(key=lambda x: [((False, x[key]) if key in x else (True, 0)) for key in keys], reverse=reverse, **sort_args) return lst_of_dct
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:parallel_deblur; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:inputs; 5, identifier:params; 6, identifier:pos_ref_db_fp; 7, identifier:neg_ref_dp_fp; 8, default_parameter; 8, 9; 8, 10; 9, identifier:jobs_to_start; 10, integer:1; 11, block; 11, 12; 11, 14; 11, 23; 11, 35; 11, 36; 11, 45; 11, 49; 11, 53; 11, 86; 11, 87; 11, 88; 11, 114; 11, 140; 11, 149; 11, 158; 11, 167; 11, 178; 11, 185; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:logger; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:logging; 20, identifier:getLogger; 21, argument_list; 21, 22; 22, identifier:__name__; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:logger; 27, identifier:info; 28, argument_list; 28, 29; 29, binary_operator:%; 29, 30; 29, 31; 30, string:'parallel deblur started for %d inputs'; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:inputs; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:remove_param_list; 39, list:['-O', '--jobs-to-start', '--seqs-fp', '--pos-ref-db-fp', '--neg-ref-db-fp']; 39, 40; 39, 41; 39, 42; 39, 43; 39, 44; 40, string:'-O'; 41, string:'--jobs-to-start'; 42, string:'--seqs-fp'; 43, string:'--pos-ref-db-fp'; 44, string:'--neg-ref-db-fp'; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:skipnext; 48, False; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:newparams; 52, list:[]; 53, for_statement; 53, 54; 53, 55; 53, 60; 54, identifier:carg; 55, subscript; 55, 56; 55, 57; 56, identifier:params; 57, slice; 57, 58; 57, 59; 58, integer:2; 59, colon; 60, block; 60, 61; 60, 69; 60, 79; 61, if_statement; 61, 62; 61, 63; 62, identifier:skipnext; 63, block; 63, 64; 63, 68; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:skipnext; 67, False; 68, continue_statement; 69, if_statement; 69, 70; 69, 73; 70, comparison_operator:in; 70, 71; 70, 72; 71, identifier:carg; 72, identifier:remove_param_list; 73, block; 73, 74; 73, 78; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:skipnext; 77, True; 78, continue_statement; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:newparams; 83, identifier:append; 84, argument_list; 84, 85; 85, identifier:carg; 86, comment; 87, comment; 88, if_statement; 88, 89; 88, 90; 89, identifier:pos_ref_db_fp; 90, block; 90, 91; 90, 100; 90, 107; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:new_pos_ref_db_fp; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:','; 97, identifier:join; 98, argument_list; 98, 99; 99, identifier:pos_ref_db_fp; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:newparams; 104, identifier:append; 105, argument_list; 105, 106; 106, string:'--pos-ref-db-fp'; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:newparams; 111, identifier:append; 112, argument_list; 112, 113; 113, identifier:new_pos_ref_db_fp; 114, if_statement; 114, 115; 114, 116; 115, identifier:neg_ref_dp_fp; 116, block; 116, 117; 116, 126; 116, 133; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:new_neg_ref_db_fp; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, string:','; 123, identifier:join; 124, argument_list; 124, 125; 125, identifier:neg_ref_dp_fp; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:newparams; 130, identifier:append; 131, argument_list; 131, 132; 132, string:'--neg-ref-db-fp'; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:newparams; 137, identifier:append; 138, argument_list; 138, 139; 139, identifier:new_neg_ref_db_fp; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, identifier:logger; 144, identifier:debug; 145, argument_list; 145, 146; 146, binary_operator:%; 146, 147; 146, 148; 147, string:'ready for functor %s'; 148, identifier:newparams; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:functor; 152, call; 152, 153; 152, 154; 153, identifier:partial; 154, argument_list; 154, 155; 154, 156; 154, 157; 155, identifier:run_functor; 156, identifier:deblur_system_call; 157, identifier:newparams; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:logger; 162, identifier:debug; 163, argument_list; 163, 164; 164, binary_operator:%; 164, 165; 164, 166; 165, string:'ready for pool %d jobs'; 166, identifier:jobs_to_start; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:pool; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:mp; 173, identifier:Pool; 174, argument_list; 174, 175; 175, keyword_argument; 175, 176; 175, 177; 176, identifier:processes; 177, identifier:jobs_to_start; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:logger; 182, identifier:debug; 183, argument_list; 183, 184; 184, string:'almost running...'; 185, for_statement; 185, 186; 185, 190; 185, 197; 186, pattern_list; 186, 187; 186, 188; 186, 189; 187, identifier:stdout; 188, identifier:stderr; 189, identifier:es; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:pool; 193, identifier:map; 194, argument_list; 194, 195; 194, 196; 195, identifier:functor; 196, identifier:inputs; 197, block; 197, 198; 198, if_statement; 198, 199; 198, 202; 199, comparison_operator:!=; 199, 200; 199, 201; 200, identifier:es; 201, integer:0; 202, block; 202, 203; 203, raise_statement; 203, 204; 204, call; 204, 205; 204, 206; 205, identifier:RuntimeError; 206, argument_list; 206, 207; 207, binary_operator:%; 207, 208; 207, 209; 208, string:"stdout: %s\nstderr: %s\nexit: %d"; 209, tuple; 209, 210; 209, 211; 209, 212; 210, identifier:stdout; 211, identifier:stderr; 212, identifier:es
def parallel_deblur(inputs, params, pos_ref_db_fp, neg_ref_dp_fp, jobs_to_start=1): """Dispatch execution over a pool of processors This code was adopted from the American Gut project: https://github.com/biocore/American-Gut/blob/master/americangut/parallel.py Parameters ---------- inputs : iterable of str File paths to input per-sample sequence files params : list of str list of CLI parameters supplied to the deblur workflow (argv - first 2 are 'deblur','workflow' and are ignored) pos_ref_db_fp : list of str the indexed positive (16s) sortmerna database (created in the main thread) neg_ref_db_fp : list of str the indexed negative (artifacts) sortmerna database (created in the main thread) jobs_to_start : int, optional The number of processors on the local system to use Returns ------- all_result_paths : list list of expected output files """ logger = logging.getLogger(__name__) logger.info('parallel deblur started for %d inputs' % len(inputs)) # remove the irrelevant parameters remove_param_list = ['-O', '--jobs-to-start', '--seqs-fp', '--pos-ref-db-fp', '--neg-ref-db-fp'] skipnext = False newparams = [] for carg in params[2:]: if skipnext: skipnext = False continue if carg in remove_param_list: skipnext = True continue newparams.append(carg) # add the ref_db_fp (since it may be not present in the # original command parameters) if pos_ref_db_fp: new_pos_ref_db_fp = ','.join(pos_ref_db_fp) newparams.append('--pos-ref-db-fp') newparams.append(new_pos_ref_db_fp) if neg_ref_dp_fp: new_neg_ref_db_fp = ','.join(neg_ref_dp_fp) newparams.append('--neg-ref-db-fp') newparams.append(new_neg_ref_db_fp) logger.debug('ready for functor %s' % newparams) functor = partial(run_functor, deblur_system_call, newparams) logger.debug('ready for pool %d jobs' % jobs_to_start) pool = mp.Pool(processes=jobs_to_start) logger.debug('almost running...') for stdout, stderr, es in pool.map(functor, inputs): if es != 0: raise RuntimeError("stdout: %s\nstderr: %s\nexit: %d" % (stdout, stderr, es))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:build_index_sortmerna; 3, parameters; 3, 4; 3, 5; 4, identifier:ref_fp; 5, identifier:working_dir; 6, block; 6, 7; 6, 9; 6, 18; 6, 31; 6, 35; 6, 166; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:logger; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:logging; 15, identifier:getLogger; 16, argument_list; 16, 17; 17, identifier:__name__; 18, expression_statement; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:logger; 22, identifier:info; 23, argument_list; 23, 24; 24, binary_operator:%; 24, 25; 24, 28; 25, concatenated_string; 25, 26; 25, 27; 26, string:'build_index_sortmerna files %s to'; 27, string:' dir %s'; 28, tuple; 28, 29; 28, 30; 29, identifier:ref_fp; 30, identifier:working_dir; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:all_db; 34, list:[]; 35, for_statement; 35, 36; 35, 37; 35, 38; 36, identifier:db; 37, identifier:ref_fp; 38, block; 38, 39; 38, 48; 38, 57; 38, 65; 38, 76; 38, 89; 38, 99; 38, 150; 38, 159; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 44; 41, pattern_list; 41, 42; 41, 43; 42, identifier:fasta_dir; 43, identifier:fasta_filename; 44, call; 44, 45; 44, 46; 45, identifier:split; 46, argument_list; 46, 47; 47, identifier:db; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:index_basename; 51, subscript; 51, 52; 51, 56; 52, call; 52, 53; 52, 54; 53, identifier:splitext; 54, argument_list; 54, 55; 55, identifier:fasta_filename; 56, integer:0; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:db_output; 60, call; 60, 61; 60, 62; 61, identifier:join; 62, argument_list; 62, 63; 62, 64; 63, identifier:working_dir; 64, identifier:index_basename; 65, expression_statement; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:logger; 69, identifier:debug; 70, argument_list; 70, 71; 71, binary_operator:%; 71, 72; 71, 73; 72, string:'processing file %s into location %s'; 73, tuple; 73, 74; 73, 75; 74, identifier:db; 75, identifier:db_output; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:params; 79, list:['indexdb_rna', '--ref', '%s,%s' % (db, db_output), '--tmpdir', working_dir]; 79, 80; 79, 81; 79, 82; 79, 87; 79, 88; 80, string:'indexdb_rna'; 81, string:'--ref'; 82, binary_operator:%; 82, 83; 82, 84; 83, string:'%s,%s'; 84, tuple; 84, 85; 84, 86; 85, identifier:db; 86, identifier:db_output; 87, string:'--tmpdir'; 88, identifier:working_dir; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 95; 91, pattern_list; 91, 92; 91, 93; 91, 94; 92, identifier:sout; 93, identifier:serr; 94, identifier:res; 95, call; 95, 96; 95, 97; 96, identifier:_system_call; 97, argument_list; 97, 98; 98, identifier:params; 99, if_statement; 99, 100; 99, 104; 100, not_operator; 100, 101; 101, comparison_operator:==; 101, 102; 101, 103; 102, identifier:res; 103, integer:0; 104, block; 104, 105; 104, 118; 104, 127; 104, 136; 104, 143; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:logger; 109, identifier:error; 110, argument_list; 110, 111; 111, binary_operator:%; 111, 112; 111, 115; 112, concatenated_string; 112, 113; 112, 114; 113, string:'Problem running indexdb_rna on file %s to dir %s. '; 114, string:'database not indexed'; 115, tuple; 115, 116; 115, 117; 116, identifier:db; 117, identifier:db_output; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:logger; 122, identifier:debug; 123, argument_list; 123, 124; 124, binary_operator:%; 124, 125; 124, 126; 125, string:'stdout: %s'; 126, identifier:sout; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:logger; 131, identifier:debug; 132, argument_list; 132, 133; 133, binary_operator:%; 133, 134; 133, 135; 134, string:'stderr: %s'; 135, identifier:serr; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:logger; 140, identifier:critical; 141, argument_list; 141, 142; 142, string:'execution halted'; 143, raise_statement; 143, 144; 144, call; 144, 145; 144, 146; 145, identifier:RuntimeError; 146, argument_list; 146, 147; 147, binary_operator:%; 147, 148; 147, 149; 148, string:'Cannot index database file %s'; 149, identifier:db; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:logger; 154, identifier:debug; 155, argument_list; 155, 156; 156, binary_operator:%; 156, 157; 156, 158; 157, string:'file %s indexed'; 158, identifier:db; 159, expression_statement; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:all_db; 163, identifier:append; 164, argument_list; 164, 165; 165, identifier:db_output; 166, return_statement; 166, 167; 167, identifier:all_db
def build_index_sortmerna(ref_fp, working_dir): """Build a SortMeRNA index for all reference databases. Parameters ---------- ref_fp: tuple filepaths to FASTA reference databases working_dir: string working directory path where to store the indexed database Returns ------- all_db: tuple filepaths to SortMeRNA indexed reference databases """ logger = logging.getLogger(__name__) logger.info('build_index_sortmerna files %s to' ' dir %s' % (ref_fp, working_dir)) all_db = [] for db in ref_fp: fasta_dir, fasta_filename = split(db) index_basename = splitext(fasta_filename)[0] db_output = join(working_dir, index_basename) logger.debug('processing file %s into location %s' % (db, db_output)) params = ['indexdb_rna', '--ref', '%s,%s' % (db, db_output), '--tmpdir', working_dir] sout, serr, res = _system_call(params) if not res == 0: logger.error('Problem running indexdb_rna on file %s to dir %s. ' 'database not indexed' % (db, db_output)) logger.debug('stdout: %s' % sout) logger.debug('stderr: %s' % serr) logger.critical('execution halted') raise RuntimeError('Cannot index database file %s' % db) logger.debug('file %s indexed' % db) all_db.append(db_output) return all_db
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 21; 2, function_name:remove_artifacts_from_biom_table; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 15; 3, 18; 4, identifier:table_filename; 5, identifier:fasta_filename; 6, identifier:ref_fp; 7, identifier:biom_table_dir; 8, identifier:ref_db_fp; 9, default_parameter; 9, 10; 9, 11; 10, identifier:threads; 11, integer:1; 12, default_parameter; 12, 13; 12, 14; 13, identifier:verbose; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:sim_thresh; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:coverage_thresh; 20, None; 21, block; 21, 22; 21, 24; 21, 33; 21, 40; 21, 41; 21, 73; 21, 89; 21, 102; 21, 103; 21, 116; 21, 130; 21, 139; 21, 146; 21, 147; 21, 168; 21, 169; 21, 174; 21, 182; 21, 188; 21, 197; 21, 198; 21, 206; 21, 212; 21, 213; 21, 226; 21, 227; 21, 232; 21, 240; 21, 246; 21, 255; 21, 256; 21, 264; 21, 270; 21, 271; 21, 278; 22, expression_statement; 22, 23; 23, comment; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:logger; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:logging; 30, identifier:getLogger; 31, argument_list; 31, 32; 32, identifier:__name__; 33, expression_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:logger; 37, identifier:info; 38, argument_list; 38, 39; 39, string:'getting 16s sequences from the biom table'; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 47; 43, pattern_list; 43, 44; 43, 45; 43, 46; 44, identifier:clean_fp; 45, identifier:num_seqs_left; 46, identifier:tmp_files; 47, call; 47, 48; 47, 49; 48, identifier:remove_artifacts_seqs; 49, argument_list; 49, 50; 49, 51; 49, 52; 49, 55; 49, 58; 49, 61; 49, 64; 49, 67; 49, 70; 50, identifier:fasta_filename; 51, identifier:ref_fp; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:working_dir; 54, identifier:biom_table_dir; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:ref_db_fp; 57, identifier:ref_db_fp; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:negate; 60, False; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:threads; 63, identifier:threads; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:verbose; 66, identifier:verbose; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:sim_thresh; 69, identifier:sim_thresh; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:coverage_thresh; 72, identifier:coverage_thresh; 73, if_statement; 73, 74; 73, 77; 74, comparison_operator:is; 74, 75; 74, 76; 75, identifier:clean_fp; 76, None; 77, block; 77, 78; 77, 87; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:logger; 82, identifier:warn; 83, argument_list; 83, 84; 84, binary_operator:%; 84, 85; 84, 86; 85, string:"No clean sequences in %s"; 86, identifier:fasta_filename; 87, return_statement; 87, 88; 88, identifier:tmp_files; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:logger; 93, identifier:debug; 94, argument_list; 94, 95; 95, binary_operator:%; 95, 96; 95, 99; 96, concatenated_string; 96, 97; 96, 98; 97, string:'removed artifacts from sequences input %s'; 98, string:' to output %s'; 99, tuple; 99, 100; 99, 101; 100, identifier:fasta_filename; 101, identifier:clean_fp; 102, comment; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:good_seqs; 106, set_comprehension; 106, 107; 106, 108; 107, identifier:s; 108, for_in_clause; 108, 109; 108, 112; 109, pattern_list; 109, 110; 109, 111; 110, identifier:_; 111, identifier:s; 112, call; 112, 113; 112, 114; 113, identifier:sequence_generator; 114, argument_list; 114, 115; 115, identifier:clean_fp; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:logger; 120, identifier:debug; 121, argument_list; 121, 122; 122, binary_operator:%; 122, 123; 122, 126; 123, concatenated_string; 123, 124; 123, 125; 124, string:'loaded %d sequences from cleaned biom table'; 125, string:' fasta file'; 126, call; 126, 127; 126, 128; 127, identifier:len; 128, argument_list; 128, 129; 129, identifier:good_seqs; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:logger; 134, identifier:debug; 135, argument_list; 135, 136; 136, binary_operator:%; 136, 137; 136, 138; 137, string:'loading biom table %s'; 138, identifier:table_filename; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:table; 142, call; 142, 143; 142, 144; 143, identifier:load_table; 144, argument_list; 144, 145; 145, identifier:table_filename; 146, comment; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:artifact_table; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:table; 153, identifier:filter; 154, argument_list; 154, 155; 154, 159; 154, 162; 154, 165; 155, call; 155, 156; 155, 157; 156, identifier:list; 157, argument_list; 157, 158; 158, identifier:good_seqs; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:axis; 161, string:'observation'; 162, keyword_argument; 162, 163; 162, 164; 163, identifier:inplace; 164, False; 165, keyword_argument; 165, 166; 165, 167; 166, identifier:invert; 167, True; 168, comment; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 172; 171, identifier:filter_minreads_samples_from_table; 172, argument_list; 172, 173; 173, identifier:artifact_table; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:output_nomatch_fp; 177, call; 177, 178; 177, 179; 178, identifier:join; 179, argument_list; 179, 180; 179, 181; 180, identifier:biom_table_dir; 181, string:'reference-non-hit.biom'; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:write_biom_table; 185, argument_list; 185, 186; 185, 187; 186, identifier:artifact_table; 187, identifier:output_nomatch_fp; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:logger; 192, identifier:info; 193, argument_list; 193, 194; 194, binary_operator:%; 194, 195; 194, 196; 195, string:'wrote artifact only filtered biom table to %s'; 196, identifier:output_nomatch_fp; 197, comment; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:output_nomatch_fasta_fp; 201, call; 201, 202; 201, 203; 202, identifier:join; 203, argument_list; 203, 204; 203, 205; 204, identifier:biom_table_dir; 205, string:'reference-non-hit.seqs.fa'; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 209; 208, identifier:fasta_from_biom; 209, argument_list; 209, 210; 209, 211; 210, identifier:artifact_table; 211, identifier:output_nomatch_fasta_fp; 212, comment; 213, expression_statement; 213, 214; 214, call; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:table; 217, identifier:filter; 218, argument_list; 218, 219; 218, 223; 219, call; 219, 220; 219, 221; 220, identifier:list; 221, argument_list; 221, 222; 222, identifier:good_seqs; 223, keyword_argument; 223, 224; 223, 225; 224, identifier:axis; 225, string:'observation'; 226, comment; 227, expression_statement; 227, 228; 228, call; 228, 229; 228, 230; 229, identifier:filter_minreads_samples_from_table; 230, argument_list; 230, 231; 231, identifier:table; 232, expression_statement; 232, 233; 233, assignment; 233, 234; 233, 235; 234, identifier:output_fp; 235, call; 235, 236; 235, 237; 236, identifier:join; 237, argument_list; 237, 238; 237, 239; 238, identifier:biom_table_dir; 239, string:'reference-hit.biom'; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 243; 242, identifier:write_biom_table; 243, argument_list; 243, 244; 243, 245; 244, identifier:table; 245, identifier:output_fp; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:logger; 250, identifier:info; 251, argument_list; 251, 252; 252, binary_operator:%; 252, 253; 252, 254; 253, string:'wrote 16s filtered biom table to %s'; 254, identifier:output_fp; 255, comment; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:output_match_fasta_fp; 259, call; 259, 260; 259, 261; 260, identifier:join; 261, argument_list; 261, 262; 261, 263; 262, identifier:biom_table_dir; 263, string:'reference-hit.seqs.fa'; 264, expression_statement; 264, 265; 265, call; 265, 266; 265, 267; 266, identifier:fasta_from_biom; 267, argument_list; 267, 268; 267, 269; 268, identifier:table; 269, identifier:output_match_fasta_fp; 270, comment; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:tmp_files; 275, identifier:append; 276, argument_list; 276, 277; 277, identifier:clean_fp; 278, return_statement; 278, 279; 279, identifier:tmp_files
def remove_artifacts_from_biom_table(table_filename, fasta_filename, ref_fp, biom_table_dir, ref_db_fp, threads=1, verbose=False, sim_thresh=None, coverage_thresh=None): """Remove artifacts from a biom table using SortMeRNA Parameters ---------- table : str name of the biom table file fasta_filename : str the fasta file containing all the sequences of the biom table Returns ------- tmp_files : list of str The temp files created during the artifact removal step """ logger = logging.getLogger(__name__) logger.info('getting 16s sequences from the biom table') # remove artifacts from the fasta file. output is in clean_fp fasta file clean_fp, num_seqs_left, tmp_files = remove_artifacts_seqs(fasta_filename, ref_fp, working_dir=biom_table_dir, ref_db_fp=ref_db_fp, negate=False, threads=threads, verbose=verbose, sim_thresh=sim_thresh, coverage_thresh=coverage_thresh) if clean_fp is None: logger.warn("No clean sequences in %s" % fasta_filename) return tmp_files logger.debug('removed artifacts from sequences input %s' ' to output %s' % (fasta_filename, clean_fp)) # read the clean fasta file good_seqs = {s for _, s in sequence_generator(clean_fp)} logger.debug('loaded %d sequences from cleaned biom table' ' fasta file' % len(good_seqs)) logger.debug('loading biom table %s' % table_filename) table = load_table(table_filename) # filter and save the artifact biom table artifact_table = table.filter(list(good_seqs), axis='observation', inplace=False, invert=True) # remove the samples with 0 reads filter_minreads_samples_from_table(artifact_table) output_nomatch_fp = join(biom_table_dir, 'reference-non-hit.biom') write_biom_table(artifact_table, output_nomatch_fp) logger.info('wrote artifact only filtered biom table to %s' % output_nomatch_fp) # and save the reference-non-hit fasta file output_nomatch_fasta_fp = join(biom_table_dir, 'reference-non-hit.seqs.fa') fasta_from_biom(artifact_table, output_nomatch_fasta_fp) # filter and save the only 16s biom table table.filter(list(good_seqs), axis='observation') # remove the samples with 0 reads filter_minreads_samples_from_table(table) output_fp = join(biom_table_dir, 'reference-hit.biom') write_biom_table(table, output_fp) logger.info('wrote 16s filtered biom table to %s' % output_fp) # and save the reference-non-hit fasta file output_match_fasta_fp = join(biom_table_dir, 'reference-hit.seqs.fa') fasta_from_biom(table, output_match_fasta_fp) # we also don't need the cleaned fasta file tmp_files.append(clean_fp) return tmp_files
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:remove_artifacts_seqs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:seqs_fp; 5, identifier:ref_fp; 6, identifier:working_dir; 7, identifier:ref_db_fp; 8, default_parameter; 8, 9; 8, 10; 9, identifier:negate; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:threads; 13, integer:1; 14, default_parameter; 14, 15; 14, 16; 15, identifier:verbose; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:sim_thresh; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:coverage_thresh; 22, None; 23, block; 23, 24; 23, 26; 23, 35; 23, 44; 23, 68; 23, 90; 23, 112; 23, 113; 23, 117; 23, 130; 23, 143; 23, 149; 23, 343; 23, 366; 23, 367; 23, 368; 23, 372; 23, 376; 23, 380; 23, 442; 23, 456; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:logger; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:logging; 32, identifier:getLogger; 33, argument_list; 33, 34; 34, identifier:__name__; 35, expression_statement; 35, 36; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:logger; 39, identifier:info; 40, argument_list; 40, 41; 41, binary_operator:%; 41, 42; 41, 43; 42, string:'remove_artifacts_seqs file %s'; 43, identifier:seqs_fp; 44, if_statement; 44, 45; 44, 53; 45, comparison_operator:==; 45, 46; 45, 52; 46, attribute; 46, 47; 46, 51; 47, call; 47, 48; 47, 49; 48, identifier:stat; 49, argument_list; 49, 50; 50, identifier:seqs_fp; 51, identifier:st_size; 52, integer:0; 53, block; 53, 54; 53, 63; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:logger; 58, identifier:warn; 59, argument_list; 59, 60; 60, binary_operator:%; 60, 61; 60, 62; 61, string:'file %s has size 0, continuing'; 62, identifier:seqs_fp; 63, return_statement; 63, 64; 64, expression_list; 64, 65; 64, 66; 64, 67; 65, None; 66, integer:0; 67, list:[]; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:is; 69, 70; 69, 71; 70, identifier:coverage_thresh; 71, None; 72, block; 72, 73; 73, if_statement; 73, 74; 73, 75; 73, 82; 74, identifier:negate; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:coverage_thresh; 79, binary_operator:*; 79, 80; 79, 81; 80, float:0.95; 81, integer:100; 82, else_clause; 82, 83; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:coverage_thresh; 87, binary_operator:*; 87, 88; 87, 89; 88, float:0.5; 89, integer:100; 90, if_statement; 90, 91; 90, 94; 91, comparison_operator:is; 91, 92; 91, 93; 92, identifier:sim_thresh; 93, None; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 97; 95, 104; 96, identifier:negate; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:sim_thresh; 101, binary_operator:*; 101, 102; 101, 103; 102, float:0.95; 103, integer:100; 104, else_clause; 104, 105; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:sim_thresh; 109, binary_operator:*; 109, 110; 109, 111; 110, float:0.65; 111, integer:100; 112, comment; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:bitscore_thresh; 116, float:0.65; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:output_fp; 120, call; 120, 121; 120, 122; 121, identifier:join; 122, argument_list; 122, 123; 122, 124; 123, identifier:working_dir; 124, binary_operator:%; 124, 125; 124, 126; 125, string:"%s.no_artifacts"; 126, call; 126, 127; 126, 128; 127, identifier:basename; 128, argument_list; 128, 129; 129, identifier:seqs_fp; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:blast_output; 133, call; 133, 134; 133, 135; 134, identifier:join; 135, argument_list; 135, 136; 135, 137; 136, identifier:working_dir; 137, binary_operator:%; 137, 138; 137, 139; 138, string:'%s.sortmerna'; 139, call; 139, 140; 139, 141; 140, identifier:basename; 141, argument_list; 141, 142; 142, identifier:seqs_fp; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:aligned_seq_ids; 146, call; 146, 147; 146, 148; 147, identifier:set; 148, argument_list; 149, for_statement; 149, 150; 149, 153; 149, 157; 150, pattern_list; 150, 151; 150, 152; 151, identifier:i; 152, identifier:db; 153, call; 153, 154; 153, 155; 154, identifier:enumerate; 155, argument_list; 155, 156; 156, identifier:ref_fp; 157, block; 157, 158; 157, 173; 157, 174; 157, 175; 157, 176; 157, 201; 157, 211; 157, 249; 157, 255; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:logger; 162, identifier:debug; 163, argument_list; 163, 164; 164, binary_operator:%; 164, 165; 164, 166; 165, string:'running on ref_fp %s working dir %s refdb_fp %s seqs %s'; 166, tuple; 166, 167; 166, 168; 166, 169; 166, 172; 167, identifier:db; 168, identifier:working_dir; 169, subscript; 169, 170; 169, 171; 170, identifier:ref_db_fp; 171, identifier:i; 172, identifier:seqs_fp; 173, comment; 174, comment; 175, comment; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:params; 179, list:['sortmerna', '--reads', seqs_fp, '--ref', '%s,%s' % (db, ref_db_fp[i]), '--aligned', blast_output, '--blast', '3', '--best', '1', '--print_all_reads', '-v', '-e', '100']; 179, 180; 179, 181; 179, 182; 179, 183; 179, 184; 179, 191; 179, 192; 179, 193; 179, 194; 179, 195; 179, 196; 179, 197; 179, 198; 179, 199; 179, 200; 180, string:'sortmerna'; 181, string:'--reads'; 182, identifier:seqs_fp; 183, string:'--ref'; 184, binary_operator:%; 184, 185; 184, 186; 185, string:'%s,%s'; 186, tuple; 186, 187; 186, 188; 187, identifier:db; 188, subscript; 188, 189; 188, 190; 189, identifier:ref_db_fp; 190, identifier:i; 191, string:'--aligned'; 192, identifier:blast_output; 193, string:'--blast'; 194, string:'3'; 195, string:'--best'; 196, string:'1'; 197, string:'--print_all_reads'; 198, string:'-v'; 199, string:'-e'; 200, string:'100'; 201, expression_statement; 201, 202; 202, assignment; 202, 203; 202, 207; 203, pattern_list; 203, 204; 203, 205; 203, 206; 204, identifier:sout; 205, identifier:serr; 206, identifier:res; 207, call; 207, 208; 207, 209; 208, identifier:_system_call; 209, argument_list; 209, 210; 210, identifier:params; 211, if_statement; 211, 212; 211, 216; 212, not_operator; 212, 213; 213, comparison_operator:==; 213, 214; 213, 215; 214, identifier:res; 215, integer:0; 216, block; 216, 217; 216, 226; 216, 235; 216, 244; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:logger; 221, identifier:error; 222, argument_list; 222, 223; 223, binary_operator:%; 223, 224; 223, 225; 224, string:'sortmerna error on file %s'; 225, identifier:seqs_fp; 226, expression_statement; 226, 227; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:logger; 230, identifier:error; 231, argument_list; 231, 232; 232, binary_operator:%; 232, 233; 232, 234; 233, string:'stdout : %s'; 234, identifier:sout; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:logger; 239, identifier:error; 240, argument_list; 240, 241; 241, binary_operator:%; 241, 242; 241, 243; 242, string:'stderr : %s'; 243, identifier:serr; 244, return_statement; 244, 245; 245, expression_list; 245, 246; 245, 247; 245, 248; 246, identifier:output_fp; 247, integer:0; 248, list:[]; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:blast_output_filename; 252, binary_operator:%; 252, 253; 252, 254; 253, string:'%s.blast'; 254, identifier:blast_output; 255, with_statement; 255, 256; 255, 266; 256, with_clause; 256, 257; 257, with_item; 257, 258; 258, as_pattern; 258, 259; 258, 264; 259, call; 259, 260; 259, 261; 260, identifier:open; 261, argument_list; 261, 262; 261, 263; 262, identifier:blast_output_filename; 263, string:'r'; 264, as_pattern_target; 264, 265; 265, identifier:bfl; 266, block; 266, 267; 267, for_statement; 267, 268; 267, 269; 267, 270; 268, identifier:line; 269, identifier:bfl; 270, block; 270, 271; 270, 284; 270, 285; 270, 293; 270, 294; 271, expression_statement; 271, 272; 272, assignment; 272, 273; 272, 274; 273, identifier:line; 274, call; 274, 275; 274, 282; 275, attribute; 275, 276; 275, 281; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:line; 279, identifier:strip; 280, argument_list; 281, identifier:split; 282, argument_list; 282, 283; 283, string:'\t'; 284, comment; 285, if_statement; 285, 286; 285, 291; 286, comparison_operator:==; 286, 287; 286, 290; 287, subscript; 287, 288; 287, 289; 288, identifier:line; 289, integer:1; 290, string:'*'; 291, block; 291, 292; 292, continue_statement; 293, comment; 294, if_statement; 294, 295; 294, 333; 295, boolean_operator:and; 295, 296; 295, 316; 295, 317; 296, boolean_operator:and; 296, 297; 296, 306; 296, 307; 297, parenthesized_expression; 297, 298; 298, comparison_operator:>=; 298, 299; 298, 305; 299, call; 299, 300; 299, 301; 300, identifier:float; 301, argument_list; 301, 302; 302, subscript; 302, 303; 302, 304; 303, identifier:line; 304, integer:2; 305, identifier:sim_thresh; 306, line_continuation:\; 307, parenthesized_expression; 307, 308; 308, comparison_operator:>=; 308, 309; 308, 315; 309, call; 309, 310; 309, 311; 310, identifier:float; 311, argument_list; 311, 312; 312, subscript; 312, 313; 312, 314; 313, identifier:line; 314, integer:13; 315, identifier:coverage_thresh; 316, line_continuation:\; 317, parenthesized_expression; 317, 318; 318, comparison_operator:>=; 318, 319; 318, 325; 319, call; 319, 320; 319, 321; 320, identifier:float; 321, argument_list; 321, 322; 322, subscript; 322, 323; 322, 324; 323, identifier:line; 324, integer:11; 325, binary_operator:*; 325, 326; 325, 327; 326, identifier:bitscore_thresh; 327, call; 327, 328; 327, 329; 328, identifier:len; 329, argument_list; 329, 330; 330, subscript; 330, 331; 330, 332; 331, identifier:line; 332, integer:0; 333, block; 333, 334; 334, expression_statement; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:aligned_seq_ids; 338, identifier:add; 339, argument_list; 339, 340; 340, subscript; 340, 341; 340, 342; 341, identifier:line; 342, integer:0; 343, if_statement; 343, 344; 343, 345; 343, 355; 344, identifier:negate; 345, block; 345, 346; 346, function_definition; 346, 347; 346, 348; 346, 350; 347, function_name:op; 348, parameters; 348, 349; 349, identifier:x; 350, block; 350, 351; 351, return_statement; 351, 352; 352, comparison_operator:not; 352, 353; 352, 354; 353, identifier:x; 354, identifier:aligned_seq_ids; 355, else_clause; 355, 356; 356, block; 356, 357; 357, function_definition; 357, 358; 357, 359; 357, 361; 358, function_name:op; 359, parameters; 359, 360; 360, identifier:x; 361, block; 361, 362; 362, return_statement; 362, 363; 363, comparison_operator:in; 363, 364; 363, 365; 364, identifier:x; 365, identifier:aligned_seq_ids; 366, comment; 367, comment; 368, expression_statement; 368, 369; 369, assignment; 369, 370; 369, 371; 370, identifier:totalseqs; 371, integer:0; 372, expression_statement; 372, 373; 373, assignment; 373, 374; 373, 375; 374, identifier:okseqs; 375, integer:0; 376, expression_statement; 376, 377; 377, assignment; 377, 378; 377, 379; 378, identifier:badseqs; 379, integer:0; 380, with_statement; 380, 381; 380, 391; 381, with_clause; 381, 382; 382, with_item; 382, 383; 383, as_pattern; 383, 384; 383, 389; 384, call; 384, 385; 384, 386; 385, identifier:open; 386, argument_list; 386, 387; 386, 388; 387, identifier:output_fp; 388, string:'w'; 389, as_pattern_target; 389, 390; 390, identifier:out_f; 391, block; 391, 392; 392, for_statement; 392, 393; 392, 396; 392, 400; 393, pattern_list; 393, 394; 393, 395; 394, identifier:label; 395, identifier:seq; 396, call; 396, 397; 396, 398; 397, identifier:sequence_generator; 398, argument_list; 398, 399; 399, identifier:seqs_fp; 400, block; 400, 401; 400, 405; 400, 415; 401, expression_statement; 401, 402; 402, augmented_assignment:+=; 402, 403; 402, 404; 403, identifier:totalseqs; 404, integer:1; 405, expression_statement; 405, 406; 406, assignment; 406, 407; 406, 408; 407, identifier:label; 408, subscript; 408, 409; 408, 414; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:label; 412, identifier:split; 413, argument_list; 414, integer:0; 415, if_statement; 415, 416; 415, 420; 415, 436; 416, call; 416, 417; 416, 418; 417, identifier:op; 418, argument_list; 418, 419; 419, identifier:label; 420, block; 420, 421; 420, 432; 421, expression_statement; 421, 422; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:out_f; 425, identifier:write; 426, argument_list; 426, 427; 427, binary_operator:%; 427, 428; 427, 429; 428, string:">%s\n%s\n"; 429, tuple; 429, 430; 429, 431; 430, identifier:label; 431, identifier:seq; 432, expression_statement; 432, 433; 433, augmented_assignment:+=; 433, 434; 433, 435; 434, identifier:okseqs; 435, integer:1; 436, else_clause; 436, 437; 437, block; 437, 438; 438, expression_statement; 438, 439; 439, augmented_assignment:+=; 439, 440; 439, 441; 440, identifier:badseqs; 441, integer:1; 442, expression_statement; 442, 443; 443, call; 443, 444; 443, 447; 444, attribute; 444, 445; 444, 446; 445, identifier:logger; 446, identifier:info; 447, argument_list; 447, 448; 448, binary_operator:%; 448, 449; 448, 452; 449, concatenated_string; 449, 450; 449, 451; 450, string:'total sequences %d, passing sequences %d, '; 451, string:'failing sequences %d'; 452, tuple; 452, 453; 452, 454; 452, 455; 453, identifier:totalseqs; 454, identifier:okseqs; 455, identifier:badseqs; 456, return_statement; 456, 457; 457, expression_list; 457, 458; 457, 459; 457, 460; 458, identifier:output_fp; 459, identifier:okseqs; 460, list:[blast_output_filename]; 460, 461; 461, identifier:blast_output_filename
def remove_artifacts_seqs(seqs_fp, ref_fp, working_dir, ref_db_fp, negate=False, threads=1, verbose=False, sim_thresh=None, coverage_thresh=None): """Remove artifacts from FASTA file using SortMeRNA. Parameters ---------- seqs_fp: string file path to FASTA input sequence file ref_fp: tuple file path(s) to FASTA database file working_dir: string working directory path ref_db_fp: tuple file path(s) to indexed FASTA database negate: boolean, optional if True, discard all input sequences aligning to reference database threads: integer, optional number of threads to use for SortMeRNA verbose: boolean, optional If true, output SortMeRNA errors sim_thresh: float, optional The minimal similarity threshold (between 0 and 1) for keeping the sequence if None, the default values used are 0.65 for negate=False, 0.95 for negate=True coverage_thresh: float, optional The minimal coverage threshold (between 0 and 1) for alignments for keeping the sequence if None, the default values used are 0.5 for negate=False, 0.95 for negate=True Returns ------- output_fp : str Name of the artifact removed fasta file okseqs : int The number of sequences left after artifact removal tmp_files : list of str Names of the tmp files created """ logger = logging.getLogger(__name__) logger.info('remove_artifacts_seqs file %s' % seqs_fp) if stat(seqs_fp).st_size == 0: logger.warn('file %s has size 0, continuing' % seqs_fp) return None, 0, [] if coverage_thresh is None: if negate: coverage_thresh = 0.95 * 100 else: coverage_thresh = 0.5 * 100 if sim_thresh is None: if negate: sim_thresh = 0.95 * 100 else: sim_thresh = 0.65 * 100 # the minimal average bitscore per nucleotide bitscore_thresh = 0.65 output_fp = join(working_dir, "%s.no_artifacts" % basename(seqs_fp)) blast_output = join(working_dir, '%s.sortmerna' % basename(seqs_fp)) aligned_seq_ids = set() for i, db in enumerate(ref_fp): logger.debug('running on ref_fp %s working dir %s refdb_fp %s seqs %s' % (db, working_dir, ref_db_fp[i], seqs_fp)) # run SortMeRNA # we use -e 100 to remove E-value based filtering by sortmerna # since we use bitscore/identity/coverage filtering instead params = ['sortmerna', '--reads', seqs_fp, '--ref', '%s,%s' % (db, ref_db_fp[i]), '--aligned', blast_output, '--blast', '3', '--best', '1', '--print_all_reads', '-v', '-e', '100'] sout, serr, res = _system_call(params) if not res == 0: logger.error('sortmerna error on file %s' % seqs_fp) logger.error('stdout : %s' % sout) logger.error('stderr : %s' % serr) return output_fp, 0, [] blast_output_filename = '%s.blast' % blast_output with open(blast_output_filename, 'r') as bfl: for line in bfl: line = line.strip().split('\t') # if * means no match if line[1] == '*': continue # check if % identity[2] and coverage[13] are large enough if (float(line[2]) >= sim_thresh) and \ (float(line[13]) >= coverage_thresh) and \ (float(line[11]) >= bitscore_thresh * len(line[0])): aligned_seq_ids.add(line[0]) if negate: def op(x): return x not in aligned_seq_ids else: def op(x): return x in aligned_seq_ids # if negate = False, only output sequences # matching to at least one of the databases totalseqs = 0 okseqs = 0 badseqs = 0 with open(output_fp, 'w') as out_f: for label, seq in sequence_generator(seqs_fp): totalseqs += 1 label = label.split()[0] if op(label): out_f.write(">%s\n%s\n" % (label, seq)) okseqs += 1 else: badseqs += 1 logger.info('total sequences %d, passing sequences %d, ' 'failing sequences %d' % (totalseqs, okseqs, badseqs)) return output_fp, okseqs, [blast_output_filename]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 24; 2, function_name:launch_workflow; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 3, 15; 3, 18; 3, 21; 4, identifier:seqs_fp; 5, identifier:working_dir; 6, identifier:mean_error; 7, identifier:error_dist; 8, identifier:indel_prob; 9, identifier:indel_max; 10, identifier:trim_length; 11, identifier:left_trim_length; 12, identifier:min_size; 13, identifier:ref_fp; 14, identifier:ref_db_fp; 15, default_parameter; 15, 16; 15, 17; 16, identifier:threads_per_sample; 17, integer:1; 18, default_parameter; 18, 19; 18, 20; 19, identifier:sim_thresh; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:coverage_thresh; 23, None; 24, block; 24, 25; 24, 27; 24, 36; 24, 43; 24, 52; 24, 53; 24, 66; 24, 109; 24, 110; 24, 123; 24, 139; 24, 140; 24, 170; 24, 193; 24, 194; 24, 288; 24, 289; 24, 302; 24, 386; 24, 387; 24, 398; 24, 405; 25, expression_statement; 25, 26; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:logger; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:logging; 33, identifier:getLogger; 34, argument_list; 34, 35; 35, identifier:__name__; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:logger; 40, identifier:info; 41, argument_list; 41, 42; 42, string:'--------------------------------------------------------'; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:logger; 47, identifier:info; 48, argument_list; 48, 49; 49, binary_operator:%; 49, 50; 49, 51; 50, string:'launch_workflow for file %s'; 51, identifier:seqs_fp; 52, comment; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:output_trim_fp; 56, call; 56, 57; 56, 58; 57, identifier:join; 58, argument_list; 58, 59; 58, 60; 59, identifier:working_dir; 60, binary_operator:%; 60, 61; 60, 62; 61, string:"%s.trim"; 62, call; 62, 63; 62, 64; 63, identifier:basename; 64, argument_list; 64, 65; 65, identifier:seqs_fp; 66, with_statement; 66, 67; 66, 77; 67, with_clause; 67, 68; 68, with_item; 68, 69; 69, as_pattern; 69, 70; 69, 75; 70, call; 70, 71; 70, 72; 71, identifier:open; 72, argument_list; 72, 73; 72, 74; 73, identifier:output_trim_fp; 74, string:'w'; 75, as_pattern_target; 75, 76; 76, identifier:out_f; 77, block; 77, 78; 78, for_statement; 78, 79; 78, 82; 78, 97; 79, pattern_list; 79, 80; 79, 81; 80, identifier:label; 81, identifier:seq; 82, call; 82, 83; 82, 84; 83, identifier:trim_seqs; 84, argument_list; 84, 85; 84, 91; 84, 94; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:input_seqs; 87, call; 87, 88; 87, 89; 88, identifier:sequence_generator; 89, argument_list; 89, 90; 90, identifier:seqs_fp; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:trim_len; 93, identifier:trim_length; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:left_trim_len; 96, identifier:left_trim_length; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, call; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:out_f; 102, identifier:write; 103, argument_list; 103, 104; 104, binary_operator:%; 104, 105; 104, 106; 105, string:">%s\n%s\n"; 106, tuple; 106, 107; 106, 108; 107, identifier:label; 108, identifier:seq; 109, comment; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:output_derep_fp; 113, call; 113, 114; 113, 115; 114, identifier:join; 115, argument_list; 115, 116; 115, 117; 116, identifier:working_dir; 117, binary_operator:%; 117, 118; 117, 119; 118, string:"%s.derep"; 119, call; 119, 120; 119, 121; 120, identifier:basename; 121, argument_list; 121, 122; 122, identifier:output_trim_fp; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 126; 125, identifier:dereplicate_seqs; 126, argument_list; 126, 127; 126, 130; 126, 133; 126, 136; 127, keyword_argument; 127, 128; 127, 129; 128, identifier:seqs_fp; 129, identifier:output_trim_fp; 130, keyword_argument; 130, 131; 130, 132; 131, identifier:output_fp; 132, identifier:output_derep_fp; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:min_size; 135, identifier:min_size; 136, keyword_argument; 136, 137; 136, 138; 137, identifier:threads; 138, identifier:threads_per_sample; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 146; 142, pattern_list; 142, 143; 142, 144; 142, 145; 143, identifier:output_artif_fp; 144, identifier:num_seqs_left; 145, identifier:_; 146, call; 146, 147; 146, 148; 147, identifier:remove_artifacts_seqs; 148, argument_list; 148, 149; 148, 152; 148, 155; 148, 158; 148, 161; 148, 164; 148, 167; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:seqs_fp; 151, identifier:output_derep_fp; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:ref_fp; 154, identifier:ref_fp; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:working_dir; 157, identifier:working_dir; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:ref_db_fp; 160, identifier:ref_db_fp; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:negate; 163, True; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:threads; 166, identifier:threads_per_sample; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:sim_thresh; 169, identifier:sim_thresh; 170, if_statement; 170, 171; 170, 173; 171, not_operator; 171, 172; 172, identifier:output_artif_fp; 173, block; 173, 174; 173, 184; 173, 191; 174, expression_statement; 174, 175; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:warnings; 178, identifier:warn; 179, argument_list; 179, 180; 179, 183; 180, binary_operator:%; 180, 181; 180, 182; 181, string:'Problem removing artifacts from file %s'; 182, identifier:seqs_fp; 183, identifier:UserWarning; 184, expression_statement; 184, 185; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:logger; 188, identifier:warning; 189, argument_list; 189, 190; 190, string:'remove artifacts failed, aborting'; 191, return_statement; 191, 192; 192, None; 193, comment; 194, if_statement; 194, 195; 194, 198; 194, 249; 194, 260; 195, comparison_operator:>; 195, 196; 195, 197; 196, identifier:num_seqs_left; 197, integer:1; 198, block; 198, 199; 198, 212; 198, 224; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:output_msa_fp; 202, call; 202, 203; 202, 204; 203, identifier:join; 204, argument_list; 204, 205; 204, 206; 205, identifier:working_dir; 206, binary_operator:%; 206, 207; 206, 208; 207, string:"%s.msa"; 208, call; 208, 209; 208, 210; 209, identifier:basename; 210, argument_list; 210, 211; 211, identifier:output_artif_fp; 212, expression_statement; 212, 213; 213, assignment; 213, 214; 213, 215; 214, identifier:alignment; 215, call; 215, 216; 215, 217; 216, identifier:multiple_sequence_alignment; 217, argument_list; 217, 218; 217, 221; 218, keyword_argument; 218, 219; 218, 220; 219, identifier:seqs_fp; 220, identifier:output_artif_fp; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:threads; 223, identifier:threads_per_sample; 224, if_statement; 224, 225; 224, 227; 225, not_operator; 225, 226; 226, identifier:alignment; 227, block; 227, 228; 227, 240; 227, 247; 228, expression_statement; 228, 229; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, identifier:warnings; 232, identifier:warn; 233, argument_list; 233, 234; 233, 239; 234, binary_operator:%; 234, 235; 234, 238; 235, concatenated_string; 235, 236; 235, 237; 236, string:'Problem performing multiple sequence alignment '; 237, string:'on file %s'; 238, identifier:seqs_fp; 239, identifier:UserWarning; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:logger; 244, identifier:warning; 245, argument_list; 245, 246; 246, string:'msa failed. aborting'; 247, return_statement; 247, 248; 248, None; 249, elif_clause; 249, 250; 249, 253; 249, 254; 249, 255; 250, comparison_operator:==; 250, 251; 250, 252; 251, identifier:num_seqs_left; 252, integer:1; 253, comment; 254, comment; 255, block; 255, 256; 256, expression_statement; 256, 257; 257, assignment; 257, 258; 257, 259; 258, identifier:output_msa_fp; 259, identifier:output_artif_fp; 260, else_clause; 260, 261; 261, block; 261, 262; 261, 271; 261, 279; 261, 286; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:err_msg; 265, parenthesized_expression; 265, 266; 266, binary_operator:%; 266, 267; 266, 270; 267, concatenated_string; 267, 268; 267, 269; 268, string:'No sequences left after artifact removal in '; 269, string:'file %s'; 270, identifier:seqs_fp; 271, expression_statement; 271, 272; 272, call; 272, 273; 272, 276; 273, attribute; 273, 274; 273, 275; 274, identifier:warnings; 275, identifier:warn; 276, argument_list; 276, 277; 276, 278; 277, identifier:err_msg; 278, identifier:UserWarning; 279, expression_statement; 279, 280; 280, call; 280, 281; 280, 284; 281, attribute; 281, 282; 281, 283; 282, identifier:logger; 283, identifier:warning; 284, argument_list; 284, 285; 285, identifier:err_msg; 286, return_statement; 286, 287; 287, None; 288, comment; 289, expression_statement; 289, 290; 290, assignment; 290, 291; 290, 292; 291, identifier:output_deblur_fp; 292, call; 292, 293; 292, 294; 293, identifier:join; 294, argument_list; 294, 295; 294, 296; 295, identifier:working_dir; 296, binary_operator:%; 296, 297; 296, 298; 297, string:"%s.deblur"; 298, call; 298, 299; 298, 300; 299, identifier:basename; 300, argument_list; 300, 301; 301, identifier:output_msa_fp; 302, with_statement; 302, 303; 302, 313; 303, with_clause; 303, 304; 304, with_item; 304, 305; 305, as_pattern; 305, 306; 305, 311; 306, call; 306, 307; 306, 308; 307, identifier:open; 308, argument_list; 308, 309; 308, 310; 309, identifier:output_deblur_fp; 310, string:'w'; 311, as_pattern_target; 311, 312; 312, identifier:f; 313, block; 313, 314; 313, 328; 313, 356; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 317; 316, identifier:seqs; 317, call; 317, 318; 317, 319; 318, identifier:deblur; 319, argument_list; 319, 320; 319, 324; 319, 325; 319, 326; 319, 327; 320, call; 320, 321; 320, 322; 321, identifier:sequence_generator; 322, argument_list; 322, 323; 323, identifier:output_msa_fp; 324, identifier:mean_error; 325, identifier:error_dist; 326, identifier:indel_prob; 327, identifier:indel_max; 328, if_statement; 328, 329; 328, 332; 329, comparison_operator:is; 329, 330; 329, 331; 330, identifier:seqs; 331, None; 332, block; 332, 333; 332, 345; 332, 354; 333, expression_statement; 333, 334; 334, call; 334, 335; 334, 338; 335, attribute; 335, 336; 335, 337; 336, identifier:warnings; 337, identifier:warn; 338, argument_list; 338, 339; 338, 344; 339, binary_operator:%; 339, 340; 339, 343; 340, concatenated_string; 340, 341; 340, 342; 341, string:'multiple sequence alignment file %s contains '; 342, string:'no sequences'; 343, identifier:output_msa_fp; 344, identifier:UserWarning; 345, expression_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, identifier:logger; 349, identifier:warn; 350, argument_list; 350, 351; 351, binary_operator:%; 351, 352; 351, 353; 352, string:'no sequences returned from deblur for file %s'; 353, identifier:output_msa_fp; 354, return_statement; 354, 355; 355, None; 356, for_statement; 356, 357; 356, 358; 356, 359; 356, 360; 357, identifier:s; 358, identifier:seqs; 359, comment; 360, block; 360, 361; 360, 375; 361, expression_statement; 361, 362; 362, assignment; 362, 363; 362, 366; 363, attribute; 363, 364; 363, 365; 364, identifier:s; 365, identifier:sequence; 366, call; 366, 367; 366, 372; 367, attribute; 367, 368; 367, 371; 368, attribute; 368, 369; 368, 370; 369, identifier:s; 370, identifier:sequence; 371, identifier:replace; 372, argument_list; 372, 373; 372, 374; 373, string:'-'; 374, string:''; 375, expression_statement; 375, 376; 376, call; 376, 377; 376, 380; 377, attribute; 377, 378; 377, 379; 378, identifier:f; 379, identifier:write; 380, argument_list; 380, 381; 381, call; 381, 382; 381, 385; 382, attribute; 382, 383; 382, 384; 383, identifier:s; 384, identifier:to_fasta; 385, argument_list; 386, comment; 387, expression_statement; 387, 388; 388, assignment; 388, 389; 388, 390; 389, identifier:output_no_chimeras_fp; 390, call; 390, 391; 390, 392; 391, identifier:remove_chimeras_denovo_from_seqs; 392, argument_list; 392, 393; 392, 394; 392, 395; 393, identifier:output_deblur_fp; 394, identifier:working_dir; 395, keyword_argument; 395, 396; 395, 397; 396, identifier:threads; 397, identifier:threads_per_sample; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:logger; 402, identifier:info; 403, argument_list; 403, 404; 404, string:'finished processing file'; 405, return_statement; 405, 406; 406, identifier:output_no_chimeras_fp
def launch_workflow(seqs_fp, working_dir, mean_error, error_dist, indel_prob, indel_max, trim_length, left_trim_length, min_size, ref_fp, ref_db_fp, threads_per_sample=1, sim_thresh=None, coverage_thresh=None): """Launch full deblur workflow for a single post split-libraries fasta file Parameters ---------- seqs_fp: string a post split library fasta file for debluring working_dir: string working directory path mean_error: float mean error for original sequence estimate error_dist: list list of error probabilities for each hamming distance indel_prob: float insertion/deletion (indel) probability indel_max: integer maximal indel number trim_length: integer sequence trim length left_trim_length: integer trim the first n reads min_size: integer upper limit on sequence abundance (discard sequences below limit) ref_fp: tuple filepath(s) to FASTA reference database for artifact removal ref_db_fp: tuple filepath(s) to SortMeRNA indexed database for artifact removal threads_per_sample: integer, optional number of threads to use for SortMeRNA/mafft/vsearch (0 for max available) sim_thresh: float, optional the minimal similarity for a sequence to the database. if None, take the defaults (0.65 for negate=False, 0.95 for negate=True) coverage_thresh: float, optional the minimal coverage for alignment of a sequence to the database. if None, take the defaults (0.3 for negate=False, 0.95 for negate=True) Return ------ output_no_chimers_fp : string filepath to fasta file with no chimeras of None if error encountered """ logger = logging.getLogger(__name__) logger.info('--------------------------------------------------------') logger.info('launch_workflow for file %s' % seqs_fp) # Step 1: Trim sequences to specified length output_trim_fp = join(working_dir, "%s.trim" % basename(seqs_fp)) with open(output_trim_fp, 'w') as out_f: for label, seq in trim_seqs( input_seqs=sequence_generator(seqs_fp), trim_len=trim_length, left_trim_len=left_trim_length): out_f.write(">%s\n%s\n" % (label, seq)) # Step 2: Dereplicate sequences output_derep_fp = join(working_dir, "%s.derep" % basename(output_trim_fp)) dereplicate_seqs(seqs_fp=output_trim_fp, output_fp=output_derep_fp, min_size=min_size, threads=threads_per_sample) # Step 3: Remove artifacts output_artif_fp, num_seqs_left, _ = remove_artifacts_seqs(seqs_fp=output_derep_fp, ref_fp=ref_fp, working_dir=working_dir, ref_db_fp=ref_db_fp, negate=True, threads=threads_per_sample, sim_thresh=sim_thresh) if not output_artif_fp: warnings.warn('Problem removing artifacts from file %s' % seqs_fp, UserWarning) logger.warning('remove artifacts failed, aborting') return None # Step 4: Multiple sequence alignment if num_seqs_left > 1: output_msa_fp = join(working_dir, "%s.msa" % basename(output_artif_fp)) alignment = multiple_sequence_alignment(seqs_fp=output_artif_fp, threads=threads_per_sample) if not alignment: warnings.warn('Problem performing multiple sequence alignment ' 'on file %s' % seqs_fp, UserWarning) logger.warning('msa failed. aborting') return None elif num_seqs_left == 1: # only one sequence after remove artifacts (but could be many reads) # no need to run MSA - just use the pre-msa file as input for next step output_msa_fp = output_artif_fp else: err_msg = ('No sequences left after artifact removal in ' 'file %s' % seqs_fp) warnings.warn(err_msg, UserWarning) logger.warning(err_msg) return None # Step 5: Launch deblur output_deblur_fp = join(working_dir, "%s.deblur" % basename(output_msa_fp)) with open(output_deblur_fp, 'w') as f: seqs = deblur(sequence_generator(output_msa_fp), mean_error, error_dist, indel_prob, indel_max) if seqs is None: warnings.warn('multiple sequence alignment file %s contains ' 'no sequences' % output_msa_fp, UserWarning) logger.warn('no sequences returned from deblur for file %s' % output_msa_fp) return None for s in seqs: # remove '-' from aligned sequences s.sequence = s.sequence.replace('-', '') f.write(s.to_fasta()) # Step 6: Chimera removal output_no_chimeras_fp = remove_chimeras_denovo_from_seqs( output_deblur_fp, working_dir, threads=threads_per_sample) logger.info('finished processing file') return output_no_chimeras_fp
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:logs; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 24; 5, 36; 5, 50; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 15; 9, not_operator; 9, 10; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:parent; 14, identifier:loaded; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:parent; 22, identifier:load; 23, argument_list; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:logs; 27, attribute; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:parent; 33, identifier:p; 34, identifier:logs_dir; 35, identifier:flat_directories; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:logs; 40, identifier:sort; 41, argument_list; 41, 42; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:key; 44, lambda; 44, 45; 44, 47; 45, lambda_parameters; 45, 46; 46, identifier:x; 47, attribute; 47, 48; 47, 49; 48, identifier:x; 49, identifier:mod_time; 50, return_statement; 50, 51; 51, identifier:logs
def logs(self): """Find the log directory and return all the logs sorted.""" if not self.parent.loaded: self.parent.load() logs = self.parent.p.logs_dir.flat_directories logs.sort(key=lambda x: x.mod_time) return logs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_string_by_pairs; 3, parameters; 3, 4; 4, identifier:strings; 5, block; 5, 6; 5, 8; 5, 17; 5, 21; 5, 28; 5, 29; 5, 83; 6, expression_statement; 6, 7; 7, comment; 8, assert_statement; 8, 9; 9, comparison_operator:==; 9, 10; 9, 16; 10, binary_operator:%; 10, 11; 10, 15; 11, call; 11, 12; 11, 13; 12, identifier:len; 13, argument_list; 13, 14; 14, identifier:strings; 15, integer:2; 16, integer:0; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:pairs; 20, list:[]; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:strings; 24, call; 24, 25; 24, 26; 25, identifier:list; 26, argument_list; 26, 27; 27, identifier:strings; 28, comment; 29, while_statement; 29, 30; 29, 31; 30, identifier:strings; 31, block; 31, 32; 31, 40; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:template; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:strings; 38, identifier:pop; 39, argument_list; 40, for_statement; 40, 41; 40, 44; 40, 48; 41, pattern_list; 41, 42; 41, 43; 42, identifier:i; 43, identifier:candidate; 44, call; 44, 45; 44, 46; 45, identifier:enumerate; 46, argument_list; 46, 47; 47, identifier:strings; 48, block; 48, 49; 49, if_statement; 49, 50; 49, 57; 50, comparison_operator:==; 50, 51; 50, 56; 51, call; 51, 52; 51, 53; 52, identifier:count_string_diff; 53, argument_list; 53, 54; 53, 55; 54, identifier:template; 55, identifier:candidate; 56, integer:1; 57, block; 57, 58; 57, 69; 57, 75; 57, 82; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:pair; 61, list:[template, strings.pop(i)]; 61, 62; 61, 63; 62, identifier:template; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:strings; 66, identifier:pop; 67, argument_list; 67, 68; 68, identifier:i; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:pair; 73, identifier:sort; 74, argument_list; 75, expression_statement; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:pairs; 79, identifier:append; 80, argument_list; 80, 81; 81, identifier:pair; 82, break_statement; 83, return_statement; 83, 84; 84, identifier:pairs
def sort_string_by_pairs(strings): """Group a list of strings by pairs, by matching those with only one character difference between each other together.""" assert len(strings) % 2 == 0 pairs = [] strings = list(strings) # This shallow copies the list while strings: template = strings.pop() for i, candidate in enumerate(strings): if count_string_diff(template, candidate) == 1: pair = [template, strings.pop(i)] pair.sort() pairs.append(pair) break return pairs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:natural_sort; 3, parameters; 3, 4; 4, identifier:item; 5, block; 5, 6; 5, 8; 5, 17; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:dre; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:re; 14, identifier:compile; 15, argument_list; 15, 16; 16, string:r'(\d+)'; 17, return_statement; 17, 18; 18, list_comprehension; 18, 19; 18, 34; 19, conditional_expression:if; 19, 20; 19, 24; 19, 29; 20, call; 20, 21; 20, 22; 21, identifier:int; 22, argument_list; 22, 23; 23, identifier:s; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:s; 27, identifier:isdigit; 28, argument_list; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:s; 32, identifier:lower; 33, argument_list; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:s; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:re; 39, identifier:split; 40, argument_list; 40, 41; 40, 42; 41, identifier:dre; 42, identifier:item
def natural_sort(item): """ Sort strings that contain numbers correctly. Works in Python 2 and 3. >>> l = ['v1.3.12', 'v1.3.3', 'v1.2.5', 'v1.2.15', 'v1.2.3', 'v1.2.1'] >>> l.sort(key=natural_sort) >>> l.__repr__() "['v1.2.1', 'v1.2.3', 'v1.2.5', 'v1.2.15', 'v1.3.3', 'v1.3.12']" """ dre = re.compile(r'(\d+)') return [int(s) if s.isdigit() else s.lower() for s in re.split(dre, item)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_elementary_intervals; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:features; 6, block; 6, 7; 6, 9; 6, 13; 6, 63; 6, 73; 6, 79; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:coords; 12, list:[]; 13, try_statement; 13, 14; 13, 50; 14, block; 14, 15; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:interval; 17, identifier:features; 18, block; 18, 19; 18, 37; 19, if_statement; 19, 20; 19, 26; 20, comparison_operator:!=; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:interval; 25, integer:3; 26, block; 26, 27; 27, raise_statement; 27, 28; 28, call; 28, 29; 28, 30; 29, identifier:SyntaxError; 30, argument_list; 30, 31; 31, binary_operator:%; 31, 32; 31, 33; 32, string:'Interval malformed %s. Allways specify start and end position for interval.'; 33, call; 33, 34; 33, 35; 34, identifier:str; 35, argument_list; 35, 36; 36, identifier:interval; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:coords; 41, identifier:extend; 42, argument_list; 42, 43; 43, list:[interval[0],interval[1]]; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:interval; 46, integer:0; 47, subscript; 47, 48; 47, 49; 48, identifier:interval; 49, integer:1; 50, except_clause; 50, 51; 50, 52; 51, identifier:IndexError; 52, block; 52, 53; 53, raise_statement; 53, 54; 54, call; 54, 55; 54, 56; 55, identifier:SyntaxError; 56, argument_list; 56, 57; 57, binary_operator:%; 57, 58; 57, 59; 58, string:'Interval malformed %s. Allways specify start and end position for interval.'; 59, call; 59, 60; 59, 61; 60, identifier:str; 61, argument_list; 61, 62; 62, identifier:interval; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:coords; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 71; 70, identifier:set; 71, argument_list; 71, 72; 72, identifier:coords; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:coords; 77, identifier:sort; 78, argument_list; 79, return_statement; 79, 80; 80, identifier:coords
def get_elementary_intervals(self, features): """Generates a sorted list of elementary intervals""" coords = [] try: for interval in features: if len(interval) != 3: raise SyntaxError('Interval malformed %s. Allways specify start and end position for interval.' % str(interval)) coords.extend([interval[0],interval[1]]) except IndexError: raise SyntaxError('Interval malformed %s. Allways specify start and end position for interval.' % str(interval)) coords = list(set(coords)) coords.sort() return coords
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:convtable2dict; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:convtable; 5, identifier:locale; 6, default_parameter; 6, 7; 6, 8; 7, identifier:update; 8, None; 9, block; 9, 10; 9, 12; 9, 23; 9, 102; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:rdict; 15, conditional_expression:if; 15, 16; 15, 21; 15, 22; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:update; 19, identifier:copy; 20, argument_list; 21, identifier:update; 22, dictionary; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:r; 25, identifier:convtable; 26, block; 26, 27; 27, if_statement; 27, 28; 27, 31; 27, 47; 27, 78; 28, comparison_operator:in; 28, 29; 28, 30; 29, string:':uni'; 30, identifier:r; 31, block; 31, 32; 32, if_statement; 32, 33; 32, 36; 33, comparison_operator:in; 33, 34; 33, 35; 34, identifier:locale; 35, identifier:r; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 44; 39, subscript; 39, 40; 39, 41; 40, identifier:rdict; 41, subscript; 41, 42; 41, 43; 42, identifier:r; 43, string:':uni'; 44, subscript; 44, 45; 44, 46; 45, identifier:r; 46, identifier:locale; 47, elif_clause; 47, 48; 47, 56; 48, comparison_operator:==; 48, 49; 48, 55; 49, subscript; 49, 50; 49, 51; 50, identifier:locale; 51, slice; 51, 52; 51, 53; 52, colon; 53, unary_operator:-; 53, 54; 54, integer:1; 55, string:'zh-han'; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 61; 58, comparison_operator:in; 58, 59; 58, 60; 59, identifier:locale; 60, identifier:r; 61, block; 61, 62; 62, for_statement; 62, 63; 62, 64; 62, 69; 63, identifier:word; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:r; 67, identifier:values; 68, argument_list; 69, block; 69, 70; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:rdict; 74, identifier:word; 75, subscript; 75, 76; 75, 77; 76, identifier:r; 77, identifier:locale; 78, else_clause; 78, 79; 79, block; 79, 80; 79, 88; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:v; 83, call; 83, 84; 83, 85; 84, identifier:fallback; 85, argument_list; 85, 86; 85, 87; 86, identifier:locale; 87, identifier:r; 88, for_statement; 88, 89; 88, 90; 88, 95; 89, identifier:word; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:r; 93, identifier:values; 94, argument_list; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:rdict; 100, identifier:word; 101, identifier:v; 102, return_statement; 102, 103; 103, identifier:rdict
def convtable2dict(convtable, locale, update=None): """ Convert a list of conversion dict to a dict for a certain locale. >>> sorted(convtable2dict([{'zh-hk': '列斯', 'zh-hans': '利兹', 'zh': '利兹', 'zh-tw': '里茲'}, {':uni': '巨集', 'zh-cn': '宏'}], 'zh-cn').items()) [('列斯', '利兹'), ('利兹', '利兹'), ('巨集', '宏'), ('里茲', '利兹')] """ rdict = update.copy() if update else {} for r in convtable: if ':uni' in r: if locale in r: rdict[r[':uni']] = r[locale] elif locale[:-1] == 'zh-han': if locale in r: for word in r.values(): rdict[word] = r[locale] else: v = fallback(locale, r) for word in r.values(): rdict[word] = v return rdict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:re_sort; 3, parameters; 3, 4; 4, identifier:data; 5, block; 5, 6; 5, 8; 5, 19; 5, 23; 5, 40; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:keys; 11, call; 11, 12; 11, 13; 12, identifier:sorted; 13, argument_list; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:data; 17, identifier:keys; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:new_data; 22, dictionary; 23, for_statement; 23, 24; 23, 27; 23, 31; 24, pattern_list; 24, 25; 24, 26; 25, identifier:number; 26, identifier:key; 27, call; 27, 28; 27, 29; 28, identifier:enumerate; 29, argument_list; 29, 30; 30, identifier:keys; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:new_data; 36, identifier:number; 37, subscript; 37, 38; 37, 39; 38, identifier:data; 39, identifier:key; 40, return_statement; 40, 41; 41, identifier:new_data
def re_sort(data): """ A data with keys that are not enumerated sequentially will be re sorted and sequentially ordered. For example:: >>> data = {16: ('1', 'b'), 3: ('1', 'a')} >>> re_sort(data) >>> {0: ('1', 'a'), 1: ('1', 'b')} """ keys = sorted(data.keys()) new_data = {} for number, key in enumerate(keys): new_data[number] = data[key] return new_data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:natural_sorted; 3, parameters; 3, 4; 4, identifier:iterable; 5, block; 5, 6; 5, 8; 5, 36; 5, 45; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:sortkey; 10, parameters; 10, 11; 11, identifier:x; 12, block; 12, 13; 13, return_statement; 13, 14; 14, list_comprehension; 14, 15; 14, 27; 15, parenthesized_expression; 15, 16; 16, conditional_expression:if; 16, 17; 16, 21; 16, 26; 17, call; 17, 18; 17, 19; 18, identifier:int; 19, argument_list; 19, 20; 20, identifier:c; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:c; 24, identifier:isdigit; 25, argument_list; 26, identifier:c; 27, for_in_clause; 27, 28; 27, 29; 28, identifier:c; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:re; 32, identifier:split; 33, argument_list; 33, 34; 33, 35; 34, identifier:numbers; 35, identifier:x; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:numbers; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:re; 42, identifier:compile; 43, argument_list; 43, 44; 44, string:r'(\d+)'; 45, return_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:sorted; 48, argument_list; 48, 49; 48, 50; 49, identifier:iterable; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:key; 52, identifier:sortkey
def natural_sorted(iterable): """Return human sorted list of strings. E.g. for sorting file names. >>> natural_sorted(['f1', 'f2', 'f10']) ['f1', 'f2', 'f10'] """ def sortkey(x): return [(int(c) if c.isdigit() else c) for c in re.split(numbers, x)] numbers = re.compile(r'(\d+)') return sorted(iterable, key=sortkey)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:sort_keys; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:keys; 6, default_parameter; 6, 7; 6, 8; 7, identifier:order; 8, attribute; 8, 9; 8, 10; 9, identifier:QuerySet; 10, identifier:ASCENDING; 11, block; 11, 12; 11, 14; 11, 15; 11, 33; 11, 53; 11, 84; 12, expression_statement; 12, 13; 13, comment; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:missing_keys; 18, list_comprehension; 18, 19; 18, 20; 18, 23; 19, identifier:key; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:key; 22, identifier:keys; 23, if_clause; 23, 24; 24, not_operator; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, subscript; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_reverse_index; 32, identifier:key; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:keys_and_values; 36, list_comprehension; 36, 37; 36, 46; 36, 49; 37, tuple; 37, 38; 37, 39; 38, identifier:key; 39, subscript; 39, 40; 39, 45; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:_reverse_index; 44, identifier:key; 45, integer:0; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:key; 48, identifier:keys; 49, if_clause; 49, 50; 50, comparison_operator:not; 50, 51; 50, 52; 51, identifier:key; 52, identifier:missing_keys; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:sorted_keys; 56, list_comprehension; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:kv; 59, integer:0; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:kv; 62, call; 62, 63; 62, 64; 63, identifier:sorted; 64, argument_list; 64, 65; 64, 66; 64, 74; 65, identifier:keys_and_values; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:key; 68, lambda; 68, 69; 68, 71; 69, lambda_parameters; 69, 70; 70, identifier:x; 71, subscript; 71, 72; 71, 73; 72, identifier:x; 73, integer:1; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:reverse; 76, conditional_expression:if; 76, 77; 76, 78; 76, 83; 77, True; 78, comparison_operator:==; 78, 79; 78, 80; 79, identifier:order; 80, attribute; 80, 81; 80, 82; 81, identifier:QuerySet; 82, identifier:DESCENDING; 83, False; 84, if_statement; 84, 85; 84, 90; 84, 95; 84, 106; 85, comparison_operator:==; 85, 86; 85, 87; 86, identifier:order; 87, attribute; 87, 88; 87, 89; 88, identifier:QuerySet; 89, identifier:ASCENDING; 90, block; 90, 91; 91, return_statement; 91, 92; 92, binary_operator:+; 92, 93; 92, 94; 93, identifier:missing_keys; 94, identifier:sorted_keys; 95, elif_clause; 95, 96; 95, 101; 96, comparison_operator:==; 96, 97; 96, 98; 97, identifier:order; 98, attribute; 98, 99; 98, 100; 99, identifier:QuerySet; 100, identifier:DESCENDING; 101, block; 101, 102; 102, return_statement; 102, 103; 103, binary_operator:+; 103, 104; 103, 105; 104, identifier:sorted_keys; 105, identifier:missing_keys; 106, else_clause; 106, 107; 107, block; 107, 108; 108, raise_statement; 108, 109; 109, call; 109, 110; 109, 111; 110, identifier:ValueError; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, string:'Unexpected order value: {:d}'; 115, identifier:format; 116, argument_list; 116, 117; 117, identifier:order
def sort_keys(self, keys, order=QuerySet.ASCENDING): """Sort keys. Keys are sorted based on the value they are indexing. :param keys: Keys to be sorted :type keys: list(str) :param order: Order criteri (asending or descending) :type order: int :return: Sorted keys :rtype: list(str) :raise ValueError: If invalid order value is passed """ # to do: check that all reverse index values are unambiguous missing_keys = [ key for key in keys if not len(self._reverse_index[key]) ] keys_and_values = [ (key, self._reverse_index[key][0]) for key in keys if key not in missing_keys ] sorted_keys = [ kv[0] for kv in sorted( keys_and_values, key=lambda x: x[1], reverse=True if order == QuerySet.DESCENDING else False) ] if order == QuerySet.ASCENDING: return missing_keys + sorted_keys elif order == QuerySet.DESCENDING: return sorted_keys + missing_keys else: raise ValueError('Unexpected order value: {:d}'.format(order))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:options; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 20; 5, 29; 5, 40; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:keys; 11, call; 11, 12; 11, 19; 12, attribute; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:__class__; 17, identifier:__dict__; 18, identifier:copy; 19, argument_list; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:keys; 24, identifier:update; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:__dict__; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:keys; 32, call; 32, 33; 32, 34; 33, identifier:sorted; 34, argument_list; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:keys; 38, identifier:keys; 39, argument_list; 40, for_statement; 40, 41; 40, 42; 40, 43; 41, identifier:opt; 42, identifier:keys; 43, block; 43, 44; 43, 53; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:val; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:get; 51, argument_list; 51, 52; 52, identifier:opt; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:is; 54, 55; 54, 56; 55, identifier:val; 56, None; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, yield; 59, 60; 60, expression_list; 60, 61; 60, 62; 61, identifier:opt; 62, identifier:val
def options(self): """ Returns an iterable of sorted option names in order to loop through all the configuration directives specified in the class. """ keys = self.__class__.__dict__.copy() keys.update(self.__dict__) keys = sorted(keys.keys()) for opt in keys: val = self.get(opt) if val is not None: yield opt, val
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 7; 2, function_name:_tidy; 3, parameters; 3, 4; 4, identifier:self; 5, type; 5, 6; 6, None; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 14; 10, 25; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:no_overlap; 14, block; 14, 15; 14, 24; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:remove_overlap; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:no_contiguous; 24, comment; 25, else_clause; 25, 26; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_sort; 32, argument_list
def _tidy(self) -> None: """ Removes overlaps, etc., and sorts. """ if self.no_overlap: self.remove_overlap(self.no_contiguous) # will sort else: self._sort()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 29; 1, 35; 2, function_name:index_list_for_sort_order; 3, parameters; 3, 4; 3, 12; 3, 24; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:x; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:List; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:Any; 12, typed_default_parameter; 12, 13; 12, 14; 12, 23; 13, identifier:key; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:Callable; 17, type_parameter; 17, 18; 17, 21; 18, type; 18, 19; 19, list:[Any]; 19, 20; 20, identifier:Any; 21, type; 21, 22; 22, identifier:Any; 23, None; 24, typed_default_parameter; 24, 25; 24, 26; 24, 28; 25, identifier:reverse; 26, type; 26, 27; 27, identifier:bool; 28, False; 29, type; 29, 30; 30, generic_type; 30, 31; 30, 32; 31, identifier:List; 32, type_parameter; 32, 33; 33, type; 33, 34; 34, identifier:int; 35, block; 35, 36; 35, 38; 35, 59; 35, 78; 35, 94; 36, expression_statement; 36, 37; 37, comment; 38, function_definition; 38, 39; 38, 40; 38, 51; 39, function_name:key_with_user_func; 40, parameters; 40, 41; 41, typed_parameter; 41, 42; 41, 43; 42, identifier:idx_val; 43, type; 43, 44; 44, generic_type; 44, 45; 44, 46; 45, identifier:Tuple; 46, type_parameter; 46, 47; 46, 49; 47, type; 47, 48; 48, identifier:int; 49, type; 49, 50; 50, identifier:Any; 51, block; 51, 52; 52, return_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:key; 55, argument_list; 55, 56; 56, subscript; 56, 57; 56, 58; 57, identifier:idx_val; 58, integer:1; 59, if_statement; 59, 60; 59, 61; 59, 67; 60, identifier:key; 61, block; 61, 62; 61, 66; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:sort_key; 65, identifier:key_with_user_func; 66, comment; 67, else_clause; 67, 68; 68, block; 68, 69; 68, 76; 68, 77; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:sort_key; 72, call; 72, 73; 72, 74; 73, identifier:itemgetter; 74, argument_list; 74, 75; 75, integer:1; 76, comment; 77, comment; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:index_value_list; 81, call; 81, 82; 81, 83; 82, identifier:sorted; 83, argument_list; 83, 84; 83, 88; 83, 91; 84, call; 84, 85; 84, 86; 85, identifier:enumerate; 86, argument_list; 86, 87; 87, identifier:x; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:key; 90, identifier:sort_key; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:reverse; 93, identifier:reverse; 94, return_statement; 94, 95; 95, list_comprehension; 95, 96; 95, 97; 96, identifier:i; 97, for_in_clause; 97, 98; 97, 101; 98, pattern_list; 98, 99; 98, 100; 99, identifier:i; 100, identifier:_; 101, identifier:index_value_list
def index_list_for_sort_order(x: List[Any], key: Callable[[Any], Any] = None, reverse: bool = False) -> List[int]: """ Returns a list of indexes of ``x``, IF ``x`` WERE TO BE SORTED. Args: x: data key: function to be applied to the data to generate a sort key; this function is passed as the ``key=`` parameter to :func:`sorted`; the default is ``itemgetter(1)`` reverse: reverse the sort order? Returns: list of integer index values Example: .. code-block:: python z = ["a", "c", "b"] index_list_for_sort_order(z) # [0, 2, 1] index_list_for_sort_order(z, reverse=True) # [1, 2, 0] q = [("a", 9), ("b", 8), ("c", 7)] index_list_for_sort_order(q, key=itemgetter(1)) """ def key_with_user_func(idx_val: Tuple[int, Any]): return key(idx_val[1]) if key: sort_key = key_with_user_func # see the simpler version below else: sort_key = itemgetter(1) # enumerate, below, will return tuples of (index, value), so # itemgetter(1) means sort by the value index_value_list = sorted(enumerate(x), key=sort_key, reverse=reverse) return [i for i, _ in index_value_list]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 1, 22; 2, function_name:sort_list_by_index_list; 3, parameters; 3, 4; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:x; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:List; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:Any; 12, typed_parameter; 12, 13; 12, 14; 13, identifier:indexes; 14, type; 14, 15; 15, generic_type; 15, 16; 15, 17; 16, identifier:List; 17, type_parameter; 17, 18; 18, type; 18, 19; 19, identifier:int; 20, type; 20, 21; 21, None; 22, block; 22, 23; 22, 25; 23, expression_statement; 23, 24; 24, comment; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 31; 27, subscript; 27, 28; 27, 29; 28, identifier:x; 29, slice; 29, 30; 30, colon; 31, list_comprehension; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:x; 34, identifier:i; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:i; 37, identifier:indexes
def sort_list_by_index_list(x: List[Any], indexes: List[int]) -> None: """ Re-orders ``x`` by the list of ``indexes`` of ``x``, in place. Example: .. code-block:: python from cardinal_pythonlib.lists import sort_list_by_index_list z = ["a", "b", "c", "d", "e"] sort_list_by_index_list(z, [4, 0, 1, 2, 3]) z # ["e", "a", "b", "c", "d"] """ x[:] = [x[i] for i in indexes]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 18; 2, function_name:fetch_all_first_values; 3, parameters; 3, 4; 3, 8; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:session; 6, type; 6, 7; 7, identifier:Session; 8, typed_parameter; 8, 9; 8, 10; 9, identifier:select_statement; 10, type; 10, 11; 11, identifier:Select; 12, type; 12, 13; 13, generic_type; 13, 14; 13, 15; 14, identifier:List; 15, type_parameter; 15, 16; 16, type; 16, 17; 17, identifier:Any; 18, block; 18, 19; 18, 21; 18, 30; 18, 31; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:rows; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:session; 27, identifier:execute; 28, argument_list; 28, 29; 29, identifier:select_statement; 30, comment; 31, try_statement; 31, 32; 31, 41; 32, block; 32, 33; 33, return_statement; 33, 34; 34, list_comprehension; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:row; 37, integer:0; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:row; 40, identifier:rows; 41, except_clause; 41, 42; 41, 46; 42, as_pattern; 42, 43; 42, 44; 43, identifier:ValueError; 44, as_pattern_target; 44, 45; 45, identifier:e; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:MultipleResultsFound; 50, argument_list; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:str; 53, argument_list; 53, 54; 54, identifier:e
def fetch_all_first_values(session: Session, select_statement: Select) -> List[Any]: """ Returns a list of the first values in each row returned by a ``SELECT`` query. A Core version of this sort of thing: http://xion.io/post/code/sqlalchemy-query-values.html Args: session: SQLAlchemy :class:`Session` object select_statement: SQLAlchemy :class:`Select` object Returns: a list of the first value of each result row """ rows = session.execute(select_statement) # type: ResultProxy try: return [row[0] for row in rows] except ValueError as e: raise MultipleResultsFound(str(e))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 1, 16; 2, function_name:atoi; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:text; 6, type; 6, 7; 7, identifier:str; 8, type; 8, 9; 9, generic_type; 9, 10; 9, 11; 10, identifier:Union; 11, type_parameter; 11, 12; 11, 14; 12, type; 12, 13; 13, identifier:int; 14, type; 14, 15; 15, identifier:str; 16, block; 16, 17; 16, 19; 17, expression_statement; 17, 18; 18, comment; 19, return_statement; 19, 20; 20, conditional_expression:if; 20, 21; 20, 25; 20, 30; 21, call; 21, 22; 21, 23; 22, identifier:int; 23, argument_list; 23, 24; 24, identifier:text; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:text; 28, identifier:isdigit; 29, argument_list; 30, identifier:text
def atoi(text: str) -> Union[int, str]: """ Converts strings to integers if they're composed of digits; otherwise returns the strings unchanged. One way of sorting strings with numbers; it will mean that ``"11"`` is more than ``"2"``. """ return int(text) if text.isdigit() else text
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:reducer_metro; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:metro; 6, identifier:values; 7, block; 7, 8; 7, 10; 7, 19; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:lookup; 13, call; 13, 14; 13, 15; 14, identifier:CachedLookup; 15, argument_list; 15, 16; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:precision; 18, identifier:POI_GEOHASH_PRECISION; 19, for_statement; 19, 20; 19, 23; 19, 27; 20, pattern_list; 20, 21; 20, 22; 21, identifier:i; 22, identifier:value; 23, call; 23, 24; 23, 25; 24, identifier:enumerate; 25, argument_list; 25, 26; 26, identifier:values; 27, block; 27, 28; 27, 35; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 34; 30, pattern_list; 30, 31; 30, 32; 30, 33; 31, identifier:type_tag; 32, identifier:lonlat; 33, identifier:data; 34, identifier:value; 35, if_statement; 35, 36; 35, 39; 35, 40; 35, 73; 36, comparison_operator:==; 36, 37; 36, 38; 37, identifier:type_tag; 38, integer:1; 39, comment; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:lookup; 45, identifier:insert; 46, argument_list; 46, 47; 46, 48; 47, identifier:i; 48, call; 48, 49; 48, 50; 49, identifier:dict; 50, argument_list; 50, 51; 50, 65; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:geometry; 53, call; 53, 54; 53, 55; 54, identifier:dict; 55, argument_list; 55, 56; 55, 59; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:type; 58, string:'Point'; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:coordinates; 61, call; 61, 62; 61, 63; 62, identifier:project; 63, argument_list; 63, 64; 64, identifier:lonlat; 65, keyword_argument; 65, 66; 65, 67; 66, identifier:properties; 67, call; 67, 68; 67, 69; 68, identifier:dict; 69, argument_list; 69, 70; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:tags; 72, identifier:data; 73, else_clause; 73, 74; 73, 75; 74, comment; 75, block; 75, 76; 75, 83; 75, 87; 75, 99; 75, 100; 75, 101; 75, 147; 76, if_statement; 76, 77; 76, 81; 77, not_operator; 77, 78; 78, attribute; 78, 79; 78, 80; 79, identifier:lookup; 80, identifier:data_store; 81, block; 81, 82; 82, return_statement; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:poi_names; 86, list:[]; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:kwargs; 90, call; 90, 91; 90, 92; 91, identifier:dict; 92, argument_list; 92, 93; 92, 96; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:buffer_size; 95, identifier:POI_DISTANCE; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:multiple; 98, True; 99, comment; 100, comment; 101, for_statement; 101, 102; 101, 103; 101, 111; 102, identifier:poi; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:lookup; 106, identifier:get; 107, argument_list; 107, 108; 107, 109; 108, identifier:lonlat; 109, dictionary_splat; 109, 110; 110, identifier:kwargs; 111, block; 111, 112; 111, 124; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:has_tag; 115, list_comprehension; 115, 116; 115, 121; 116, comparison_operator:in; 116, 117; 116, 118; 117, identifier:tag; 118, subscript; 118, 119; 118, 120; 119, identifier:poi; 120, string:'tags'; 121, for_in_clause; 121, 122; 121, 123; 122, identifier:tag; 123, identifier:POI_TAGS; 124, if_statement; 124, 125; 124, 135; 125, boolean_operator:and; 125, 126; 125, 130; 126, call; 126, 127; 126, 128; 127, identifier:any; 128, argument_list; 128, 129; 129, identifier:has_tag; 130, comparison_operator:in; 130, 131; 130, 132; 131, string:'name'; 132, subscript; 132, 133; 132, 134; 133, identifier:poi; 134, string:'tags'; 135, block; 135, 136; 136, expression_statement; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:poi_names; 140, identifier:append; 141, argument_list; 141, 142; 142, subscript; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:poi; 145, string:'tags'; 146, string:'name'; 147, for_statement; 147, 148; 147, 149; 147, 153; 148, identifier:poi; 149, call; 149, 150; 149, 151; 150, identifier:set; 151, argument_list; 151, 152; 152, identifier:poi_names; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, yield; 155, 156; 156, expression_list; 156, 157; 156, 160; 157, tuple; 157, 158; 157, 159; 158, identifier:metro; 159, identifier:poi; 160, integer:1
def reducer_metro(self, metro, values): """ Output tags of POI locations nearby tweet locations Values will be sorted coming into reducer. First element in each value tuple will be either 1 (osm POI) or 2 (geotweet). Build a spatial index with POI records. For each tweet lookup nearby POI, and emit tag values for predefined tags. """ lookup = CachedLookup(precision=POI_GEOHASH_PRECISION) for i, value in enumerate(values): type_tag, lonlat, data = value if type_tag == 1: # OSM POI node, construct geojson and add to Rtree index lookup.insert(i, dict( geometry=dict(type='Point', coordinates=project(lonlat)), properties=dict(tags=data) )) else: # geotweet, lookup nearest POI from index if not lookup.data_store: return poi_names = [] kwargs = dict(buffer_size=POI_DISTANCE, multiple=True) # lookup nearby POI from Rtree index (caching results) # for any tags we care about emit the tags value and 1 for poi in lookup.get(lonlat, **kwargs): has_tag = [ tag in poi['tags'] for tag in POI_TAGS ] if any(has_tag) and 'name' in poi['tags']: poi_names.append(poi['tags']['name']) for poi in set(poi_names): yield (metro, poi), 1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 1, 19; 2, function_name:list_file_extensions; 3, parameters; 3, 4; 3, 8; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:path; 6, type; 6, 7; 7, identifier:str; 8, typed_default_parameter; 8, 9; 8, 10; 8, 12; 9, identifier:reportevery; 10, type; 10, 11; 11, identifier:int; 12, integer:1; 13, type; 13, 14; 14, generic_type; 14, 15; 14, 16; 15, identifier:List; 16, type_parameter; 16, 17; 17, type; 17, 18; 18, identifier:str; 19, block; 19, 20; 19, 22; 19, 28; 19, 32; 19, 88; 20, expression_statement; 20, 21; 21, comment; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:extensions; 25, call; 25, 26; 25, 27; 26, identifier:set; 27, argument_list; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:count; 31, integer:0; 32, for_statement; 32, 33; 32, 37; 32, 43; 33, pattern_list; 33, 34; 33, 35; 33, 36; 34, identifier:root; 35, identifier:dirs; 36, identifier:files; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:os; 40, identifier:walk; 41, argument_list; 41, 42; 42, identifier:path; 43, block; 43, 44; 43, 48; 43, 64; 44, expression_statement; 44, 45; 45, augmented_assignment:+=; 45, 46; 45, 47; 46, identifier:count; 47, integer:1; 48, if_statement; 48, 49; 48, 54; 49, comparison_operator:==; 49, 50; 49, 53; 50, binary_operator:%; 50, 51; 50, 52; 51, identifier:count; 52, identifier:reportevery; 53, integer:0; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:log; 59, identifier:debug; 60, argument_list; 60, 61; 60, 62; 60, 63; 61, string:"Walking directory {}: {!r}"; 62, identifier:count; 63, identifier:root; 64, for_statement; 64, 65; 64, 66; 64, 67; 65, identifier:file; 66, identifier:files; 67, block; 67, 68; 67, 81; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 73; 70, pattern_list; 70, 71; 70, 72; 71, identifier:filename; 72, identifier:ext; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:os; 77, identifier:path; 78, identifier:splitext; 79, argument_list; 79, 80; 80, identifier:file; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:extensions; 85, identifier:add; 86, argument_list; 86, 87; 87, identifier:ext; 88, return_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:sorted; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:list; 94, argument_list; 94, 95; 95, identifier:extensions
def list_file_extensions(path: str, reportevery: int = 1) -> List[str]: """ Returns a sorted list of every file extension found in a directory and its subdirectories. Args: path: path to scan reportevery: report directory progress after every *n* steps Returns: sorted list of every file extension found """ extensions = set() count = 0 for root, dirs, files in os.walk(path): count += 1 if count % reportevery == 0: log.debug("Walking directory {}: {!r}", count, root) for file in files: filename, ext = os.path.splitext(file) extensions.add(ext) return sorted(list(extensions))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:sort_by_dependencies; 3, parameters; 3, 4; 3, 5; 4, identifier:exts; 5, default_parameter; 5, 6; 5, 7; 6, identifier:retry; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 31; 8, 45; 8, 163; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 16; 13, pattern_list; 13, 14; 13, 15; 14, identifier:sorted_ext; 15, identifier:features_from_sorted; 16, expression_list; 16, 17; 16, 18; 17, list:[]; 18, call; 18, 19; 18, 20; 19, identifier:set; 20, argument_list; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:pending; 24, list_comprehension; 24, 25; 24, 28; 25, tuple; 25, 26; 25, 27; 26, identifier:e; 27, integer:0; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:e; 30, identifier:exts; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:retry; 34, conditional_expression:if; 34, 35; 34, 41; 34, 44; 35, binary_operator:*; 35, 36; 35, 40; 36, call; 36, 37; 36, 38; 37, identifier:len; 38, argument_list; 38, 39; 39, identifier:exts; 40, integer:100; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:retry; 43, None; 44, identifier:retry; 45, while_statement; 45, 46; 45, 47; 46, identifier:pending; 47, block; 47, 48; 47, 59; 47, 91; 47, 99; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:ext; 52, identifier:cnt; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:pending; 56, identifier:pop; 57, argument_list; 57, 58; 58, integer:0; 59, if_statement; 59, 60; 59, 73; 60, boolean_operator:and; 60, 61; 60, 67; 61, not_operator; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:isinstance; 64, argument_list; 64, 65; 64, 66; 65, identifier:ext; 66, identifier:Extractor; 67, not_operator; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:issubclass; 70, argument_list; 70, 71; 70, 72; 71, identifier:ext; 72, identifier:Extractor; 73, block; 73, 74; 73, 78; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:msg; 77, string:"Only Extractor instances are allowed. Found {}."; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:TypeError; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:msg; 85, identifier:format; 86, argument_list; 86, 87; 87, call; 87, 88; 87, 89; 88, identifier:type; 89, argument_list; 89, 90; 90, identifier:ext; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:deps; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:ext; 97, identifier:get_dependencies; 98, argument_list; 99, if_statement; 99, 100; 99, 106; 99, 143; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:deps; 103, identifier:difference; 104, argument_list; 104, 105; 105, identifier:features_from_sorted; 106, block; 106, 107; 106, 132; 107, if_statement; 107, 108; 107, 113; 108, comparison_operator:>; 108, 109; 108, 112; 109, binary_operator:+; 109, 110; 109, 111; 110, identifier:cnt; 111, integer:1; 112, identifier:retry; 113, block; 113, 114; 113, 118; 114, expression_statement; 114, 115; 115, assignment; 115, 116; 115, 117; 116, identifier:msg; 117, string:"Maximun retry ({}) to sort achieved from extractor {}."; 118, raise_statement; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:RuntimeError; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:msg; 125, identifier:format; 126, argument_list; 126, 127; 126, 128; 127, identifier:retry; 128, call; 128, 129; 128, 130; 129, identifier:type; 130, argument_list; 130, 131; 131, identifier:ext; 132, expression_statement; 132, 133; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:pending; 136, identifier:append; 137, argument_list; 137, 138; 138, tuple; 138, 139; 138, 140; 139, identifier:ext; 140, binary_operator:+; 140, 141; 140, 142; 141, identifier:cnt; 142, integer:1; 143, else_clause; 143, 144; 144, block; 144, 145; 144, 152; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:sorted_ext; 149, identifier:append; 150, argument_list; 150, 151; 151, identifier:ext; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:features_from_sorted; 156, identifier:update; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:ext; 161, identifier:get_features; 162, argument_list; 163, return_statement; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:tuple; 166, argument_list; 166, 167; 167, identifier:sorted_ext
def sort_by_dependencies(exts, retry=None): """Calculate the Feature Extractor Resolution Order. """ sorted_ext, features_from_sorted = [], set() pending = [(e, 0) for e in exts] retry = len(exts) * 100 if retry is None else retry while pending: ext, cnt = pending.pop(0) if not isinstance(ext, Extractor) and not issubclass(ext, Extractor): msg = "Only Extractor instances are allowed. Found {}." raise TypeError(msg.format(type(ext))) deps = ext.get_dependencies() if deps.difference(features_from_sorted): if cnt + 1 > retry: msg = "Maximun retry ({}) to sort achieved from extractor {}." raise RuntimeError(msg.format(retry, type(ext))) pending.append((ext, cnt + 1)) else: sorted_ext.append(ext) features_from_sorted.update(ext.get_features()) return tuple(sorted_ext)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_sort_layers; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:_layers; 13, call; 13, 14; 13, 15; 14, identifier:OrderedDict; 15, argument_list; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:sorted; 18, argument_list; 18, 19; 18, 26; 19, call; 19, 20; 19, 25; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:_layers; 24, identifier:items; 25, argument_list; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:key; 28, lambda; 28, 29; 28, 31; 29, lambda_parameters; 29, 30; 30, identifier:t; 31, subscript; 31, 32; 31, 33; 32, identifier:t; 33, integer:0
def _sort_layers(self): """Sort the layers by depth.""" self._layers = OrderedDict(sorted(self._layers.items(), key=lambda t: t[0]))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort_sections; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:order; 6, block; 6, 7; 6, 9; 6, 21; 6, 43; 6, 69; 6, 81; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:order_lc; 12, list_comprehension; 12, 13; 12, 18; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:e; 16, identifier:lower; 17, argument_list; 18, for_in_clause; 18, 19; 18, 20; 19, identifier:e; 20, identifier:order; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:sections; 24, call; 24, 25; 24, 26; 25, identifier:OrderedDict; 26, generator_expression; 26, 27; 26, 34; 26, 37; 27, tuple; 27, 28; 27, 29; 28, identifier:k; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:sections; 33, identifier:k; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:k; 36, identifier:order_lc; 37, if_clause; 37, 38; 38, comparison_operator:in; 38, 39; 38, 40; 39, identifier:k; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:sections; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:sections; 47, identifier:update; 48, generator_expression; 48, 49; 48, 56; 48, 65; 49, tuple; 49, 50; 49, 51; 50, identifier:k; 51, subscript; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:self; 54, identifier:sections; 55, identifier:k; 56, for_in_clause; 56, 57; 56, 58; 57, identifier:k; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:sections; 63, identifier:keys; 64, argument_list; 65, if_clause; 65, 66; 66, comparison_operator:not; 66, 67; 66, 68; 67, identifier:k; 68, identifier:order_lc; 69, assert_statement; 69, 70; 70, comparison_operator:==; 70, 71; 70, 77; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, attribute; 74, 75; 74, 76; 75, identifier:self; 76, identifier:sections; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:sections; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:self; 85, identifier:sections; 86, identifier:sections
def sort_sections(self, order): """ Sort sections according to the section names in the order list. All remaining sections are added to the end in their original order :param order: Iterable of section names :return: """ order_lc = [e.lower() for e in order] sections = OrderedDict( (k,self.sections[k]) for k in order_lc if k in self.sections) sections.update( (k,self.sections[k]) for k in self.sections.keys() if k not in order_lc) assert len(self.sections) == len(sections) self.sections = sections
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:split_iterable_as_iterable; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:values; 6, block; 6, 7; 6, 9; 6, 19; 6, 25; 6, 66; 6, 77; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:values; 12, call; 12, 13; 12, 14; 13, identifier:iter; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:enumerate; 17, argument_list; 17, 18; 18, identifier:values; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:cache; 22, call; 22, 23; 22, 24; 23, identifier:dict; 24, argument_list; 25, function_definition; 25, 26; 25, 27; 25, 29; 26, function_name:get_value; 27, parameters; 27, 28; 28, identifier:ti; 29, block; 29, 30; 30, try_statement; 30, 31; 30, 39; 31, block; 31, 32; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:cache; 36, identifier:pop; 37, argument_list; 37, 38; 38, identifier:ti; 39, except_clause; 39, 40; 40, block; 40, 41; 41, while_statement; 41, 42; 41, 43; 42, True; 43, block; 43, 44; 43, 53; 43, 60; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 49; 46, pattern_list; 46, 47; 46, 48; 47, identifier:i; 48, identifier:v; 49, call; 49, 50; 49, 51; 50, identifier:next; 51, argument_list; 51, 52; 52, identifier:values; 53, if_statement; 53, 54; 53, 57; 54, comparison_operator:==; 54, 55; 54, 56; 55, identifier:i; 56, identifier:ti; 57, block; 57, 58; 58, return_statement; 58, 59; 59, identifier:v; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:cache; 64, identifier:i; 65, identifier:v; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:s; 69, call; 69, 70; 69, 71; 70, identifier:iter; 71, argument_list; 71, 72; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:index; 76, identifier:sorter; 77, for_statement; 77, 78; 77, 79; 77, 82; 78, identifier:c; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:count; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, yield; 84, 85; 85, generator_expression; 85, 86; 85, 90; 86, call; 86, 87; 86, 88; 87, identifier:get_value; 88, argument_list; 88, 89; 89, identifier:i; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:i; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:itertools; 95, identifier:islice; 96, argument_list; 96, 97; 96, 98; 97, identifier:s; 98, call; 98, 99; 98, 100; 99, identifier:int; 100, argument_list; 100, 101; 101, identifier:c
def split_iterable_as_iterable(self, values): """Group iterable into iterables, in the order of the keys Parameters ---------- values : iterable of length equal to keys iterable of values to be grouped Yields ------ iterable of items in values Notes ----- Memory consumption depends on the amount of sorting required Worst case, if index.sorter[-1] = 0, we need to consume the entire value iterable, before we can start yielding any output But to the extent that the keys are already sorted, the grouping is lazy """ values = iter(enumerate(values)) cache = dict() def get_value(ti): try: return cache.pop(ti) except: while True: i, v = next(values) if i==ti: return v cache[i] = v s = iter(self.index.sorter) for c in self.count: yield (get_value(i) for i in itertools.islice(s, int(c)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:as_index; 3, parameters; 3, 4; 3, 5; 3, 10; 3, 13; 3, 16; 4, identifier:keys; 5, default_parameter; 5, 6; 5, 7; 6, identifier:axis; 7, attribute; 7, 8; 7, 9; 8, identifier:semantics; 9, identifier:axis_default; 10, default_parameter; 10, 11; 10, 12; 11, identifier:base; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:stable; 15, True; 16, default_parameter; 16, 17; 16, 18; 17, identifier:lex_as_struct; 18, False; 19, block; 19, 20; 19, 22; 19, 53; 19, 79; 19, 97; 19, 110; 20, expression_statement; 20, 21; 21, comment; 22, if_statement; 22, 23; 22, 28; 23, call; 23, 24; 23, 25; 24, identifier:isinstance; 25, argument_list; 25, 26; 25, 27; 26, identifier:keys; 27, identifier:Index; 28, block; 28, 29; 29, if_statement; 29, 30; 29, 40; 29, 48; 30, boolean_operator:and; 30, 31; 30, 37; 31, comparison_operator:is; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:type; 34, argument_list; 34, 35; 35, identifier:keys; 36, identifier:BaseIndex; 37, comparison_operator:==; 37, 38; 37, 39; 38, identifier:base; 39, False; 40, block; 40, 41; 40, 47; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:keys; 44, attribute; 44, 45; 44, 46; 45, identifier:keys; 46, identifier:keys; 47, comment; 48, else_clause; 48, 49; 49, block; 49, 50; 49, 52; 50, return_statement; 50, 51; 51, identifier:keys; 52, comment; 53, if_statement; 53, 54; 53, 59; 54, call; 54, 55; 54, 56; 55, identifier:isinstance; 56, argument_list; 56, 57; 56, 58; 57, identifier:keys; 58, identifier:tuple; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 62; 60, 71; 61, identifier:lex_as_struct; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:keys; 66, call; 66, 67; 66, 68; 67, identifier:as_struct_array; 68, argument_list; 68, 69; 69, list_splat; 69, 70; 70, identifier:keys; 71, else_clause; 71, 72; 72, block; 72, 73; 73, return_statement; 73, 74; 74, call; 74, 75; 74, 76; 75, identifier:LexIndex; 76, argument_list; 76, 77; 76, 78; 77, identifier:keys; 78, identifier:stable; 79, try_statement; 79, 80; 79, 90; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:keys; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:np; 87, identifier:asarray; 88, argument_list; 88, 89; 89, identifier:keys; 90, except_clause; 90, 91; 91, block; 91, 92; 92, raise_statement; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:TypeError; 95, argument_list; 95, 96; 96, string:'Given object does not form a valid set of keys'; 97, if_statement; 97, 98; 97, 101; 98, comparison_operator:is; 98, 99; 98, 100; 99, identifier:axis; 100, None; 101, block; 101, 102; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:keys; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:keys; 108, identifier:flatten; 109, argument_list; 110, if_statement; 110, 111; 110, 116; 110, 135; 111, comparison_operator:==; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:keys; 114, identifier:ndim; 115, integer:1; 116, block; 116, 117; 117, if_statement; 117, 118; 117, 119; 117, 125; 118, identifier:base; 119, block; 119, 120; 120, return_statement; 120, 121; 121, call; 121, 122; 121, 123; 122, identifier:BaseIndex; 123, argument_list; 123, 124; 124, identifier:keys; 125, else_clause; 125, 126; 126, block; 126, 127; 127, return_statement; 127, 128; 128, call; 128, 129; 128, 130; 129, identifier:Index; 130, argument_list; 130, 131; 130, 132; 131, identifier:keys; 132, keyword_argument; 132, 133; 132, 134; 133, identifier:stable; 134, identifier:stable; 135, else_clause; 135, 136; 136, block; 136, 137; 137, return_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:ObjectIndex; 140, argument_list; 140, 141; 140, 142; 140, 143; 141, identifier:keys; 142, identifier:axis; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:stable; 145, identifier:stable
def as_index(keys, axis=semantics.axis_default, base=False, stable=True, lex_as_struct=False): """ casting rules for a keys object to an index object the preferred semantics is that keys is a sequence of key objects, except when keys is an instance of tuple, in which case the zipped elements of the tuple are the key objects the axis keyword specifies the axis which enumerates the keys if axis is None, the keys array is flattened if axis is 0, the first axis enumerates the keys which of these two is the default depends on whether backwards_compatible == True if base==True, the most basic index possible is constructed. this avoids an indirect sort; if it isnt required, this has better performance """ if isinstance(keys, Index): if type(keys) is BaseIndex and base==False: keys = keys.keys #need to upcast to an indirectly sorted index type else: return keys #already done here if isinstance(keys, tuple): if lex_as_struct: keys = as_struct_array(*keys) else: return LexIndex(keys, stable) try: keys = np.asarray(keys) except: raise TypeError('Given object does not form a valid set of keys') if axis is None: keys = keys.flatten() if keys.ndim==1: if base: return BaseIndex(keys) else: return Index(keys, stable=stable) else: return ObjectIndex(keys, axis, stable=stable)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:axis_as_object; 3, parameters; 3, 4; 3, 5; 4, identifier:arr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:axis; 7, unary_operator:-; 7, 8; 8, integer:1; 9, block; 9, 10; 9, 12; 9, 18; 9, 19; 9, 37; 9, 38; 9, 50; 9, 51; 9, 64; 9, 65; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:shape; 15, attribute; 15, 16; 15, 17; 16, identifier:arr; 17, identifier:shape; 18, comment; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:arr; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:np; 25, identifier:ascontiguousarray; 26, argument_list; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:np; 30, identifier:rollaxis; 31, argument_list; 31, 32; 31, 33; 31, 34; 32, identifier:arr; 33, identifier:axis; 34, attribute; 34, 35; 34, 36; 35, identifier:arr; 36, identifier:ndim; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:nbytes; 41, binary_operator:*; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:arr; 45, identifier:dtype; 46, identifier:itemsize; 47, subscript; 47, 48; 47, 49; 48, identifier:shape; 49, identifier:axis; 50, comment; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:voidtype; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:np; 57, identifier:dtype; 58, argument_list; 58, 59; 59, tuple; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:np; 62, identifier:void; 63, identifier:nbytes; 64, comment; 65, return_statement; 65, 66; 66, call; 66, 67; 66, 75; 67, attribute; 67, 68; 67, 74; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:arr; 71, identifier:view; 72, argument_list; 72, 73; 73, identifier:voidtype; 74, identifier:reshape; 75, argument_list; 75, 76; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:delete; 80, argument_list; 80, 81; 80, 82; 81, identifier:shape; 82, identifier:axis
def axis_as_object(arr, axis=-1): """cast the given axis of an array to a void object if the axis to be cast is contiguous, a view is returned, otherwise a copy is made this is useful for efficiently sorting by the content of an axis, for instance Parameters ---------- arr : ndarray array to view as void object type axis : int axis to view as a void object type Returns ------- ndarray array with the given axis viewed as a void object """ shape = arr.shape # make axis to be viewed as a void object as contiguous items arr = np.ascontiguousarray(np.rollaxis(arr, axis, arr.ndim)) # number of bytes in each void object nbytes = arr.dtype.itemsize * shape[axis] # void type with the correct number of bytes voidtype = np.dtype((np.void, nbytes)) # return the view as such, with the reduced shape return arr.view(voidtype).reshape(np.delete(shape, axis))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:rank; 3, parameters; 3, 4; 3, 5; 4, identifier:keys; 5, default_parameter; 5, 6; 5, 7; 6, identifier:axis; 7, attribute; 7, 8; 7, 9; 8, identifier:semantics; 9, identifier:axis_default; 10, block; 10, 11; 10, 13; 10, 21; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:index; 16, call; 16, 17; 16, 18; 17, identifier:as_index; 18, argument_list; 18, 19; 18, 20; 19, identifier:keys; 20, identifier:axis; 21, return_statement; 21, 22; 22, attribute; 22, 23; 22, 24; 23, identifier:index; 24, identifier:rank
def rank(keys, axis=semantics.axis_default): """where each item is in the pecking order. Parameters ---------- keys : indexable object Returns ------- ndarray, [keys.size], int unique integers, ranking the sorting order Notes ----- we should have that index.sorted[index.rank] == keys """ index = as_index(keys, axis) return index.rank
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_by; 3, parameters; 3, 4; 4, identifier:function; 5, block; 5, 6; 5, 8; 5, 18; 5, 30; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:f; 11, call; 11, 12; 11, 13; 12, identifier:partial; 13, argument_list; 13, 14; 13, 15; 14, identifier:sorted; 15, keyword_argument; 15, 16; 15, 17; 16, identifier:key; 17, identifier:function; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:f; 22, identifier:attrs; 23, dictionary; 23, 24; 24, pair; 24, 25; 24, 26; 25, string:'descending'; 26, call; 26, 27; 26, 28; 27, identifier:_descending_sort_by; 28, argument_list; 28, 29; 29, identifier:function; 30, return_statement; 30, 31; 31, identifier:f
def sort_by(function): """ Sorts an incoming sequence by using the given `function` as key. >>> range(10) > sort_by(-X) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Supports automatic data-structure creation:: users > sort_by([X.last_name, X.first_name]) There is also a shortcut for ``sort_by(X)`` called ``sort``: >>> [4, 5, 8, -3, 0] > sort [-3, 0, 4, 5, 8] And (as of ``0.2.3``) a shortcut for reversing the sort: >>> 'asdfaSfa' > sort_by(X.lower()).descending ['s', 'S', 'f', 'f', 'd', 'a', 'a', 'a'] """ f = partial(sorted, key=function) f.attrs = {'descending': _descending_sort_by(function)} return f
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:in_sorted; 3, parameters; 3, 4; 3, 5; 4, identifier:values; 5, identifier:value; 6, block; 6, 7; 6, 9; 6, 19; 6, 29; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:index; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:bisect; 15, identifier:bisect_left; 16, argument_list; 16, 17; 16, 18; 17, identifier:values; 18, identifier:value; 19, if_statement; 19, 20; 19, 26; 20, comparison_operator:>=; 20, 21; 20, 22; 21, identifier:index; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:values; 26, block; 26, 27; 27, return_statement; 27, 28; 28, False; 29, return_statement; 29, 30; 30, comparison_operator:==; 30, 31; 30, 34; 31, subscript; 31, 32; 31, 33; 32, identifier:values; 33, identifier:index; 34, identifier:value
def in_sorted(values, value): """Checks if a value is in a sorted list. Uses the :mod:`bisect` builtin to find the insertion point for ``value``. Args: values (List[int]): Integers sorted in ascending order. value (int): Value to check if contained in ``values``. Returns: bool: Indicating if the value is contained. """ index = bisect.bisect_left(values, value) if index >= len(values): return False return values[index] == value
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:handle_keypress; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:k; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 9, 26; 9, 40; 9, 54; 10, comparison_operator:==; 10, 11; 10, 12; 11, identifier:k; 12, string:"esc"; 13, block; 13, 14; 13, 20; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:save_file; 19, argument_list; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:urwid; 24, identifier:ExitMainLoop; 25, argument_list; 26, elif_clause; 26, 27; 26, 30; 26, 31; 27, comparison_operator:==; 27, 28; 27, 29; 28, identifier:k; 29, string:"delete"; 30, comment; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, call; 33, 34; 33, 39; 34, attribute; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:walker; 38, identifier:combine_focus_with_next; 39, argument_list; 40, elif_clause; 40, 41; 40, 44; 40, 45; 41, comparison_operator:==; 41, 42; 41, 43; 42, identifier:k; 43, string:"backspace"; 44, comment; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 53; 48, attribute; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:self; 51, identifier:walker; 52, identifier:combine_focus_with_prev; 53, argument_list; 54, elif_clause; 54, 55; 54, 58; 54, 59; 55, comparison_operator:==; 55, 56; 55, 57; 56, identifier:k; 57, string:"enter"; 58, comment; 59, block; 59, 60; 59, 68; 59, 69; 59, 79; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:walker; 66, identifier:split_focus; 67, argument_list; 68, comment; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:view; 75, identifier:keypress; 76, argument_list; 76, 77; 76, 78; 77, identifier:size; 78, string:"down"; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:view; 85, identifier:keypress; 86, argument_list; 86, 87; 86, 88; 87, identifier:size; 88, string:"home"
def handle_keypress(self, k): """Last resort for keypresses.""" if k == "esc": self.save_file() raise urwid.ExitMainLoop() elif k == "delete": # delete at end of line self.walker.combine_focus_with_next() elif k == "backspace": # backspace at beginning of line self.walker.combine_focus_with_prev() elif k == "enter": # start new line self.walker.split_focus() # move the cursor to the new line and reset pref_col self.view.keypress(size, "down") self.view.keypress(size, "home")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sorted; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:fsort; 6, block; 6, 7; 6, 9; 6, 23; 6, 31; 7, expression_statement; 7, 8; 8, string:''' Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score. '''; 9, if_statement; 9, 10; 9, 14; 10, not_operator; 10, 11; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:params; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:params; 20, call; 20, 21; 20, 22; 21, identifier:dict; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 30; 25, subscript; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:params; 29, string:'sort'; 30, identifier:fsort; 31, return_statement; 31, 32; 32, identifier:self
def sorted(self, fsort): ''' Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score. ''' if not self.params: self.params = dict() self.params['sort'] = fsort return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_filter_sources; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:sources; 6, block; 6, 7; 6, 9; 6, 17; 6, 43; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, pattern_list; 11, 12; 11, 13; 12, identifier:filtered; 13, identifier:hosts; 14, expression_list; 14, 15; 14, 16; 15, list:[]; 16, list:[]; 17, for_statement; 17, 18; 17, 19; 17, 20; 18, identifier:source; 19, identifier:sources; 20, block; 20, 21; 20, 27; 20, 34; 21, if_statement; 21, 22; 21, 25; 22, comparison_operator:in; 22, 23; 22, 24; 23, string:'error'; 24, identifier:source; 25, block; 25, 26; 26, continue_statement; 27, expression_statement; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:filtered; 31, identifier:append; 32, argument_list; 32, 33; 33, identifier:source; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:hosts; 38, identifier:append; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 42; 41, identifier:source; 42, string:'host_name'; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:sorted; 46, argument_list; 46, 47; 46, 48; 47, identifier:filtered; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:key; 50, lambda; 50, 51; 50, 53; 51, lambda_parameters; 51, 52; 52, identifier:s; 53, call; 53, 54; 53, 62; 54, attribute; 54, 55; 54, 61; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:self; 58, identifier:_hosts_by_success; 59, argument_list; 59, 60; 60, identifier:hosts; 61, identifier:index; 62, argument_list; 62, 63; 63, subscript; 63, 64; 63, 65; 64, identifier:s; 65, string:'host_name'
def _filter_sources(self, sources): """Remove sources with errors and return ordered by host success. :param sources: List of potential sources to connect to. :type sources: list :returns: Sorted list of potential sources without errors. :rtype: list """ filtered, hosts = [], [] for source in sources: if 'error' in source: continue filtered.append(source) hosts.append(source['host_name']) return sorted(filtered, key=lambda s: self._hosts_by_success(hosts).index(s['host_name']))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_cantons; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:sorted; 11, argument_list; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:set; 17, argument_list; 17, 18; 18, list_comprehension; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:location; 21, identifier:canton; 22, for_in_clause; 22, 23; 22, 24; 23, identifier:location; 24, call; 24, 25; 24, 32; 25, attribute; 25, 26; 25, 31; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:get_locations; 30, argument_list; 31, identifier:values; 32, argument_list
def get_cantons(self): """ Return the list of unique cantons, sorted by name. """ return sorted(list(set([ location.canton for location in self.get_locations().values() ])))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_municipalities; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 11; 10, identifier:sorted; 11, argument_list; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:list; 14, argument_list; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:set; 17, argument_list; 17, 18; 18, list_comprehension; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:location; 21, identifier:municipality; 22, for_in_clause; 22, 23; 22, 24; 23, identifier:location; 24, call; 24, 25; 24, 32; 25, attribute; 25, 26; 25, 31; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:get_locations; 30, argument_list; 31, identifier:values; 32, argument_list
def get_municipalities(self): """ Return the list of unique municipalities, sorted by name. """ return sorted(list(set([ location.municipality for location in self.get_locations().values() ])))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:execute; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 8; 4, identifier:self; 5, identifier:sql; 6, list_splat_pattern; 6, 7; 7, identifier:args; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 10, 48; 10, 58; 10, 65; 11, expression_statement; 11, 12; 12, string:''' Run raw SQL on the database, and receive relaxing output. This is sort of the foundational method that most of the others build on. '''; 13, try_statement; 13, 14; 13, 26; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:cursor; 21, identifier:execute; 22, argument_list; 22, 23; 22, 24; 23, identifier:sql; 24, list_splat; 24, 25; 25, identifier:args; 26, except_clause; 26, 27; 26, 32; 26, 33; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:sqlite3; 31, identifier:InterfaceError; 32, identifier:msg; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:sqlite3; 40, identifier:InterfaceError; 41, argument_list; 41, 42; 42, binary_operator:+; 42, 43; 42, 47; 43, call; 43, 44; 43, 45; 44, identifier:unicode; 45, argument_list; 45, 46; 46, identifier:msg; 47, string:'\nTry converting types or pickling.'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:rows; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:self; 55, identifier:cursor; 56, identifier:fetchall; 57, argument_list; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:self; 62, identifier:__commit_if_necessary; 63, argument_list; 63, 64; 64, identifier:kwargs; 65, if_statement; 65, 66; 65, 73; 65, 76; 66, comparison_operator:==; 66, 67; 66, 68; 67, None; 68, attribute; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:cursor; 72, identifier:description; 73, block; 73, 74; 74, return_statement; 74, 75; 75, None; 76, else_clause; 76, 77; 77, block; 77, 78; 77, 97; 77, 112; 78, expression_statement; 78, 79; 79, assignment; 79, 80; 79, 81; 80, identifier:colnames; 81, list_comprehension; 81, 82; 81, 90; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:d; 86, integer:0; 87, identifier:decode; 88, argument_list; 88, 89; 89, string:'utf-8'; 90, for_in_clause; 90, 91; 90, 92; 91, identifier:d; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:self; 95, identifier:cursor; 96, identifier:description; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:rawdata; 100, list_comprehension; 100, 101; 100, 109; 101, call; 101, 102; 101, 103; 102, identifier:OrderedDict; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:zip; 106, argument_list; 106, 107; 106, 108; 107, identifier:colnames; 108, identifier:row; 109, for_in_clause; 109, 110; 109, 111; 110, identifier:row; 111, identifier:rows; 112, return_statement; 112, 113; 113, identifier:rawdata
def execute(self, sql, *args, **kwargs): ''' Run raw SQL on the database, and receive relaxing output. This is sort of the foundational method that most of the others build on. ''' try: self.cursor.execute(sql, *args) except self.sqlite3.InterfaceError, msg: raise self.sqlite3.InterfaceError(unicode(msg) + '\nTry converting types or pickling.') rows = self.cursor.fetchall() self.__commit_if_necessary(kwargs) if None == self.cursor.description: return None else: colnames = [d[0].decode('utf-8') for d in self.cursor.description] rawdata = [OrderedDict(zip(colnames,row)) for row in rows] return rawdata
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:check_message; 3, parameters; 3, 4; 3, 5; 4, identifier:message; 5, dictionary_splat_pattern; 5, 6; 6, identifier:kwargs; 7, block; 7, 8; 7, 10; 7, 31; 7, 41; 7, 52; 7, 63; 7, 67; 7, 76; 7, 100; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 18; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:kwargs; 14, identifier:pop; 15, argument_list; 15, 16; 15, 17; 16, string:"allow_empty"; 17, False; 18, block; 18, 19; 19, if_statement; 19, 20; 19, 28; 20, boolean_operator:or; 20, 21; 20, 23; 21, not_operator; 21, 22; 22, identifier:message; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:message; 26, identifier:isspace; 27, argument_list; 28, block; 28, 29; 29, return_statement; 29, 30; 30, list:[]; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:lines; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:re; 37, identifier:split; 38, argument_list; 38, 39; 38, 40; 39, string:r"\r\n|\r|\n"; 40, identifier:message; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:errors; 44, call; 44, 45; 44, 46; 45, identifier:_check_1st_line; 46, argument_list; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:lines; 49, integer:0; 50, dictionary_splat; 50, 51; 51, identifier:kwargs; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, pattern_list; 54, 55; 54, 56; 55, identifier:err; 56, identifier:signature_lines; 57, call; 57, 58; 57, 59; 58, identifier:_check_bullets; 59, argument_list; 59, 60; 59, 61; 60, identifier:lines; 61, dictionary_splat; 61, 62; 62, identifier:kwargs; 63, expression_statement; 63, 64; 64, augmented_assignment:+=; 64, 65; 64, 66; 65, identifier:errors; 66, identifier:err; 67, expression_statement; 67, 68; 68, augmented_assignment:+=; 68, 69; 68, 70; 69, identifier:errors; 70, call; 70, 71; 70, 72; 71, identifier:_check_signatures; 72, argument_list; 72, 73; 72, 74; 73, identifier:signature_lines; 74, dictionary_splat; 74, 75; 75, identifier:kwargs; 76, function_definition; 76, 77; 76, 78; 76, 82; 77, function_name:_format; 78, parameters; 78, 79; 78, 80; 78, 81; 79, identifier:code; 80, identifier:lineno; 81, identifier:args; 82, block; 82, 83; 83, return_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, string:"{0}: {1} {2}"; 87, identifier:format; 88, argument_list; 88, 89; 88, 90; 88, 91; 89, identifier:lineno; 90, identifier:code; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:_messages_codes; 95, identifier:code; 96, identifier:format; 97, argument_list; 97, 98; 98, list_splat; 98, 99; 99, identifier:args; 100, return_statement; 100, 101; 101, call; 101, 102; 101, 103; 102, identifier:list; 103, argument_list; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:map; 106, argument_list; 106, 107; 106, 124; 107, lambda; 107, 108; 107, 110; 108, lambda_parameters; 108, 109; 109, identifier:x; 110, call; 110, 111; 110, 112; 111, identifier:_format; 112, argument_list; 112, 113; 112, 116; 112, 119; 113, subscript; 113, 114; 113, 115; 114, identifier:x; 115, integer:0; 116, subscript; 116, 117; 116, 118; 117, identifier:x; 118, integer:1; 119, subscript; 119, 120; 119, 121; 120, identifier:x; 121, slice; 121, 122; 121, 123; 122, integer:2; 123, colon; 124, call; 124, 125; 124, 126; 125, identifier:sorted; 126, argument_list; 126, 127; 126, 128; 127, identifier:errors; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:key; 130, lambda; 130, 131; 130, 133; 131, lambda_parameters; 131, 132; 132, identifier:x; 133, subscript; 133, 134; 133, 135; 134, identifier:x; 135, integer:0
def check_message(message, **kwargs): """Check the message format. Rules: - the first line must start by a component name - and a short description (52 chars), - then bullet points are expected - and finally signatures. :param components: compontents, e.g. ``('auth', 'utils', 'misc')`` :type components: `list` :param signatures: signatures, e.g. ``('Signed-off-by', 'Reviewed-by')`` :type signatures: `list` :param alt_signatures: alternative signatures, e.g. ``('Tested-by',)`` :type alt_signatures: `list` :param trusted: optional list of reviewers, e.g. ``('[email protected]',)`` :type trusted: `list` :param max_length: optional maximum line length (by default: 72) :type max_length: int :param max_first_line: optional maximum first line length (by default: 50) :type max_first_line: int :param allow_empty: optional way to allow empty message (by default: False) :type allow_empty: bool :return: errors sorted by line number :rtype: `list` """ if kwargs.pop("allow_empty", False): if not message or message.isspace(): return [] lines = re.split(r"\r\n|\r|\n", message) errors = _check_1st_line(lines[0], **kwargs) err, signature_lines = _check_bullets(lines, **kwargs) errors += err errors += _check_signatures(signature_lines, **kwargs) def _format(code, lineno, args): return "{0}: {1} {2}".format(lineno, code, _messages_codes[code].format(*args)) return list(map(lambda x: _format(x[0], x[1], x[2:]), sorted(errors, key=lambda x: x[0])))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_getArrays; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:items; 5, identifier:attr; 6, identifier:defaultValue; 7, block; 7, 8; 7, 10; 7, 23; 7, 45; 7, 69; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:arrays; 13, call; 13, 14; 13, 15; 14, identifier:dict; 15, argument_list; 15, 16; 16, list_comprehension; 16, 17; 16, 20; 17, tuple; 17, 18; 17, 19; 18, identifier:key; 19, list:[]; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:key; 22, identifier:attr; 23, for_statement; 23, 24; 23, 25; 23, 26; 24, identifier:item; 25, identifier:items; 26, block; 26, 27; 27, for_statement; 27, 28; 27, 29; 27, 30; 28, identifier:key; 29, identifier:attr; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:arrays; 36, identifier:key; 37, identifier:append; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:getattr; 41, argument_list; 41, 42; 41, 43; 41, 44; 42, identifier:item; 43, identifier:key; 44, identifier:defaultValue; 45, for_statement; 45, 46; 45, 47; 45, 55; 46, identifier:key; 47, list_comprehension; 47, 48; 47, 49; 48, identifier:_; 49, for_in_clause; 49, 50; 49, 51; 50, identifier:_; 51, call; 51, 52; 51, 53; 52, identifier:viewkeys; 53, argument_list; 53, 54; 54, identifier:arrays; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 61; 58, subscript; 58, 59; 58, 60; 59, identifier:arrays; 60, identifier:key; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:numpy; 64, identifier:array; 65, argument_list; 65, 66; 66, subscript; 66, 67; 66, 68; 67, identifier:arrays; 68, identifier:key; 69, return_statement; 69, 70; 70, identifier:arrays
def _getArrays(items, attr, defaultValue): """Return arrays with equal size of item attributes from a list of sorted "items" for fast and convenient data processing. :param attr: list of item attributes that should be added to the returned array. :param defaultValue: if an item is missing an attribute, the "defaultValue" is added to the array instead. :returns: {'attribute1': numpy.array([attributeValue1, ...]), ...} """ arrays = dict([(key, []) for key in attr]) for item in items: for key in attr: arrays[key].append(getattr(item, key, defaultValue)) for key in [_ for _ in viewkeys(arrays)]: arrays[key] = numpy.array(arrays[key]) return arrays
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_log_board_ports; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:ports; 6, block; 6, 7; 6, 9; 6, 28; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:ports; 12, call; 12, 13; 12, 14; 13, identifier:sorted; 14, argument_list; 14, 15; 14, 16; 15, identifier:ports; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:key; 18, lambda; 18, 19; 18, 21; 19, lambda_parameters; 19, 20; 20, identifier:port; 21, tuple; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:port; 24, identifier:tile_id; 25, attribute; 25, 26; 25, 27; 26, identifier:port; 27, identifier:direction; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:_logln; 33, argument_list; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, string:'ports: {0}'; 37, identifier:format; 38, argument_list; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, string:' '; 42, identifier:join; 43, generator_expression; 43, 44; 43, 60; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, string:'{}({} {})'; 47, identifier:format; 48, argument_list; 48, 49; 48, 54; 48, 57; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:p; 52, identifier:type; 53, identifier:value; 54, attribute; 54, 55; 54, 56; 55, identifier:p; 56, identifier:tile_id; 57, attribute; 57, 58; 57, 59; 58, identifier:p; 59, identifier:direction; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:p; 62, identifier:ports
def _log_board_ports(self, ports): """ A board with no ports is allowed. In the logfile, ports must be sorted - ascending by tile identifier (primary) - alphabetical by edge direction (secondary) :param ports: list of catan.board.Port objects """ ports = sorted(ports, key=lambda port: (port.tile_id, port.direction)) self._logln('ports: {0}'.format(' '.join('{}({} {})'.format(p.type.value, p.tile_id, p.direction) for p in ports)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:attr; 6, block; 6, 7; 6, 9; 6, 28; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:entries; 14, call; 14, 15; 14, 27; 15, attribute; 15, 16; 15, 26; 16, call; 16, 17; 16, 18; 17, identifier:Sorter; 18, argument_list; 18, 19; 18, 22; 18, 25; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:entries; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:category; 25, identifier:attr; 26, identifier:sort_entries; 27, argument_list; 28, return_statement; 28, 29; 29, identifier:self
def sort(self, attr): """Sort the ratings based on an attribute""" self.entries = Sorter(self.entries, self.category, attr).sort_entries() return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_visit; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:L; 6, identifier:marked; 7, identifier:tempmarked; 8, block; 8, 9; 8, 11; 8, 16; 8, 26; 9, expression_statement; 9, 10; 10, comment; 11, assert_statement; 11, 12; 12, not_operator; 12, 13; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:is_pseudo; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:in; 17, 18; 17, 19; 18, identifier:self; 19, identifier:tempmarked; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:Exception; 24, argument_list; 24, 25; 25, string:'feature graph is cyclic'; 26, if_statement; 26, 27; 26, 30; 27, comparison_operator:not; 27, 28; 27, 29; 28, identifier:self; 29, identifier:marked; 30, block; 30, 31; 30, 37; 30, 43; 30, 66; 30, 85; 30, 106; 30, 112; 30, 116; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 36; 33, subscript; 33, 34; 33, 35; 34, identifier:tempmarked; 35, identifier:self; 36, True; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:features; 40, call; 40, 41; 40, 42; 41, identifier:list; 42, argument_list; 43, if_statement; 43, 44; 43, 53; 44, boolean_operator:and; 44, 45; 44, 50; 45, comparison_operator:is; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:siblings; 49, None; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:is_toplevel; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:features; 58, identifier:extend; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:reversed; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:siblings; 66, if_statement; 66, 67; 66, 72; 67, comparison_operator:is; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:children; 71, None; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:features; 77, identifier:extend; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:reversed; 81, argument_list; 81, 82; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:children; 85, if_statement; 85, 86; 85, 92; 86, comparison_operator:>; 86, 87; 86, 91; 87, call; 87, 88; 87, 89; 88, identifier:len; 89, argument_list; 89, 90; 90, identifier:features; 91, integer:0; 92, block; 92, 93; 93, for_statement; 93, 94; 93, 95; 93, 96; 94, identifier:feature; 95, identifier:features; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:feature; 101, identifier:_visit; 102, argument_list; 102, 103; 102, 104; 102, 105; 103, identifier:L; 104, identifier:marked; 105, identifier:tempmarked; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:marked; 110, identifier:self; 111, True; 112, delete_statement; 112, 113; 113, subscript; 113, 114; 113, 115; 114, identifier:tempmarked; 115, identifier:self; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:L; 120, identifier:insert; 121, argument_list; 121, 122; 121, 123; 122, integer:0; 123, identifier:self
def _visit(self, L, marked, tempmarked): """ Sort features topologically. This recursive function uses depth-first search to find an ordering of the features in the feature graph that is sorted both topologically and with respect to genome coordinates. Implementation based on Wikipedia's description of the algorithm in Cormen's *Introduction to Algorithms*. http://en.wikipedia.org/wiki/Topological_sorting#Algorithms There are potentially many valid topological sorts of a feature graph, but only one that is also sorted with respect to genome coordinates (excluding different orderings of, for example, exons and CDS features with the same coordinates). Iterating through feature children in reversed order (in this functions' inner-most loop) seems to be the key to sorting with respect to genome coordinates. """ assert not self.is_pseudo if self in tempmarked: raise Exception('feature graph is cyclic') if self not in marked: tempmarked[self] = True features = list() if self.siblings is not None and self.is_toplevel: features.extend(reversed(self.siblings)) if self.children is not None: features.extend(reversed(self.children)) if len(features) > 0: for feature in features: feature._visit(L, marked, tempmarked) marked[self] = True del tempmarked[self] L.insert(0, self)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:pseudoify; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 16; 5, 27; 5, 33; 5, 51; 5, 69; 5, 76; 5, 82; 5, 90; 5, 98; 5, 106; 5, 125; 5, 136; 5, 147; 6, expression_statement; 6, 7; 7, comment; 8, assert_statement; 8, 9; 9, attribute; 9, 10; 9, 11; 10, identifier:self; 11, identifier:is_toplevel; 12, assert_statement; 12, 13; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:is_multi; 16, assert_statement; 16, 17; 17, comparison_operator:>; 17, 18; 17, 26; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:multi_rep; 25, identifier:siblings; 26, integer:0; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:rep; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:multi_rep; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:start; 36, call; 36, 37; 36, 38; 37, identifier:min; 38, argument_list; 38, 39; 39, list_comprehension; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:s; 42, identifier:start; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:s; 45, binary_operator:+; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:rep; 48, identifier:siblings; 49, list:[rep]; 49, 50; 50, identifier:rep; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:end; 54, call; 54, 55; 54, 56; 55, identifier:max; 56, argument_list; 56, 57; 57, list_comprehension; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:s; 60, identifier:end; 61, for_in_clause; 61, 62; 61, 63; 62, identifier:s; 63, binary_operator:+; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:rep; 66, identifier:siblings; 67, list:[rep]; 67, 68; 68, identifier:rep; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:parent; 72, call; 72, 73; 72, 74; 73, identifier:Feature; 74, argument_list; 74, 75; 75, None; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:parent; 80, identifier:_pseudo; 81, True; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:parent; 86, identifier:_seqid; 87, attribute; 87, 88; 87, 89; 88, identifier:self; 89, identifier:_seqid; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:parent; 94, identifier:set_coord; 95, argument_list; 95, 96; 95, 97; 96, identifier:start; 97, identifier:end; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:parent; 102, identifier:_strand; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_strand; 106, for_statement; 106, 107; 106, 108; 106, 114; 107, identifier:sibling; 108, binary_operator:+; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:rep; 111, identifier:siblings; 112, list:[rep]; 112, 113; 113, identifier:rep; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:parent; 119, identifier:add_child; 120, argument_list; 120, 121; 120, 122; 121, identifier:sibling; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:rangecheck; 124, True; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:parent; 129, identifier:children; 130, call; 130, 131; 130, 132; 131, identifier:sorted; 132, argument_list; 132, 133; 133, attribute; 133, 134; 133, 135; 134, identifier:parent; 135, identifier:children; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:rep; 140, identifier:siblings; 141, call; 141, 142; 141, 143; 142, identifier:sorted; 143, argument_list; 143, 144; 144, attribute; 144, 145; 144, 146; 145, identifier:rep; 146, identifier:siblings; 147, return_statement; 147, 148; 148, identifier:parent
def pseudoify(self): """ Derive a pseudo-feature parent from the given multi-feature. The provided multi-feature does not need to be the representative. The newly created pseudo-feature has the same seqid as the provided multi- feature, and spans its entire range. Otherwise, the pseudo-feature is empty. It is used only for convenience in sorting. """ assert self.is_toplevel assert self.is_multi assert len(self.multi_rep.siblings) > 0 rep = self.multi_rep start = min([s.start for s in rep.siblings + [rep]]) end = max([s.end for s in rep.siblings + [rep]]) parent = Feature(None) parent._pseudo = True parent._seqid = self._seqid parent.set_coord(start, end) parent._strand = self._strand for sibling in rep.siblings + [rep]: parent.add_child(sibling, rangecheck=True) parent.children = sorted(parent.children) rep.siblings = sorted(rep.siblings) return parent
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:attribute_crawl; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:key; 6, block; 6, 7; 6, 9; 6, 15; 6, 46; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:union; 12, call; 12, 13; 12, 14; 13, identifier:set; 14, argument_list; 15, for_statement; 15, 16; 15, 17; 15, 18; 16, identifier:feature; 17, identifier:self; 18, block; 18, 19; 18, 31; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:values; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:feature; 25, identifier:get_attribute; 26, argument_list; 26, 27; 26, 28; 27, identifier:key; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:as_list; 30, True; 31, if_statement; 31, 32; 31, 35; 32, comparison_operator:is; 32, 33; 32, 34; 33, identifier:values; 34, None; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:union; 40, identifier:update; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:set; 44, argument_list; 44, 45; 45, identifier:values; 46, return_statement; 46, 47; 47, identifier:union
def attribute_crawl(self, key): """ Grab all attribute values associated with the given feature. Traverse the given feature (and all of its descendants) to find all values associated with the given attribute key. >>> import tag >>> reader = tag.GFF3Reader(tag.pkgdata('otau-no-seqreg.gff3')) >>> features = tag.select.features(reader) >>> for feature in features: ... names = feature.attribute_crawl('Name') ... print(sorted(list(names))) ['Ot01g00060', 'XM_003074019.1', 'XP_003074065.1'] ['Ot01g00070', 'XM_003074020.1', 'XP_003074066.1'] ['Ot01g00080', 'XM_003074021.1', 'XP_003074067.1'] ['Ot01g00090', 'XM_003074022.1', 'XP_003074068.1'] ['Ot01g00100', 'XM_003074023.1', 'XP_003074069.1'] ['Ot01g00110', 'XM_003074024.1', 'XP_003074070.1'] """ union = set() for feature in self: values = feature.get_attribute(key, as_list=True) if values is not None: union.update(set(values)) return union
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_findSamesetProteins; 3, parameters; 3, 4; 3, 5; 4, identifier:protToPeps; 5, default_parameter; 5, 6; 5, 7; 6, identifier:proteins; 7, None; 8, block; 8, 9; 8, 11; 8, 23; 8, 30; 8, 55; 8, 61; 8, 89; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:proteins; 14, conditional_expression:if; 14, 15; 14, 19; 14, 22; 15, call; 15, 16; 15, 17; 16, identifier:viewkeys; 17, argument_list; 17, 18; 18, identifier:protToPeps; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:proteins; 21, None; 22, identifier:proteins; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:equalEvidence; 26, call; 26, 27; 26, 28; 27, identifier:ddict; 28, argument_list; 28, 29; 29, identifier:set; 30, for_statement; 30, 31; 30, 32; 30, 33; 31, identifier:protein; 32, identifier:proteins; 33, block; 33, 34; 33, 40; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:peptides; 37, subscript; 37, 38; 37, 39; 38, identifier:protToPeps; 39, identifier:protein; 40, expression_statement; 40, 41; 41, call; 41, 42; 41, 53; 42, attribute; 42, 43; 42, 52; 43, subscript; 43, 44; 43, 45; 44, identifier:equalEvidence; 45, call; 45, 46; 45, 47; 46, identifier:tuple; 47, argument_list; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:sorted; 50, argument_list; 50, 51; 51, identifier:peptides; 52, identifier:add; 53, argument_list; 53, 54; 54, identifier:protein; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:equalProteins; 58, call; 58, 59; 58, 60; 59, identifier:list; 60, argument_list; 61, for_statement; 61, 62; 61, 63; 61, 67; 62, identifier:proteins; 63, call; 63, 64; 63, 65; 64, identifier:viewvalues; 65, argument_list; 65, 66; 66, identifier:equalEvidence; 67, block; 67, 68; 68, if_statement; 68, 69; 68, 75; 69, comparison_operator:>; 69, 70; 69, 74; 70, call; 70, 71; 70, 72; 71, identifier:len; 72, argument_list; 72, 73; 73, identifier:proteins; 74, integer:1; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:equalProteins; 80, identifier:append; 81, argument_list; 81, 82; 82, call; 82, 83; 82, 84; 83, identifier:tuple; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:sorted; 87, argument_list; 87, 88; 88, identifier:proteins; 89, return_statement; 89, 90; 90, identifier:equalProteins
def _findSamesetProteins(protToPeps, proteins=None): """Find proteins that are mapped to an identical set of peptides. :param protToPeps: dict, for each protein (=key) contains a set of associated peptides (=value). For Example {protein: {peptide, ...}, ...} :param proteins: iterable, proteins that are tested for having equal evidence. If not specified all proteins are tested :returns: a list of sorted protein tuples that share equal peptide evidence """ proteins = viewkeys(protToPeps) if proteins is None else proteins equalEvidence = ddict(set) for protein in proteins: peptides = protToPeps[protein] equalEvidence[tuple(sorted(peptides))].add(protein) equalProteins = list() for proteins in viewvalues(equalEvidence): if len(proteins) > 1: equalProteins.append(tuple(sorted(proteins))) return equalProteins